Open12

PlanetScale Workflow を整理する

Ryosuke MiyamotoRyosuke Miyamoto
$ npx prisma db push
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": MySQL database "platforms" at "127.0.0.1:3309"
Error: P3022

Direct execution of DDL (Data Definition Language) SQL statements is disabled on this database. Please read more here how to handle this: https://pris.ly/d/migrate-no-direct-ddl

planetscale メインブランチに直接push をすると、上記エラーになる(ブランチを切って、作業をする必要がある)

Ryosuke MiyamotoRyosuke Miyamoto

ざっと流れをまとめると、

ブランチを作る

$branch create hoge-project-name hoge-branch-name 

ブランチのローカルDBとして起動する

$pscale connect hoge-project-name hoge-branch-name --port 3309

prisma/schema.prisma で schema 編集後

$npx prisma db push

--- 以下は、Plantscale のサイト上でもできる

デプロイリクエスト(pullrequest みたいなやつ)

$pscale deploy-request create hoge-project-name hoge-branch-name

main にデプロイ(merge)

$pscale deploy-request deploy hoge-project-name 1
Kohei SugiKohei Sugi
$ pscale shell --org yahsan2 platforms test
platforms/test> ^D%
$ pscale shell --org yahsan2 platforms main
platforms/|⚠ main ⚠|> show tables;
+---------------------+
| Tables_in_platforms |
+---------------------+
| Account             |
| Example             |
| Post                |
| Session             |
| Site                |
| User                |
| VerificationToken   |
+---------------------+
platforms/|⚠ main ⚠|> select * from Post ;
platforms/|⚠ main ⚠|> select * from Post
    -> ;
platforms/|⚠ main ⚠|> desc User;
+---------------+--------------+------+-----+----------------------+-------------------+
| Field         | Type         | Null | Key | Default              | Extra             |
+---------------+--------------+------+-----+----------------------+-------------------+
| id            | varchar(191) | NO   | PRI | NULL                 |                   |
| name          | varchar(191) | YES  |     | NULL                 |                   |
| username      | varchar(191) | YES  |     | NULL                 |                   |
| gh_username   | varchar(191) | YES  |     | NULL                 |                   |
| email         | varchar(191) | YES  | UNI | NULL                 |                   |
| emailVerified | datetime(3)  | YES  |     | NULL                 |                   |
| image         | varchar(191) | YES  |     | NULL                 |                   |
| createdAt     | datetime(3)  | NO   |     | CURRENT_TIMESTAMP(3) | DEFAULT_GENERATED |
| updatedAt     | datetime(3)  | NO   |     | NULL                 |                   |
| tw_username   | varchar(191) | YES  |     | NULL                 |                   |
+---------------+--------------+------+-----+----------------------+-------------------+
platforms/|⚠ main ⚠|>

mainにCLIからなら繋げた。

Kohei SugiKohei Sugi
$ npx prisma db push
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": MySQL database "platforms" at "127.0.0.1:3309"
Error: P1001

Can't reach database server at `127.0.0.1`:`3309`

Please make sure your database server is running at `127.0.0.1`:`3309`.

.env

$ cat .env
### DEVELOPMENT ONLY VARIABLES

# Mandatory next-auth URL for localhost
NEXTAUTH_URL=http://app.localhost:3000

### PRODUCTION & DEVELOPMENT VARIABLES

# MySQL database URL for Prisma
DATABASE_URL=mysql://root@127.0.0.1:3309/platforms

# Github OAuth https://docs.github.com/en/developers/apps/building-oauth-apps/creating-an-oauth-app
GITHUB_ID=
GITHUB_SECRET=

# Twitter Auth Bearer token (for static tweets)
TWITTER_AUTH_TOKEN=

# Secret key (generate one here: https://generate-secret.vercel.app/32)
SECRET=

AUTH_BEARER_TOKEN=
VERCEL_PROJECT_ID=
VERCEL_TEAM_ID=
# DATABASE_URL=mysql://root@127.0.0.1:3309/platforms ブランチを作ったDBのconnectのところで出てくるpasswordに変更する必要があった
DATABASE_URL='mysql://XXXXXXX:XXXXXXX@XXXXXXXXX/platforms?sslaccept=strict'

コントリビュータは毎回.envをブランチを変えるたびに変更する必要あり??流石に何か方法ありそう。