uff_db_loader 4.0.0 → 4.1.1

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: '060096b6203e8ec9ad703842bd1a0c8e54a8ac83bb242316b9d4e1943f3ac293'
4
- data.tar.gz: 9957904bdfdd43ad6562d96e7d5cbb6b5fdc4e1c610687fcb11aff706707d051
3
+ metadata.gz: 4d3709fc6ef4c0ba44fdefcf81cdc25383e1602d13b764351d7768eea8c12d93
4
+ data.tar.gz: ddccaa836fdfbeea03e41b60af5131f26e6618adfc8c4bef0f849593874e4a24
5
5
  SHA512:
6
- metadata.gz: dc35bb6b7d806afdd007acbc131651fa232d904fdc8ff51f2253fd4d9e45002e37ab01d93e0aa8823b25d2e386a1c7ba65ce25ad9170a0f4e35b10abf67079b7
7
- data.tar.gz: fb6f49b13663d44312fc38ba118b7b140c38ed7ab2bb99f38dc37619e500aee1ad1137b332f0a5460f5346791d66a1885153385edc72819175419df6146b0c8d
6
+ metadata.gz: 48c0b323e3048c04219d18895aa291047a4a6bf2766435f6414a9178f0a0c3bc8a6f1eae6f6d52a245bcdc756efb79cf8c39c8ebf4d55f6510c1f288a7a4a511
7
+ data.tar.gz: aabdfc61022f10e079ee9d3aa3de6505d2fc040fab18a2d6db263e1620a1209d11873949b27ee62a0338eed49afd2ffed8898ce69489e73181ee028137fc5385
data/.gitignore CHANGED
@@ -12,3 +12,4 @@ Gemfile.lock
12
12
  .rspec_status
13
13
 
14
14
  .DS_Store
15
+ *.gem
data/README.md CHANGED
@@ -31,9 +31,9 @@ You can configure the gem by running the following during the initialization of
31
31
  ```ruby
32
32
  UffDbLoader.configure do |config|
33
33
  config.environments = ['staging', 'production']
34
- config.ssh_user = 'Francina'
35
- config.ssh_host = 'host.of.yoursite'
36
- config.db_name = 'twotter'
34
+ config.ssh_user = ->(_app_name, environment) { environment == 'production' ? 'Francina' : 'FrancinaStaging' } # It accepts a static string too.
35
+ config.ssh_host = ->(_app_name, environment) { "#{environment}.host.of.yoursite" } # It accepts a static string too.
36
+ config.db_name = ->(app_name, environment) { "#{app_name}_#{environment}" } # Defaults to ssh_user. It accepts a static string too.
37
37
  config.db_system = :postgresql # Possible values are 'postgresql' and 'mysql'.
38
38
  config.app_name = 'my_app' # Defaults to the Rails app name
39
39
  config.dumps_directory = '/path/to/dumps' # Defaults to Rails.root.join('dumps')
data/lib/configuration.rb CHANGED
@@ -33,10 +33,6 @@ module UffDbLoader
33
33
  @restore_command_template = nil
34
34
  end
35
35
 
36
- def database
37
- db_name || ssh_user
38
- end
39
-
40
36
  def database_system
41
37
  case db_system.to_sym
42
38
  when :postgresql
@@ -29,10 +29,7 @@ namespace :uff_db_loader do
29
29
  task restore: :environment do
30
30
  UffDbLoader.ensure_installation!
31
31
 
32
- # switch to default db so we can restore the currently connected database
33
- UffDbLoader.remember_database_name("")
34
- ActiveRecord::Base.remove_connection
35
- ActiveRecord::Base.establish_connection(Rails.configuration.database_configuration["development"])
32
+ UffDbLoader.connect_to_default_database
36
33
 
37
34
  prompt = TTY::Prompt.new
38
35
  database_name = prompt.select("Which dump should be restored?", UffDbLoader.dumps, filter: true)
@@ -57,13 +54,14 @@ namespace :uff_db_loader do
57
54
  desc "Dumps a remote database from a selected environment to #{UffDbLoader.config.dumps_directory}, then restores and selects the database"
58
55
  task load: :environment do
59
56
  UffDbLoader.ensure_installation!
57
+ UffDbLoader.connect_to_default_database
60
58
 
61
59
  prompt = TTY::Prompt.new
62
60
  environment = prompt.select("Which environment should we get the dump from?", UffDbLoader.config.environments, filter: true)
63
61
  UffDbLoader.ensure_valid_environment!(environment)
64
62
  result_file_path = UffDbLoader.dump_from(environment)
65
63
 
66
- UffDbLoader.log "🤓 Reading from to #{result_file_path}"
64
+ UffDbLoader.log "🤓 Reading from #{result_file_path}"
67
65
 
68
66
  database_name = File.basename(result_file_path, ".*")
69
67
  UffDbLoader.load_dump_into_database(database_name)
@@ -14,6 +14,9 @@ if defined?(UffDbLoader)
14
14
  # config.app_name = 'my_app' # Defaults to the Rails app name
15
15
  # config.dumps_directory = '/path/to/dumps' # Defaults to Rails.root.join('dumps')
16
16
  # config.database_config_file = 'path/to/database.yml' # Defaults to Rails.root.join('config', 'database.yml')
17
+ # config.db_name = ->(app_name, environment) { "#{app_name}_#{environment}" } # Defaults to ssh_user. It accepts a static string too.
18
+ # config.ssh_user = ->(_app_name, environment) { "my-user-for-#{environment}" } # It accepts a static string too.
19
+ # config.ssh_host = ->(_app_name, environment) { "#{environment}.host.of.yoursite" } # It accepts a static string too.
17
20
  # config.container_name = ->(app_name, environment) { "#{app_name}_#{environment}_custom_suffix" } # Defaults to "#{app_name}_#{environment}_db". It accepts a static string too.
18
21
  # config.local_restore_command_path = '' # Sets the path to the db-cli (pg_restore, mysql). Defaults to nil so it's using the default binary (mysql, pg_dump) in $PATH.
19
22
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UffDbLoader
4
- VERSION = "4.0.0"
4
+ VERSION = "4.1.1"
5
5
  end
data/lib/uff_db_loader.rb CHANGED
@@ -9,6 +9,8 @@ module UffDbLoader
9
9
  TIMESTAMP_FORMAT = "%Y_%m_%d_%H_%M_%S"
10
10
  TIMESTAMP_LENGTH = Time.now.strftime(TIMESTAMP_FORMAT).size
11
11
 
12
+ class DefaultDatabaseNotFoundError < StandardError; end
13
+
12
14
  class << self
13
15
  def config
14
16
  @configuration ||= Configuration.new
@@ -30,6 +32,21 @@ module UffDbLoader
30
32
  system("bin/rails restart")
31
33
  end
32
34
 
35
+ def connect_to_default_database
36
+ database_config = Rails.configuration.database_configuration["development"]
37
+
38
+ # Switch away from a selected dump database before drop/create operations.
39
+ remember_database_name("")
40
+ ActiveRecord::Base.remove_connection
41
+ ActiveRecord::Base.establish_connection(database_config)
42
+ ActiveRecord::Base.connection
43
+ rescue ActiveRecord::NoDatabaseError
44
+ database_name = database_config["database"].inspect
45
+
46
+ raise DefaultDatabaseNotFoundError,
47
+ "Default local database #{database_name} does not exist. Run `bin/rails db:create` before using UffDbLoader."
48
+ end
49
+
33
50
  def dump_from(environment)
34
51
  FileUtils.mkdir_p(config.dumps_directory)
35
52
 
@@ -145,9 +162,29 @@ module UffDbLoader
145
162
 
146
163
  def container_name(environment)
147
164
  return "#{config.app_name}_#{environment}_db" if config.container_name.blank?
148
- return config.container_name unless config.container_name.respond_to? :call
149
165
 
150
- config.container_name.call(config.app_name, environment)
166
+ resolve_dynamic_setting(config.container_name, environment)
167
+ end
168
+
169
+ def ssh_user(environment)
170
+ resolve_dynamic_setting(config.ssh_user, environment)
171
+ end
172
+
173
+ def ssh_host(environment)
174
+ resolve_dynamic_setting(config.ssh_host, environment)
175
+ end
176
+
177
+ def database_name(environment)
178
+ database_name = resolve_dynamic_setting(config.db_name, environment)
179
+ return database_name if database_name.present?
180
+
181
+ ssh_user(environment)
182
+ end
183
+
184
+ def resolve_dynamic_setting(setting, environment)
185
+ return setting unless setting.respond_to? :call
186
+
187
+ setting.call(config.app_name, environment)
151
188
  end
152
189
 
153
190
  def initializer_template_path
@@ -169,9 +206,9 @@ module UffDbLoader
169
206
  config
170
207
  .database_system
171
208
  .dump_command_template
172
- .gsub("%host%", config.ssh_host)
173
- .gsub("%user%", config.ssh_user)
174
- .gsub("%database%", config.database)
209
+ .gsub("%host%", ssh_host(environment))
210
+ .gsub("%user%", ssh_user(environment))
211
+ .gsub("%database%", database_name(environment))
175
212
  .gsub("%target%", target)
176
213
  .gsub("%container_name%", container_name(environment))
177
214
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uff_db_loader
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Hellwig
@@ -87,9 +87,9 @@ licenses:
87
87
  - MIT
88
88
  metadata:
89
89
  bug_tracker_uri: https://github.com/rmehner/uff_db_loader/issues
90
- changelog_uri: https://github.com/rmehner/uff_db_loader/releases/tag/4.0.0
90
+ changelog_uri: https://github.com/rmehner/uff_db_loader/releases/tag/4.1.1
91
91
  homepage_uri: https://github.com/rmehner/uff_db_loader
92
- source_code_uri: https://github.com/rmehner/uff_db_loader/tree/4.0.0
92
+ source_code_uri: https://github.com/rmehner/uff_db_loader/tree/4.1.1
93
93
  post_install_message: Please run `bin/rails uff_db_loader:install` to complete the
94
94
  installation.
95
95
  rdoc_options: []
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  - !ruby/object:Gem::Version
107
107
  version: '0'
108
108
  requirements: []
109
- rubygems_version: 3.6.9
109
+ rubygems_version: 4.0.10
110
110
  specification_version: 4
111
111
  summary: Allows to dump, download and restore databases from docker servers.
112
112
  test_files: []