Skip to content

Commit

Permalink
Add Custom Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
leonvogt committed Nov 6, 2022
1 parent fae31e3 commit 9ea190f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.)

Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions lib/remote_database_importer/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions lib/remote_database_importer/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]}"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 9ea190f

Please sign in to comment.