1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Neon Localでコンテナ作成時にブランチを作成する挙動を試してみたメモ

Last updated at Posted at 2025-05-19

概要

前回Local Development with Neonによるローカル環境でdockerのpostgresqlを使う方法を試した。
今回はNeon Localを使ってコンテナを立ち上げるとブランチが作成され、コンテナ停止時にはブランチが削除される動作を確認する。

この時点のソース

ProjectIDの取得

image.png

API Keyの取得

Neon API Key
image.png

image.png

image.png

image.png

ソースコード

infra/local/neon_local/docker-compose.yml
services:
  neon-local:
    image: neondatabase/neon_local:latest
    ports:
      - '5432:5432'
    environment:
      NEON_API_KEY: ${NEON_API_KEY}
      NEON_PROJECT_ID: ${NEON_PROJECT_ID}
      DRIVER: serverless
      DELETE_BRANCH: true
    profiles:
      - dev

infra/local/neon_local/.env
NEON_API_KEY=your_api_key
NEON_PROJECT_ID=your_project_id
infra/local/neon_local/bin/up.sh
#!/bin/bash

bin_dir=$(cd $(dirname $0) && pwd)
docker_dir=$(cd $bin_dir/.. && pwd)

# up.sh docker-compose.camp.yml
composeFile=${1:-"docker-compose.yml"}


# docker-composeの起動
cd $docker_dir && docker-compose --profile dev --env-file .env -f $composeFile up

起動してみたときのエラー対応

環境変数にPARENT_BRANCH_ID: mainを設定したら
404 Client Error: Not Found for url: https://console.neon.tech/api/v2/projects/your_project_id/branchesのエラーが発生。

環境変数を削除したら requests.exceptions.HTTPError: 412 Client Error: Precondition Failed for url: https://console.neon.tech/api/v2/projects/your_project_id/branches/hogehoge/roles/user_role/reveal_passwordのエラーが発生。

直接たたいてみると下記のメッセージが返ってきた。

storing passwords is not enabled for the project

エラーは発生したが、ブランチは増えている。

image.png

412が発生しているAPIについて

getprojectbranchrolepassword データベースのパスワードを取得するAPI?

Projectを新規作成してみる。

これでエラーが起きなくなった。Postgresqlのバージョンは同じ14。
コンテナを立ち上げるとブランチが増え、止めると消える。

新プロジェクトでは初期状態のブランチがproductとdevelopがあり、仕様が異なりそうので、昔に作ったプロジェクトには対応していないのかもしれない。

image.png

新Projectでマイグレーション

サーバレスDB(Postgresql : NeonDB ) の無料版にCloudflare Workers ( Node.js) から繋いだメモでやったのと同じように接続する。

接続文字列の取得

image.png

image.png

マイグレーション

drizzle-kit migrateでマイグレーション

結果確認

image.png

マイグレーションしたDBからブランチを切ってみる

infra/local/neon_local/docker-compose.yml
services:
  neon-local:
    image: neondatabase/neon_local:latest
    ports:
      - '5432:5432'
    environment:
      NEON_API_KEY: ${NEON_API_KEY}
      NEON_PROJECT_ID: ${NEON_PROJECT_ID}
      DRIVER: serverless
      DELETE_BRANCH: true
+      PARENT_BRANCH_ID: ${PARENT_BRANCH_ID}
    profiles:
      - dev

infra/local/neon_local/.env
NEON_API_KEY=your_api_key
NEON_PROJECT_ID=your_project_id
+ PARENT_BRANCH_ID=xx-xxxx-xxxx-xxxxxxx

ブランチのIDはコンソールから取得可能。
image.png

Dockerの起動後にブランチができていることを確認

image.png

ブランチにマイグレーション内容が入っていることを確認

image.png

参考

Make Yourself at Home with Neon Local
How to set up Neon Local with Docker Compose and JavaScript Postgres clients
Neon Local
GitHub - neon_local
DockerHub - neon_local

Docker Compose Env

Neon(Postgres)のDockerをローカルで稼働させ、Drizzleを使ったマイグレーションとCloudflare Workersのローカル実行からの接続を行ったメモ
サーバレスDB(Postgresql : NeonDB ) の無料版にCloudflare Workers ( Node.js) から繋いだメモ
サーバレスDBのNeonからDBファーストでPrismaを使ってスキーマを作りzodを連携して型解決をしたlambda(Node.js)を作成したメモ

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?