uff_db_loader 1.0.0 → 1.2.0

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: 95175e122ab4572d155f684796d9093587d5764d330855f3f50707491d223185
4
- data.tar.gz: da94a660905f4e8a768735343347bf7b8016d67a2a706c245360a14ab0eb7a02
3
+ metadata.gz: 7bd8cbc61f1a00104d4331da35c5e46cd08d3458977a8b59f2f16bd97ff1d9f3
4
+ data.tar.gz: c164595210489cc21d090672c3ff2e571184440a1a1aeb7810e070ad976d6ad4
5
5
  SHA512:
6
- metadata.gz: f9ca8396b85bb4d5665f209d50dc183b97ce8020ac0393429e6f884f3455fa81cf4c6206bf05c80a6c8c1ac85d35ae254cfc919db0aa14b7a6c6425c39ac7dd1
7
- data.tar.gz: a64fb6d2c537c3b5004234253ad7c7fc5acd150fa1e5b09265d8a3d89a730834dd49a1510251e1934684fe88df34a74e6453948d44ff4e45e9f5ab94eac0d1d2
6
+ metadata.gz: 8c63cf3e6671df456a8a5094505e28151d4c98c9410b920742ca685c91ecf683167471ad6c91d16f43dcac97e0f93f9b7ed21e784cb9f8d808fd36f3ab15544b
7
+ data.tar.gz: bd6e2f2f5b5ea9e5d95dd7e2fe3eb40280d52d7b2803601f71fe3daa4f5b5edb3921fe7ddabd9e0868662457c30d760a5ef63920c2881443c59ff01d963c1216
data/README.md CHANGED
@@ -43,7 +43,7 @@ Make sure the app's database user has the superuser role. Otherwise the app will
43
43
 
44
44
  ## Usage
45
45
 
46
- `uff_db_loader` provides `rails remote_database:dump` and `rails remote_database:load` which will prompt for a configured environment.
46
+ `uff_db_loader` provides `rails uff_db_loader:dump` and `rails uff_db_loader:load` which will prompt for a configured environment.
47
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
 
49
49
 
@@ -13,5 +13,17 @@ module UffDbLoader
13
13
  def self.restore_command(database_name, result_file_path)
14
14
  "mysql -uroot #{database_name} < #{result_file_path}"
15
15
  end
16
+
17
+ def self.list_databases
18
+ ActiveRecord::Base.connection.execute("SHOW DATABASES;").to_a.flatten
19
+ end
20
+
21
+ def self.create_database(database_name)
22
+ ActiveRecord::Base.connection.execute("CREATE DATABASE #{database_name};")
23
+ end
24
+
25
+ def self.drop_database(database_name)
26
+ ActiveRecord::Base.connection.execute("DROP DATABASE IF EXISTS #{database_name};")
27
+ end
16
28
  end
17
29
  end
@@ -13,5 +13,21 @@ module UffDbLoader
13
13
  def self.restore_command(database_name, result_file_path)
14
14
  "pg_restore --username postgres --clean --if-exists --no-owner --no-acl --dbname #{database_name} #{result_file_path}"
15
15
  end
16
+
17
+ def self.list_databases
18
+ ActiveRecord::Base
19
+ .connection
20
+ .execute("SELECT datname FROM pg_database;")
21
+ .values
22
+ .flatten
23
+ end
24
+
25
+ def self.create_database(database_name)
26
+ ActiveRecord::Base.connection.execute("CREATE DATABASE #{database_name};")
27
+ end
28
+
29
+ def self.drop_database(database_name)
30
+ ActiveRecord::Base.connection.execute("DROP DATABASE IF EXISTS #{database_name};")
31
+ end
16
32
  end
17
33
  end
@@ -5,33 +5,19 @@ require "tty-prompt"
5
5
  namespace :remote_database do
6
6
  desc "Dumps a remote database to #{UffDbLoader.config.dumps_directory}"
7
7
  task dump: :environment do
8
- prompt = TTY::Prompt.new
9
- environment = prompt.select("Which environment should we get the dump from?", UffDbLoader.config.environments)
10
- UffDbLoader.ensure_valid_environment!(environment)
11
- UffDbLoader.dump_from(environment)
8
+ puts "🧐 Please note this task is called 'uff_db_loader:dump' now."
9
+ Rake::Task["uff_db_loader:dump"].invoke
12
10
  end
13
11
 
14
12
  desc "Gets a dump from remote and loads it into the local database"
15
13
  task load: :environment do
16
- prompt = TTY::Prompt.new
17
- environment = prompt.select("Which environment should we get the dump from?", UffDbLoader.config.environments)
18
- UffDbLoader.ensure_valid_environment!(environment)
19
- result_file_path = UffDbLoader.dump_from(environment)
20
-
21
- puts "🤓 Reading from to #{result_file_path}"
22
-
23
- database_name = File.basename(result_file_path, ".*")
24
- ActiveRecord::Base.connection.execute("CREATE DATABASE #{database_name};")
25
-
26
- puts "🗂 Created database #{database_name}"
27
-
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
30
-
31
- puts "✅ Succesfully loaded #{result_file_path} into #{database_name}"
14
+ puts "🧐 Please note this task is called 'uff_db_loader:load' now."
15
+ Rake::Task["uff_db_loader:load"].invoke
16
+ end
32
17
 
33
- puts "💩 Because YAML is a wonderful format, you need to adapt your config file by hand."
34
- puts "🆗 Go to #{UffDbLoader.config.database_config_file} and change the development database value to: #{database_name}"
35
- puts "🧑🏾‍🏫 Don't forgot to restart the Rails server after changing the database config (`rails restart`)"
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
36
22
  end
37
23
  end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "tty-prompt"
4
+
5
+ namespace :uff_db_loader do
6
+ desc "Dumps a remote database to #{UffDbLoader.config.dumps_directory}"
7
+ task dump: :environment do
8
+ prompt = TTY::Prompt.new
9
+ environment = prompt.select("Which environment should we get the dump from?", UffDbLoader.config.environments)
10
+ UffDbLoader.ensure_valid_environment!(environment)
11
+ UffDbLoader.dump_from(environment)
12
+ end
13
+
14
+ desc "Gets a dump from remote and loads it into the local database"
15
+ task load: :environment do
16
+ prompt = TTY::Prompt.new
17
+ environment = prompt.select("Which environment should we get the dump from?", UffDbLoader.config.environments)
18
+ UffDbLoader.ensure_valid_environment!(environment)
19
+ result_file_path = UffDbLoader.dump_from(environment)
20
+
21
+ puts "🤓 Reading from to #{result_file_path}"
22
+
23
+ database_name = File.basename(result_file_path, ".*")
24
+ UffDbLoader.create_database(database_name)
25
+
26
+ puts "🗂 Created database #{database_name}"
27
+
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
30
+
31
+ puts "✅ Succesfully loaded #{result_file_path} into #{database_name}"
32
+
33
+ puts "💩 Because YAML is a wonderful format, you need to adapt your config file by hand."
34
+ puts "🆗 Go to #{UffDbLoader.config.database_config_file} and change the development database value to: #{database_name}"
35
+ puts "🧑🏾‍🏫 Don't forgot to restart the Rails server after changing the database config (`rails restart`)"
36
+ end
37
+
38
+ desc "Delete all downloaded db dumps and emove all databases created by UffDbLoader"
39
+ task prune: :environment do
40
+ UffDbLoader.databases.each do |database_name|
41
+ puts "Dropping #{database_name}"
42
+ UffDbLoader.drop_database(database_name)
43
+ end
44
+
45
+ puts "Removing dumps from #{UffDbLoader.config.dumps_directory}"
46
+ UffDbLoader.prune_dump_directory
47
+ end
48
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UffDbLoader
4
- VERSION = "1.0.0"
4
+ VERSION = "1.2.0"
5
5
  end
data/lib/uff_db_loader.rb CHANGED
@@ -18,12 +18,20 @@ module UffDbLoader
18
18
  yield(config)
19
19
  end
20
20
 
21
+ def self.dump_filename(environment)
22
+ File.join(
23
+ config.dumps_directory,
24
+ Time.now.strftime("#{config.app_name}_#{environment}_%Y_%m_%d_%H_%M_%S.#{config.database_system.dump_extension}")
25
+ )
26
+ end
27
+
21
28
  def self.dump_from(environment)
22
29
  FileUtils.mkdir_p(config.dumps_directory)
23
30
 
24
31
  puts "⬇️ Creating dump ..."
25
32
 
26
- target = File.join(config.dumps_directory, Time.now.strftime("#{config.app_name}_#{environment}_%Y_%m_%d_%H_%M_%S.#{config.database_system.dump_extension}"))
33
+ target = dump_filename(environment)
34
+
27
35
  command_successful = system(dump_command(environment, target))
28
36
  raise "Command did not run succesful: #{dump_command(environment, target)}" unless command_successful
29
37
 
@@ -32,6 +40,10 @@ module UffDbLoader
32
40
  target
33
41
  end
34
42
 
43
+ def self.create_database(database_name)
44
+ config.database_system.create_database(database_name)
45
+ end
46
+
35
47
  def self.dump_command(environment, target)
36
48
  config
37
49
  .database_system
@@ -54,6 +66,20 @@ module UffDbLoader
54
66
  config.database_system.restore_command(database_name, result_file_path)
55
67
  end
56
68
 
69
+ def self.prune_dump_directory
70
+ FileUtils.rm_rf("#{config.dumps_directory}/.", secure: true)
71
+ end
72
+
73
+ def self.drop_database(database_name)
74
+ config.database_system.drop_database(database_name)
75
+ end
76
+
77
+ def self.databases
78
+ config.database_system.list_databases.select do |line|
79
+ line =~ /#{config.app_name}_(#{config.environments.join("|")})_(\d|_)+/
80
+ end
81
+ end
82
+
57
83
  class ForbiddenEnvironmentError < StandardError; end
58
84
 
59
85
  class UnknownDatabaseSystem < StandardError; end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.description = "Allows to dump, download and restore databases from servers with a specific docker setup, that UFF uses commonly."
11
11
  spec.homepage = "https://github.com/rmehner/uff_db_loader"
12
12
  spec.license = "MIT"
13
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
14
14
 
15
15
  spec.metadata = {
16
16
  "bug_tracker_uri" => "#{spec.homepage}/issues",
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.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Hellwig
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2022-07-21 00:00:00.000000000 Z
13
+ date: 2022-08-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: tty-prompt
@@ -80,6 +80,7 @@ files:
80
80
  - lib/uff_db_loader/mysql.rb
81
81
  - lib/uff_db_loader/postgresql.rb
82
82
  - lib/uff_db_loader/tasks/remote_database.rake
83
+ - lib/uff_db_loader/tasks/uff_db_loader.rake
83
84
  - lib/uff_db_loader/version.rb
84
85
  - uff_db_loader.gemspec
85
86
  homepage: https://github.com/rmehner/uff_db_loader
@@ -87,9 +88,9 @@ licenses:
87
88
  - MIT
88
89
  metadata:
89
90
  bug_tracker_uri: https://github.com/rmehner/uff_db_loader/issues
90
- changelog_uri: https://github.com/rmehner/uff_db_loader/releases/tag/1.0.0
91
+ changelog_uri: https://github.com/rmehner/uff_db_loader/releases/tag/1.2.0
91
92
  homepage_uri: https://github.com/rmehner/uff_db_loader
92
- source_code_uri: https://github.com/rmehner/uff_db_loader/tree/1.0.0
93
+ source_code_uri: https://github.com/rmehner/uff_db_loader/tree/1.2.0
93
94
  post_install_message:
94
95
  rdoc_options: []
95
96
  require_paths:
@@ -98,7 +99,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
98
99
  requirements:
99
100
  - - ">="
100
101
  - !ruby/object:Gem::Version
101
- version: 2.3.0
102
+ version: 2.6.0
102
103
  required_rubygems_version: !ruby/object:Gem::Requirement
103
104
  requirements:
104
105
  - - ">="