filbert 0.0.3 → 0.0.4

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.
data/lib/filbert/task.rb CHANGED
@@ -29,17 +29,14 @@ module Filbert
29
29
  end
30
30
 
31
31
  method_option :config, type: :string, default: "config/database.yml"
32
- method_option :env, type: :string, default: "production"
32
+ method_option :env, type: :string, default: "development"
33
33
  desc "restore", "restore the latest db dump"
34
34
  def restore
35
35
  most_recent_file = ordered_dumps.last
36
- db_config = DbConfig.new(options[:config], options[:env])
37
-
38
36
  check_dump_ready(most_recent_file)
39
- check_config_ready(db_config)
40
37
 
41
38
  say "Restoring: #{db_config.database} <--- #{most_recent_file.path}"
42
- kill_connections(db_config.database, db_config.username)
39
+ invoke :kill_connections
43
40
  ENV['PGPASSWORD'] = db_config.password
44
41
  run! "pg_restore -U #{db_config.username} -d #{db_config.database} -w #{most_recent_file.path}"
45
42
 
@@ -49,6 +46,21 @@ module Filbert
49
46
  ENV['PGPASSWORD'] = nil
50
47
  end
51
48
 
49
+ method_option :config, type: :string, default: "config/database.yml"
50
+ method_option :env, type: :string, default: "development"
51
+ desc "kill_connections", "Kills all open connections to the db"
52
+ def kill_connections
53
+ database = db_config.database
54
+ user = db_config.username
55
+
56
+ ENV['PGPASSWORD'] = db_config.password
57
+ sql = "SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE procpid <> pg_backend_pid();"
58
+ run! "echo \"#{sql}\" | psql -d #{database} -U #{user}"
59
+ say "Killed connections to #{database} as #{user}"
60
+ ensure
61
+ ENV['PGPASSWORD'] = nil
62
+ end
63
+
52
64
  private
53
65
 
54
66
  def run!(cmd)
@@ -90,16 +102,15 @@ module Filbert
90
102
  end
91
103
  end
92
104
 
93
- def check_config_ready(db_config)
94
- if db_config.config.nil?
95
- say "Could not find config for \"#{options[:env]}\" in #{options[:config]}"
96
- exit 0
105
+ def db_config
106
+ @db_config ||= begin
107
+ db_config = DbConfig.new(options[:config], options[:env])
108
+ if db_config.config.nil?
109
+ say "Could not find config for \"#{options[:env]}\" in #{options[:config]}"
110
+ exit 0
111
+ end
112
+ db_config
97
113
  end
98
114
  end
99
-
100
- def kill_connections(database, user)
101
- sql = "SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE procpid <> pg_backend_pid();"
102
- run "echo #{sql} | psql -d #{database} -U #{user}"
103
- end
104
115
  end
105
116
  end
@@ -1,3 +1,3 @@
1
1
  module Filbert
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filbert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: