# Type Alias cargo::util::sqlite::Migration

source ·

    pub type Migration = Box<dyn Fn(&Connection) -> CargoResult<()>>;

Expand description

A function or closure representing a database migration.

Migrations support evolving the schema and contents of the database across new versions of cargo. The `migrate` function should be called immediately after opening a connection to a database in order to configure the schema. Whether or not a migration has been done is tracked by the `pragma_user_version` value in the database. Typically you include the initial `CREATE TABLE` statements in the initial list, but as time goes on you can add new tables or `ALTER TABLE` statements. The migration code will only execute statements that haven’t previously been run.

Important things to note about how you define migrations:

- Never remove a migration entry from the list. Migrations are tracked by the index number in the list.
- Never perform any schema modifications that would be backwards incompatible. For example, don’t drop tables or columns.

The `basic_migration` function is a convenience function for specifying migrations that are simple SQL statements. If you need to do something more complex, then you can specify a closure that takes a [`Connection`] and does whatever is needed.

For example:

    migrate(
        &mut conn,
        &[
            basic_migration(
                "CREATE TABLE foo (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    name STRING NOT NULL
                )",
            ),
            Box::new(|conn| {
                conn.execute("INSERT INTO foo (name) VALUES (?1)", [generate_name()])?;
                Ok(())
            }),
            basic_migration("ALTER TABLE foo ADD COLUMN size INTEGER"),
        ],
    )?;

## Aliased Type§

    struct Migration(Unique<dyn Fn(&Connection) -> Result<(), Error>>, Global);

## Fields§

§`0: Unique<dyn Fn(&Connection) -> Result<(), Error>>`§`1: Global`

## Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain `repr(...)` attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 16 bytes