uff_db_loader 1.4.0 → 2.0.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: b64fcfeeeda9c2895262cabcf104828511ae1a17abe6c906c040f5b132c48bc3
4
- data.tar.gz: 166953900412522babe8958ddbadef253b8e3558c487d67c53962679252dbd29
3
+ metadata.gz: 28a567bb65dcdcc545605a5b8b921abe6fb6b4b666d97928e659bf535a1675ed
4
+ data.tar.gz: 4deffc2f8bad009cd16d04f08280765be75042da0f6e6e97d488addd22a51065
5
5
  SHA512:
6
- metadata.gz: bf80b9b22d70bf31fe2b8f2d4c9d2f84e9b23a5448e2a94fcdbfe43fdd5a4155a06da935846f3df41cf34528b1e71a17dcc697bce68a5263e09becad424ae31c
7
- data.tar.gz: 74b533e7f45a5b5292d74c7660709f14b60f2a3c8dfa61f390314c4145f7cdcc00f079cd3c253f24f44599518b16eb00ea6edcb37640139f2e1ae9b214b64c19
6
+ metadata.gz: 22b2e2a27364e1afccda77edf18218c9c0862ee0ef389126e745848fceb08fb9fb73b042e07a71a9bddacc2818262d36c9ba109cadc5840c1c3b6175190ccfb5
7
+ data.tar.gz: d8ddddf06be94aa6d2799e7beaeff371c2897cbb7cf6e83855ed112325cc2796732c23082214aa9aa938be023f881e354d2607197dd01a9923777962068c555f
data/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  ## Installation
8
8
 
9
- Add this line to your application's Gemfile:
9
+ Add this line to your application's `Gemfile`:
10
10
 
11
11
  ```ruby
12
12
  gem 'uff_db_loader'
@@ -20,14 +20,16 @@ Or install it yourself as:
20
20
 
21
21
  $ gem install uff_db_loader
22
22
 
23
+ Run the installation script:
24
+
25
+ $ bin/rails uff_db_loader:install
26
+
23
27
  ## Configuration
24
28
 
25
29
  You can configure the gem by running the following during the initialization of the Rails app:
26
30
  ```ruby
27
- # frozen_string_literal: true
28
-
29
31
  UffDbLoader.configure do |config|
30
- config.environments = ['sandbox', 'production'] # default is "['staging', 'production']"
32
+ config.environments = ['staging', 'production']
31
33
  config.ssh_user = 'Francina'
32
34
  config.ssh_host = 'host.of.yoursite'
33
35
  config.db_name = 'twotter'
@@ -43,10 +45,13 @@ Make sure the app's database user has the superuser role. Otherwise the app will
43
45
 
44
46
  ## Usage
45
47
 
46
- `uff_db_loader` provides `rails uff_db_loader:dump` and `rails uff_db_loader:load` which will prompt for a configured environment.
47
- `dump` will only create and download a current database dump, while `load`, will do the same and restore the database content into a new database and gives instructions on how to use it in development.
48
-
48
+ `uff_db_loader` can be called like `bin/rails uff_db_loader:<task>` where `<task>` is one of the following:
49
49
 
50
+ - `dump`: Dumps a remote database from a selected environment and downloads it
51
+ - `restore`: Restores a downloaded dump into a local database
52
+ - `switch`: Selects a restored local database to use
53
+ - `load`: Dumps a remote database from a selected environment and downloads it then restores and selects the database
54
+ - `prune`: Delete all downloaded db dumps and removes all databases created by UffDbLoader
50
55
 
51
56
  ## Development
52
57
 
data/lib/configuration.rb CHANGED
@@ -8,7 +8,7 @@ module UffDbLoader
8
8
  attr_accessor :environments, :ssh_host, :ssh_user, :db_name, :db_system, :app_name, :dumps_directory, :database_config_file
9
9
 
10
10
  def initialize
11
- @environments = %w[staging production]
11
+ @environments = nil
12
12
  @ssh_host = nil
13
13
  @ssh_user = nil
14
14
  @db_name = nil
@@ -3,9 +3,11 @@
3
3
  require "tty-prompt"
4
4
 
5
5
  namespace :uff_db_loader do
6
- desc "Install uff_db_loader"
6
+ desc "Set up UffDbLoader"
7
7
  task install: :environment do
8
- UffDbLoader.remember_database_name("") # ensure database file exists
8
+ UffDbLoader.create_initializer
9
+
10
+ puts "👶 Created a Rails initializer file at #{UffDbLoader.initializer_path}."
9
11
 
10
12
  if UffDbLoader.setup_dynamic_database_name_in_config
11
13
  puts "🤖 Updated #{UffDbLoader.config.database_config_file}. Happy hacking, beep boop!"
@@ -15,7 +17,7 @@ namespace :uff_db_loader do
15
17
  end
16
18
  end
17
19
 
18
- desc "Dumps a remote database to #{UffDbLoader.config.dumps_directory}"
20
+ desc "Dumps a remote database from a selected environment to #{UffDbLoader.config.dumps_directory}"
19
21
  task dump: :environment do
20
22
  prompt = TTY::Prompt.new
21
23
  environment = prompt.select("Which environment should we get the dump from?", UffDbLoader.config.environments)
@@ -23,22 +25,7 @@ namespace :uff_db_loader do
23
25
  UffDbLoader.dump_from(environment)
24
26
  end
25
27
 
26
- desc "Gets a dump from remote and loads it into the local database"
27
- task load: :environment do
28
- UffDbLoader.ensure_installation!
29
-
30
- prompt = TTY::Prompt.new
31
- environment = prompt.select("Which environment should we get the dump from?", UffDbLoader.config.environments)
32
- UffDbLoader.ensure_valid_environment!(environment)
33
- result_file_path = UffDbLoader.dump_from(environment)
34
-
35
- puts "🤓 Reading from to #{result_file_path}"
36
-
37
- database_name = File.basename(result_file_path, ".*")
38
- UffDbLoader.load_dump_into_database(database_name)
39
- end
40
-
41
- desc "Loads an existing dump into the local database"
28
+ desc "Restores a downloaded dump into a local database"
42
29
  task restore: :environment do
43
30
  UffDbLoader.ensure_installation!
44
31
 
@@ -49,7 +36,7 @@ namespace :uff_db_loader do
49
36
  UffDbLoader.load_dump_into_database(database_name)
50
37
  end
51
38
 
52
- desc "Switches to an existing database"
39
+ desc "Selects a restored local database to use"
53
40
  task switch: :environment do
54
41
  UffDbLoader.ensure_installation!
55
42
 
@@ -58,13 +45,32 @@ namespace :uff_db_loader do
58
45
  new_database = prompt.select("Which database do you want to switch to?", databases)
59
46
 
60
47
  UffDbLoader.remember_database_name(new_database)
61
- system("bin/rails restart")
48
+ UffDbLoader.restart_rails_server
62
49
 
63
50
  puts "♻️ Restarted rails server with new database."
64
51
  end
65
52
 
66
- desc "Delete all downloaded db dumps and remove all databases created by UffDbLoader"
53
+ desc "Dumps a remote database from a selected environment to #{UffDbLoader.config.dumps_directory}, then restores and selects the database"
54
+ task load: :environment do
55
+ UffDbLoader.ensure_installation!
56
+
57
+ prompt = TTY::Prompt.new
58
+ environment = prompt.select("Which environment should we get the dump from?", UffDbLoader.config.environments)
59
+ UffDbLoader.ensure_valid_environment!(environment)
60
+ result_file_path = UffDbLoader.dump_from(environment)
61
+
62
+ puts "🤓 Reading from to #{result_file_path}"
63
+
64
+ database_name = File.basename(result_file_path, ".*")
65
+ UffDbLoader.load_dump_into_database(database_name)
66
+ end
67
+
68
+ desc "Delete all downloaded db dumps and removes all databases created by UffDbLoader"
67
69
  task prune: :environment do
70
+ # switch to default db so we can also drop the currently connected database
71
+ UffDbLoader.remember_database_name("")
72
+ ActiveRecord::Base.connection.reconnect!
73
+
68
74
  UffDbLoader.databases.each do |database_name|
69
75
  puts "Dropping #{database_name}"
70
76
  UffDbLoader.drop_database(database_name)
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Check out our README: https://github.com/rmehner/uff_db_loader/blob/main/README.md
4
+
5
+ if defined?(UffDbLoader)
6
+ UffDbLoader.configure do |config|
7
+ config.ssh_user = 'SSH_USER'
8
+ config.ssh_host = 'HOST_OF_YOUR_SITE'
9
+ config.db_name = 'YOUR_DATABASE_NAME'
10
+ config.db_system = <%= used_database_system %> # Possible values are :postgresql and :mysql
11
+ config.environments = <%= environments %>
12
+
13
+ # Optional settings:
14
+ # config.app_name = 'my_app' # Defaults to the Rails app name
15
+ # config.dumps_directory = '/path/to/dumps' # Defaults to Rails.root.join('dumps')
16
+ # config.database_config_file = 'path/to/database.yml' # Defaults to Rails.root.join('config', 'database.yml')
17
+ end
18
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UffDbLoader
4
- VERSION = "1.4.0"
4
+ VERSION = "2.0.0"
5
5
  end
data/lib/uff_db_loader.rb CHANGED
@@ -6,143 +6,183 @@ require "configuration"
6
6
  module UffDbLoader
7
7
  require "railtie"
8
8
 
9
- def self.config
10
- @configuration ||= Configuration.new
11
- end
9
+ class << self
10
+ def config
11
+ @configuration ||= Configuration.new
12
+ end
12
13
 
13
- def self.database_name_file
14
- Rails.root.join("tmp", "uff_db_loader_database_name")
15
- end
14
+ def reset
15
+ @configuration = Configuration.new
16
+ end
16
17
 
17
- def self.reset
18
- @configuration = Configuration.new
19
- end
18
+ def configure
19
+ yield(config)
20
+ end
20
21
 
21
- def self.configure
22
- yield(config)
23
- end
22
+ def restart_rails_server
23
+ system("bin/rails restart")
24
+ end
24
25
 
25
- def self.dump_filename(environment)
26
- File.join(
27
- config.dumps_directory,
28
- Time.now.strftime("#{config.app_name}_#{environment}_%Y_%m_%d_%H_%M_%S.#{config.database_system.dump_extension}")
29
- )
30
- end
26
+ def dump_from(environment)
27
+ FileUtils.mkdir_p(config.dumps_directory)
31
28
 
32
- def self.dump_from(environment)
33
- FileUtils.mkdir_p(config.dumps_directory)
29
+ puts "⬇️ Creating dump ..."
34
30
 
35
- puts "⬇️ Creating dump ..."
31
+ target = dump_file_path(Time.now.strftime("#{config.app_name}_#{environment}_%Y_%m_%d_%H_%M_%S)"))
36
32
 
37
- target = dump_filename(environment)
33
+ command_successful = system(dump_command(environment, target))
34
+ raise "Command did not run succesful: #{dump_command(environment, target)}" unless command_successful
38
35
 
39
- command_successful = system(dump_command(environment, target))
40
- raise "Command did not run succesful: #{dump_command(environment, target)}" unless command_successful
36
+ puts "✅ Succesfully dumped to #{target}"
41
37
 
42
- puts "✅ Succesfully dumped to #{target}"
38
+ target
39
+ end
43
40
 
44
- target
45
- end
41
+ def ensure_valid_environment!(environment)
42
+ unless config.environments.include?(environment)
43
+ raise ForbiddenEnvironmentError, "Invalid environment: #{environment}."
44
+ end
45
+ end
46
46
 
47
- def self.create_database(database_name)
48
- config.database_system.create_database(database_name)
49
- end
47
+ def prune_dump_directory
48
+ FileUtils.rm_rf("#{config.dumps_directory}/.", secure: true)
49
+ end
50
50
 
51
- def self.dump_command(environment, target)
52
- config
53
- .database_system
54
- .dump_command_template
55
- .gsub("%environment%", environment)
56
- .gsub("%host%", config.ssh_host)
57
- .gsub("%user%", config.ssh_user)
58
- .gsub("%database%", config.database)
59
- .gsub("%target%", target)
60
- .gsub("%app_name%", config.app_name)
61
- end
51
+ def create_database(database_name)
52
+ config.database_system.create_database(database_name)
53
+ end
62
54
 
63
- def self.ensure_valid_environment!(environment)
64
- unless config.environments.include?(environment)
65
- raise ForbiddenEnvironmentError, "Invalid environment: #{environment}."
55
+ def drop_database(database_name)
56
+ config.database_system.drop_database(database_name)
66
57
  end
67
- end
68
58
 
69
- def self.restore_command(database_name, result_file_path)
70
- config.database_system.restore_command(database_name, result_file_path)
71
- end
59
+ def databases
60
+ config.database_system.list_databases.select do |line|
61
+ line =~ /#{config.app_name}_(#{config.environments.join("|")})_(\d|_)+/
62
+ end
63
+ end
72
64
 
73
- def self.prune_dump_directory
74
- FileUtils.rm_rf("#{config.dumps_directory}/.", secure: true)
75
- end
65
+ def setup_dynamic_database_name_in_config
66
+ old_database_name = Rails.configuration.database_configuration["development"]["database"]
76
67
 
77
- def self.drop_database(database_name)
78
- config.database_system.drop_database(database_name)
79
- end
68
+ return false if old_database_name.nil?
80
69
 
81
- def self.databases
82
- config.database_system.list_databases.select do |line|
83
- line =~ /#{config.app_name}_(#{config.environments.join("|")})_(\d|_)+/
70
+ old_config = File.read(UffDbLoader.config.database_config_file)
71
+ new_config = old_config.sub(old_database_name, database_name_template(old_database_name))
72
+ File.write(UffDbLoader.config.database_config_file, new_config)
84
73
  end
85
- end
86
74
 
87
- def self.database_name_template(old_database_name)
88
- "<%= UffDbLoader.current_database_name || '#{old_database_name}' %>"
89
- end
75
+ def current_database_name
76
+ File.read(database_name_file).strip.presence
77
+ rescue IOError, Errno::ENOENT => e
78
+ puts "Could not read #{database_name_file}. #{e.message} – Falling back to default database. 🥱"
79
+ end
80
+
81
+ def remember_database_name(database_name)
82
+ File.write(database_name_file, database_name)
83
+ end
90
84
 
91
- def self.setup_dynamic_database_name_in_config
92
- old_database_name = Rails.configuration.database_configuration["development"]["database"]
85
+ def ensure_installation!
86
+ unless File.read(UffDbLoader.config.database_config_file).include?("UffDbLoader.current_database_name")
87
+ raise InstallationDidNotRunError, "Please run bin/rails uff_db_loader:install"
88
+ end
89
+ end
93
90
 
94
- return false if old_database_name.nil?
91
+ def load_dump_into_database(database_name)
92
+ UffDbLoader.drop_database(database_name)
93
+ UffDbLoader.create_database(database_name)
95
94
 
96
- old_config = File.read(UffDbLoader.config.database_config_file)
97
- new_config = old_config.sub(old_database_name, database_name_template(old_database_name))
98
- File.write(UffDbLoader.config.database_config_file, new_config)
99
- end
95
+ puts "🗂 Created database #{database_name}"
100
96
 
101
- def self.current_database_name
102
- File.read(database_name_file).strip.presence
103
- rescue IOError, Errno::ENOENT => e
104
- puts "Could not read #{database_name_file}. #{e.message}"
105
- end
97
+ dump_file_path = dump_file_path(database_name)
106
98
 
107
- def self.remember_database_name(database_name)
108
- File.write(database_name_file, database_name)
109
- end
99
+ command_successful = system(restore_command(database_name, dump_file_path))
100
+ raise "Command did not run succesful: #{restore_command(database_name, dump_file_path)}" unless command_successful
101
+
102
+ puts "✅ Succesfully loaded #{dump_file_path} into #{database_name}"
103
+
104
+ remember_database_name(database_name)
105
+ restart_rails_server
110
106
 
111
- def self.ensure_installation!
112
- unless File.read(UffDbLoader.config.database_config_file).include?("UffDbLoader.current_database_name")
113
- raise InstallationDidNotRunError, "Please run bin/rails uff_db_loader:install"
107
+ puts "♻️ Restarted rails server with new database."
114
108
  end
115
- end
116
109
 
117
- def self.dump_file_path(database_name)
118
- File.join(
119
- config.dumps_directory,
120
- "#{database_name}.#{config.database_system.dump_extension}"
121
- )
122
- end
110
+ def initializer_path
111
+ File.join(__dir__, "uff_db_loader", "templates", "uff_db_loader_initializer.erb")
112
+ end
123
113
 
124
- def self.load_dump_into_database(database_name)
125
- UffDbLoader.drop_database(database_name)
126
- UffDbLoader.create_database(database_name)
114
+ def create_initializer
115
+ template = ERB.new(File.read(initializer_path))
127
116
 
128
- puts "🗂 Created database #{database_name}"
117
+ File.write(
118
+ Rails.root.join("config", "initializers", "uff_db_loader.rb"),
119
+ template.result_with_hash(
120
+ used_database_system: used_database_system,
121
+ environments: environments
122
+ )
123
+ )
124
+ end
129
125
 
130
- dump_file_path = dump_file_path(database_name)
126
+ private
131
127
 
132
- command_successful = system(restore_command(database_name, dump_file_path))
133
- raise "Command did not run succesful: #{restore_command(database_name, dump_file_path)}" unless command_successful
128
+ def database_name_file
129
+ Rails.root.join("tmp", "uff_db_loader_database_name")
130
+ end
134
131
 
135
- puts "✅ Succesfully loaded #{dump_file_path} into #{database_name}"
132
+ def dump_filename(environment)
133
+ File.join(
134
+ config.dumps_directory,
135
+ Time.now.strftime("#{config.app_name}_#{environment}_%Y_%m_%d_%H_%M_%S.#{config.database_system.dump_extension}")
136
+ )
137
+ end
136
138
 
137
- remember_database_name(database_name)
138
- system("bin/rails restart")
139
+ def dump_command(environment, target)
140
+ config
141
+ .database_system
142
+ .dump_command_template
143
+ .gsub("%environment%", environment)
144
+ .gsub("%host%", config.ssh_host)
145
+ .gsub("%user%", config.ssh_user)
146
+ .gsub("%database%", config.database)
147
+ .gsub("%target%", target)
148
+ .gsub("%app_name%", config.app_name)
149
+ end
139
150
 
140
- puts "♻️ Restarted rails server with new database."
141
- end
151
+ def restore_command(database_name, result_file_path)
152
+ config.database_system.restore_command(database_name, result_file_path)
153
+ end
154
+
155
+ def database_name_template(old_database_name)
156
+ "<%= defined?(UffDbLoader) && UffDbLoader.current_database_name || '#{old_database_name}' %>"
157
+ end
158
+
159
+ def dump_file_path(database_name)
160
+ File.join(
161
+ config.dumps_directory,
162
+ "#{database_name}.#{config.database_system.dump_extension}"
163
+ )
164
+ end
165
+
166
+ def used_database_system
167
+ case Rails.configuration.database_configuration["development"]["adapter"]
168
+ when "mysql", "mysql2", "trilogy"
169
+ ":mysql"
170
+ when "postgresql"
171
+ ":postgresql"
172
+ else
173
+ puts "🙃 Could not automatically determine your used database system. Please adapt in the initializer."
174
+ ":unknown"
175
+ end
176
+ end
142
177
 
143
- class ForbiddenEnvironmentError < StandardError; end
178
+ def environments
179
+ ActiveRecord::Base.configurations.configurations.to_a.map(&:env_name) - ["test", "development"]
180
+ end
181
+
182
+ class ForbiddenEnvironmentError < StandardError; end
144
183
 
145
- class UnknownDatabaseSystem < StandardError; end
184
+ class UnknownDatabaseSystem < StandardError; end
146
185
 
147
- class InstallationDidNotRunError < StandardError; end
186
+ class InstallationDidNotRunError < StandardError; end
187
+ end
148
188
  end
@@ -8,6 +8,7 @@ Gem::Specification.new do |spec|
8
8
 
9
9
  spec.summary = "Allows to dump, download and restore databases from docker servers."
10
10
  spec.description = "Allows to dump, download and restore databases from servers with a specific docker setup, that UFF uses commonly."
11
+ spec.post_install_message = "Please run `bin/rails uff_db_loader:install` to complete the installation."
11
12
  spec.homepage = "https://github.com/rmehner/uff_db_loader"
12
13
  spec.license = "MIT"
13
14
  spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uff_db_loader
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Hellwig
8
8
  - Fynn Heintz
9
9
  - Robin Mehner
10
- autorequire:
10
+ autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2022-08-25 00:00:00.000000000 Z
13
+ date: 2022-09-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: tty-prompt
@@ -79,8 +79,8 @@ files:
79
79
  - lib/uff_db_loader.rb
80
80
  - lib/uff_db_loader/mysql.rb
81
81
  - lib/uff_db_loader/postgresql.rb
82
- - lib/uff_db_loader/tasks/remote_database.rake
83
82
  - lib/uff_db_loader/tasks/uff_db_loader.rake
83
+ - lib/uff_db_loader/templates/uff_db_loader_initializer.erb
84
84
  - lib/uff_db_loader/version.rb
85
85
  - uff_db_loader.gemspec
86
86
  homepage: https://github.com/rmehner/uff_db_loader
@@ -88,10 +88,11 @@ licenses:
88
88
  - MIT
89
89
  metadata:
90
90
  bug_tracker_uri: https://github.com/rmehner/uff_db_loader/issues
91
- changelog_uri: https://github.com/rmehner/uff_db_loader/releases/tag/1.4.0
91
+ changelog_uri: https://github.com/rmehner/uff_db_loader/releases/tag/2.0.0
92
92
  homepage_uri: https://github.com/rmehner/uff_db_loader
93
- source_code_uri: https://github.com/rmehner/uff_db_loader/tree/1.4.0
94
- post_install_message:
93
+ source_code_uri: https://github.com/rmehner/uff_db_loader/tree/2.0.0
94
+ post_install_message: Please run `bin/rails uff_db_loader:install` to complete the
95
+ installation.
95
96
  rdoc_options: []
96
97
  require_paths:
97
98
  - lib
@@ -106,8 +107,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
107
  - !ruby/object:Gem::Version
107
108
  version: '0'
108
109
  requirements: []
109
- rubygems_version: 3.0.3
110
- signing_key:
110
+ rubygems_version: 3.3.8
111
+ signing_key:
111
112
  specification_version: 4
112
113
  summary: Allows to dump, download and restore databases from docker servers.
113
114
  test_files: []
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "tty-prompt"
4
-
5
- namespace :remote_database do
6
- desc "Dumps a remote database to #{UffDbLoader.config.dumps_directory}"
7
- task dump: :environment do
8
- puts "🧐 Please note this task is called 'uff_db_loader:dump' now."
9
- Rake::Task["uff_db_loader:dump"].invoke
10
- end
11
-
12
- desc "Gets a dump from remote and loads it into the local database"
13
- task load: :environment do
14
- puts "🧐 Please note this task is called 'uff_db_loader:load' now."
15
- Rake::Task["uff_db_loader:load"].invoke
16
- end
17
-
18
- desc "Delete all downloaded db dumps and emove all databases created by UffDbLoader"
19
- task prune: :environment do
20
- puts "🧐 Please note this task is called 'uff_db_loader:prune' now."
21
- Rake::Task["uff_db_loader:prune"].invoke
22
- end
23
- end