planetscale_rails 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +33 -10
- data/actions-example.md +129 -0
- data/lib/planetscale_rails/tasks/psdb.rake +1 -2
- data/lib/planetscale_rails/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1fac50d24f5d87d2be01f128dd42f63b75023d7761d5516c92ac8ad92f776fae
|
4
|
+
data.tar.gz: 29a0fc6de3302ccd5b228c458facf7516147c2ca78b5f1717979bfbd6d8534d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3993c4de9fb27abda2d3f9506f28a8f8c8b12312540fdee58d7838d37fb8af25fba31b2b7c291915e9f1488fc8586708f516e6149dede3fe26a4c22ce5e44cb9
|
7
|
+
data.tar.gz: 93bcfb6f5d2f2bc0096de75029a2c12de9db97ab1fa591aa50e37d8d1548c23142a2dbda0079d51b8078d160f3535ba8bed3965042331e7b447136d69abdd997
|
data/README.md
CHANGED
@@ -12,10 +12,10 @@ The rake tasks allow you to use local MySQL for development. When you're ready t
|
|
12
12
|
against it. See [usage](#usage) for details.
|
13
13
|
|
14
14
|
```
|
15
|
-
rake psdb:migrate
|
16
|
-
rake psdb:rollback
|
17
|
-
rake psdb:schema:load
|
18
|
-
rake psdb:setup_pscale
|
15
|
+
rake psdb:migrate # Migrate the database for current environment
|
16
|
+
rake psdb:rollback # Rollback primary database for current environment
|
17
|
+
rake psdb:schema:load # Load the current schema into the database
|
18
|
+
rake psdb:setup_pscale # Setup a proxy to connect to PlanetScale
|
19
19
|
```
|
20
20
|
|
21
21
|
## Installation
|
@@ -28,20 +28,40 @@ group :development do
|
|
28
28
|
end
|
29
29
|
```
|
30
30
|
|
31
|
-
And then execute:
|
31
|
+
And then execute in your terminal:
|
32
32
|
|
33
|
-
|
33
|
+
```
|
34
|
+
bundle install
|
35
|
+
```
|
36
|
+
|
37
|
+
### Update database.yml
|
38
|
+
|
39
|
+
In your application's `database.yml`, you'll need to make two changes. This will allow the app to run migrations via a connection setup by the pscale CLI.
|
40
|
+
|
41
|
+
1. Swap to port 3305 if `ENV['ENABLE_PSDB']` is true.
|
42
|
+
2. Use production database name if `ENV['ENABLE_PSDB']` is true.
|
43
|
+
|
44
|
+
Here is an example
|
45
|
+
|
46
|
+
```
|
47
|
+
development:
|
48
|
+
<<: *default
|
49
|
+
database: <%= ENV['ENABLE_PSDB'] ? 'your_production_db_name' : 'your_development_db_name' %>
|
50
|
+
port: <%= ENV['ENABLE_PSDB'] ? 3305 : ENV.fetch("DB_PORT", 3306) %>
|
51
|
+
```
|
34
52
|
|
35
53
|
## Usage
|
36
54
|
|
37
55
|
First, make sure you have the [`pscale` CLI installed](https://github.com/planetscale/cli#installation). You'll use `pscale` to create a new branch.
|
38
56
|
|
39
|
-
1.
|
40
|
-
|
41
|
-
Run this locally, it will create a new branch off of `main`. The `switch` command will update a `.pscale.yml` file to track
|
57
|
+
1. Run this locally, it will create a new branch off of `main`. The `switch` command will update a `.pscale.yml` file to track
|
42
58
|
that this is the branch you want to migrate.
|
43
59
|
|
44
|
-
|
60
|
+
```
|
61
|
+
pscale branch switch my-new-branch-name --database my-db-name --create
|
62
|
+
```
|
63
|
+
|
64
|
+
**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.
|
45
65
|
|
46
66
|
2. Once your branch is ready, you can then use the `psdb` rake task to connect to your branch and run `db:migrate`.
|
47
67
|
|
@@ -68,6 +88,9 @@ pscale deploy-request create database-name my-new-branch-name
|
|
68
88
|
|
69
89
|
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.
|
70
90
|
|
91
|
+
## Usage with GitHub Actions
|
92
|
+
|
93
|
+
See the [GitHub Actions examples](actions-example.md) doc for ways to automate your schema migrations with PlanetScale + Actions.
|
71
94
|
|
72
95
|
## Development
|
73
96
|
|
data/actions-example.md
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
## GitHub Actions example
|
2
|
+
|
3
|
+
To automate branch creation and running migrations, you can setup a branch and run `rails psdb:migrate` from GitHub Actions.
|
4
|
+
|
5
|
+
Here is a full example workflow that:
|
6
|
+
1. Creates a branch matching the git branch name.
|
7
|
+
2. Runs rails migrations.
|
8
|
+
3. Opens a deploy request if any migrations were run.
|
9
|
+
4. Comments on the pull request with the diff + link to the deploy request.
|
10
|
+
|
11
|
+
### Migrate schema workflow
|
12
|
+
|
13
|
+
This workflow will run on any pull request that is opened with a change to `db/schema.rb`.
|
14
|
+
|
15
|
+
Secrets needed to be set:
|
16
|
+
- `PLANETSCALE_ORG_NAME`
|
17
|
+
- `PLANETSCALE_DATABASE_NAME`
|
18
|
+
- `PLANETSCALE_SERVICE_TOKEN_ID`
|
19
|
+
- `PLANETSCALE_SERVICE_TOKEN`
|
20
|
+
|
21
|
+
The PlanetScale service token must have permission on the database to create/read development branches, as well as create deploy requests.
|
22
|
+
|
23
|
+
```yaml
|
24
|
+
name: Run database migrations
|
25
|
+
on:
|
26
|
+
pull_request:
|
27
|
+
branches: [ main ]
|
28
|
+
paths:
|
29
|
+
- 'db/schema.rb'
|
30
|
+
|
31
|
+
jobs:
|
32
|
+
planetscale:
|
33
|
+
permissions:
|
34
|
+
pull-requests: write
|
35
|
+
contents: read
|
36
|
+
|
37
|
+
runs-on: ubuntu-latest
|
38
|
+
steps:
|
39
|
+
- name: checkout
|
40
|
+
uses: actions/checkout@v3
|
41
|
+
- name: Create a branch
|
42
|
+
uses: planetscale/create-branch-action@v4
|
43
|
+
id: create_branch
|
44
|
+
with:
|
45
|
+
org_name: ${{ secrets.PLANETSCALE_ORG_NAME }}
|
46
|
+
database_name: ${{ secrets.PLANETSCALE_DATABASE_NAME }}
|
47
|
+
branch_name: ${{ github.head_ref }}
|
48
|
+
from: main
|
49
|
+
check_exists: true
|
50
|
+
wait: true
|
51
|
+
env:
|
52
|
+
PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }}
|
53
|
+
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }}
|
54
|
+
- uses: actions/checkout@v3
|
55
|
+
- name: Set up Ruby
|
56
|
+
uses: ruby/setup-ruby@v1
|
57
|
+
with:
|
58
|
+
ruby-version: 3.2.1
|
59
|
+
- name: Cache Ruby gems
|
60
|
+
uses: actions/cache@v3
|
61
|
+
with:
|
62
|
+
path: vendor/bundle
|
63
|
+
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
|
64
|
+
restore-keys: |
|
65
|
+
${{ runner.os }}-gems-
|
66
|
+
- name: Install dependencies
|
67
|
+
run: |
|
68
|
+
bundle config --local path vendor/bundle
|
69
|
+
bundle config --local deployment true
|
70
|
+
bundle install
|
71
|
+
- name: Set migration config
|
72
|
+
run: |
|
73
|
+
echo "org: ${{ secrets.PLANETSCALE_ORG_NAME }}" > .pscale.yml
|
74
|
+
echo "database: ${{ secrets.PLANETSCALE_DATABASE_NAME }}" >> .pscale.yml
|
75
|
+
echo "branch: ${{ github.head_ref }}" >> .pscale.yml
|
76
|
+
- name: Setup pscale
|
77
|
+
uses: planetscale/setup-pscale-action@v1
|
78
|
+
- name: Run migrations
|
79
|
+
run: |
|
80
|
+
bundle exec rails psdb:migrate > migration-output.txt
|
81
|
+
env:
|
82
|
+
PSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }}
|
83
|
+
PSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }}
|
84
|
+
- name: Open DR if migrations
|
85
|
+
run: |
|
86
|
+
if grep -q "migrated" migration-output.txt; then
|
87
|
+
echo "DB_MIGRATED=true" >> $GITHUB_ENV
|
88
|
+
if pscale deploy-request create ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ github.head_ref }}; then
|
89
|
+
cat migration-output.txt
|
90
|
+
echo "DR_OPENED=true" >> $GITHUB_ENV
|
91
|
+
echo "Deploy request successfully opened"
|
92
|
+
else
|
93
|
+
echo "Error: Deployment request failed"
|
94
|
+
exit 0
|
95
|
+
fi
|
96
|
+
else
|
97
|
+
echo "Did not open a DR since nothing found in migration-output.txt"
|
98
|
+
cat migration-output.txt
|
99
|
+
exit 0
|
100
|
+
fi
|
101
|
+
env:
|
102
|
+
PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }}
|
103
|
+
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }}
|
104
|
+
- name: Get Deploy Requests
|
105
|
+
if: ${{ env.DR_OPENED }}
|
106
|
+
env:
|
107
|
+
PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }}
|
108
|
+
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }}
|
109
|
+
run: |
|
110
|
+
deploy_request_number=$(pscale deploy-request show ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ github.head_ref }} -f json | jq -r '.number')
|
111
|
+
echo "DEPLOY_REQUEST_NUMBER=$deploy_request_number" >> $GITHUB_ENV
|
112
|
+
- name: Comment PR - db migrated
|
113
|
+
if: ${{ env.DR_OPENED }}
|
114
|
+
env:
|
115
|
+
PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }}
|
116
|
+
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }}
|
117
|
+
run: |
|
118
|
+
sleep 2
|
119
|
+
echo "Deploy request opened: https://app.planetscale.com/${{ secrets.PLANETSCALE_ORG_NAME }}/${{ secrets.PLANETSCALE_DATABASE_NAME }}/deploy-requests/${{ env.DEPLOY_REQUEST_NUMBER }}" >> migration-message.txt
|
120
|
+
echo "" >> migration-message.txt
|
121
|
+
echo "\`\`\`diff" >> migration-message.txt
|
122
|
+
pscale deploy-request diff ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.DEPLOY_REQUEST_NUMBER }} -f json | jq -r '.[].raw' >> migration-message.txt
|
123
|
+
echo "\`\`\`" >> migration-message.txt
|
124
|
+
- name: Comment PR - db migrated
|
125
|
+
uses: thollander/actions-comment-pull-request@v2
|
126
|
+
if: ${{ env.DR_OPENED }}
|
127
|
+
with:
|
128
|
+
filePath: migration-message.txt
|
129
|
+
```
|
@@ -46,7 +46,7 @@ namespace :psdb do
|
|
46
46
|
database = ps_config["database"]
|
47
47
|
branch = ps_config["branch"]
|
48
48
|
|
49
|
-
raise "You must have `pscale` installed on your computer" unless command?("pscale")
|
49
|
+
raise "You must have `pscale` installed on your computer".colorize(:red) unless command?("pscale")
|
50
50
|
if branch.blank? || database.blank?
|
51
51
|
raise "Your branch is not properly setup, please switch to a branch by using the CLI.".colorize(:red)
|
52
52
|
end
|
@@ -88,7 +88,6 @@ namespace :psdb do
|
|
88
88
|
if branch.blank? || database.blank?
|
89
89
|
raise "Your branch is not properly setup, please switch to a branch by using the CLI."
|
90
90
|
end
|
91
|
-
raise "Unable to run migrations against the main branch" if branch == "main"
|
92
91
|
|
93
92
|
config = Rails.configuration.database_configuration[Rails.env][name]
|
94
93
|
|
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.1.
|
4
|
+
version: 0.1.4
|
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: 2023-
|
12
|
+
date: 2023-07-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: colorize
|
@@ -60,6 +60,7 @@ files:
|
|
60
60
|
- LICENSE.txt
|
61
61
|
- README.md
|
62
62
|
- Rakefile
|
63
|
+
- actions-example.md
|
63
64
|
- bin/console
|
64
65
|
- bin/setup
|
65
66
|
- lib/Rakefile
|