remote_database_importer 0.1.2 → 0.1.4

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: 78760d816e20de850e798b2246f3dd22d43fe2e4223ed305776e2bc0ff8e7ba9
4
- data.tar.gz: 6aa3a9165a3fad0b3659a80bc389f907561c09c08477eb11460e9c3b05297712
3
+ metadata.gz: 28a65a635e4ee78fdbce0dfce253b88a44fa190a887a8d68f928f5193d298fec
4
+ data.tar.gz: c23343009f9e8cf5d819fdf4a2dae6e88e380d30ef7591382fc900bcb1f40e04
5
5
  SHA512:
6
- metadata.gz: ac6d8f895e5ba3ad931b1767ae5e01082b280931917290df70eb2d5f4fbe49861ee62195f3e216d42b25b635b8a7a5da827f8991775ad7724cbdcaf7c1aa0c19
7
- data.tar.gz: 4c6498283f68c0dbc26d5c1b3b6eae4c7af12c01e4d13cc4b876d05ba07dc0d6ac6641d310955510eed1805787bcac9a3e1a201f011a22e8e5ab0b8ef74cd3f6
6
+ metadata.gz: 7b7bd6d20734483e93dcefec67a7890300f6946047003de8a12915a982063d0de086f0b372f188271b6f3c0c51b22bb6b5baac50f25034e7bb548bee5a19817e
7
+ data.tar.gz: 7cc6a60c1141353e8cdf2d910fcab0d0f9c991e56c7cc0fbda513f3461fbaa6ba1abe16f0bcb07f13a99717fed81c519b01455da6b257fa0852cede0366d9e03
data/CHANGELOG.md CHANGED
@@ -1,11 +1,19 @@
1
1
  # Changelog
2
2
 
3
- ## [0.1.2] - 2022-10-29
3
+ ## 0.1.4 (2022-11-17)
4
+ - Remove colorize as a dependency
5
+ - Code refactoring
6
+
7
+ ## 0.1.3 (2022-11-06)
8
+ - Remove Thor as a dependency
9
+ - Add custom commands that run after the successful import of the remote database
10
+
11
+ ## 0.1.2 (2022-10-29)
4
12
 
5
13
  - Terminate current DB sessions after dump remote database. So the chance of an open session is smaller
6
14
  - Print out where the config was saved
7
15
  - Print how long the import went
8
16
 
9
- ## [0.1.0] - 2022-10-24
17
+ ## 0.1.0 (2022-10-24)
10
18
 
11
19
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,9 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- remote_database_importer (0.1.2)
5
- colorize (~> 0.8)
6
- thor (~> 1.2)
4
+ remote_database_importer (0.1.4)
7
5
  tty-config (~> 0.6)
8
6
  tty-spinner (~> 0.9)
9
7
 
@@ -11,7 +9,6 @@ GEM
11
9
  remote: https://rubygems.org/
12
10
  specs:
13
11
  ast (2.4.2)
14
- colorize (0.8.1)
15
12
  diff-lcs (1.5.0)
16
13
  json (2.6.2)
17
14
  parallel (1.22.1)
@@ -53,7 +50,6 @@ GEM
53
50
  standard (1.16.1)
54
51
  rubocop (= 1.35.1)
55
52
  rubocop-performance (= 1.14.3)
56
- thor (1.2.1)
57
53
  tty-config (0.6.0)
58
54
  tty-cursor (0.7.1)
59
55
  tty-spinner (0.9.3)
data/README.md CHANGED
@@ -7,6 +7,7 @@ Its well possible that unexpected errors can occur.
7
7
  ## Features
8
8
  - Define multiple environments (such as staging, production etc.)
9
9
  - Rails intergration via rake task
10
+ - Define custom commands that should run after successful import
10
11
  - Decide for yourself if the dump should be done over ssh or if pg_dump should connect to the DB port directly
11
12
  - It can therefore be used for almost all hosting providers (Heroku, Kubernetes, self-hosted, etc.)
12
13
 
@@ -43,8 +44,10 @@ When you first run the rake task, it will dynamically create this file for you.
43
44
  ![Config sample](readme_assets/config_sample.png)
44
45
 
45
46
  ### DB Access
46
- The easiest and fastest way is to exchange your ssh-key with the server beforehand, so you don't have to enter a password.
47
- Otherwise during the rake task execution a password entry is required.
47
+ The dump of the remote database happens with the tool: [pg_dump](https://www.postgresql.org/docs/current/app-pgdump.html).
48
+ There are two opions for connecting to the remote databse:
49
+ - pg_dump connects directly to the databse
50
+ - pg_dump connects through a shh tunnel to the databse
48
51
 
49
52
  The effective dump call is as follows:
50
53
  ```ruby
@@ -53,9 +56,14 @@ or
53
56
  "pg_dump -Fc 'host=HOST dbname=DB_NAME user=DB_USER port=POSTGRES_PORT' > DB_DUMP_LOCATION"
54
57
  ```
55
58
 
59
+ ### Password Input
60
+ Because this gem doesn't store passwords you will have to enter the passwords manually during the rake task.
61
+ Depending on the choosen connection model the password prompt will be for the ssh connection or the DB access.
62
+ If you choose to dump the databse over an ssh_tunnel, the easiest way will be to exchange your ssh-key with the server beforehand, so you don't have to enter a password.
63
+
56
64
  ## Limitations
57
65
  - At the moment only Postgres databases are supported
58
- - It has to run inside a Rails app. There is a CLI command `remote_database_importer import` as an alternative to the rake task, but there are still some Rails commands like `rails db:drop db:create - rails db:migrate`, which makes it currently not possible to use the gem outside of Rails
66
+ - It has to run inside a Rails app.
59
67
  - Not suitable for very large databases, you could run into SSH timeouts
60
68
 
61
69
  ## Contributing
@@ -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
5
 
6
+ attr_accessor :config
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
 
@@ -77,7 +80,7 @@ module RemoteDatabaseImporter
77
80
  }
78
81
  }
79
82
  }
80
- @config.append(env_config, to: :environments)
83
+ config.append(env_config, to: :environments)
81
84
 
82
85
  continue = ask("Do you wanna add another environment? (anything other than 'yes' will exit)")
83
86
  if continue&.downcase == "yes"
@@ -87,7 +90,12 @@ module RemoteDatabaseImporter
87
90
  end
88
91
  end
89
92
 
90
- @config.write
93
+ puts Colorize.green("Define custom commands that run after successful import:")
94
+ custom_commands = ask("Enter semicolon separated commands that should run after importing the DB:", default: "rake db:migrate; echo 'All Done'")
95
+ puts
96
+
97
+ config.set(:custom_commands, value: custom_commands)
98
+ config.write
91
99
  end
92
100
  end
93
101
  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
@@ -54,9 +56,9 @@ module RemoteDatabaseImporter
54
56
  {name: "Terminate current DB sessions", command: terminate_current_db_sessions},
55
57
  {name: "Drop and create local DB", command: drop_and_create_local_db},
56
58
  {name: "Restore remote DB", command: restore_db},
57
- {name: "Run migrations", command: run_migrations},
58
59
  {name: "Remove logfile", command: remove_logfile},
59
- {name: "Remove dumpfile", command: remove_dumpfile}
60
+ {name: "Remove dumpfile", command: remove_dumpfile},
61
+ {name: "Custom commands", command: custom_commands}
60
62
  ]
61
63
  tasks.each.with_index(1) do |task, index|
62
64
  task[:spinner] = multi_spinner.register "#{index}/#{tasks.length} :spinner #{task[:name]}"
@@ -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,11 +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
- end
96
-
97
- def run_migrations
98
- "rake db:migrate > #{LOG_FILE}"
95
+ "pg_restore --jobs 8 --no-privileges --no-owner --dbname #{config.fetch("local_db_name")} #{db_dump_location}"
99
96
  end
100
97
 
101
98
  def remove_logfile
@@ -106,8 +103,12 @@ module RemoteDatabaseImporter
106
103
  "rm #{db_dump_location}"
107
104
  end
108
105
 
106
+ def custom_commands
107
+ config.fetch("custom_commands")
108
+ end
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.2"
4
+ VERSION = "0.1.4"
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,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remote_database_importer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
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-10-29 00:00:00.000000000 Z
11
+ date: 2022-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: thor
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.2'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.2'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: tty-config
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -52,26 +38,11 @@ dependencies:
52
38
  - - "~>"
53
39
  - !ruby/object:Gem::Version
54
40
  version: '0.9'
55
- - !ruby/object:Gem::Dependency
56
- name: colorize
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '0.8'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '0.8'
69
41
  description: Dump remote databases and import it locally. At the moment only Postgres
70
42
  databases are supported
71
43
  email:
72
44
  - nonick@nonick.ch
73
- executables:
74
- - remote_database_importer
45
+ executables: []
75
46
  extensions: []
76
47
  extra_rdoc_files: []
77
48
  files:
@@ -83,16 +54,15 @@ files:
83
54
  - LICENSE.txt
84
55
  - README.md
85
56
  - Rakefile
86
- - bin/remote_database_importer
87
57
  - lib/railtie.rb
88
58
  - lib/remote_database_importer.rb
59
+ - lib/remote_database_importer/colorize.rb
89
60
  - lib/remote_database_importer/config.rb
90
61
  - lib/remote_database_importer/operation.rb
91
62
  - lib/remote_database_importer/version.rb
92
63
  - lib/tasks/remote_database_importer.rake
93
64
  - readme_assets/config_sample.png
94
65
  - readme_assets/import-job.gif
95
- - readme_images/.DS_Store
96
66
  - sig/remote_database_importer.rbs
97
67
  homepage: https://github.com/leon-vogt/remote_database_importer
98
68
  licenses:
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'thor'
4
- require_relative '../lib/remote_database_importer/operation'
5
-
6
- class RemoteDatabaseImporterCLI < Thor
7
- desc "Restore remote database and import it locally", "asdf"
8
- def import
9
- importer = RemoteDatabaseImporter::Operation.new
10
- importer.import
11
- end
12
- end
13
-
14
- RemoteDatabaseImporterCLI.start
Binary file