prick 0.25.2 → 0.27.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3dc4e433b296e10221975eb85be4abea219720e0612c0f9720369fd69e18daa1
4
- data.tar.gz: 68215c2e6557dd7288bd3754a65780d4b6c3794ed10c7aecc714b856f5d94fc8
3
+ metadata.gz: 83646fd3fd6a7fecab082cdc10ee5c3bd1330630422edf9640ff6579384aba5d
4
+ data.tar.gz: 5e49929c6b878929581e46428e036bd5bad00a0695612c6a6d6a7f2d5bcf80da
5
5
  SHA512:
6
- metadata.gz: fd193deed4c0d020c6396ebb3862aaa8a65c6edbd1c28e822548db28412a60fb7e85ef357d1a72f883adc6375463415d926129988c8938cab767ce2ebf99a29b
7
- data.tar.gz: 22c84f80ecc0eaec3d73e1c87586e705074d3793cafef5e954aaf9538c9fad04b012f50527a63bc611272c324f91e122e4366ac9d7315e91cd43a9ecabc33f2e
6
+ metadata.gz: 7c52f1241c25e8a3aaa9fafba21abc97020ac376cf458e2ec0fba28c9f310183175c3d932515cf644615b3824663cfb891eef91e0b68f741b9e53dd8b5e74165
7
+ data.tar.gz: d10e893a58d87e5ec8b08eb66ff0744b8b6a452935b779f016db4729ad156d36ab97c81b616a328c8dd07ed52f8170575cbc18343961bec3c0e4518fc9426d31
data/TODO CHANGED
@@ -1,3 +1,5 @@
1
+ o Use standard directory layout
2
+
1
3
  o Have the following entries
2
4
 
3
5
  sql: SQL_STATEMENT
data/exe/prick CHANGED
@@ -200,7 +200,7 @@ begin
200
200
  database, username, args.expect(0..1), force: cmd.force?, timer: cmd.time?, dump: dump)
201
201
 
202
202
  when :make!
203
- dump = cmd.dump? ? cmd.dump("batches").to_sym : nil
203
+ dump = cmd.dump? ? cmd.dump("batches")&.to_sym || :batches : nil
204
204
  Prick::SubCommand.make(database, username, args.expect(0..1), timer: cmd.time?, dump: dump)
205
205
 
206
206
  when :fox!
@@ -34,7 +34,7 @@ module Prick
34
34
  forward_to :pool, :schemas,
35
35
  :nodes, :decl_nodes, :init_nodes, :term_nodes,
36
36
  :seed_nodes, :fox_seed_nodes, :sql_seed_nodes,
37
- :builder_nodes,
37
+ :build_nodes,
38
38
  :pg_graph_ignore_schemas,
39
39
  :refresh_schemas, :keep_schemas
40
40
 
@@ -87,12 +87,13 @@ module Prick
87
87
  end
88
88
  batch.nodes << node
89
89
  end
90
+
90
91
  @batches << batch if batch
91
92
  end
92
93
 
93
94
  def execute(conn, create_schemas: schemas)
94
95
  group if batches.nil?
95
- conn.exec create_schemas.grep_v("public").map { |schema| "create schema #{schema}" }
96
+ conn.exec create_schemas.map { |schema| "create schema #{schema}" }
96
97
  for batch in batches
97
98
  batch.execute
98
99
  end
@@ -8,11 +8,12 @@ module Prick
8
8
 
9
9
  attr_reader :conn
10
10
  attr_reader :dir
11
- attr_reader :unit
11
+ attr_reader :unit # The singular RootBuildNode object
12
12
  # attr_reader :schemas
13
13
 
14
14
  def initialize(conn)
15
15
  @conn = conn
16
+ @unit = nil
16
17
  end
17
18
 
18
19
  def parse(dir)
@@ -24,7 +25,7 @@ module Prick
24
25
 
25
26
  private
26
27
 
27
- # First build unit is a RootBuildNode, the rest are regular BuildNode objects
28
+ # First built unit is a RootBuildNode, the rest are regular BuildNode objects
28
29
  def make_build_unit(parent, path)
29
30
  if @unit
30
31
  BuildNode.new(parent, path)
@@ -76,11 +77,51 @@ module Prick
76
77
  unit
77
78
  end
78
79
 
80
+ # Expand $ENVIRONMENT variable. The function implements a hierarchy of
81
+ # environments:
82
+ #
83
+ # production
84
+ # online
85
+ # development
86
+ # online
87
+ # offline
88
+ # backend
89
+ # frontend
90
+ # test
91
+ #
92
+ # If an environment doesn't exist in #dir, then the higher level
93
+ # environments are tried in turn. Eg. if the current environment is
94
+ # 'test', the algorithm expands '$ENVIRONMENT' to 'test', 'offline', and
95
+ # 'development' until an existing file is found. If no file is found, the
96
+ # algorithm returns the unexpanded value
97
+ #
98
+ def expand_environment(dir, name)
99
+ env = Prick.state.environment.to_s
100
+ file = name
101
+ if !File.exist?("#{dir}/#{file}") && name =~ /^(.*?)\$ENVIRONMENT/
102
+ while true
103
+ file = expand_variables(name, ENVIRONMENT: env)
104
+ break if File.exist? "#{dir}/#{file}"
105
+ case env
106
+ when "production"; break
107
+ when "development"; break
108
+ when "online"; env = "development"
109
+ when "offline"; env = "development"
110
+ when "backend"; env = "offline"
111
+ when "frontend"; env = "offline"
112
+ when "test"; env = "offline"
113
+ else
114
+ raise Error, "Illegal env: '#{env}'"
115
+ end
116
+ end
117
+ end
118
+ return file
119
+ end
120
+
79
121
  def parse_file_entry(dir, entry)
80
- name = entry.dup
81
- name.sub!(/\$ENVIRONMENT/, Prick.state.environment.to_s)
122
+ name = expand_environment(dir, entry)
82
123
  name.sub!(/\/$/, "")
83
- if name =~ /^(\S+)\s+(.*)$/ # exe
124
+ if name =~ /^(\S+)\s+(.+)$/ # has arguments -> exe
84
125
  file = $1
85
126
  args = $2.split
86
127
  else
@@ -0,0 +1,27 @@
1
+
2
+ # Expands variables in a template string. Variables are prefixed with a $ sign
3
+ # and consist of word characters (letters, digits, underscores). The name can
4
+ # be enclosed in curly braces to avoid unintended expansions. The $ sign can be
5
+ # escaped with a backslash (\), and backslashes can be escaped with another
6
+ # backslash. All occurrences of a variable in the string are replaced
7
+ #
8
+ # The str argument is the template string and the variables argument is a hash
9
+ # from variable name (Symbol) to variable value
10
+ #
11
+ # Note that the characters '\x00' and '\x01' are used internally and may not be
12
+ # present in the template string
13
+ #
14
+ def expand_variables(str, variables) # ChatGPT
15
+ # Replace escaped bashslashes and dollar signs
16
+ str = str.gsub('\\\\', "\x00").gsub('\\$', "\x01")
17
+
18
+ # Expand variables
19
+ str.gsub!(/\$(\w+)\b|\$\{(\w+)\}/) do |match| # Strange that '\b' is necessary
20
+ key = ($1 || $2).to_sym
21
+ variables[key] || match
22
+ end
23
+
24
+ # Restore escaped characters
25
+ str.gsub("\x00", '\\').gsub("\x01", '$')
26
+ end
27
+
data/lib/prick/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Prick
4
- VERSION = "0.25.2"
4
+ VERSION = "0.27.0"
5
5
  end
data/lib/prick.rb CHANGED
@@ -12,6 +12,7 @@ module Prick
12
12
  end
13
13
 
14
14
  require 'prick/constants.rb'
15
+ require 'ext/expand_variables.rb'
15
16
 
16
17
  require 'local/command.rb'
17
18
  require 'local/git.rb'
@@ -48,10 +48,6 @@ module Prick::SubCommand
48
48
 
49
49
  # Drop refresh schemes
50
50
  refresh_schemas.each { |schema| conn.schema.drop(schema, cascade: true) }
51
-
52
- # Re-create public schema
53
- conn.schema.create("public", authorization: "postgres")
54
- conn.exec "grant usage, create on schema public to public"
55
51
  end
56
52
  end
57
53
 
@@ -5,7 +5,7 @@ require 'builder/builder.rb'
5
5
  module Prick::SubCommand
6
6
  def self.make(database, username, schema, timer: nil, dump: nil)
7
7
  Timer.on! if timer
8
- time "Prick::Command#build" do
8
+ time "Prick::Command#make" do
9
9
  begin
10
10
  super_conn = PgConn.new
11
11
  conn = nil
@@ -17,7 +17,7 @@ module Prick::SubCommand
17
17
  time "Load build object" do
18
18
  if super_conn.rdbms.exist? database
19
19
  conn = PgConn.new(database, username)
20
- if conn.schema.exist_table? "prick", "versions" && !conn.empty?("prick.versions")
20
+ if conn.schema.exist_table?("prick", "versions") && !conn.empty?("prick.versions")
21
21
  built_at = conn.value("select built_at from prick.versions")
22
22
  end
23
23
  else
@@ -28,11 +28,19 @@ module Prick::SubCommand
28
28
 
29
29
  builder = Prick::Build::Builder.new(conn, "schema", clean)
30
30
 
31
- if schema
32
- builder.pool.after_schema(schema).each { |schema|
33
- conn.schema.drop(schema, cascade: true)
34
- builder.pool.delete_schema(schema)
31
+ if schema
32
+ after_schemas = builder.pool.after_schema(schema)
33
+ refresh_schemas = builder.pool.refresh_schemas
34
+ conn.schema.drop(schema, cascade: true) # Marks it dirty
35
+
36
+ # Nuke later schemas
37
+ # FIXME Not a good idea when sitting on a leaf schema.
38
+ # Forced-delete could be requested using 'prick make schema'
39
+ #
40
+ (after_schemas & refresh_schemas).each { |drop_schema|
41
+ conn.schema.drop(drop_schema, cascade: true)
35
42
  }
43
+ after_schemas.each { |delete_schema| builder.pool.delete_schema(delete_schema) }
36
44
  end
37
45
 
38
46
  touched_nodes = builder.nodes.select { |node| File.mtime(node.path) > built_at }
@@ -41,7 +49,7 @@ module Prick::SubCommand
41
49
  touched_schema = touched_nodes.first&.schema
42
50
  missing_schema = builder.schemas.find { |schema| !conn.schema.exist?(schema) }
43
51
  build_schema = builder.schemas.find { |schema| [touched_schema, missing_schema].include? schema }
44
-
52
+
45
53
  if build_schema.nil?
46
54
  puts "#{database} is up to date"
47
55
  exit
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.25.2
4
+ version: 0.27.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-18 00:00:00.000000000 Z
11
+ date: 2023-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: semantic
@@ -173,6 +173,7 @@ files:
173
173
  - lib/builder/node.rb
174
174
  - lib/builder/node_pool.rb
175
175
  - lib/builder/parser.rb
176
+ - lib/ext/expand_variables.rb
176
177
  - lib/local/command.rb
177
178
  - lib/local/git.rb
178
179
  - lib/local/timer.rb