Skip to content

Commit 52f5772

Browse files
authored
Merge pull request #120 from zedtux/features/import-db-from-a-local-file
Adds a task using a local file (Closes #85)
2 parents 54164f9 + c0fad3d commit 52f5772

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,27 @@ db:remote:sync || db:push # Synchronize your remote database using loc
8080

8181
## Example
8282

83-
```
83+
#### Replace your local database with the production database
84+
85+
This use case allows you to have the same data on your machine as your production.
86+
You then can reproduce or test things before to apply to your production.
87+
88+
```bash
8489
cap db:pull
8590
cap production db:pull # if you are using capistrano-ext to have multistages
8691
```
8792

93+
#### Replace your local database using a dump file stored on your machine
94+
95+
In case you have a dump file on your machine (you used `db:pull` and kept some files)
96+
and you want to replay one of them, you can use the `db:local:load`:
97+
98+
```
99+
cap development db:local:load DUMP_FILE=db/myapp_production_2018-01-10-150434.sql
100+
```
101+
(You have to create a `config/deploy/development.rb` file containing
102+
`set :stage, :development` at least in order to get this working)
103+
88104
## Contributors
89105

90106
* tilsammans (http://github.com/tilsammansee)

lib/capistrano-db-tasks/database.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,20 @@ def execute(cmd)
199199
end
200200

201201
class << self
202-
def check(local_db, remote_db)
203-
raise 'Only mysql or postgresql on remote and local server is supported' unless (local_db.mysql? && remote_db.mysql?) || (local_db.postgresql? && remote_db.postgresql?)
202+
def check(local_db, remote_db = nil)
203+
return if mysql_db_valid?(local_db, remote_db)
204+
return if postgresql_db_valid?(local_db, remote_db)
205+
206+
raise 'Only mysql or postgresql on remote and local server is supported'
207+
end
208+
209+
def mysql_db_valid?(local_db, remote_db)
210+
local_db.mysql? && (remote_db.nil? || remote_db && remote_db.mysql?)
211+
end
212+
213+
def postgresql_db_valid?(local_db, remote_db)
214+
local_db.postgresql? &&
215+
(remote_db.nil? || (remote_db && remote_db.postgresql?))
204216
end
205217

206218
def remote_to_local(instance)
@@ -227,5 +239,13 @@ def local_to_remote(instance)
227239
remote_db.load(local_db.output_file, instance.fetch(:db_local_clean))
228240
File.unlink(local_db.output_file) if instance.fetch(:db_local_clean)
229241
end
242+
243+
def local_to_local(instance, dump_file)
244+
local_db = Database::Local.new(instance)
245+
246+
check(local_db)
247+
248+
local_db.load(dump_file, instance.fetch(:db_local_clean))
249+
end
230250
end
231251
end

lib/capistrano-db-tasks/dbtasks.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@
4343
end
4444
end
4545
end
46+
47+
desc 'Replace your local database using a dump file from the DUMP_FILE ' \
48+
'environment variable'
49+
task :load do
50+
run_locally do
51+
if ENV['DUMP_FILE'].nil?
52+
raise 'You must give a dump file using the DUMP_FILE environment ' \
53+
'variable'
54+
end
55+
56+
unless File.exist?(ENV['DUMP_FILE'])
57+
raise "File #{ENV['DUMP_FILE']} doesn't exists"
58+
end
59+
60+
if fetch(:skip_data_sync_confirm) ||
61+
Util.prompt('Are you sure you want to erase your local database ' \
62+
"with the dump file #{ENV['DUMP_FILE']}")
63+
Database.local_to_local(self, ENV['DUMP_FILE'])
64+
end
65+
end
66+
end
4667
end
4768

4869
desc 'Synchronize your local database using remote database data'

0 commit comments

Comments
 (0)