prick 0.11.0 → 0.16.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: 2788abf0737d8f89b08f4dcbe8cf026ffb4927a2a155655b28350c53951c2c1f
4
- data.tar.gz: a336758b7ff556e11346f7bb0f6da9e949a2d51b7f4f377b24d9e91d0b92af50
3
+ metadata.gz: 2ace97cb91395a8de26e7b53d840e28592d01c08b0e670d58b4b5b5dc09533fe
4
+ data.tar.gz: 4795c70d4dcbdb3a9792f6eb1946236485f2923e7a09fb5fb9cf89d585db1639
5
5
  SHA512:
6
- metadata.gz: e8352c55f6fd37f84c4495f8c1ffe89b5594f936fe74d500e77fe44a7c5fe367c7995dd6824a446c67c36891af892bc277c4c757fe5e6c60a4b07b12e8698809
7
- data.tar.gz: 25e407b34434192eb797cb83bd265b6b2b7004341dee7ba7ab8b64582d9730d2a5ea19dacde7eaa9474d2c2f7e96559a0fbdf4d9fb9fc1e07c8ac9fb6a0c55e6
6
+ metadata.gz: d1bf65cb0a904ac5875a679a0065ab1a55f6c20ec21c229b653e6ca8354a3e962f4ed3154d194054a20564c8e3d27284972dbcfe8c61cec4ddf7a18b71398668
7
+ data.tar.gz: 3cb98096319ac679df0a70c59680120a781db47657e532c9f75be8d37a13ff62313eae88b2a6704860b47c3d529379cb99404e15a9e3310bbf510ac152d66df5
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}'"
data/lib/prick/builder.rb CHANGED
@@ -19,7 +19,7 @@ module Prick
19
19
  # When a resource match a directory, the directory can contain a special
20
20
  # default resource ('build' or 'migrate') that takes over the rest of the
21
21
  # build process for that directory. Typically, you'll use the 'build.yml' or
22
- # 'migrate.yml' to control the build order of the other resources If a
22
+ # 'migrate.yml' to control the build order of the other resources. If a
23
23
  # directory doesn't contain a build resource, the resources in the directory
24
24
  # are built in alphabetic order
25
25
  #
@@ -38,6 +38,7 @@ module Prick
38
38
  @default = default
39
39
  @execute = true
40
40
  @lines = []
41
+ @cwd = Dir.getwd # used in error messages
41
42
  end
42
43
 
43
44
  def build(subject = default, execute: true)
@@ -67,7 +68,13 @@ module Prick
67
68
  # puts "do_sql(#{path})"
68
69
  do_dir(path) { |file|
69
70
  if @execute
70
- Rdbms.exec_file(database.name, file, user: database.user, schema: schema)
71
+ begin
72
+ Rdbms.exec_file(database.name, file, user: database.user, schema: schema)
73
+ rescue Command::Error => ex
74
+ $stderr.puts ex.stderr
75
+ $stderr.puts "in #{reldir}/#{file}"
76
+ exit 1
77
+ end
71
78
  else
72
79
  @lines += IO.readlines(file).map(&:chomp)
73
80
  end
@@ -78,9 +85,15 @@ module Prick
78
85
  def do_exe(path, schema: nil)
79
86
  # puts "do_exe(#{path})"
80
87
  do_dir(path) { |file|
81
- lines = Command.command "#{file} #{database.name} #{database.user}" # FIXME Security
88
+ lines = Command.command "./#{file} #{database.name} #{database.user}" # FIXME Security
82
89
  if @execute
83
- Rdbms.exec_sql(database.name, lines.join("\n"), user: database.user, schema: schema)
90
+ begin
91
+ Rdbms.exec_sql(database.name, lines.join("\n"), user: database.user, schema: schema)
92
+ rescue Command::Error => ex
93
+ $stderr.puts ex.stderr
94
+ $stderr.puts "from #{reldir}/#{file}"
95
+ exit 1
96
+ end
84
97
  else
85
98
  @lines += lines
86
99
  end
@@ -130,6 +143,14 @@ module Prick
130
143
  end
131
144
  }
132
145
  end
146
+
147
+ private
148
+ # Return the relative path to the current directory from the directory of
149
+ # the time of the instantiation of the Builder object. Used in error
150
+ # messages
151
+ def reldir
152
+ Dir.getwd.sub(/^#{@cwd}\//, "")
153
+ end
133
154
  end
134
155
 
135
156
  class MigrationBuilder < Builder
data/lib/prick/command.rb CHANGED
@@ -55,7 +55,7 @@ module Command
55
55
  @status = Process.waitpid2(pid)[1].exitstatus
56
56
 
57
57
  out = pr[0].readlines.collect { |line| line.chop }
58
- err = pe[0].readlines.collect { |line| line.chop }
58
+ err = pe[0].readlines.collect { |line| line.chop }.grep_v(/^NOTICE:/)
59
59
 
60
60
  pw[1].close
61
61
  pr[0].close
@@ -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/version.rb CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  # Required by gem
6
6
  module Prick
7
- VERSION = "0.11.0"
7
+ VERSION = "0.16.0"
8
8
  end
9
9
 
10
10
  # Project related code starts here
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.11.0
4
+ version: 0.16.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-03-08 00:00:00.000000000 Z
11
+ date: 2021-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shellopts