db_tools 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1a794fb33fcc06107733201ad198b0804e82b9b4ae81b824d89bc294457a576b
4
- data.tar.gz: 25541b182fa3e9c8fdb063ee30aa47c2b246b1d3dab4ed200ff9f69f805ba7d3
3
+ metadata.gz: 326c65b22a64f78c54f46531150d1f3c83bf37e6a37ae5d2aeca513626acf46e
4
+ data.tar.gz: ec4411bf59f70d88d115e4ce63fecf253f178fd1b03151c81b2da745e9b46818
5
5
  SHA512:
6
- metadata.gz: f46480ded35a0b168cd0e60c0117d74053d5c65afc8bedacd57c54787236646889f8c49689b6fd55b79a2b3a786e0ce58158332eb36d547205d8568daf5eea7e
7
- data.tar.gz: c575fe57177204d03cccf39284c43e75378253605e1591bcad0134125bfd6934e3b69f48c0ca414e9628b6952657b7004cf34b9a6a248b1ee74f48cd3358eb20
6
+ metadata.gz: d3e455432fece050f0b0f9345e5026ff9bdd5136b8fea3a2747d22cf27a8bae25901d979c1900e93a5d2b4ce6a6ff2b0b25c1c1998e2cb1221caa783665a4c0c
7
+ data.tar.gz: 9f14cd782f4bb211c913f6de11c3446d3394bb96368f0155571435e561ce29a644ceeff3147b79747821118d979add3bd03e2cba0f8c6e52e7da38b3af658084
@@ -1,3 +1,3 @@
1
1
  module DbTools
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/db_tools.rb CHANGED
@@ -38,7 +38,17 @@ module DbTools
38
38
  path = File.join(root, "config", "database.yml")
39
39
  raise Error, "No config/database.yml in #{root}" unless File.exist?(path)
40
40
 
41
- config = YAML.safe_load(render_erb(path, root), aliases: true)&.fetch("development", nil)
41
+ source = File.read(path)
42
+ config = begin
43
+ development_section(ERB.new(source).result(Stubs.module_eval("binding")))
44
+ rescue NameError
45
+ UNRESOLVED
46
+ end
47
+ if config.to_s.include?(UNRESOLVED)
48
+ # The development database references the app (e.g. Rails credentials): boot it and try again.
49
+ require File.join(root, "config", "environment")
50
+ config = development_section(ERB.new(source).result)
51
+ end
42
52
  raise Error, "config/database.yml has no development database" unless config.is_a?(Hash)
43
53
 
44
54
  # Multi-database apps nest each database under a name; snapshot the primary one.
@@ -46,12 +56,18 @@ module DbTools
46
56
  config
47
57
  end
48
58
 
49
- def self.render_erb(path, root)
50
- ERB.new(File.read(path)).result
51
- rescue NameError
52
- # The YAML references the app (e.g. Rails credentials): boot it and try again.
53
- require File.join(root, "config", "environment")
54
- ERB.new(File.read(path)).result
59
+ def self.development_section(yaml)
60
+ YAML.safe_load(yaml, aliases: true)&.fetch("development", nil)
61
+ end
62
+
63
+ UNRESOLVED = "__db_tools_unresolved__"
64
+
65
+ # Stands in for Rails while rendering database.yml, so other environments'
66
+ # credentials don't force booting the app (seconds) just to read development.
67
+ module Stubs
68
+ Rails = BasicObject.new
69
+ def Rails.method_missing(*) = self
70
+ def Rails.to_s = UNRESOLVED
55
71
  end
56
72
 
57
73
  # Runs a command without a shell and returns its stdout, raising on failure.
@@ -12,7 +12,7 @@ namespace :db do
12
12
  delete: "Delete a snapshot"
13
13
  }.each do |command, description|
14
14
  desc description
15
- task command, [:name] do |_task, args|
15
+ task command, (command == :list) ? [] : [:name] do |_task, args|
16
16
  abort "db:snapshot only works in development (Rails.env is #{Rails.env})." unless Rails.env.development?
17
17
 
18
18
  DbTools::Snapshot.new(DbTools.adapter(root: Rails.root.to_s)).run([command.to_s, args[:name]].compact)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seth Horsley