list

Function list 

Source
pub fn list(migrations_dir_name: Option<&str>) -> Result<()>
Expand description

Discovers and lists all SQL migration files for inclusion at compile time.

This function should be called in build.rs to generate code that embeds all migration files into the binary. It scans the specified directory for .sql files and generates Rust code to include them.

The function will:

  1. Look for SQL files in the specified directory (relative to Cargo.toml)
  2. Sort them alphabetically by filename
  3. Generate code that includes their content at compile time
  4. Set up cargo to rebuild when migration files change

§Arguments

  • migrations_dir_name - Optional custom directory name (defaults to “migrations”)

§Example in build.rs

// In your canister's build.rs file
fn main() {
    // Use default "migrations" directory
    ic_sql_migrate::list(None).unwrap();

    // Or specify a custom directory relative to Cargo.toml
    ic_sql_migrate::list(Some("migrations")).unwrap();
}

§File Naming Convention

Migration files should be named with a sortable prefix to ensure correct execution order:

  • 001_initial_schema.sql
  • 002_add_users_table.sql
  • 003_add_indexes.sql

§Errors

Returns an I/O error if:

  • The output directory (OUT_DIR) cannot be written to
  • File system operations fail
  • Environment variables CARGO_MANIFEST_DIR or OUT_DIR are not set