heroku_pg_loader 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a6cf49270202e1edc2a5ca3f0858eef8cf6ee294
4
+ data.tar.gz: ab6a2255562156f612d9e2795c0e853b512fc178
5
+ SHA512:
6
+ metadata.gz: 56f6d0cef29e38ef5f9850bf25a32edcfef3295f834b38c2a437e069a2e571980c9a5c9d08314c8dca877053b276b10a625e42c3ef21ed194a5171892f2900d7
7
+ data.tar.gz: 494c4f30fcd70433f76d332350ab43bf1ec6c7625eb0b8a3a744926950778f0b677f5cd8e8c1361468a0bc4bcbf0d9009f6fd2064ba8e46ac17bc279c311fc88
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'heroku_pg_loader'
4
+
5
+ HerokuPgLoader.new.start
@@ -0,0 +1,118 @@
1
+ require 'yaml'
2
+ require 'uri'
3
+
4
+ class HerokuPgLoader
5
+
6
+ def start
7
+ begin
8
+ get_app_name
9
+ get_development_database
10
+
11
+ load_database
12
+
13
+
14
+ rescue Exception => e
15
+ puts "Error: #{e}"
16
+ ensure
17
+ dump_file.close
18
+ dump_file.delete
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def get_url_contents(url_string)
25
+ require 'open-uri'
26
+ open(url_string).read
27
+ end
28
+
29
+ def load_database
30
+ run_command "heroku pgbackups:capture --app #{@heroku_app_name} --expire"
31
+
32
+ heroku_backup_url = run_command "heroku pgbackups:url --app #{@heroku_app_name}"
33
+
34
+ puts "Dumping to: #{dump_file.path}"
35
+
36
+ dump_file.write get_url_contents(heroku_backup_url)
37
+
38
+ run_command "echo \"DROP DATABASE #{@db_name}; CREATE DATABASE #{@db_name};\" | psql -U #{@db_username} -d postgres"
39
+
40
+ run_command "pg_restore --verbose --clean --no-acl --no-owner -h 127.0.0.1 -U #{@db_username} -d #{@db_name} #{dump_file.path}"
41
+ end
42
+
43
+ def dump_file
44
+ if @dump_file.nil?
45
+ path = "#{working_dir}/latest.dump"
46
+ puts "Creating dump file #{path}"
47
+ @dump_file = File.open(path, 'w')
48
+ end
49
+ @dump_file
50
+ end
51
+
52
+ def run_command(command)
53
+ puts "[Running] #{command}"
54
+ output = `#{command}`; result = $?.success?
55
+ raise "Error" unless result
56
+
57
+ output
58
+ end
59
+
60
+ def get_development_database
61
+
62
+ # Read database.yml
63
+ db_config = YAML.load_file("#{working_dir}/config/database.yml")
64
+
65
+ dev_config = db_config["development"]
66
+ if dev_config["adapter"] == "postgresql"
67
+ @db_name = dev_config["database"]
68
+ @db_username = dev_config["username"]
69
+ @db_password = dev_config["password"]
70
+ end
71
+
72
+ @db_name = prompt "Local Database Name:\n (WARNING: THIS DATABASE WILL BE ERASED)",
73
+ @db_name,
74
+ "Database name not entered"
75
+
76
+ @db_username = prompt "Database Username:",
77
+ @db_username,
78
+ "Database username not entered"
79
+
80
+ @db_password = prompt "Database Password:",
81
+ @db_password,
82
+ "Database Password not entered"
83
+ end
84
+
85
+ def working_dir
86
+ File.expand_path File.dirname(__FILE__)
87
+ end
88
+
89
+ def prompt(message, current_value, error_message)
90
+ puts "#{message} [#{current_value}]"
91
+ print ">"
92
+ input = gets.chomp
93
+ current_value = input if input != ""
94
+ raise error_message if current_value.nil?
95
+
96
+ current_value
97
+ end
98
+
99
+ def get_app_name
100
+ detect_app_name
101
+
102
+ @heroku_app_name = prompt "Heroku App Name:",
103
+ @heroku_app_name,
104
+ "No Heroku App Name entered"
105
+ end
106
+
107
+ def detect_app_name
108
+ git_remote = `git remote -v | grep heroku`
109
+
110
+ /git@heroku.com:(\w*).git/.match(git_remote)
111
+ @heroku_app_name = $1
112
+
113
+ if @heroku_app_name
114
+ puts "Heroku App Detected: #{@heroku_app_name}"
115
+ end
116
+ end
117
+
118
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heroku_pg_loader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gerry Eng
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple gem toload your Heroku Postgres production database into a local
14
+ database
15
+ email: m@gerryeng.com
16
+ executables:
17
+ - heroku_pg_load
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/heroku_pg_loader.rb
22
+ - bin/heroku_pg_load
23
+ homepage: http://gerryeng.com
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.0.3
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: A simple gem to load your Heroku Postgres production database into a local
47
+ database
48
+ test_files: []