Function Signature
Description
Migrates the database by running all pending migrations found in the migration folder. This function applies all.up.sql migration files that haven’t been applied yet, tracks them in the migrations table, and optionally dumps the database schema after successful migration.
Parameters
The database connection URL. Supports multiple database types:
- PostgreSQL:
postgres://user:password@host:port/database?sslmode=disable - MySQL:
mysql://root:password@localhost:3306/app - MariaDB:
mariadb://root:password@localhost:3307/app - SQLite:
sqlite://./database.sqlite - LibSQL/Turso:
https://your-database.turso.io
Authentication token for LibSQL/Turso databases. Use
None for other database types or when authentication is not required.Name of the table used to track applied migrations. Common values:
"schema_migrations" or "migrations".Path to the directory containing migration files. Example:
"./migrations" or "./db/migrations".Name of the schema dump file to generate after migrations. Example:
"schema.sql".Timeout in seconds to wait for the database to be ready before attempting migrations. Use
Some(30) for 30 seconds, or None for default timeout.Whether to dump the database schema to a file after successful migration. Set to
true to enable schema dumping, false to skip.Return Value
ReturnsResult<()> which:
- Returns
Ok(())if all migrations were applied successfully - Returns
Errif there was a connection error, migration file error, or SQL execution error
Usage Example
PostgreSQL Example
LibSQL/Turso Example
Behavior
- Reads all
.up.sqlfiles from the migration folder - Compares with migrations already applied (stored in the migration table)
- Executes pending migrations in timestamp order
- Each migration runs in a transaction by default (unless
-- transaction:nois specified in the migration file) - Updates the migration tracking table after each successful migration
- Optionally dumps the database schema after all migrations complete
Error Handling
The function may return errors in these cases:- Database connection failure
- Migration folder doesn’t exist or is unreadable
- Invalid SQL syntax in migration files
- Transaction rollback due to SQL errors
- Permission issues when writing schema dump file
See Also
- migate_down - Rollback migrations
- status_migrations - Check migration status
- new_migration - Create new migration files