planetscale_rails 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +7 -1
- data/Gemfile.lock +4 -2
- data/README.md +14 -6
- data/actions-example.md +1 -1
- data/lib/planetscale_rails/tasks/psdb.rake +18 -8
- data/lib/planetscale_rails/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e1c96f1384c6bcee4a33e81124384b80ce8cff99823b1d5fc77ad926967087a4
|
4
|
+
data.tar.gz: '0880e54628a3c0b22a98f1883dce8fd6ac4aee57315fffab014d07c9da66e014'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4fabd32053c4456bdd002729f21fd9548905a7fc8472a97dd23fb0f1aed9df5798381cae839fd4495b0acacdc97170c47b92312cc38c78a0a31df3911097765
|
7
|
+
data.tar.gz: 2f50ab3be8305f60c81c11d625f470775678bf311d103c1301d982fb1aa5f6c39ac8b176f21bd8eb4ea511098f22e8a77a3b09cdb459130ea60bdddcee37ae57
|
data/.rubocop.yml
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
AllCops:
|
2
2
|
TargetRubyVersion: 3
|
3
3
|
|
4
|
+
Metrics/CyclomaticComplexity:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
Metrics/PerceivedComplexity:
|
8
|
+
Enabled: false
|
9
|
+
|
4
10
|
Style/StringLiterals:
|
5
11
|
Enabled: true
|
6
12
|
EnforcedStyle: double_quotes
|
@@ -10,7 +16,7 @@ Style/StringLiteralsInInterpolation:
|
|
10
16
|
EnforcedStyle: double_quotes
|
11
17
|
|
12
18
|
Layout/LineLength:
|
13
|
-
|
19
|
+
Enabled: false
|
14
20
|
|
15
21
|
Metrics/MethodLength:
|
16
22
|
Enabled: false
|
data/Gemfile.lock
CHANGED
@@ -108,12 +108,14 @@ GEM
|
|
108
108
|
net-smtp (0.3.3)
|
109
109
|
net-protocol
|
110
110
|
nio4r (2.5.9)
|
111
|
-
nokogiri (1.15.
|
111
|
+
nokogiri (1.15.4-x86_64-darwin)
|
112
|
+
racc (~> 1.4)
|
113
|
+
nokogiri (1.15.4-x86_64-linux)
|
112
114
|
racc (~> 1.4)
|
113
115
|
parallel (1.22.1)
|
114
116
|
parser (3.2.1.0)
|
115
117
|
ast (~> 2.4.1)
|
116
|
-
racc (1.7.
|
118
|
+
racc (1.7.3)
|
117
119
|
rack (2.2.7)
|
118
120
|
rack-test (2.1.0)
|
119
121
|
rack (>= 1.3)
|
data/README.md
CHANGED
@@ -33,12 +33,11 @@ And then execute in your terminal:
|
|
33
33
|
bundle install
|
34
34
|
```
|
35
35
|
|
36
|
-
|
36
|
+
Make sure you have the [`pscale` CLI installed](https://github.com/planetscale/cli#installation).
|
37
37
|
|
38
|
-
|
38
|
+
## Usage
|
39
39
|
|
40
|
-
1.
|
41
|
-
that this is the branch you want to migrate.
|
40
|
+
1. Using pscale, create a new branch. This command will create a local `.pscale.yml` file. You should add it to your `.gitignore`.
|
42
41
|
|
43
42
|
```
|
44
43
|
pscale branch switch my-new-branch-name --database my-db-name --create --wait
|
@@ -46,8 +45,7 @@ pscale branch switch my-new-branch-name --database my-db-name --create --wait
|
|
46
45
|
|
47
46
|
**Tip:** In your database settings. Enable "Automatically copy migration data." Select "Rails" as the migration framework. This will auto copy your `schema_migrations` table between branches.
|
48
47
|
|
49
|
-
2.
|
50
|
-
the branch specified in your `.pscale.yml` file.
|
48
|
+
2. Run your schema migrations against the branch.
|
51
49
|
|
52
50
|
```
|
53
51
|
bundle exec rails psdb:migrate
|
@@ -70,6 +68,16 @@ pscale deploy-request create database-name my-new-branch-name
|
|
70
68
|
|
71
69
|
4. To get your schema change to production, run the deploy request. Then, once it's complete, you can merge your code changes into your `main` branch in git and deploy your application code.
|
72
70
|
|
71
|
+
## Using PlanetScale deploy requests vs `psdb:migrate` directly in production.
|
72
|
+
|
73
|
+
PlanetScale's deploy requests [solve the schema change problem](https://planetscale.com/docs/learn/how-online-schema-change-tools-work). They make a normally high risk operation, safe. This is done by running your schema change using [Vitess's online schema change](https://vitess.io/docs/18.0/user-guides/schema-changes/) tools. Once the change is made, a deploy request is [also revertible without data loss](https://planetscale.com/blog/revert-a-migration-without-losing-data). None of this is possible when running `rails db:migrate` directly against your production database.
|
74
|
+
|
75
|
+
We recommend using GitHub Actions to automate the creation of PlanetScale branches and deploy requests. Then when you are ready to merge, you can run the deploy request before merging in your code.
|
76
|
+
|
77
|
+
**When not to use deploy requests**
|
78
|
+
|
79
|
+
If your application has minimal data and schema changes are a low risk event, then running `psdb:migrate` directly against production is perfectly fine. As your datasize grows and your application becomes busier, the risk of schema changes increase and we highly recommend using the deploy request flow. It's the best way available to safely migrate your schema.
|
80
|
+
|
73
81
|
## Usage with GitHub Actions
|
74
82
|
|
75
83
|
See the [GitHub Actions examples](actions-example.md) doc for ways to automate your schema migrations with PlanetScale + Actions.
|
data/actions-example.md
CHANGED
@@ -18,7 +18,7 @@ Secrets needed to be set:
|
|
18
18
|
- `PLANETSCALE_SERVICE_TOKEN_ID`
|
19
19
|
- `PLANETSCALE_SERVICE_TOKEN`
|
20
20
|
|
21
|
-
The PlanetScale service token must have the `connect_branch`, `create_branch`, `read_branch`, `create_deploy_request`, and `read_deploy_request` permissions on the database.
|
21
|
+
The PlanetScale service token must have the `connect_branch`, `create_branch`, `delete_branch_password`, `read_branch`, `create_deploy_request`, and `read_deploy_request` permissions on the database.
|
22
22
|
|
23
23
|
```yaml
|
24
24
|
name: Run database migrations
|
@@ -62,7 +62,7 @@ namespace :psdb do
|
|
62
62
|
|
63
63
|
raise "You must have `pscale` installed on your computer".colorize(:red) unless command?("pscale")
|
64
64
|
if branch.blank? || database.blank?
|
65
|
-
raise "Could not determine which PlanetScale branch to use from .pscale.yml. Please switch to a branch by using: `pscale switch
|
65
|
+
raise "Could not determine which PlanetScale branch to use from .pscale.yml. Please switch to a branch by using: `pscale branch switch branch-name --database db-name --create --wait`".colorize(:red)
|
66
66
|
end
|
67
67
|
|
68
68
|
short_hash = SecureRandom.hex(2)[0, 4]
|
@@ -85,7 +85,17 @@ namespace :psdb do
|
|
85
85
|
adapter = "trilogy"
|
86
86
|
end
|
87
87
|
|
88
|
-
|
88
|
+
# Setting schema_dump to nil is intentional. It prevents Rails from creating a dump after running migrations.
|
89
|
+
url = "#{adapter}://#{username}:#{password}@#{host}:3306/#{database}?ssl_mode=VERIFY_IDENTITY&schema_dump="
|
90
|
+
|
91
|
+
# Check common CA paths for certs.
|
92
|
+
ssl_ca_path = %w[/etc/ssl/certs/ca-certificates.crt /etc/pki/tls/certs/ca-bundle.crt /etc/ssl/ca-bundle.pem /etc/ssl/cert.pem].find { |f| File.exist?(f) }
|
93
|
+
|
94
|
+
if ssl_ca_path
|
95
|
+
url += "&sslca=#{ssl_ca_path}"
|
96
|
+
end
|
97
|
+
|
98
|
+
url
|
89
99
|
else
|
90
100
|
puts "Failed to create credentials for PlanetScale #{db_branch_colorized(database, branch)}"
|
91
101
|
puts "Command: #{command}"
|
@@ -110,7 +120,7 @@ namespace :psdb do
|
|
110
120
|
|
111
121
|
puts "Running migrations..."
|
112
122
|
|
113
|
-
command = "DATABASE_URL
|
123
|
+
command = "DATABASE_URL=\"#{ENV["PSCALE_DATABASE_URL"]}\" bundle exec rails db:migrate"
|
114
124
|
IO.popen(command) do |io|
|
115
125
|
io.each_line do |line|
|
116
126
|
puts line
|
@@ -133,7 +143,7 @@ namespace :psdb do
|
|
133
143
|
puts "Running migrations..."
|
134
144
|
|
135
145
|
name_env_key = "#{name.upcase}_DATABASE_URL"
|
136
|
-
command = "#{name_env_key}
|
146
|
+
command = "#{name_env_key}=\"#{ENV["PSCALE_DATABASE_URL"]}\" bundle exec rails db:migrate:#{name}"
|
137
147
|
|
138
148
|
IO.popen(command) do |io|
|
139
149
|
io.each_line do |line|
|
@@ -160,7 +170,7 @@ namespace :psdb do
|
|
160
170
|
puts "Loading schema..."
|
161
171
|
|
162
172
|
name_env_key = "#{name.upcase}_DATABASE_URL"
|
163
|
-
command = "#{name_env_key}
|
173
|
+
command = "#{name_env_key}=\"#{ENV["PSCALE_DATABASE_URL"]}\" bundle exec rake db:schema:load:#{name}"
|
164
174
|
|
165
175
|
IO.popen(command) do |io|
|
166
176
|
io.each_line do |line|
|
@@ -186,7 +196,7 @@ namespace :psdb do
|
|
186
196
|
raise "Found multiple database configurations, please specify which database you want to rollback using `psdb:rollback:<database_name>`".colorize(:red)
|
187
197
|
end
|
188
198
|
|
189
|
-
command = "DATABASE_URL
|
199
|
+
command = "DATABASE_URL=\"#{ENV["PSCALE_DATABASE_URL"]}\" bundle exec rails db:rollback"
|
190
200
|
|
191
201
|
IO.popen(command) do |io|
|
192
202
|
io.each_line do |line|
|
@@ -209,13 +219,13 @@ namespace :psdb do
|
|
209
219
|
rails_version = Gem::Version.new(Rails.version)
|
210
220
|
|
211
221
|
if rails_version < required_version
|
212
|
-
|
222
|
+
raise "This version of Rails does not support rollback commands for multi-database Rails apps. Please upgrade to at least Rails 6.1"
|
213
223
|
end
|
214
224
|
|
215
225
|
puts "Rolling back migrations..."
|
216
226
|
|
217
227
|
name_env_key = "#{name.upcase}_DATABASE_URL"
|
218
|
-
command = "#{name_env_key}
|
228
|
+
command = "#{name_env_key}=\"#{ENV["PSCALE_DATABASE_URL"]}\" bundle exec rake db:rollback:#{name}"
|
219
229
|
|
220
230
|
IO.popen(command) do |io|
|
221
231
|
io.each_line do |line|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: planetscale_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Coutermarsh
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2024-01-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: colorize
|