prick 0.9.0 → 0.14.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: 3009c0da5619e908746d98eefe922851d886105d10b6bc49cc0295cf47a50033
4
- data.tar.gz: 3ee5cd54a3b29171306c3ad2fe8ea10c2e6d1ad84e1b8446b6314770c6734aef
3
+ metadata.gz: c155692c268326430a0e9225d4a7c156fedd52533114b62fae7e7ce61da01177
4
+ data.tar.gz: 4e245212208c3ecc518f313cd9f1af709db5ce41b7281c7247a1078293c486de
5
5
  SHA512:
6
- metadata.gz: 7ee1ce2cd13293542db575cbbf5d955f542eff9b3cc6e4b87027800e7b0effb12ca134df2665f6a2a240cb0c191cd2fc9e5cca9b943c307591b499e75d27b5db
7
- data.tar.gz: 7f81e321f4875bce049b5adf7eb7fe7394a8112135d13b73ebdd48680b014329517e7137df419f504861a2d855243d37dfcafc2f67226fef57c537e28dd27dde
6
+ metadata.gz: 38ff771885f6a73a20f36985403cb61a63fc2f025c20f92bb1dea4cb2ba047f908e48c4a564a50b707594d32199a87f796e0640135f6f0da5a521fe572c70cdb
7
+ data.tar.gz: 79641d89e168fe1be4d7493aed420fd40cd71cfb29ac98c359c34f82457bd1b7c265883e83cf661c29ff1ea6437bf1a087a652987409221cc4c439768b480922
data/exe/prick CHANGED
@@ -195,6 +195,13 @@ begin
195
195
  exit 0
196
196
  end
197
197
 
198
+ # Change to parent directory containing the Prick version file if not found
199
+ # in the current directory
200
+ program.current_directory = Dir.getwd
201
+ while !Dir.getwd != "/" && !File.exist?(PRICK_VERSION_FILE)
202
+ Dir.chdir("..")
203
+ end
204
+
198
205
  # Check prick version
199
206
  file = PrickVersion.new
200
207
  file.exist? or raise Prick::Error, "Can't find prick version file '#{file.path}'"
@@ -251,7 +258,7 @@ begin
251
258
  program.save(version, file)
252
259
 
253
260
  when :drop
254
- program.drop(args.expect(0..1), opts.drop!.all)
261
+ program.drop(args.expect(0..1), opts.drop!.all?)
255
262
 
256
263
  when :diff
257
264
  mark = opts.diff!.mark
data/lib/prick/builder.rb CHANGED
@@ -7,18 +7,24 @@ module Prick
7
7
  # and is used to match a build file. Build files are looked up in the
8
8
  # following order:
9
9
  #
10
- # #{name}, #{name}.* executable
10
+ # #{name} executable
11
+ # #{name}.* executable
11
12
  # #{name}.yml
12
13
  # #{name}.sql
13
14
  # #{name}/
14
15
  #
16
+ # The output from executable objects is expected to be SQL statements that
17
+ # are fed into postgres
18
+ #
15
19
  # When a resource match a directory, the directory can contain a special
16
20
  # default resource ('build' or 'migrate') that takes over the rest of the
17
21
  # build process for that directory. Typically, you'll use the 'build.yml' or
18
- # 'migrate.yml' to control the build order of the other resources
22
+ # 'migrate.yml' to control the build order of the other resources If a
23
+ # directory doesn't contain a build resource, the resources in the directory
24
+ # are built in alphabetic order
19
25
  #
20
- # If a directory doesn't contain a build resource, the resources in the
21
- # directory are built in alphabetic order
26
+ # Build (but not migration) SQL scripts and executables are executed with
27
+ # search path set to containing schema
22
28
  #
23
29
  class Builder
24
30
  attr_reader :database
@@ -57,11 +63,11 @@ module Prick
57
63
  Dir.chdir(dir) { yield(file) }
58
64
  end
59
65
 
60
- def do_sql(path)
66
+ def do_sql(path, schema: nil)
61
67
  # puts "do_sql(#{path})"
62
68
  do_dir(path) { |file|
63
69
  if @execute
64
- Rdbms.exec_file(database.name, file, user: database.user)
70
+ Rdbms.exec_file(database.name, file, user: database.user, schema: schema)
65
71
  else
66
72
  @lines += IO.readlines(file).map(&:chomp)
67
73
  end
@@ -69,12 +75,12 @@ module Prick
69
75
  true
70
76
  end
71
77
 
72
- def do_exe(path)
78
+ def do_exe(path, schema: nil)
73
79
  # puts "do_exe(#{path})"
74
80
  do_dir(path) { |file|
75
- lines = Command.command "#{file} #{database.name} #{database.user}" # FIXME Security
81
+ lines = Command.command "./#{file} #{database.name} #{database.user}" # FIXME Security
76
82
  if @execute
77
- Rdbms.exec_sql(database.name, lines.join("\n"), database.user)
83
+ Rdbms.exec_sql(database.name, lines.join("\n"), user: database.user, schema: schema)
78
84
  else
79
85
  @lines += lines
80
86
  end
@@ -159,6 +165,17 @@ module Prick
159
165
  end
160
166
 
161
167
  def self.yml_file(directory) File.join(directory, "build") + ".yml" end
168
+
169
+ protected
170
+ def do_sql(path)
171
+ schema = Dir.getwd.sub(/^.*schema\/([^\/]+)(?:\/.*)?$/, '\1')
172
+ super(path, schema: schema)
173
+ end
174
+
175
+ def do_exe(path)
176
+ schema = Dir.getwd.sub(/^.*schema\/([^\/]+)(?:\/.*)?$/, '\1')
177
+ super(path, schema: schema)
178
+ end
162
179
  end
163
180
  end
164
181
 
@@ -59,7 +59,23 @@ module Prick
59
59
  @version = nil
60
60
  end
61
61
 
62
- def clean() recreate end # TODO: Find solution that doesn't involve dropping the database
62
+ # Hollow-out a database without dropping it. This is useful compared to a
63
+ # simple drop database since it wont block on other sessions wont block
64
+ def clean()
65
+ if exist?
66
+ schemas = Rdbms.select_values(
67
+ "postgres",
68
+ "select nspname from pg_namespace where nspowner != 10")
69
+ for schema in schemas
70
+ Rdbms.exec_sql(name, "drop schema \"#{schema}\" cascade")
71
+ end
72
+ Rdbms.exec_sql(name, "drop schema if exists public cascade")
73
+ Rdbms.exec_sql(name, "create schema public authorization postgres")
74
+ Rdbms.exec_sql(name, "grant usage, create on schema public to public")
75
+ else
76
+ create
77
+ end
78
+ end
63
79
 
64
80
  def to_s() name end
65
81
  end
data/lib/prick/program.rb CHANGED
@@ -10,8 +10,10 @@ module Prick
10
10
 
11
11
  attr_accessor :quiet
12
12
  attr_accessor :verbose
13
+ attr_accessor :current_directory # Path to the directory where prick was called from
13
14
 
14
- def initialize(quiet: false, verbose: false)
15
+ def initialize(current_directory = Dir.getwd, quiet: false, verbose: false)
16
+ @current_directory = current_directory
15
17
  @quiet = quiet
16
18
  @verbose = verbose
17
19
  end
@@ -95,8 +97,9 @@ module Prick
95
97
  database = database ? Database.new(database, project.user) : project.database(version)
96
98
  project.load(database, version: version)
97
99
  mesg "Loaded v#{version}", into_mesg, "from cache"
98
- else file = file_or_version
99
- project.load(database, file: file)
100
+ else
101
+ file = file_or_version
102
+ project.load(database, file: norm_path(file))
100
103
  mesg "Loaded #{File.basename(file)}", into_mesg
101
104
  end
102
105
  end
@@ -104,9 +107,8 @@ module Prick
104
107
  def save(version, file)
105
108
  database = project.database(version)
106
109
  database.exist? or raise "Can't find database '#{database}'"
107
- subj_mesg = file ? file : "cache"
108
- project.save(database, file: file)
109
- mesg "Saved #{database} to #{subj_mesg}"
110
+ project.save(database, file: norm_path(file))
111
+ mesg "Saved #{database} to #{file || 'cache'}"
110
112
  end
111
113
 
112
114
  def drop(database, all)
@@ -230,15 +232,16 @@ module Prick
230
232
 
231
233
  # TODO: Create a Backup class and a Project#backup_store object
232
234
  def backup(file = nil)
233
- file = file || File.join(SPOOL_DIR, "#{project.name}-#{Time.now.utc.strftime("%Y%m%d-%H%M%S")}.sql.gz")
234
- project.save(file: file)
235
+ path = norm_path(file) ||
236
+ File.join(SPOOL_DIR, "#{project.name}-#{Time.now.utc.strftime("%Y%m%d-%H%M%S")}.sql.gz")
237
+ project.save(file: path)
235
238
  mesg "Backed-up database to #{file}"
236
239
  end
237
240
 
238
- def restore(file_arg = nil)
239
- file = file_arg || Dir.glob(File.join(SPOOL_DIR, "#{name}-*.sql.gz")).sort.last
240
- File.exist?(file) or raise Error, "Can't find #{file_arg || "any backup file"}"
241
- project.load(file: file)
241
+ def restore(file = nil)
242
+ path = norm_path(file) || Dir.glob(File.join(SPOOL_DIR, "#{name}-*.sql.gz")).sort.last
243
+ File.exist?(path) or raise Error, "Can't find #{file || "any backup file"}"
244
+ project.load(file: path)
242
245
  mesg "Restored database from #{file}"
243
246
  end
244
247
 
@@ -249,6 +252,16 @@ module Prick
249
252
  project_name == project.name or raise Error, "Database not owned by this prick project: #{database}"
250
253
  end
251
254
 
255
+ # Expand relative path with respect to the directory where prick was called
256
+ # from. Return nil if file is nil
257
+ def norm_path(file)
258
+ if file && !file.start_with?("/")
259
+ File.join(current_directory, file)
260
+ else
261
+ file
262
+ end
263
+ end
264
+
252
265
  def mesg(*args) puts args.compact.grep(/\S/).join(' ') if !quiet end
253
266
  def verb(*args) puts args.compact.grep(/\S/).join(' ') if verbose end
254
267
  end
data/lib/prick/rdbms.rb CHANGED
@@ -4,16 +4,15 @@ require 'csv'
4
4
 
5
5
  module Prick
6
6
  module Rdbms
7
- # extend Ensure
8
-
9
7
  ### EXECUTE SQL
10
8
 
11
9
  # Execute the SQL statement and return stdout as an array of tuples
12
- def self.exec_sql(db, sql, user: ENV['USER'])
10
+ def self.exec_sql(db, sql, user: ENV['USER'], schema: nil)
11
+ schema ||= "public"
13
12
  stdout = Command.command %(
14
13
  {
15
14
  echo "set role #{user};"
16
- echo "set search_path to public;"
15
+ echo "set search_path to #{schema};"
17
16
  cat <<'EOF'
18
17
  #{sql}
19
18
  EOF
@@ -23,8 +22,8 @@ EOF
23
22
  end
24
23
 
25
24
  # Execute the given file and return stdout as an array of tuples
26
- def self.exec_file(db, file, user: ENV['USER'])
27
- self.exec_sql(db, File.read(file), user: user)
25
+ def self.exec_file(db, file, user: ENV['USER'], schema: nil)
26
+ self.exec_sql(db, File.read(file), user: user, schema: schema)
28
27
  end
29
28
 
30
29
  # Execute the SQL statement and return the result as an array of record tuples
data/lib/prick/version.rb CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  # Required by gem
6
6
  module Prick
7
- VERSION = "0.9.0"
7
+ VERSION = "0.14.0"
8
8
  end
9
9
 
10
10
  # Project related code starts here
@@ -1,5 +1,3 @@
1
1
 
2
2
  drop schema if exists [<SCHEMA>] cascade;
3
3
  create schema [<SCHEMA>];
4
-
5
- set search_path to [<SCHEMA>];
@@ -1,5 +1,3 @@
1
1
 
2
2
  drop schema if exists prick cascade;
3
3
  create schema prick;
4
-
5
- set search_path to prick;
@@ -1,7 +1,6 @@
1
1
  # Default schema files
2
2
  #
3
3
  ---
4
- - schema
5
4
  - roles
6
5
  - types
7
6
  - tables
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.9.0
4
+ version: 0.14.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: 2021-02-10 00:00:00.000000000 Z
11
+ date: 2021-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shellopts
@@ -133,7 +133,6 @@ files:
133
133
  - share/schema/schema/prick/tables.sql
134
134
  - share/schema/schema/public/.keep
135
135
  - share/schema/schema/public/build.yml
136
- - share/schema/schema/public/schema.sql
137
136
  - test_assorted
138
137
  - test_feature
139
138
  - test_refactor
@@ -156,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
155
  - !ruby/object:Gem::Version
157
156
  version: '0'
158
157
  requirements: []
159
- rubygems_version: 3.1.4
158
+ rubygems_version: 3.1.2
160
159
  signing_key:
161
160
  specification_version: 4
162
161
  summary: A release control and management system for postgresql
@@ -1,3 +0,0 @@
1
- set search_path to public;
2
-
3
-