From 9ea190f6103bcaf866d2fc397328d4e601d28c46 Mon Sep 17 00:00:00 2001 From: Leon Date: Sun, 6 Nov 2022 16:34:19 +0100 Subject: [PATCH] Add Custom Commands --- README.md | 12 ++++++++++-- lib/remote_database_importer/config.rb | 5 +++++ lib/remote_database_importer/operation.rb | 12 ++++++------ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 56e24b1..5b1b98d 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Its well possible that unexpected errors can occur. ## Features - Define multiple environments (such as staging, production etc.) - Rails intergration via rake task +- Define custom commands that should run after successful import - Decide for yourself if the dump should be done over ssh or if pg_dump should connect to the DB port directly - It can therefore be used for almost all hosting providers (Heroku, Kubernetes, self-hosted, etc.) @@ -43,8 +44,10 @@ When you first run the rake task, it will dynamically create this file for you. ![Config sample](readme_assets/config_sample.png) ### DB Access -The easiest and fastest way is to exchange your ssh-key with the server beforehand, so you don't have to enter a password. -Otherwise during the rake task execution a password entry is required. +The dump of the remote database happens with the tool: [pg_dump](https://www.postgresql.org/docs/current/app-pgdump.html). +There are two opions for connecting to the remote databse: +- pg_dump connects directly to the databse +- pg_dump connects through a shh tunnel to the databse The effective dump call is as follows: ```ruby @@ -53,6 +56,11 @@ or "pg_dump -Fc 'host=HOST dbname=DB_NAME user=DB_USER port=POSTGRES_PORT' > DB_DUMP_LOCATION" ``` +### Password Input +Because this gem doesn't store passwords you will have to enter the passwords manually during the rake task. +Depending on the choosen connection model the password prompt will be for the ssh connection or the DB access. +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. + ## Limitations - At the moment only Postgres databases are supported - It has to run inside a Rails app. diff --git a/lib/remote_database_importer/config.rb b/lib/remote_database_importer/config.rb index 44da4cb..130e224 100644 --- a/lib/remote_database_importer/config.rb +++ b/lib/remote_database_importer/config.rb @@ -87,6 +87,11 @@ def create_default_config end end + puts "Define custom commands that run after successful import:".colorize(:green) + custom_commands = ask("Enter semicolon separated commands that should run after importing the DB:", default: "rake db:migrate; echo 'All Done'") + puts + + @config.set(:custom_commands, value: custom_commands) @config.write end end diff --git a/lib/remote_database_importer/operation.rb b/lib/remote_database_importer/operation.rb index d106561..669f78c 100644 --- a/lib/remote_database_importer/operation.rb +++ b/lib/remote_database_importer/operation.rb @@ -54,9 +54,9 @@ def create_tasks_and_spinners(multi_spinner) {name: "Terminate current DB sessions", command: terminate_current_db_sessions}, {name: "Drop and create local DB", command: drop_and_create_local_db}, {name: "Restore remote DB", command: restore_db}, - {name: "Run migrations", command: run_migrations}, {name: "Remove logfile", command: remove_logfile}, - {name: "Remove dumpfile", command: remove_dumpfile} + {name: "Remove dumpfile", command: remove_dumpfile}, + {name: "Custom commands", command: custom_commands} ] tasks.each.with_index(1) do |task, index| task[:spinner] = multi_spinner.register "#{index}/#{tasks.length} :spinner #{task[:name]}" @@ -94,10 +94,6 @@ def restore_db "pg_restore --jobs 8 --no-privileges --no-owner --dbname #{@config.fetch("local_db_name")} #{db_dump_location}" end - def run_migrations - "rake db:migrate > #{LOG_FILE}" - end - def remove_logfile "rm #{LOG_FILE}" end @@ -106,6 +102,10 @@ def remove_dumpfile "rm #{db_dump_location}" end + def custom_commands + @config.fetch("custom_commands") + end + def db_dump_location "tmp/#{@current_environment["database"]["name"]}.dump" end