3. Applying and Inspecting Migrations¶
What You'll Learn¶
- How
dbwarden migrateapplies pending SQL - How to roll back and downgrade to specific versions
- How to inspect migration history and status
- How to validate schema integrity and database connectivity
Prerequisites¶
- Completed Section 2 (migration file exists)
examples/core/project
Step 1: Apply Migrations¶
The Migrate Command¶
When you run migrate, DBWarden:
- Creates the metadata table (
_dbwarden_migrations) if it doesn't exist - Creates the lock table (
_dbwarden_lock) if it doesn't exist - Acquires a migration lock (prevents concurrent runs)
- Reads all migration files and filters to pending (unapplied) ones
- Executes the
-- upgradeSQL of each pending migration - Records each migration's version, checksum, and timestamp
- Writes a schema snapshot file for future diffs
- Releases the lock
[DBWarden] Applying primary__0001_create_core_tables...
[DBWarden] Migration applied successfully (42ms)
[DBWarden] All migrations applied. Pending: 0
Verify Status¶
Output:
View History¶
Output:
The checksum (a1b2c3d4...) is a SHA-256 hash of the migration file content. This detects tampering or accidental edits after apply.
Step 2: Rollback¶
Rollback executes the -- rollback section of the most recently applied migration. After rollback:
Rollback Mechanics¶
- Rollback always executes the
-- rollbacksection of the file; never auto-generates reverse SQL --countcontrols how many migrations to roll back (default: 1)- Rollbacks are also lock-protected
- After rollback, the migration is considered "pending" again and can be re-applied
Step 3: Re-apply¶
Re-applies the migration. Since rollback removed the tracking record, the migration runs again.
Step 4: Downgrade to a Version¶
downgrade is a bulk rollback: it rolls back all migrations down to (but not including) the target version. --to 0000 rolls back everything.
[DBWarden] Rolling back primary__0001_create_core_tables...
[DBWarden] Downgrade complete. At version: 0000
migrate vs rollback vs downgrade¶
| Command | What it does | Safe to run twice? |
|---|---|---|
migrate |
Applies pending migrations | Yes (idempotent) |
rollback |
Reverses the last N applied migrations | Yes (tracks what's applied) |
downgrade |
Rolls back to a specific target version | Yes |
Step 5: Re-apply All¶
After the final apply, status should show:
Step 6: Schema Validation¶
check scans each migration file and classifies operations by safety level:
- SAFE: Adding a nullable column, creating an index
- INFO: Table comment changes
- WARN: Dropping a default, changing column type
- CRITICAL: Dropping a table or column, removing a NOT NULL
Checking migrations for 'primary'...
primary__0001_create_core_tables:
CREATE TABLE users SAFE
CREATE TABLE posts SAFE
CREATE TABLE products SAFE
CREATE TABLE tags SAFE
CREATE INDEX SAFE
COMMENT ON TABLE INFO
Result: 5 SAFE, 1 INFO, 0 WARN, 0 CRITICAL
Step 7: Database Connectivity Check¶
check-db connects to the live database and reports its schema:
Database: primary
Connection: OK
Tables:
users (6 columns)
posts (5 columns)
products (6 columns)
tags (2 columns)
Migration table: _dbwarden_migrations (present)
Lock table: _dbwarden_lock (present)
This confirms the database is reachable and has the expected schema.
Key Takeaways¶
migrateapplies pending SQL with lock protection and checksum recordingrollbackanddowngradegive you precise control over reversalstatusandhistoryare your windows into migration statecheckclassifies each operation by safety before it runscheck-dbvalidates database connectivity and schema existence
Related Documentation¶
migratecommandrollbackcommanddowngradecommandstatuscommandhistorycommandcheckcommandcheck-dbcommand