cigogne
Types
The configuration used to create a MigrationEngine
pub type DatabaseConfig {
EnvVarConfig
UrlConfig(url: String)
PogConfig(config: pog.Config)
ConnectionConfig(connection: pog.Connection)
}
Constructors
-
EnvVarConfigThe default configuration. It uses the DATABASE_URL envvar to connect
-
UrlConfig(url: String)A configuration using the URL to the database
-
PogConfig(config: pog.Config)A configuration using a pog.Config to connect. It disables schema generation.
-
ConnectionConfig(connection: pog.Connection)A configuration using a pog.Connection directly. It disables schema generation.
The MigrationEngine contains all the data required to apply and roll back migrations.
pub type MigrationEngine {
MigrationEngine(
db_url: option.Option(String),
connection: pog.Connection,
applied: List(types.Migration),
files: List(types.Migration),
zero_applied: Bool,
)
}
Constructors
-
MigrationEngine( db_url: option.Option(String), connection: pog.Connection, applied: List(types.Migration), files: List(types.Migration), zero_applied: Bool, )
Values
pub fn apply_migration(
engine: MigrationEngine,
migration: types.Migration,
) -> Result(Nil, types.MigrateError)
Apply a migration to the database. This function does not create a schema file.
pub fn apply_migration_if_not_applied(
engine: MigrationEngine,
migration: types.Migration,
) -> Result(Nil, types.MigrateError)
Apply a migration to the database if it hasn’t been applied. This function does not create a schema file.
pub fn apply_next_migration(
engine: MigrationEngine,
) -> Result(Nil, types.MigrateError)
Apply the next migration that wasn’t applied yet. The migrations are acquired from priv/migrations/*.sql files. This function does not create a schema file.
pub fn create_migration_engine(
config: DatabaseConfig,
) -> Result(MigrationEngine, types.MigrateError)
Creates a MigrationEngine from a configuration. This function will try to connect to the database. Then it will fetch the applied migrations and the existing migration files.
pub fn create_zero_migration(
name: String,
queries_up: List(String),
queries_down: List(String),
) -> types.Migration
Create a “zero” migration that should be applied before the user’s migrations
pub fn execute_migrations_to_last(
engine: MigrationEngine,
) -> Result(Nil, types.MigrateError)
Apply migrations until we reach the last defined migration. The migrations are acquired from priv/migrations/*.sql files. This function does not create a schema file.
pub fn execute_n_migrations(
engine: MigrationEngine,
count: Int,
) -> Result(Nil, types.MigrateError)
Apply or roll back migrations until we reach the migration corresponding to the provided number. The migrations are acquired from priv/migrations/*.sql files. This function does not create a schema file.
pub fn get_migrations(
,
) -> Result(List(types.Migration), types.MigrateError)
Get all defined migrations in your project.
Migration files are searched in the priv/migrations folder.
pub fn get_schema(
url: String,
) -> Result(String, types.MigrateError)
Get details about the schema of the database at the provided url.
pub fn is_zero_migration(migration: types.Migration) -> Bool
Checks if a migration is a zero migration (has been created with create_zero_migration)
pub fn migrate_down() -> Result(Nil, types.MigrateError)
Roll back the last applied migration.
This function will get the database url from the DATABASE_URL environment variable.
The migrations are then acquired from priv/migrations/*.sql files.
If successful, it will also create a file and write details of the new schema in it.
pub fn migrate_n(count: Int) -> Result(Nil, types.MigrateError)
Apply or roll back migrations until we reach the migration corresponding to the provided number.
This function will get the database url from the DATABASE_URL environment variable.
The migrations are then acquired from priv/migrations/*.sql files.
If successful, it will also create a file and write details of the new schema in it.
pub fn migrate_to_last() -> Result(Nil, types.MigrateError)
Apply migrations until we reach the last defined migration.
This function will get the database url from the DATABASE_URL environment variable.
The migrations are then acquired from priv/migrations/*.sql files.
If successful, it will also create a file and write details of the new schema in it.
pub fn migrate_up() -> Result(Nil, types.MigrateError)
Apply the next migration that wasn’t applied yet.
This function will get the database url from the DATABASE_URL environment variable.
The migrations are then acquired from priv/migrations/*.sql files.
If successful, it will also create a file and write details of the new schema in it.
pub fn new_migration(
name: String,
) -> Result(Nil, types.MigrateError)
Create a new migration file in the priv/migrations folder with the provided name.
pub fn print_error(error: types.MigrateError) -> Nil
Print a MigrateError to the standard error stream.
pub fn roll_back_migration(
engine: MigrationEngine,
migration: types.Migration,
) -> Result(Nil, types.MigrateError)
Roll back a migration from the database. This function does not create a schema file.
pub fn roll_back_previous_migration(
engine: MigrationEngine,
) -> Result(Nil, types.MigrateError)
Roll back the last applied migration. The migrations are acquired from priv/migrations/*.sql files. This function does not create a schema file.
pub const timestamp_format: tempo.NaiveDateTimeFormat
pub fn update_schema(
engine: MigrationEngine,
) -> Result(Nil, types.MigrateError)
Create or update a schema file if the engine configuration permits it. See update_schema_file.
pub fn update_schema_file(
url: String,
) -> Result(Nil, types.MigrateError)
Create or update a schema file with details of the schema of the database at the provided url.
The schema file is created at ./sql.schema.
pub fn verify_applied_migration_hashes(
engine: MigrationEngine,
) -> Result(Nil, types.MigrateError)
Verify the hash integrity of applied migrations. If an already applied migration has been modified, it should most likely be run again by the user.