Function Signature
Description
Rollbacks previously applied migrations by executing the corresponding.down.sql files. This function allows you to revert database changes by running rollback migrations in reverse order.
The function name has a typo (
migate_down instead of migrate_down). Use the actual function name as shown in the signature.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. Should match the table name used in
migrate_database.Path to the directory containing migration files. Must contain the
.down.sql files corresponding to applied migrations.Name of the schema dump file to update after rollback. Example:
"schema.sql".Timeout in seconds to wait for the database to be ready. Use
Some(30) for 30 seconds, or None for default timeout.Whether to dump the database schema after successful rollback. Set to
true to update the schema file, false to skip.Number of migrations to rollback. Use
1 to rollback the most recent migration, 2 for the last two migrations, etc.Return Value
ReturnsResult<()> which:
- Returns
Ok(())if the specified number of migrations were rolled back successfully - Returns
Errif there was a connection error, migration file not found, or SQL execution error
Usage Example
Rollback Multiple Migrations
LibSQL/Turso Example
Behavior
- Retrieves the most recent applied migrations from the migration tracking table
- Executes the corresponding
.down.sqlfiles in reverse chronological order - Rolls back the specified number of migrations (
rollback_amount) - Each rollback runs in a transaction by default (unless
-- transaction:nois specified) - Removes migration records from the tracking table after successful rollback
- Optionally updates the schema dump file to reflect the current database state
Error Handling
The function may return errors in these cases:- Database connection failure
- No migrations available to rollback
- Missing
.down.sqlfile for a migration - Invalid SQL syntax in rollback migration files
- Transaction rollback due to SQL errors
- Attempting to rollback more migrations than exist
Important Notes
- The
.down.sqlfile should reverse the changes made by the corresponding.up.sqlfile - If a migration created a table, the rollback should drop that table
- If a migration added columns, the rollback should remove those columns
- Data modifications in migrations may not be reversible
See Also
- migrate_database - Apply migrations
- status_migrations - Check migration status
- new_migration - Create new migration files