planetscale_rails 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f38096cc4396fecb1efbfba9ea4bfe7f72b39c915990fb7036f8ab4ef1a1aaa2
4
- data.tar.gz: c609fbd59e9caf5682fddd1367533c3a99a80fe1bbd7c93591da6047f8ff22cf
3
+ metadata.gz: 1fac50d24f5d87d2be01f128dd42f63b75023d7761d5516c92ac8ad92f776fae
4
+ data.tar.gz: 29a0fc6de3302ccd5b228c458facf7516147c2ca78b5f1717979bfbd6d8534d8
5
5
  SHA512:
6
- metadata.gz: b4e11cfda4e78884bd30fe672b06517320f74a36244d96c61688d5d339478c99cff30050e31f049578fa27e5358692697b9019d0032108223d4c6dbb2e9a7c97
7
- data.tar.gz: d236ee3a8a70d8e37be836e344c8e1a565c99149d7f301760810c8efc03ebea30e1cdb1e78458fc788b9fd9f41b1fe98fdaa4f9fc9e29cad1fb64636bf6d579f
6
+ metadata.gz: 3993c4de9fb27abda2d3f9506f28a8f8c8b12312540fdee58d7838d37fb8af25fba31b2b7c291915e9f1488fc8586708f516e6149dede3fe26a4c22ce5e44cb9
7
+ data.tar.gz: 93bcfb6f5d2f2bc0096de75029a2c12de9db97ab1fa591aa50e37d8d1548c23142a2dbda0079d51b8078d160f3535ba8bed3965042331e7b447136d69abdd997
data/README.md CHANGED
@@ -34,6 +34,22 @@ And then execute in your terminal:
34
34
  bundle install
35
35
  ```
36
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
+ ```
52
+
37
53
  ## Usage
38
54
 
39
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.
@@ -45,7 +61,7 @@ that this is the branch you want to migrate.
45
61
  pscale branch switch my-new-branch-name --database my-db-name --create
46
62
  ```
47
63
 
48
- **Tip:** In your database settings. Enable "Automatically copy migration data." Select "Rails/Phoenix" as the migration framework. This will auto copy your `schema_migrations` table between branches.
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.
49
65
 
50
66
  2. Once your branch is ready, you can then use the `psdb` rake task to connect to your branch and run `db:migrate`.
51
67
 
@@ -72,6 +88,9 @@ pscale deploy-request create database-name my-new-branch-name
72
88
 
73
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.
74
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.
75
94
 
76
95
  ## Development
77
96
 
@@ -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
+ ```
@@ -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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PlanetscaleRails
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.4"
5
5
  end
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.3
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-03-18 00:00:00.000000000 Z
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