Stacks Database Migrations
Schema change management via migration files.
Key Paths
- Migration files:
database/migrations/ (96+ files)
- Database config:
config/database.ts
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 diffs model definitions against the current schema and generates the necessary SQL.
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 migrate to apply
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
--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-migrations-33description: 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`1415## CLI Commands1617```bash18buddy migrate # run pending migrations19buddy migrate --diff # show SQL without running20buddy migrate --auth # include auth tables21buddy migrate:fresh # drop ALL tables and re-migrate22buddy migrate:fresh --seed # drop, migrate, then seed23buddy migrate:dns # DNS-specific migration24buddy make:migration <name> # create new migration file25buddy seed # seed database26buddy generate:migrations # generate migrations from model diffs27```2829## Creating a Migration3031```bash32buddy make:migration create_orders_table33```3435Creates a timestamped migration file in `database/migrations/`.3637## Migration Generation from Models3839When you define or modify a model, generate migrations automatically:4041```bash42buddy generate:migrations43```4445This diffs model definitions against the current schema and generates the necessary SQL.4647## Built-in Migrations (96+)4849The framework includes migrations for all built-in models:5051### Core Tables52- `users` — id, name, email (unique), password, timestamps53- `personal_access_tokens` — auth tokens54- `passkeys` — WebAuthn credentials55- `password_resets` — password reset tokens5657### Content58- `posts` — title, content, excerpt, views, status, published_at, author_id59- `pages`, `categories`, `tags`, `comments`60- `authors` — linked to users6162### Commerce (20+ tables)63- `products`, `product_variants`, `product_units`64- `orders`, `order_items`65- `carts`, `cart_items`66- `coupons`, `gift_cards`, `reviews`67- `customers`, `manufacturers`6869### Payments70- `payments`, `payment_methods`, `payment_products`, `payment_transactions`71- `subscriptions`, `transactions`7273### Shipping74- `shipping_methods`, `shipping_rates`, `shipping_zones`75- `delivery_routes`, `drivers`7677### System78- `jobs`, `failed_jobs` — queue tables79- `errors`, `logs`, `notifications`80- `activities`, `requests`, `websockets`8182### Indexes83- `users.email` (unique), `users(email, name)` (composite)84- `subscribers.email` (unique)85- `coupons.code` (unique), `gift_cards.code` (unique)86- `payments.transaction_id` (unique)87- `subscriptions.provider_id` (unique)8889## Workflow90911. Define/modify model in `storage/framework/defaults/app/Models/` or `app/Models/`922. Run `buddy generate:migrations` to generate SQL diffs933. Review generated migration files944. Run `buddy migrate` to apply9596## Gotchas97- `migrate:fresh` drops ALL tables — only use in development98- Migrations run in filename order (timestamps ensure correct sequence)99- Never edit a migration that's been run in production — create a new one100- `--seed` flag after `migrate:fresh` seeds the database with factory data101- 96+ migration files exist by default for all framework models102- SQLite >= 3.47.2 is required (system requirement)103- For the database API (queries, connections), see the `stacks-database` skill