remote_database_importer 0.1.3 → 0.1.5

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: cbe347c485be41742ebab8205d5d803b12cace72407f351c6e05ba9988fb8a80
4
- data.tar.gz: 21ab68290d08a418b5921ec81ef46e927c41e3ffe7e99c2916acb27561cd9d30
3
+ metadata.gz: e87b7d089101bd0f324f2dcaf9be16c2a038f9d369c4d18f86362492c7e25218
4
+ data.tar.gz: ea76970e0e136fc5469bf4f82fec6dd90bb7b7bad5622947c375a9a6640031fe
5
5
  SHA512:
6
- metadata.gz: facbe97e036a224e1fa47fabe1e1774147d88154e1f77ff299dde2df89a7e8780c65bf7d5706b78e84d0928f01aebbf59add7f301ae2feb8022c50f6b64a6755
7
- data.tar.gz: a3c042ad221b0b3273b8fe6b202bf724f9abecabe0fdea6a10acdbb848964426bb4c68d97cdb7b25bd90929155c2d44a0137f065e6943a8428077a8a48af7d0a
6
+ metadata.gz: cc71eabc31b5df7c4238fd0196733eb942f8704a31f3c33ba4373eab4278ec5be93828065fdaf6d6af52f9cad556b40aea692ed0d8d6756a8a20661b72f84e04
7
+ data.tar.gz: 14e17552a0ed444ad14904245bcb7adac5d4c5d795be684bb0acaecaefa1f12ec868cfe88034bc90413fd5986b8b99c881ea0a61f04734717bbf56c0aae0316c
data/CHANGELOG.md CHANGED
@@ -1,11 +1,22 @@
1
1
  # Changelog
2
2
 
3
- ## [0.1.2] - 2022-10-29
3
+ ## 0.1.5 (2022-12-03)
4
+ - Define custom commands for each environment
5
+
6
+ ## 0.1.4 (2022-11-17)
7
+ - Remove colorize as a dependency
8
+ - Code refactoring
9
+
10
+ ## 0.1.3 (2022-11-06)
11
+ - Remove Thor as a dependency
12
+ - Add custom commands that run after the successful import of the remote database
13
+
14
+ ## 0.1.2 (2022-10-29)
4
15
 
5
16
  - Terminate current DB sessions after dump remote database. So the chance of an open session is smaller
6
17
  - Print out where the config was saved
7
18
  - Print how long the import went
8
19
 
9
- ## [0.1.0] - 2022-10-24
20
+ ## 0.1.0 (2022-10-24)
10
21
 
11
22
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,8 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- remote_database_importer (0.1.3)
5
- colorize (~> 0.8)
4
+ remote_database_importer (0.1.4)
6
5
  tty-config (~> 0.6)
7
6
  tty-spinner (~> 0.9)
8
7
 
@@ -10,7 +9,6 @@ GEM
10
9
  remote: https://rubygems.org/
11
10
  specs:
12
11
  ast (2.4.2)
13
- colorize (0.8.1)
14
12
  diff-lcs (1.5.0)
15
13
  json (2.6.2)
16
14
  parallel (1.22.1)
@@ -0,0 +1,13 @@
1
+ class Colorize
2
+ def self.red(text)
3
+ "\033[31m#{text}\033[0m"
4
+ end
5
+
6
+ def self.green(text)
7
+ "\e[32m#{text}\e[0m"
8
+ end
9
+
10
+ def self.blue(text)
11
+ "\e[94m#{text}\e[0m"
12
+ end
13
+ end
@@ -1,32 +1,35 @@
1
1
  module RemoteDatabaseImporter
2
2
  class Config
3
3
  require "tty/config"
4
- require "colorize"
4
+ require_relative "colorize"
5
+
6
+ attr_accessor :config
5
7
 
6
8
  def initialize
7
9
  @config = TTY::Config.new
8
- @config.filename = "remote_database_importer"
9
- @config.extname = ".yml"
10
- @config.append_path Dir.pwd
10
+
11
+ config.filename = "remote_database_importer"
12
+ config.extname = ".yml"
13
+ config.append_path Dir.pwd
11
14
  end
12
15
 
13
16
  def read_or_create_configfile
14
- unless @config.exist?
15
- puts "===========================================================".colorize(:green)
17
+ unless config.exist?
18
+ puts Colorize.green("===========================================================")
16
19
  puts "Hi there! There is no config file yet, lets create one! 😄"
17
20
  create_default_config
18
- config_location = [@config.filename, @config.extname].join
21
+ config_location = [config.filename, config.extname].join
19
22
  puts "Created config file: #{config_location}"
20
- puts "===========================================================".colorize(:green)
23
+ puts Colorize.green("===========================================================")
21
24
  end
22
- @config.read
25
+ config.read
23
26
  end
24
27
 
25
28
  def ask(question, default: nil, options: nil)
26
29
  question += " (#{options.join(" / ")})" if options.present?
27
30
  question += " [#{default}]" if default.present?
28
31
 
29
- puts question.colorize(:light_blue)
32
+ puts Colorize.blue(question)
30
33
  answer = $stdin.gets.chomp
31
34
  answer.present? ? answer : default
32
35
  end
@@ -36,20 +39,20 @@ module RemoteDatabaseImporter
36
39
  environment_count = 1
37
40
 
38
41
  local_db_name = ask("Whats the name of the local database you wanna import to?", default: "myawesomeapp_development")
39
- @config.set(:local_db_name, value: local_db_name)
42
+ config.set(:local_db_name, value: local_db_name)
40
43
  puts
41
44
 
42
45
  while enter_new_environments
43
- puts "#{environment_count}. Environment".colorize(:green)
46
+ puts Colorize.green("#{environment_count}. Environment")
44
47
  env = ask("Whats the name of the #{environment_count}. environment you wanna add?", default: "staging")
45
48
  puts
46
49
 
47
- puts "Database settings:".colorize(:green)
50
+ puts Colorize.green("Database settings:")
48
51
  db_name = ask("Enter the DB name for the #{env} environment:", default: "myawesomeapp_#{env}")
49
52
  db_user = ask("Enter the DB user for the #{env} environment:", default: "deployer")
50
53
  puts
51
54
 
52
- puts "Connection settings:".colorize(:green)
55
+ puts Colorize.green("Connection settings:")
53
56
  host = ask("Enter the IP or hostname of the DB server:", default: "myawesomeapp.com")
54
57
  dump_type = ask("Should the DB dump happen over a ssh tunnel or can pg_dump connect to the DB port directly?", default: "pg_dump", options: ["ssh_tunnel", "pg_dump"])
55
58
 
@@ -60,6 +63,9 @@ module RemoteDatabaseImporter
60
63
  else
61
64
  postgres_port = ask("Enter the database port for the pg_dump command:", default: "5432")
62
65
  end
66
+
67
+ puts Colorize.green("Define custom commands that run after successful import:")
68
+ custom_commands = ask("Enter semicolon separated commands that should run after importing the DB:", default: "rake db:migrate; echo 'All Done'")
63
69
  puts
64
70
 
65
71
  env_config = {
@@ -74,10 +80,11 @@ module RemoteDatabaseImporter
74
80
  "postgres_port" => postgres_port,
75
81
  "ssh_user" => ssh_user,
76
82
  "ssh_port" => ssh_port
77
- }
83
+ },
84
+ "custom_commands" => custom_commands
78
85
  }
79
86
  }
80
- @config.append(env_config, to: :environments)
87
+ config.append(env_config, to: :environments)
81
88
 
82
89
  continue = ask("Do you wanna add another environment? (anything other than 'yes' will exit)")
83
90
  if continue&.downcase == "yes"
@@ -87,12 +94,7 @@ module RemoteDatabaseImporter
87
94
  end
88
95
  end
89
96
 
90
- puts "Define custom commands that run after successful import:".colorize(:green)
91
- custom_commands = ask("Enter semicolon separated commands that should run after importing the DB:", default: "rake db:migrate; echo 'All Done'")
92
- puts
93
-
94
- @config.set(:custom_commands, value: custom_commands)
95
- @config.write
97
+ config.write
96
98
  end
97
99
  end
98
100
  end
@@ -3,15 +3,17 @@ module RemoteDatabaseImporter
3
3
  require "remote_database_importer/config"
4
4
  require "tty/spinner/multi"
5
5
 
6
+ attr_accessor :config
7
+ attr_accessor :current_environment
8
+
6
9
  LOG_FILE = "tmp/remote_database_importer.log"
7
10
 
8
11
  def initialize
9
- config_handler = RemoteDatabaseImporter::Config.new
10
- @config = config_handler.read_or_create_configfile
12
+ @config = RemoteDatabaseImporter::Config.new.read_or_create_configfile
11
13
  end
12
14
 
13
15
  def environments
14
- @config.fetch("environments")
16
+ config.fetch("environments")
15
17
  end
16
18
 
17
19
  def select_environment
@@ -66,18 +68,17 @@ module RemoteDatabaseImporter
66
68
 
67
69
  # terminate local db sessions, otherwise the db can't be dropped
68
70
  def terminate_current_db_sessions
69
- "psql -d #{@config.fetch("local_db_name")} -c 'SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE datname = current_database() AND pid <> pg_backend_pid();' > #{LOG_FILE}"
71
+ "psql -d #{config.fetch("local_db_name")} -c 'SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE datname = current_database() AND pid <> pg_backend_pid();' > #{LOG_FILE}"
70
72
  end
71
73
 
72
74
  def dump_remote_db
73
- env = @current_environment
74
- host = env["connection"]["host"]
75
- db_name = env["database"]["name"]
76
- db_user = env["database"]["user"]
77
- dump_type = env["connection"]["dump_type"]
78
- ssh_user = env["connection"]["ssh_user"]
79
- ssh_port = env["connection"]["ssh_port"]
80
- postgres_port = env["connection"]["postgres_port"]
75
+ host = current_environment["connection"]["host"]
76
+ db_name = current_environment["database"]["name"]
77
+ db_user = current_environment["database"]["user"]
78
+ dump_type = current_environment["connection"]["dump_type"]
79
+ ssh_user = current_environment["connection"]["ssh_user"]
80
+ ssh_port = current_environment["connection"]["ssh_port"]
81
+ postgres_port = current_environment["connection"]["postgres_port"]
81
82
 
82
83
  if dump_type == "ssh_tunnel"
83
84
  "ssh #{ssh_user}@#{host} -p #{ssh_port} 'pg_dump -Fc -U #{db_user} -d #{db_name} -h localhost -C' > #{db_dump_location}"
@@ -91,7 +92,7 @@ module RemoteDatabaseImporter
91
92
  end
92
93
 
93
94
  def restore_db
94
- "pg_restore --jobs 8 --no-privileges --no-owner --dbname #{@config.fetch("local_db_name")} #{db_dump_location}"
95
+ "pg_restore --jobs 8 --no-privileges --no-owner --dbname #{config.fetch("local_db_name")} #{db_dump_location}"
95
96
  end
96
97
 
97
98
  def remove_logfile
@@ -103,11 +104,11 @@ module RemoteDatabaseImporter
103
104
  end
104
105
 
105
106
  def custom_commands
106
- @config.fetch("custom_commands")
107
+ current_environment["custom_commands"]
107
108
  end
108
109
 
109
110
  def db_dump_location
110
- "tmp/#{@current_environment["database"]["name"]}.dump"
111
+ "tmp/#{current_environment["database"]["name"]}.dump"
111
112
  end
112
113
 
113
114
  def seconds_to_human_readable_time(secs)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RemoteDatabaseImporter
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.5"
5
5
  end
@@ -1,7 +1,6 @@
1
1
  namespace :remote_database do
2
2
  desc "Pulls a database to your filesystem"
3
3
  task import: :environment do
4
- importer = RemoteDatabaseImporter::Operation.new
5
- importer.import
4
+ RemoteDatabaseImporter::Operation.new.import
6
5
  end
7
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remote_database_importer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leon Vogt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-06 00:00:00.000000000 Z
11
+ date: 2022-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tty-config
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.9'
41
- - !ruby/object:Gem::Dependency
42
- name: colorize
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '0.8'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '0.8'
55
41
  description: Dump remote databases and import it locally. At the moment only Postgres
56
42
  databases are supported
57
43
  email:
@@ -70,6 +56,7 @@ files:
70
56
  - Rakefile
71
57
  - lib/railtie.rb
72
58
  - lib/remote_database_importer.rb
59
+ - lib/remote_database_importer/colorize.rb
73
60
  - lib/remote_database_importer/config.rb
74
61
  - lib/remote_database_importer/operation.rb
75
62
  - lib/remote_database_importer/version.rb