uff_db_loader 1.0.1 → 1.1.1

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: b3810d2c3cfd3142f0449b86a72121062cb0cbff2a6ec15f7aef872cc8cb912b
4
- data.tar.gz: 5e370eef8b819a1501ade5f3e5da94e961c465782f858324fad3a5f17c8bf065
3
+ metadata.gz: a5f7c50d83a5de12e47520cccc509935f3944ae5d4aec252e27a085483487833
4
+ data.tar.gz: 961477955168e96b46559142a99d5ff139c4a865920ad32d006beec1a9f0482a
5
5
  SHA512:
6
- metadata.gz: 702acb4e846ef4630064b1b08c127fd40cee5b48700d99e3553a829f7ab80a8a3a04c99825c0148fb1f1d75a814435ab390aff5cd785374d18f93a445dc0f25e
7
- data.tar.gz: abe992067bc2a0f329047c47c96246cdfd8f594451bf6b57a5f76d7b17ad8ca02a896c861d76dd07dfd445e0c67dfdd849f585366158e849915718cc628058b6
6
+ metadata.gz: ecc86542755c63a15f5b617af89816632bda117dd24fc2e8eb145994ef6b5eef33a8cae83e907ab5279f0ac49135e7187555112df5d57f267b8b11380d4ee93f
7
+ data.tar.gz: dafc87709eae316b58a94b92894dff04357fcd750b3056114873b8fd6ed5ef42cae3655db24f44b3bf978a39933292b276f9a632c00c9eedb46c7fbccadfc927
@@ -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
@@ -21,7 +21,7 @@ namespace :remote_database do
21
21
  puts "🤓 Reading from to #{result_file_path}"
22
22
 
23
23
  database_name = File.basename(result_file_path, ".*")
24
- ActiveRecord::Base.connection.execute("CREATE DATABASE #{database_name};")
24
+ UffDbLoader.create_database(database_name)
25
25
 
26
26
  puts "🗂 Created database #{database_name}"
27
27
 
@@ -34,4 +34,15 @@ namespace :remote_database do
34
34
  puts "🆗 Go to #{UffDbLoader.config.database_config_file} and change the development database value to: #{database_name}"
35
35
  puts "🧑🏾‍🏫 Don't forgot to restart the Rails server after changing the database config (`rails restart`)"
36
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
37
48
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UffDbLoader
4
- VERSION = "1.0.1"
4
+ VERSION = "1.1.1"
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
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.1
4
+ version: 1.1.1
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
@@ -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/1.0.1
90
+ changelog_uri: https://github.com/rmehner/uff_db_loader/releases/tag/1.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/1.0.1
92
+ source_code_uri: https://github.com/rmehner/uff_db_loader/tree/1.1.1
93
93
  post_install_message:
94
94
  rdoc_options: []
95
95
  require_paths: