2026-03-173 phút đọcVI
Chuỗi: supabase-series
- 0.Chuỗi bài: Supabase từ setup đến deploy
- 1.Tổng quan Supabase
- 2.Project setup, CLI và biến môi trường
- 3.Thiết kế schema Postgres và bảng
- 4.Migrations: viết và áp dụng(bài này)
- 5.Auth: JWT, session và tích hợp backend
- 6.Prisma + Supabase: kết nối và đồng bộ schema
- 7.Row Level Security (RLS) và policy
- 8.API: PostgREST, custom API và khi nào dùng gì
- 9.Realtime và Edge Functions
- 10.Deploy, CI/CD và multi-project
Migrations: write and apply
Introduction
Schema trong Supabase (và mọi Postgres nghiêm túc) nên được quản lý bằng migrations: file SQL versioned, áp dụng theo thứ tự. Bài này mô tả cách viết migration, thư mục và quy ước đặt tên, áp dụng qua CLI hoặc Studio, và rollback cơ bản.
Mục tiêu: Biết tạo migration mới, chạy migration lên local/cloud an toàn, và hiểu nguyên tắc “schema versioned, reproducible”.
Features
What is a migration
- A migration is a one-off SQL file that typically creates/alters/drops tables, columns, indexes, functions, RLS. Each file has a unique name (often with a timestamp) so application order is defined.
- Supabase CLI manages migrations in
supabase/migrations/; when you runsupabase db push(orsupabase migration up), the CLI compares with thesupabase_migrations.schema_migrationstable and only runs migrations that have not been applied. - Reproducible: From a repo + migrations you can recreate the DB from scratch; dev and CI share the same schema.
Studio vs CLI
- Studio: Supabase Dashboard has a SQL Editor; you can run SQL directly. Good for quick experiments; do not use it as the source of truth for production schema (hard to version and reproduce).
- CLI: Migrations in the repo, applied by command; you get history and PR review. Use this for all formal schema changes.
Workflow / Process
- Tạo file migration:
supabase migration new <tên_mô_tả>→ tạo filesupabase/migrations/<timestamp>_<tên>.sql. - Viết SQL: Chỉ thêm DDL (CREATE/ALTER/DROP) và nếu cần data seed (INSERT) có điều kiện; tránh DML tùy tiện (UPDATE/DELETE) trừ khi đã rõ ảnh hưởng.
- Chạy local:
supabase start(nếu chưa) rồisupabase db resethoặc migrations tự áp khi start; hoặcsupabase db pushsau khi link project. - Chạy lên cloud:
supabase linktrỏ đúng project (staging/prod) →supabase db push. Nên chạy staging trước, verify rồi mới prod. - Rollback: Supabase không tự rollback; muốn “undo” phải viết migration mới (ALTER/DROP) và push. Giữ migration chỉ thêm/sửa, tránh xóa dữ liệu trong migration trừ khi đã backup.
Quy ước: mỗi migration một mục đích (vd. thêm bảng X, thêm cột Y); tên file mô tả ngắn (vd. add_messages_content_index).
Sample code
Tạo migration mới
supabase migration new add_conversations_archived
# Tạo file: supabase/migrations/20260317120000_add_conversations_archived.sqlNội dung file migration
-- supabase/migrations/20260317120000_add_conversations_archived.sql
ALTER TABLE public.conversations
ADD COLUMN IF NOT EXISTS archived BOOLEAN NOT NULL DEFAULT false;
CREATE INDEX IF NOT EXISTS idx_conversations_archived
ON public.conversations(archived) WHERE archived = false;Kiểm tra trạng thái (sau khi link)
supabase migration list
# Liệt kê migration đã áp dụng / chưa áp dụngReset DB local (xóa data, chạy lại toàn bộ migrations)
supabase db resetÁp dụng trong kiến trúc
- Schema versioned: Migrations là nguồn sự thật duy nhất cho cấu trúc DB; mọi thay đổi schema đều qua file migration, review trong PR. Tránh sửa tay trên production.
- Reproducible: Bất kỳ env nào (dev, CI, staging) đều có thể tạo DB từ trống bằng cách chạy toàn bộ migrations; giảm “chạy được trên máy tôi nhưng không chạy trên máy bạn”.
- Tách môi trường: Cùng bộ migrations, khác project (dev/staging/prod); deploy migration lên từng env theo quy trình (staging trước, sau đó prod).
Kết
Migrations cho phép quản lý schema Postgres dưới dạng file versioned, áp dụng nhất quán qua CLI. Bài tiếp sẽ đi Auth: JWT, session và cách verify JWT ở backend.
Bài tiếp: 05 — Auth: JWT và tích hợp backend
Đọc thêm: Supabase — Database Migrations