Stacks Database Migrations
Schema change management via migration files.
Key Paths
- Migration files:
database/migrations/ (96+ files)
- Database config:
config/database.ts
- Model snapshot:
storage/framework/database/model-snapshot.<dialect>.json
CLI Commands
buddy migrate # run pending migrations
buddy migrate --diff # show SQL without running
buddy migrate --auth # include auth tables
buddy migrate:fresh # drop ALL tables and re-migrate
buddy migrate:fresh --seed # drop, migrate, then seed
buddy migrate:dns # DNS-specific migration
buddy make:migration <name> # create new migration file
buddy seed # seed database
buddy generate:migrations # generate migrations from model diffs
Creating a Migration
buddy make:migration create_orders_table
Creates a timestamped migration file in database/migrations/.
Migration Generation from Models
When you define or modify a model, generate migrations automatically:
buddy generate:migrations
This recursively loads both model roots, with app/Models/ overriding framework
models that have the same model name:
storage/framework/defaults/app/Models/
app/Models/
It diffs the merged registry against the committed dialect snapshot and
generates the necessary SQL. An application does not need an app/Models/
directory for framework model migrations to be discovered.
The snapshot is part of the schema history and must be committed with the
generated migration. It lives under storage/framework/database/, not .qb/.
Run the generator a second time before committing. A stable change reports
Nothing to migrate and Model snapshot unchanged.
Built-in Migrations (96+)
The framework includes migrations for all built-in models:
Core Tables
users — id, name, email (unique), password, timestamps
personal_access_tokens — auth tokens
passkeys — WebAuthn credentials
password_resets — password reset tokens
Content
posts — title, content, excerpt, views, status, published_at, author_id
pages, categories, tags, comments
authors — linked to users
Commerce (20+ tables)
products, product_variants, product_units
orders, order_items
carts, cart_items
coupons, gift_cards, reviews
customers, manufacturers
Payments
payments, payment_methods, payment_products, payment_transactions
subscriptions, transactions
Shipping
shipping_methods, shipping_rates, shipping_zones
delivery_routes, drivers
System
jobs, failed_jobs — queue tables
errors, logs, notifications
activities, requests, websockets
Indexes
users.email (unique), users(email, name) (composite)
subscribers.email (unique)
coupons.code (unique), gift_cards.code (unique)
payments.transaction_id (unique)
subscriptions.provider_id (unique)
Workflow
- Define/modify model in
storage/framework/defaults/app/Models/ or app/Models/
- Run
buddy generate:migrations to generate SQL diffs
- Review generated migration files
- Run
buddy generate:migrations again and confirm there is no remaining diff
- Run
buddy migrate to apply
- Commit the generated SQL and
storage/framework/database/model-snapshot.<dialect>.json
Gotchas
migrate:fresh drops ALL tables — only use in development
- Migrations run in filename order (timestamps ensure correct sequence)
- Never edit a migration that's been run in production — create a new one
- Keep the committed model snapshot in sync with every generated migration
- Do not commit a second snapshot under
.qb/; that indicates a missing snapshotDir configuration
- If a generated SQLite migration rebuilds tables, test it against a copy of the current database and run
PRAGMA integrity_check plus PRAGMA foreign_key_check
--seed flag after migrate:fresh seeds the database with factory data
- 96+ migration files exist by default for all framework models
- SQLite >= 3.47.2 is required (system requirement)
- For the database API (queries, connections), see the
stacks-database skill
1---2name: stacks-migrations3description: Use when working with database migrations in a Stacks application - creating migration files, running migrations, fresh migration (drop + recreate), seeding after migration, migration file naming conventions, or the 96+ built-in migration files. For the database API itself (queries, connections, SQL helpers), see stacks-database.4license: MIT5---67# Stacks Database Migrations89Schema change management via migration files.1011## Key Paths12- Migration files: `database/migrations/` (96+ files)13- Database config: `config/database.ts`14- Model snapshot: `storage/framework/database/model-snapshot.<dialect>.json`1516## CLI Commands1718```bash19buddy migrate # run pending migrations20buddy migrate --diff # show SQL without running21buddy migrate --auth # include auth tables22buddy migrate:fresh # drop ALL tables and re-migrate23buddy migrate:fresh --seed # drop, migrate, then seed24buddy migrate:dns # DNS-specific migration25buddy make:migration <name> # create new migration file26buddy seed # seed database27buddy generate:migrations # generate migrations from model diffs28```2930## Creating a Migration3132```bash33buddy make:migration create_orders_table34```3536Creates a timestamped migration file in `database/migrations/`.3738## Migration Generation from Models3940When you define or modify a model, generate migrations automatically:4142```bash43buddy generate:migrations44```4546This recursively loads both model roots, with `app/Models/` overriding framework47models that have the same model name:4849- `storage/framework/defaults/app/Models/`50- `app/Models/`5152It diffs the merged registry against the committed dialect snapshot and53generates the necessary SQL. An application does not need an `app/Models/`54directory for framework model migrations to be discovered.5556The snapshot is part of the schema history and must be committed with the57generated migration. It lives under `storage/framework/database/`, not `.qb/`.58Run the generator a second time before committing. A stable change reports59`Nothing to migrate` and `Model snapshot unchanged`.6061## Built-in Migrations (96+)6263The framework includes migrations for all built-in models:6465### Core Tables66- `users` — id, name, email (unique), password, timestamps67- `personal_access_tokens` — auth tokens68- `passkeys` — WebAuthn credentials69- `password_resets` — password reset tokens7071### Content72- `posts` — title, content, excerpt, views, status, published_at, author_id73- `pages`, `categories`, `tags`, `comments`74- `authors` — linked to users7576### Commerce (20+ tables)77- `products`, `product_variants`, `product_units`78- `orders`, `order_items`79- `carts`, `cart_items`80- `coupons`, `gift_cards`, `reviews`81- `customers`, `manufacturers`8283### Payments84- `payments`, `payment_methods`, `payment_products`, `payment_transactions`85- `subscriptions`, `transactions`8687### Shipping88- `shipping_methods`, `shipping_rates`, `shipping_zones`89- `delivery_routes`, `drivers`9091### System92- `jobs`, `failed_jobs` — queue tables93- `errors`, `logs`, `notifications`94- `activities`, `requests`, `websockets`9596### Indexes97- `users.email` (unique), `users(email, name)` (composite)98- `subscribers.email` (unique)99- `coupons.code` (unique), `gift_cards.code` (unique)100- `payments.transaction_id` (unique)101- `subscriptions.provider_id` (unique)102103## Workflow1041051. Define/modify model in `storage/framework/defaults/app/Models/` or `app/Models/`1062. Run `buddy generate:migrations` to generate SQL diffs1073. Review generated migration files1084. Run `buddy generate:migrations` again and confirm there is no remaining diff1095. Run `buddy migrate` to apply1106. Commit the generated SQL and `storage/framework/database/model-snapshot.<dialect>.json`111112## Gotchas113- `migrate:fresh` drops ALL tables — only use in development114- Migrations run in filename order (timestamps ensure correct sequence)115- Never edit a migration that's been run in production — create a new one116- Keep the committed model snapshot in sync with every generated migration117- Do not commit a second snapshot under `.qb/`; that indicates a missing `snapshotDir` configuration118- If a generated SQLite migration rebuilds tables, test it against a copy of the current database and run `PRAGMA integrity_check` plus `PRAGMA foreign_key_check`119- `--seed` flag after `migrate:fresh` seeds the database with factory data120- 96+ migration files exist by default for all framework models121- SQLite >= 3.47.2 is required (system requirement)122- For the database API (queries, connections), see the `stacks-database` skill