database_syncer 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -16
- data/Rakefile +1 -1
- data/bin/console +3 -3
- data/database_syncer.gemspec +2 -0
- data/lib/database_syncer.rb +1 -1
- data/lib/database_syncer/version.rb +1 -1
- data/lib/tasks/data_importer.rake +32 -13
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43b9d28ffb72fff53b6057d82e94f2f37656c3d8
|
4
|
+
data.tar.gz: 5bb81101fe4073e05f4f61a49a5ffef158fdf5b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a99eae04f84ab2a882417e1fc4a0bb193f4656cbd590d7cea1bac4eee9c66420cffc54013b053267d1a58886c891f4df40b85763fd1db848aed5b886efba314f
|
7
|
+
data.tar.gz: c82bdddd328f8924a406b2f9ee444a7db05906910367cf4ce825a09f379b35fb0f5565c439164ac417e5d712f04820b8c5eb727fe2ba470db4d977140f27e595
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# DatabaseSyncer
|
2
2
|
|
3
|
-
This gem provides a rake task to simply sync the local database with the production database on Heroku. The gem in meant to be used on a Rails project.
|
3
|
+
This gem provides a rake task to simply sync the local Postgres database with the production database on Heroku. The gem in meant to be used on a Rails project.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -14,27 +14,23 @@ And then execute:
|
|
14
14
|
|
15
15
|
$ bundle
|
16
16
|
|
17
|
+
## Requirements
|
18
|
+
Git remotes on the specific app this gem is included in are used to determine the app names for the heroku production and staging apps.
|
19
|
+
|
20
|
+
##### Remotes must be named as follows:
|
21
|
+
* heroku (for production which is the default)
|
22
|
+
* staging (for staging)
|
23
|
+
|
17
24
|
## Usage
|
18
25
|
|
19
|
-
|
20
|
-
```console
|
21
|
-
rake db:sync:local["postgres://..."]
|
22
|
-
```
|
23
|
-
The "postgres://..." sting should be your Heroku DATABASE_URL and can be found by running this command:
|
24
|
-
```console
|
25
|
-
heroku config:get DATABASE_URL -a app-name
|
26
|
-
```
|
27
|
-
---
|
28
|
-
You can also get these instructions by running:
|
26
|
+
##### Sync the local database from the heroku production database:
|
29
27
|
```console
|
30
28
|
rake db:sync:local
|
31
29
|
```
|
32
|
-
|
30
|
+
|
31
|
+
##### Sync the staging heroku database from the heroku production database:
|
33
32
|
```console
|
34
|
-
|
35
|
-
$ heroku config:get DATABASE_URL -a app-name
|
36
|
-
copy the DATABASE_URL variable and then call the rake task again
|
37
|
-
$ rake db:sync:local["postgres://svym..."]
|
33
|
+
rake db:sync:staging
|
38
34
|
```
|
39
35
|
|
40
36
|
## License
|
data/Rakefile
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
data/bin/console
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'database_syncer'
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +10,5 @@ require "database_syncer"
|
|
10
10
|
# require "pry"
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
require
|
13
|
+
require 'irb'
|
14
14
|
IRB.start
|
data/database_syncer.gemspec
CHANGED
@@ -19,6 +19,8 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
20
|
spec.require_paths = ['lib']
|
21
21
|
|
22
|
+
spec.add_dependency 'uiux'
|
23
|
+
|
22
24
|
spec.add_development_dependency 'bundler', '~> 1.10'
|
23
25
|
spec.add_development_dependency 'rake', '~> 10.0'
|
24
26
|
end
|
data/lib/database_syncer.rb
CHANGED
@@ -2,26 +2,39 @@ namespace :db do
|
|
2
2
|
namespace :sync do
|
3
3
|
desc 'This task updates the local development database with the production data from Heroku'
|
4
4
|
task :local, [:heroku_config_database_url] => :environment do |_t, args|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
UI.start 'Syncing the local database from the heroku production one...' do
|
6
|
+
UI.heading 'Getting the heroku database url...'
|
7
|
+
match_database(args[:heroku_config_database_url])
|
8
|
+
|
9
|
+
UI.heading 'Copying the latest data from the production site...'
|
10
|
+
download_remote_data
|
11
|
+
|
12
|
+
UI.heading 'Importing the data in the local database...'
|
9
13
|
set_local_data
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'This task updates the staging database with the production data from Heroku'
|
18
|
+
task staging: :environment do
|
19
|
+
UI.start 'Syncing the staging database with the produciton data from Heroku...' do
|
20
|
+
staging_app_name = app_name('staging')
|
21
|
+
system "heroku pg:copy #{app_name}::DATABASE_URL DATABASE_URL -a #{staging_app_name} --confirm #{staging_app_name}"
|
16
22
|
end
|
17
23
|
end
|
18
24
|
|
19
25
|
private
|
20
26
|
|
21
|
-
def
|
27
|
+
def match_database(database_url)
|
28
|
+
database_url ||= `heroku config:get DATABASE_URL -a #{app_name}`.chomp
|
29
|
+
@db = database_url.match(%r{^postgres://(.*?):(.*?)@(.*?):\d*?/(.*?)$})
|
30
|
+
fail 'Invalid database url format' unless @db
|
31
|
+
UI.message @db
|
32
|
+
end
|
33
|
+
|
34
|
+
def download_remote_data
|
22
35
|
@export_file = File.join('tmp', 'db-data-dump.sql')
|
23
|
-
db
|
24
|
-
|
36
|
+
system "export PGPASSWORD=\"#{@db[2]}\"; pg_dump -U#{@db[1]} -h#{@db[3]} -a -Tschema_migrations #{@db[4]} > #{@export_file}"
|
37
|
+
UI.message "DB dump temporarily saved at: #{@export_file}"
|
25
38
|
end
|
26
39
|
|
27
40
|
def set_local_data
|
@@ -32,5 +45,11 @@ namespace :db do
|
|
32
45
|
system "psql -h localhost #{database} -f #{@export_file}"
|
33
46
|
`rm #{@export_file}`
|
34
47
|
end
|
48
|
+
|
49
|
+
def app_name(remote_name = 'heroku')
|
50
|
+
`git remote -v`.split("\n").map do |remote|
|
51
|
+
remote.match(%r{^#{remote_name}\s+(?:https:\/\/)?git[@\.]heroku\.com[:\/](?<app_name>.+)\.git\s+\(push\)})
|
52
|
+
end.compact.first[:app_name]
|
53
|
+
end
|
35
54
|
end
|
36
55
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: database_syncer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Endri Gjiri
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: uiux
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -77,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
91
|
version: '0'
|
78
92
|
requirements: []
|
79
93
|
rubyforge_project:
|
80
|
-
rubygems_version: 2.
|
94
|
+
rubygems_version: 2.4.5.1
|
81
95
|
signing_key:
|
82
96
|
specification_version: 4
|
83
97
|
summary: Sync the local database with production data from Heroku
|