Skip to main content

database-migration

Manage database schema migrations with Flyway, Alembic, Prisma, Knex, and raw SQL migration patterns.

来源信息

仓库
ThinkfleetAI/thinkfleet-engine
最近来源活动
2026年2月1日 16:21
检测到的 SKILL.md 语言
英语
星标
0
Forks
0

安装方式

默认使用会先检查来源的 Prompt;你也可以切换为直接命令,或下载本地副本。

检查来源文件

决定是否安装前,请先阅读 SKILL.md,以及 SkillsMP 当前展示的配套文件。

正在显示 SKILL.md

SKILL.md
来源说明 · 只读预览
name
database-migration
description
Manage database schema migrations with Flyway, Alembic, Prisma, Knex, and raw SQL migration patterns.
metadata
{"thinkfleetbot":{"emoji":"🔄","requires":{"anyBins":"[Truncated]"}}}
# Database Migration Manage schema changes with versioned, repeatable migrations. ## Prisma (Node.js) ```bash # Create migration from schema changes npx prisma migrate dev --name add_users_table # Apply migrations in production npx prisma migrate deploy # Reset database (destroys data) npx prisma migrate reset # Check migration status npx prisma migrate status # Generate client after schema change npx prisma generate # View current schema npx prisma db pull ``` ## Knex (Node.js) ```bash # Create migration file npx knex migrate:make add_users_table # Run pending migrations npx knex migrate:latest # Rollback last batch npx knex migrate:rollback # Rollback all npx knex migrate:rollback --all # Check status npx knex migrate:status # Create seed file npx knex seed:make seed_users # Run seeds npx knex seed:run ``` ## Alembic (Python/SQLAlchemy) ```bash # Initialize alembic init alembic # Auto-generate migration from model changes alembic revision --autogenerate -m "add users table" # Apply all pending alembic upgrade head # Rollback one step alembic downgrade -1 # Check current version alembic current # Show migration history alembic history --verbose # Generate SQL without applying alembic upgrade head --sql > migration.sql ``` ## Flyway (Java/multi-language) ```bash # Apply migrations flyway migrate # Check status flyway info # Validate migrations match applied flyway validate # Rollback (Teams edition only) flyway undo # Clean database (destroys everything) flyway clean # Baseline existing database flyway baseline ``` ## Raw SQL Migrations ```bash # PostgreSQL — apply migration file psql $DATABASE_URL -f migrations/001_create_users.sql # MySQL mysql -u $DB_USER -p$DB_PASS $DB_NAME < migrations/001_create_users.sql # Track manually with a versions table psql $DATABASE_URL -c "CREATE TABLE IF NOT EXISTS schema_migrations (version TEXT PRIMARY KEY, applied_at TIMESTAMPTZ DEFAULT NOW());" psql $DATABASE_URL -c "INSERT INTO schema_migrations (version) VALUES ('001_create_users');" ``` ## Schema Diffing ```bash # Prisma — diff current DB vs schema npx prisma migrate diff --from-schema-datasource prisma/schema.prisma --to-schema-datamodel prisma/schema.prisma # PostgreSQL — dump schema for comparison pg_dump --schema-only $DATABASE_URL > schema_current.sql # Compare with previous diff schema_previous.sql schema_current.sql ``` ## Notes - Always test migrations on a copy of production data before applying. - Migrations should be idempotent where possible (`IF NOT EXISTS`, `IF EXISTS`). - Never edit an already-applied migration. Create a new one. - For zero-downtime deployments: add columns as nullable first, backfill, then add constraints. - Back up the database before running migrations in production.
在 GitHub 查看