2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Supabase CLI(db・migration) v2.30.4

Posted at

Supabase のリモート環境のマイグレーション履歴を管理しようとしているのですが、コマンドと、その仕様がわからなかったので、以下に書き出しました。

※筆者環境では、Node.js で作業しているので、コマンド実行時、supabase ~の前にnpxを付けて実行しています

supabase db [commant] のヘルプ

※筆者は混同していたのですが、dbコマンドはローカル環境とリモート環境の差分を操作するためのコマンドであり、マイグレーションの履歴に関わる操作を行う際は、基本migrationコマンドで行う必要があります

Usage:
  supabase db [command]

Available Commands:
  diff        Diffs the local database for schema changes
  dump        Dumps data or schemas from the remote database
  lint        Checks local database for typing error
  pull        Pull schema from the remote database
  push        Push new migrations to the remote database
  reset       Resets the local database to current migrations
  start       Starts local Postgres database

supabase migration [commant] のヘルプ

Usage:
  supabase migration [command]

Aliases:
  migration, migrations

Available Commands:
  down        Resets applied migrations up to the last n versions
  fetch       Fetch migration files from history table
  list        List local and remote migrations
  new         Create an empty migration script
  repair      Repair the migration history table
  squash      Squash migrations to a single file
  up          Apply pending migrations to local database

コマンドの使用例

ヘルプでも、英文で概要が記載されていますが、以下に補足として使用方法を残しておこうと思います

マイグレーション履歴を確認する(migration list)

以下コマンドで、ローカル環境と、接続先のリモート環境のマイグレーション履歴一覧がコマンドライン上に出力されます

supabase migration list

リモート環境のマイグレーションをローカル環境にダウンロードする(migration fetch)

以下コマンドで、リモート環境内のマイグレーション履歴をローカル環境のsupabase/migrationsフォルダー内に.sqlファイルとしてダウンロードできます
(筆者はsupabase db pull でマイグレーションファイルがダウンロードできるのかと、勘違いしていました^^;)

supabase migration fetch

マイグレーションを新しく生成する(migration new、migration up、db push)

以下コマンドで、ローカルのsupabase/migrationsフォルダー配下に、新しい.sqlファイルが生成されます。

supabase migration new [ファイル名]

生成された .sqlファイルに実行したい SQL を記載し、以下コマンドを実行することで、.sqlファイルに記載されたSQLを実際の DB に反映することができます

-- ローカル環境のDBにマイグレーションを反映する
supabase migration up

-- 接続しているリモート環境にマイグレーションを反映する
supabase db push
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?