搜索

PythonReactSEOPDF
GitHub Skill

migration

Write database migrations. Use when creating schema changes, adding tables, columns, indexes, or modifying database structure.

brendanlong☆ 1 仓库 Stars0 仓库 Forks

安装与使用

请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。

查看来源 ↗使用指南

技能说明

Database Migrations

Before Writing a Migration

Always read the current schema first:

cat drizzle/schema.sql

This file contains a pg_dump of the current database schema. Review it to understand existing tables, columns, constraints, and indexes before making changes.

Writing Migrations

Migrations are written as raw SQL files in the drizzle/ folder. We do NOT use drizzle-kit generate.

File Naming

Use an incrementing numeric prefix followed by a descriptive name:

0035_add_user_preferences.sql

Check existing migrations to find the next available number:

ls drizzle/*.sql | tail -5
SQL Format

Separate statements with --> statement-breakpoint:

-- Description of what this migration does

CREATE TABLE example (
  id uuid PRIMARY KEY,
  user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  name text NOT NULL,
  created_at timestamptz NOT NULL DEFAULT now()
);

--> statement-breakpoint

CREATE INDEX idx_example_user ON example(user_id);
PostgreSQL Conventions
  • IDs: Use uuid with UUIDv7 (time-ordered)
  • Timestamps: Always use timestamptz, never timestamp
  • Foreign keys: Include ON DELETE CASCADE for user-owned data
  • Case-insensitive text: Use citext extension when needed

Registering Migrations

Migrations won't run unless registered in the journal.

Edit drizzle/meta/_journal.json and add an entry:

{
  "idx": 35,
  "version": "7",
  "when": 1767500000000,
  "tag": "0035_add_user_preferences",
  "breakpoints": true
}
  • idx: Next sequential index
  • when: Unix timestamp in milliseconds (use current time)
  • tag: Filename without .sql extension
  • breakpoints: Always true

Enum Changes

Enum additions MUST be in their own migration file.

PostgreSQL doesn't allow using new enum values in the same transaction they were added. If you need to add an enum value and use it:

  1. Migration 1: Add the enum value
  2. Migration 2: Use the new enum value

Running Migrations

# Run migrations on development database
pnpm db:migrate

# Run migrations on test database
pnpm db:migrate:test

Verifying Migrations

When Docker/Postgres is available: Always run the integration tests after writing a migration:

pnpm test:integration

This runs the migrations against a real Postgres instance and updates drizzle/schema.sql with the current database state.

In cloud environments without Docker: If you cannot run Docker, manually update drizzle/schema.sql as best you can to reflect your migration changes. The schema dump will be corrected automatically by a future run of the integration tests.

使用示例

查看来源仓库中的使用示例与配置说明。

查看来源 ↗

来源历史

收录时间
2026-01-20
索引更新时间
2026-01-26
Branch
main

收录与同步日期属于本站记录。源码修改历史请查看来源仓库。

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!