pgclone 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2474bcd73e35f5a44843eca0dc277ca258dcd9f6
4
+ data.tar.gz: c326dbac4f21db2e000a97491389770b6d0cef82
5
+ SHA512:
6
+ metadata.gz: e63e80322ec29399883dfecbdb1d4520df38beeea74cf7e16a1ee4c3e2103d5a46d24bc871bfb8a6de10d09d66b5ae4a54f92259ec5e7e4f9b3c25555d2bc20f
7
+ data.tar.gz: 2e34346d8d37b382e7515891df09a87b2b866d52f66703c43763a0c068aa2053532f4c2ec6d8471e42cf1e8c1b92217eaaccd7660b8548701aa76231bb0f0748
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pgclone.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # PG Clone
2
+
3
+ This is a dead simple Postgres/Heroku cloning gem, to make it easier to pull database information from production to local.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pgclone', git: "git://github.com/sashafklein/pgclone.git"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ The gem is usable either from the command line or from your Rails app.
20
+
21
+ ### Command Line
22
+
23
+ Just call `pgclone` with the required arguments specified (or `-h` for more info). Those arguments are `--appname` (your Heroku appname), `--owner` (the local database owner), and `--local-db` (the local database name). PG Clone will take care of the rest:
24
+
25
+ ```bash
26
+ $ pgclone -a my-heroku-app -o my-db-owner-username -l app_development
27
+ ```
28
+
29
+ ### In Rails App
30
+
31
+ Set the mandatory configuration options in a config file:
32
+
33
+ ```ruby
34
+ # config/initializers/p_g_clone.rb
35
+ Pgclone.configure do |config|
36
+ config.appname = 'my-heroku-app'
37
+ config.owner = 'my-db-owner-username'
38
+ config.local_db = 'app_development'
39
+ config.file = 'temporary-dumpfile.dump' # optional
40
+ end
41
+ ```
42
+
43
+ Then pull the data from your app like so:
44
+
45
+ ```ruby
46
+ Pgclone::Restore.new.go!
47
+ ```
48
+
49
+ Or call directly with an options hash:
50
+
51
+ ```ruby
52
+ Pgclone::Restore.new({ appname: 'whatever' }).go!
53
+ ```
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it ( https://github.com/sashafklein/pgclone/fork )
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pgclone"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/pgclone ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "pgclone"
4
+ require 'trollop'
5
+
6
+ flags = {
7
+ appname: "Heroku app name (required).",
8
+ owner: 'Local database owner (required).',
9
+ local_db: 'Local database name (required).',
10
+ file: 'File to save temporary DB dump to.'
11
+ }
12
+
13
+ opts = Trollop::options do
14
+ flags.each do |k, v|
15
+ opt k, v, short: k.to_s.chars.first, type: :string, required: k != :file
16
+ end
17
+ end
18
+
19
+ Pgclone::Restore.new( opts ).go!
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/pgclone.rb ADDED
@@ -0,0 +1,71 @@
1
+ require "pgclone/version"
2
+
3
+ module Pgclone
4
+
5
+ class Configuration
6
+ attr_accessor :owner, :appname, :local_db, :file
7
+ end
8
+
9
+ class << self
10
+ attr_writer :configuration
11
+ end
12
+
13
+ def self.configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+
17
+ def self.configure
18
+ yield( configuration )
19
+ end
20
+
21
+ class Restore
22
+ attr_accessor :appname, :owner, :local_db, :file
23
+ def initialize(opts={})
24
+ @appname = opts[:appname] || config(:appname)
25
+ @owner = opts[:owner] || config(:owner)
26
+ @local_db = opts[:local_db] || config(:local_db)
27
+ @file = opts[:file] || config(:file) || 'latest.dump'
28
+ raise_missing_keys!
29
+ end
30
+
31
+ def config(key)
32
+ Pgclone.configuration.send(key)
33
+ end
34
+
35
+ def go!
36
+ capture_backup
37
+ grab_dump
38
+ restore_db
39
+ end
40
+
41
+ private
42
+
43
+ def raise_missing_keys!
44
+ [:appname, :owner, :local_db].each do |key|
45
+ raise "Missing required option: #{key}." unless instance_variable_get("@#{ key }")
46
+ end
47
+ end
48
+
49
+ def capture_backup
50
+ puts "Capturing new backup"
51
+ `heroku pg:backups capture --app #{appname}`
52
+ puts "Captured the production database"
53
+ end
54
+
55
+ def grab_dump
56
+ "Finding DB URL"
57
+ url = `heroku pg:backups public-url --app #{appname}`.split(" ").last.strip
58
+ puts "Dumping database to #{file}"
59
+ `curl -o #{file} "#{url}"`
60
+ end
61
+
62
+ def restore_db
63
+ puts "Restoring local DB"
64
+ `pg_restore --verbose --clean --no-acl --no-owner -h localhost -U #{owner} -d #{local_db} #{file}`
65
+
66
+ puts "Local database restored"
67
+ `rm #{file}`
68
+ puts "#{file} removed"
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ module Pgclone
2
+ VERSION = "0.1.1"
3
+ end
data/pgclone.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pgclone/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pgclone"
8
+ spec.version = Pgclone::VERSION
9
+ spec.authors = ["Sasha Klein"]
10
+ spec.email = ["sashafklein@gmail.com"]
11
+
12
+ spec.summary = %q{A simple tool for cloning a production database to local.}
13
+ spec.description = %q{For PostgreSQL only.}
14
+ spec.homepage = "https://www.github.com/sashafklein/pgclone"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.executables = ['pgclone']
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.test_files = spec.files.grep(%r{^(spec)/})
21
+ spec.add_development_dependency "bundler", "~> 1.9"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_runtime_dependency "trollop", '2.1.2'
24
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pgclone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Sasha Klein
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: trollop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 2.1.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 2.1.2
55
+ description: For PostgreSQL only.
56
+ email:
57
+ - sashafklein@gmail.com
58
+ executables:
59
+ - pgclone
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/pgclone
71
+ - bin/setup
72
+ - lib/pgclone.rb
73
+ - lib/pgclone/version.rb
74
+ - pgclone.gemspec
75
+ homepage: https://www.github.com/sashafklein/pgclone
76
+ licenses: []
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.6
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: A simple tool for cloning a production database to local.
98
+ test_files: []