db_parity 3.6.0
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 +7 -0
- data/README.md +204 -0
- data/bin/development +10 -0
- data/bin/pr_app +19 -0
- data/bin/production +10 -0
- data/bin/staging +10 -0
- data/lib/parity/backup.rb +191 -0
- data/lib/parity/environment.rb +160 -0
- data/lib/parity/heroku_app_name.rb +18 -0
- data/lib/parity/usage.rb +13 -0
- data/lib/parity/version.rb +3 -0
- data/lib/parity.rb +11 -0
- metadata +58 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: '086b1bda374d4a029c91ad9aabab4e9cad3362d3b307fcd29c804938b19abf52'
|
|
4
|
+
data.tar.gz: b1b618fc38f46e2600dd711e272d3e122c09af7e9d7950bdb358dbc81f3e05c0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 326b2167fb52af3cfe0c7fdf91176a6ff81ad3da0deca93e5e80e9f86b13df99b8a7689d2e493c4eae043397b18acf9aca86eca2eb5b5f5b83d3442557e2a7ca
|
|
7
|
+
data.tar.gz: 3c26529ccbd656b02d97ab567955913bb8e057e531fc92742bf643f135ca7ef9b4d3385b6d7d1878cb8228cb8a49d03b4d4c79521f72c37c1f4c8046489dad3a
|
data/README.md
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
Parity
|
|
2
|
+
======
|
|
3
|
+
|
|
4
|
+
[](https://houndci.com)
|
|
5
|
+
|
|
6
|
+
Shell commands for development, staging, and production parity for Heroku apps.
|
|
7
|
+
|
|
8
|
+
Install
|
|
9
|
+
-------
|
|
10
|
+
|
|
11
|
+
Bundle it in your project:
|
|
12
|
+
|
|
13
|
+
gem "db_parity", github: "sethhorsley/db_parity"
|
|
14
|
+
|
|
15
|
+
[releases]: https://github.com/sethhorsley/db_parity/releases
|
|
16
|
+
|
|
17
|
+
Parity requires these command-line programs:
|
|
18
|
+
|
|
19
|
+
git
|
|
20
|
+
heroku
|
|
21
|
+
pg_restore
|
|
22
|
+
|
|
23
|
+
Usage
|
|
24
|
+
-----
|
|
25
|
+
|
|
26
|
+
Backup a database:
|
|
27
|
+
|
|
28
|
+
production backup
|
|
29
|
+
staging backup
|
|
30
|
+
|
|
31
|
+
Restore a production or staging database backup into development:
|
|
32
|
+
|
|
33
|
+
development restore production
|
|
34
|
+
development restore staging
|
|
35
|
+
|
|
36
|
+
Or, if `restore-from` reads better to you, it's the same thing:
|
|
37
|
+
|
|
38
|
+
development restore-from production
|
|
39
|
+
development restore-from staging
|
|
40
|
+
|
|
41
|
+
* Note that the `restore` command will use the most recent backup (from _staging_ or _production_). You may first need to create a more recent backup before restoring, to prevent download of a very old backup.
|
|
42
|
+
|
|
43
|
+
Push your local development database backup up to staging:
|
|
44
|
+
|
|
45
|
+
staging restore development
|
|
46
|
+
|
|
47
|
+
Deploy main to production (note that prior versions of Parity would run
|
|
48
|
+
database migrations, that's now better handled using [Heroku release phase]):
|
|
49
|
+
|
|
50
|
+
production deploy
|
|
51
|
+
|
|
52
|
+
[Heroku release phase]: https://devcenter.heroku.com/articles/release-phase
|
|
53
|
+
|
|
54
|
+
Deploy the current branch to staging:
|
|
55
|
+
|
|
56
|
+
staging deploy
|
|
57
|
+
|
|
58
|
+
_Note that deploys to non-production environments use `git push --force`._
|
|
59
|
+
|
|
60
|
+
Open a console:
|
|
61
|
+
|
|
62
|
+
production console
|
|
63
|
+
staging console
|
|
64
|
+
pr_app 1234 console
|
|
65
|
+
|
|
66
|
+
Migrate a database and restart the dynos:
|
|
67
|
+
|
|
68
|
+
production migrate
|
|
69
|
+
staging migrate
|
|
70
|
+
pr_app 1234 migrate
|
|
71
|
+
|
|
72
|
+
Tail a log:
|
|
73
|
+
|
|
74
|
+
production tail
|
|
75
|
+
staging tail
|
|
76
|
+
pr_app 1234 tail
|
|
77
|
+
|
|
78
|
+
The scripts also pass through, so you can do anything with them that you can do
|
|
79
|
+
with `heroku ______ --remote staging` or `heroku ______ --remote production`:
|
|
80
|
+
|
|
81
|
+
watch production ps
|
|
82
|
+
staging open
|
|
83
|
+
|
|
84
|
+
You can optionally parallelize a DB restore by passing `--parallelize`
|
|
85
|
+
as a flag to the `development` or `production` commands:
|
|
86
|
+
```
|
|
87
|
+
development restore-from production --parallelize
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
To restore development from a backup file you already have (e.g. one downloaded
|
|
91
|
+
with `heroku pg:backups:download`), pass `--file`. The file is not deleted:
|
|
92
|
+
```
|
|
93
|
+
development restore-from production --file latest.dump
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
[2]: http://redis.io/commands
|
|
97
|
+
|
|
98
|
+
Convention
|
|
99
|
+
----------
|
|
100
|
+
|
|
101
|
+
Parity expects:
|
|
102
|
+
|
|
103
|
+
* A `staging` remote pointing to the staging Heroku app.
|
|
104
|
+
* A `production` remote pointing to the production Heroku app.
|
|
105
|
+
```
|
|
106
|
+
heroku git:remote -r staging -a your-staging-app
|
|
107
|
+
heroku git:remote -r production -a your-production-app
|
|
108
|
+
```
|
|
109
|
+
* There is a `config/database.yml` file that can be parsed as YAML for
|
|
110
|
+
`['development']['database']`.
|
|
111
|
+
|
|
112
|
+
Pipelines
|
|
113
|
+
---------
|
|
114
|
+
|
|
115
|
+
If you deploy review applications with Heroku pipelines, run commands against
|
|
116
|
+
those applications with the `pr_app` command, followed by the PR number for your
|
|
117
|
+
application:
|
|
118
|
+
|
|
119
|
+
```
|
|
120
|
+
pr_app 1234 console
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
This command assumes that your review applications have a name derived from the
|
|
124
|
+
name of the application your `staging` Git remote points at.
|
|
125
|
+
|
|
126
|
+
Customization
|
|
127
|
+
-------------
|
|
128
|
+
|
|
129
|
+
If you have Heroku environments beyond staging and production (such as a feature
|
|
130
|
+
environment for each developer), you can add a [binstub] to the `bin` folder of
|
|
131
|
+
your application. Custom environments share behavior with staging: they can be
|
|
132
|
+
backed up and can restore from production.
|
|
133
|
+
|
|
134
|
+
Using feature environments requires including Parity as a gem in your
|
|
135
|
+
application's Gemfile.
|
|
136
|
+
|
|
137
|
+
```ruby
|
|
138
|
+
gem "db_parity", github: "sethhorsley/db_parity"
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
[binstub]: https://github.com/sstephenson/rbenv/wiki/Understanding-binstubs
|
|
142
|
+
|
|
143
|
+
Here's an example binstub for a 'feature-geoff' environment, hosted at
|
|
144
|
+
myapp-feature-geoff.herokuapp.com.
|
|
145
|
+
|
|
146
|
+
```ruby
|
|
147
|
+
#!/usr/bin/env ruby
|
|
148
|
+
|
|
149
|
+
require "parity"
|
|
150
|
+
|
|
151
|
+
if ARGV.empty?
|
|
152
|
+
puts Parity::Usage.new
|
|
153
|
+
else
|
|
154
|
+
Parity::Environment.new("feature-geoff", ARGV).run
|
|
155
|
+
end
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Issues
|
|
159
|
+
------
|
|
160
|
+
Please fill out our [issues template](.github/issue_template.md) if you are
|
|
161
|
+
having problems.
|
|
162
|
+
|
|
163
|
+
Contributing
|
|
164
|
+
------------
|
|
165
|
+
|
|
166
|
+
Please see [`CONTRIBUTING.md`](CONTRIBUTING.md) for details.
|
|
167
|
+
|
|
168
|
+
Version History
|
|
169
|
+
---------------
|
|
170
|
+
|
|
171
|
+
Please see the [releases page](https://github.com/sethhorsley/db_parity/releases)
|
|
172
|
+
for the version history, along with a description of the changes in each
|
|
173
|
+
release.
|
|
174
|
+
|
|
175
|
+
Releasing
|
|
176
|
+
---------
|
|
177
|
+
|
|
178
|
+
See guidelines in [`RELEASING.md`](RELEASING.md) for details
|
|
179
|
+
|
|
180
|
+
License
|
|
181
|
+
-------
|
|
182
|
+
|
|
183
|
+
Parity is © 2013 thoughtbot, inc.
|
|
184
|
+
It is free software,
|
|
185
|
+
and may be redistributed under the terms specified in the [LICENSE] file.
|
|
186
|
+
|
|
187
|
+
[LICENSE]: LICENSE
|
|
188
|
+
|
|
189
|
+
<!-- START /templates/footer.md -->
|
|
190
|
+
## About thoughtbot
|
|
191
|
+
|
|
192
|
+

|
|
193
|
+
|
|
194
|
+
This repo is maintained and funded by thoughtbot, inc.
|
|
195
|
+
The names and logos for thoughtbot are trademarks of thoughtbot, inc.
|
|
196
|
+
|
|
197
|
+
We love open source software!
|
|
198
|
+
See [our other projects][community].
|
|
199
|
+
We are [available for hire][hire].
|
|
200
|
+
|
|
201
|
+
[community]: https://thoughtbot.com/community?utm_source=github
|
|
202
|
+
[hire]: https://thoughtbot.com/hire-us?utm_source=github
|
|
203
|
+
|
|
204
|
+
<!-- END /templates/footer.md -->
|
data/bin/development
ADDED
data/bin/pr_app
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
$LOAD_PATH.unshift File.expand_path(File.join("..", "..", "lib"), __FILE__)
|
|
4
|
+
require "open3"
|
|
5
|
+
require "parity"
|
|
6
|
+
|
|
7
|
+
if ARGV.empty?
|
|
8
|
+
puts Parity::Usage.new
|
|
9
|
+
else
|
|
10
|
+
review_app_number = ARGV.first
|
|
11
|
+
staging_git_remote = Open3.capture3("git remote get-url staging")[0].strip
|
|
12
|
+
review_app_prefix = staging_git_remote.split("/").last.gsub(/\.git\Z/, "")[0, 20]
|
|
13
|
+
|
|
14
|
+
exit Parity::Environment.new(
|
|
15
|
+
"#{review_app_prefix}-pr-#{review_app_number}",
|
|
16
|
+
ARGV.drop(1),
|
|
17
|
+
app_argument: "--app",
|
|
18
|
+
).run
|
|
19
|
+
end
|
data/bin/production
ADDED
data/bin/staging
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
require "etc"
|
|
2
|
+
require "shellwords"
|
|
3
|
+
|
|
4
|
+
module Parity
|
|
5
|
+
class Backup
|
|
6
|
+
BLANK_ARGUMENTS = "".freeze
|
|
7
|
+
DATABASE_YML_RELATIVE_PATH = "config/database.yml".freeze
|
|
8
|
+
DEVELOPMENT_ENVIRONMENT_KEY_NAME = "development".freeze
|
|
9
|
+
DATABASE_KEY_NAME = "database".freeze
|
|
10
|
+
|
|
11
|
+
def initialize(args)
|
|
12
|
+
@from, @to = args.values_at(:from, :to)
|
|
13
|
+
@additional_args = args[:additional_args] || BLANK_ARGUMENTS
|
|
14
|
+
@parallelize = args[:parallelize] || false
|
|
15
|
+
@backup_id = args[:backup_id]
|
|
16
|
+
@file = args[:file]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def restore
|
|
20
|
+
if file && to != DEVELOPMENT_ENVIRONMENT_KEY_NAME
|
|
21
|
+
raise ArgumentError, "--file can only restore to development"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
if to == DEVELOPMENT_ENVIRONMENT_KEY_NAME
|
|
25
|
+
restore_to_development
|
|
26
|
+
elsif from == DEVELOPMENT_ENVIRONMENT_KEY_NAME
|
|
27
|
+
restore_from_development
|
|
28
|
+
else
|
|
29
|
+
restore_to_remote_environment
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
attr_reader :additional_args, :from, :to, :parallelize, :backup_id, :file
|
|
36
|
+
|
|
37
|
+
alias_method :parallelize?, :parallelize
|
|
38
|
+
|
|
39
|
+
def log_restore_info
|
|
40
|
+
if file
|
|
41
|
+
puts "Restoring from file #{file} to #{to}"
|
|
42
|
+
elsif backup_id
|
|
43
|
+
puts "Restoring from #{from} backup ID: #{backup_id} to #{to}"
|
|
44
|
+
else
|
|
45
|
+
puts "Restoring from #{from} (latest backup) to #{to}"
|
|
46
|
+
end
|
|
47
|
+
puts "Starting backup restoration process..."
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def restore_from_development
|
|
51
|
+
log_restore_info
|
|
52
|
+
reset_remote_database
|
|
53
|
+
Kernel.system(
|
|
54
|
+
"heroku pg:push #{development_db} DATABASE_URL --remote #{to} " \
|
|
55
|
+
"#{additional_args}"
|
|
56
|
+
)
|
|
57
|
+
puts "Backup restoration to #{to} completed successfully!"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def restore_to_development
|
|
61
|
+
log_restore_info
|
|
62
|
+
unless file
|
|
63
|
+
ensure_temp_directory_exists
|
|
64
|
+
download_remote_backup
|
|
65
|
+
end
|
|
66
|
+
wipe_development_database
|
|
67
|
+
create_heroku_ext_schema
|
|
68
|
+
restore_from_local_temp_backup
|
|
69
|
+
delete_local_temp_backup unless file
|
|
70
|
+
delete_rails_production_environment_settings
|
|
71
|
+
puts "Backup restoration to #{to} completed successfully!"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def wipe_development_database
|
|
75
|
+
Kernel.system(
|
|
76
|
+
"dropdb --if-exists #{development_db} --force && createdb #{development_db}"
|
|
77
|
+
)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def create_heroku_ext_schema
|
|
81
|
+
Kernel.system(<<~SHELL)
|
|
82
|
+
psql #{development_db} -c "
|
|
83
|
+
CREATE SCHEMA IF NOT EXISTS heroku_ext;
|
|
84
|
+
"
|
|
85
|
+
SHELL
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def reset_remote_database
|
|
89
|
+
Kernel.system(
|
|
90
|
+
"heroku pg:reset --remote #{to} #{additional_args} " \
|
|
91
|
+
"--confirm #{heroku_app_name}"
|
|
92
|
+
)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def heroku_app_name
|
|
96
|
+
HerokuAppName.new(to).to_s
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def ensure_temp_directory_exists
|
|
100
|
+
Kernel.system("mkdir -p tmp")
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def download_remote_backup
|
|
104
|
+
if backup_id
|
|
105
|
+
puts "Downloading backup #{backup_id} from #{from}..."
|
|
106
|
+
Kernel.system(
|
|
107
|
+
"curl -o tmp/#{backup_id}.backup \"$(heroku pg:backups:url #{backup_id} --remote #{from})\""
|
|
108
|
+
)
|
|
109
|
+
else
|
|
110
|
+
puts "Downloading latest backup from #{from}..."
|
|
111
|
+
Kernel.system(
|
|
112
|
+
"curl -o tmp/latest.backup \"$(heroku pg:backups:url --remote #{from})\""
|
|
113
|
+
)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def restore_from_local_temp_backup
|
|
118
|
+
puts "Restoring backup to #{development_db}..."
|
|
119
|
+
# Filter out --backup-id from additional_args as it's not needed for pg_restore
|
|
120
|
+
filtered_args = additional_args.gsub(/--backup-id\s+\S+/, "").strip
|
|
121
|
+
backup_path = file ? Shellwords.escape(file) : "tmp/#{backup_id || "latest"}.backup"
|
|
122
|
+
Kernel.system(
|
|
123
|
+
"pg_restore #{backup_path} --verbose --no-acl --no-owner " \
|
|
124
|
+
"--dbname #{development_db} --jobs=#{processor_cores} " \
|
|
125
|
+
"#{filtered_args}"
|
|
126
|
+
)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def delete_local_temp_backup
|
|
130
|
+
backup_filename = backup_id ? "#{backup_id}.backup" : "latest.backup"
|
|
131
|
+
Kernel.system("rm tmp/#{backup_filename}")
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def delete_rails_production_environment_settings
|
|
135
|
+
Kernel.system(<<-SHELL)
|
|
136
|
+
psql #{development_db} -c "CREATE TABLE IF NOT EXISTS public.ar_internal_metadata (key character varying NOT NULL, value character varying, created_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL, CONSTRAINT ar_internal_metadata_pkey PRIMARY KEY (key)); UPDATE ar_internal_metadata SET value = 'development' WHERE key = 'environment'"
|
|
137
|
+
SHELL
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def restore_to_remote_environment
|
|
141
|
+
log_restore_info
|
|
142
|
+
reset_remote_database
|
|
143
|
+
# Filter out --backup-id from additional_args as it's handled separately
|
|
144
|
+
filtered_args = additional_args.gsub(/--backup-id\s+\S+/, "").strip
|
|
145
|
+
Kernel.system(
|
|
146
|
+
"heroku pg:backups:restore #{backup_from} --remote #{to} " \
|
|
147
|
+
"#{filtered_args}"
|
|
148
|
+
)
|
|
149
|
+
puts "Backup restoration to #{to} completed successfully!"
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def backup_from
|
|
153
|
+
"`#{remote_db_backup_url}` DATABASE"
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def remote_db_backup_url
|
|
157
|
+
if backup_id
|
|
158
|
+
"heroku pg:backups:url #{backup_id} --remote #{from}"
|
|
159
|
+
else
|
|
160
|
+
"heroku pg:backups:url --remote #{from}"
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def development_db
|
|
165
|
+
YAML.safe_load(database_yaml_file, aliases: true)
|
|
166
|
+
.fetch(DEVELOPMENT_ENVIRONMENT_KEY_NAME)
|
|
167
|
+
.fetch(DATABASE_KEY_NAME)
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def database_yaml_file
|
|
171
|
+
# Load Rails environment if Rails is not already loaded
|
|
172
|
+
# This is needed when database.yml uses Rails.application.credentials
|
|
173
|
+
unless defined?(Rails)
|
|
174
|
+
require File.expand_path("config/environment", Dir.pwd)
|
|
175
|
+
end
|
|
176
|
+
ERB.new(IO.read(DATABASE_YML_RELATIVE_PATH)).result
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def processor_cores
|
|
180
|
+
if parallelize? && ruby_version_over_2_2?
|
|
181
|
+
Etc.nprocessors
|
|
182
|
+
else
|
|
183
|
+
1
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def ruby_version_over_2_2?
|
|
188
|
+
Etc.respond_to?(:nprocessors)
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
require "parity/backup"
|
|
2
|
+
|
|
3
|
+
module Parity
|
|
4
|
+
class Environment
|
|
5
|
+
def initialize(environment, subcommands, app_argument: "--remote")
|
|
6
|
+
self.environment = environment
|
|
7
|
+
self.subcommand = subcommands[0]
|
|
8
|
+
self.arguments = subcommands[1..-1]
|
|
9
|
+
self.app_argument = app_argument
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def run
|
|
13
|
+
run_command || false
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
PROTECTED_ENVIRONMENTS = %w(development production)
|
|
19
|
+
|
|
20
|
+
attr_accessor :app_argument, :environment, :subcommand, :arguments
|
|
21
|
+
|
|
22
|
+
def run_command
|
|
23
|
+
if self.class.private_method_defined?(methodized_subcommand)
|
|
24
|
+
send(methodized_subcommand)
|
|
25
|
+
else
|
|
26
|
+
run_via_cli
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def open
|
|
31
|
+
run_via_cli
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def run_via_cli
|
|
35
|
+
Kernel.exec("heroku", subcommand, *arguments, app_argument, environment)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def backup
|
|
39
|
+
Kernel.system("heroku pg:backups:capture #{app_argument} #{environment}")
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def deploy
|
|
43
|
+
if production?
|
|
44
|
+
Kernel.system("git push production #{branch_ref}")
|
|
45
|
+
else
|
|
46
|
+
Kernel.system(
|
|
47
|
+
"git push #{environment} HEAD:#{branch_ref} --force",
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def branch_ref
|
|
53
|
+
main_ref_exists = system("git show-ref --verify --quiet refs/heads/main")
|
|
54
|
+
master_ref_exists = system(
|
|
55
|
+
"git show-ref --verify --quiet refs/heads/master",
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
if main_ref_exists && !master_ref_exists
|
|
59
|
+
"main"
|
|
60
|
+
else
|
|
61
|
+
"master"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def restore
|
|
66
|
+
if production? && !forced?
|
|
67
|
+
$stdout.puts "Parity does not support restoring backups into your "\
|
|
68
|
+
"production environment. Use `--force` to override."
|
|
69
|
+
else
|
|
70
|
+
backup_args = {
|
|
71
|
+
from: arguments.first,
|
|
72
|
+
to: environment,
|
|
73
|
+
parallelize: parallelize?,
|
|
74
|
+
additional_args: additional_restore_arguments,
|
|
75
|
+
}
|
|
76
|
+
backup_args[:backup_id] = backup_id? if backup_id?
|
|
77
|
+
backup_args[:file] = backup_file if backup_file
|
|
78
|
+
|
|
79
|
+
Backup.new(backup_args).restore
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
alias :restore_from :restore
|
|
84
|
+
|
|
85
|
+
def production?
|
|
86
|
+
environment == "production"
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def forced?
|
|
90
|
+
arguments.include?("--force")
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def parallelize?
|
|
94
|
+
arguments.include?("--parallelize")
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def backup_id?
|
|
98
|
+
backup_id_index = arguments.index { |arg| arg == "--backup-id" }
|
|
99
|
+
if backup_id_index && backup_id_index + 1 < arguments.length
|
|
100
|
+
arguments[backup_id_index + 1]
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def backup_file
|
|
105
|
+
file_index = arguments.index("--file")
|
|
106
|
+
arguments[file_index + 1] if file_index
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def additional_restore_arguments
|
|
110
|
+
# Filter out special flags that are handled separately
|
|
111
|
+
filtered_args = arguments.drop(1) - ["--force", "--parallelize", "--file", backup_file]
|
|
112
|
+
# Filter out --backup-id as it's handled separately by the Backup class
|
|
113
|
+
filtered_args = filtered_args.reject { |arg| arg.start_with?("--backup-id") }
|
|
114
|
+
(filtered_args + [restore_confirmation_argument]).compact.join(" ")
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def restore_confirmation_argument
|
|
118
|
+
unless PROTECTED_ENVIRONMENTS.include?(environment) || from_development?
|
|
119
|
+
"--confirm #{heroku_app_name}"
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def from_development?
|
|
124
|
+
arguments.first == "development"
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def console
|
|
128
|
+
Kernel.system(
|
|
129
|
+
command_for_remote(
|
|
130
|
+
"run bundle exec rails console #{arguments.join(' ')}",
|
|
131
|
+
),
|
|
132
|
+
)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def migrate
|
|
136
|
+
Kernel.system(%{
|
|
137
|
+
#{command_for_remote('run rake db:migrate')} &&
|
|
138
|
+
#{command_for_remote('restart')}
|
|
139
|
+
})
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def tail
|
|
143
|
+
Kernel.system(
|
|
144
|
+
command_for_remote("logs --tail #{arguments.join(' ')}"),
|
|
145
|
+
)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def heroku_app_name
|
|
149
|
+
HerokuAppName.new(environment).to_s
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def command_for_remote(command)
|
|
153
|
+
"heroku #{command} #{app_argument} #{environment}"
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def methodized_subcommand
|
|
157
|
+
subcommand.gsub("-", "_").to_sym
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module Parity
|
|
2
|
+
class HerokuAppName
|
|
3
|
+
def initialize(environment)
|
|
4
|
+
@environment = environment
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def to_s
|
|
8
|
+
@heroku_app_name ||= Open3.
|
|
9
|
+
capture3("heroku info --remote #{environment}")[0].
|
|
10
|
+
split("\n")[0].
|
|
11
|
+
gsub(/(\s|=)+/, "")
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
attr_reader :environment
|
|
17
|
+
end
|
|
18
|
+
end
|
data/lib/parity/usage.rb
ADDED
data/lib/parity.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: db_parity
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 3.6.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Dan Croak
|
|
8
|
+
- Geoff Harcourt
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-11-24 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: |2
|
|
14
|
+
Development/staging/production parity makes it easier for
|
|
15
|
+
those who write the code to deploy the code.
|
|
16
|
+
email:
|
|
17
|
+
- ralph@thoughtbot.com
|
|
18
|
+
executables:
|
|
19
|
+
- development
|
|
20
|
+
- staging
|
|
21
|
+
- production
|
|
22
|
+
- pr_app
|
|
23
|
+
extensions: []
|
|
24
|
+
extra_rdoc_files: []
|
|
25
|
+
files:
|
|
26
|
+
- README.md
|
|
27
|
+
- bin/development
|
|
28
|
+
- bin/pr_app
|
|
29
|
+
- bin/production
|
|
30
|
+
- bin/staging
|
|
31
|
+
- lib/parity.rb
|
|
32
|
+
- lib/parity/backup.rb
|
|
33
|
+
- lib/parity/environment.rb
|
|
34
|
+
- lib/parity/heroku_app_name.rb
|
|
35
|
+
- lib/parity/usage.rb
|
|
36
|
+
- lib/parity/version.rb
|
|
37
|
+
homepage: https://github.com/sethhorsley/db_parity
|
|
38
|
+
licenses:
|
|
39
|
+
- MIT
|
|
40
|
+
metadata: {}
|
|
41
|
+
rdoc_options: []
|
|
42
|
+
require_paths:
|
|
43
|
+
- lib
|
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: 2.2.0
|
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
requirements: []
|
|
55
|
+
rubygems_version: 3.6.2
|
|
56
|
+
specification_version: 4
|
|
57
|
+
summary: Shell commands for development, staging, and production parity.
|
|
58
|
+
test_files: []
|