uff_db_loader 1.3.0 → 1.4.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: 5f807430f4dff47a2a9a98b6f360720a51a7f96c9af2554e0784383adaf0fe63
4
- data.tar.gz: dfaf6dad64eea0e267872426f1426534703e52257c8e29a6440c174a94a94f72
3
+ metadata.gz: b64fcfeeeda9c2895262cabcf104828511ae1a17abe6c906c040f5b132c48bc3
4
+ data.tar.gz: 166953900412522babe8958ddbadef253b8e3558c487d67c53962679252dbd29
5
5
  SHA512:
6
- metadata.gz: c1bed481bd67cc2d5dc3000b6d8b67d17ae0e3f7631302b717c436827862538ab679b52edea76777e8a6f2e1d103caca6c5b75796f64dcf457c7bec7e5a6b226
7
- data.tar.gz: 05defe18ffd2c6719180c26fcab48c85de42cba697834162c9ca81a15e8394760afe8e5daf60e287bc7611b2d257d3a58cbe98778ea2c119275a0a692673bbb3
6
+ metadata.gz: bf80b9b22d70bf31fe2b8f2d4c9d2f84e9b23a5448e2a94fcdbfe43fdd5a4155a06da935846f3df41cf34528b1e71a17dcc697bce68a5263e09becad424ae31c
7
+ data.tar.gz: 74b533e7f45a5b5292d74c7660709f14b60f2a3c8dfa61f390314c4145f7cdcc00f079cd3c253f24f44599518b16eb00ea6edcb37640139f2e1ae9b214b64c19
@@ -3,6 +3,18 @@
3
3
  require "tty-prompt"
4
4
 
5
5
  namespace :uff_db_loader do
6
+ desc "Install uff_db_loader"
7
+ task install: :environment do
8
+ UffDbLoader.remember_database_name("") # ensure database file exists
9
+
10
+ if UffDbLoader.setup_dynamic_database_name_in_config
11
+ puts "🤖 Updated #{UffDbLoader.config.database_config_file}. Happy hacking, beep boop!"
12
+ else
13
+ puts "💩 Because YAML is a wonderful format, you need to adapt your config file by hand."
14
+ puts "🆗 Go to #{UffDbLoader.config.database_config_file} and change the development database value to: #{UffDbLoader.database_name_template("default_database_name")}"
15
+ end
16
+ end
17
+
6
18
  desc "Dumps a remote database to #{UffDbLoader.config.dumps_directory}"
7
19
  task dump: :environment do
8
20
  prompt = TTY::Prompt.new
@@ -13,6 +25,8 @@ namespace :uff_db_loader do
13
25
 
14
26
  desc "Gets a dump from remote and loads it into the local database"
15
27
  task load: :environment do
28
+ UffDbLoader.ensure_installation!
29
+
16
30
  prompt = TTY::Prompt.new
17
31
  environment = prompt.select("Which environment should we get the dump from?", UffDbLoader.config.environments)
18
32
  UffDbLoader.ensure_valid_environment!(environment)
@@ -21,26 +35,35 @@ namespace :uff_db_loader do
21
35
  puts "🤓 Reading from to #{result_file_path}"
22
36
 
23
37
  database_name = File.basename(result_file_path, ".*")
24
- UffDbLoader.create_database(database_name)
38
+ UffDbLoader.load_dump_into_database(database_name)
39
+ end
25
40
 
26
- puts "🗂 Created database #{database_name}"
41
+ desc "Loads an existing dump into the local database"
42
+ task restore: :environment do
43
+ UffDbLoader.ensure_installation!
27
44
 
28
- command_successful = system(UffDbLoader.restore_command(database_name, result_file_path))
29
- raise "Command did not run succesful: #{UffDbLoader.restore_command(database_name, result_file_path)}" unless command_successful
45
+ prompt = TTY::Prompt.new
46
+ existing_dumps = Dir.glob("#{UffDbLoader.config.dumps_directory}/#{UffDbLoader.config.app_name}*").map { |f| File.basename(f, ".*") }
47
+ database_name = prompt.select("Which dump should be restored?", existing_dumps)
30
48
 
31
- puts "✅ Succesfully loaded #{result_file_path} into #{database_name}"
49
+ UffDbLoader.load_dump_into_database(database_name)
50
+ end
32
51
 
33
- if UffDbLoader.replace_database_name_in_config(database_name)
34
- system("bin/rails restart")
35
- puts "🤖 Updated #{UffDbLoader.config.database_config_file} and restarted the rails server. Happy hacking, beep boop!"
36
- else
37
- puts "💩 Because YAML is a wonderful format, you need to adapt your config file by hand."
38
- puts "🆗 Go to #{UffDbLoader.config.database_config_file} and change the development database value to: #{database_name}"
39
- puts "🧑🏾‍🏫 Don't forgot to restart the Rails server after changing the database config (`rails restart`)"
40
- end
52
+ desc "Switches to an existing database"
53
+ task switch: :environment do
54
+ UffDbLoader.ensure_installation!
55
+
56
+ prompt = TTY::Prompt.new
57
+ databases = UffDbLoader.databases
58
+ new_database = prompt.select("Which database do you want to switch to?", databases)
59
+
60
+ UffDbLoader.remember_database_name(new_database)
61
+ system("bin/rails restart")
62
+
63
+ puts "♻️ Restarted rails server with new database."
41
64
  end
42
65
 
43
- desc "Delete all downloaded db dumps and emove all databases created by UffDbLoader"
66
+ desc "Delete all downloaded db dumps and remove all databases created by UffDbLoader"
44
67
  task prune: :environment do
45
68
  UffDbLoader.databases.each do |database_name|
46
69
  puts "Dropping #{database_name}"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UffDbLoader
4
- VERSION = "1.3.0"
4
+ VERSION = "1.4.0"
5
5
  end
data/lib/uff_db_loader.rb CHANGED
@@ -10,6 +10,10 @@ module UffDbLoader
10
10
  @configuration ||= Configuration.new
11
11
  end
12
12
 
13
+ def self.database_name_file
14
+ Rails.root.join("tmp", "uff_db_loader_database_name")
15
+ end
16
+
13
17
  def self.reset
14
18
  @configuration = Configuration.new
15
19
  end
@@ -80,17 +84,65 @@ module UffDbLoader
80
84
  end
81
85
  end
82
86
 
83
- def self.replace_database_name_in_config(new_database_name)
87
+ def self.database_name_template(old_database_name)
88
+ "<%= UffDbLoader.current_database_name || '#{old_database_name}' %>"
89
+ end
90
+
91
+ def self.setup_dynamic_database_name_in_config
84
92
  old_database_name = Rails.configuration.database_configuration["development"]["database"]
85
93
 
86
94
  return false if old_database_name.nil?
87
95
 
88
96
  old_config = File.read(UffDbLoader.config.database_config_file)
89
- new_config = old_config.sub(old_database_name, new_database_name)
97
+ new_config = old_config.sub(old_database_name, database_name_template(old_database_name))
90
98
  File.write(UffDbLoader.config.database_config_file, new_config)
91
99
  end
92
100
 
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
106
+
107
+ def self.remember_database_name(database_name)
108
+ File.write(database_name_file, database_name)
109
+ end
110
+
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"
114
+ end
115
+ end
116
+
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
123
+
124
+ def self.load_dump_into_database(database_name)
125
+ UffDbLoader.drop_database(database_name)
126
+ UffDbLoader.create_database(database_name)
127
+
128
+ puts "🗂 Created database #{database_name}"
129
+
130
+ dump_file_path = dump_file_path(database_name)
131
+
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
134
+
135
+ puts "✅ Succesfully loaded #{dump_file_path} into #{database_name}"
136
+
137
+ remember_database_name(database_name)
138
+ system("bin/rails restart")
139
+
140
+ puts "♻️ Restarted rails server with new database."
141
+ end
142
+
93
143
  class ForbiddenEnvironmentError < StandardError; end
94
144
 
95
145
  class UnknownDatabaseSystem < StandardError; end
146
+
147
+ class InstallationDidNotRunError < StandardError; end
96
148
  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: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Hellwig
@@ -88,9 +88,9 @@ 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.3.0
91
+ changelog_uri: https://github.com/rmehner/uff_db_loader/releases/tag/1.4.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.3.0
93
+ source_code_uri: https://github.com/rmehner/uff_db_loader/tree/1.4.0
94
94
  post_install_message:
95
95
  rdoc_options: []
96
96
  require_paths: