cigogne
Types
The CigogneError type contains all the possible errors that can happen in Cigogne. Some errors are just wrappers around other error types from the different modules.
pub type CigogneError {
DatabaseError(error: @internal DatabaseError)
FSError(error: @internal FSError)
MigrationError(error: migration.MigrationError)
ParserError(error: @internal ParserError)
ConfigError(error: config.ConfigError)
NothingToRollback
NothingToApply
LibNotIncluded(name: String)
CompoundError(errors: List(CigogneError))
}
Constructors
-
DatabaseError(error: @internal DatabaseError) -
FSError(error: @internal FSError) -
MigrationError(error: migration.MigrationError) -
ParserError(error: @internal ParserError) -
ConfigError(error: config.ConfigError) -
NothingToRollback -
NothingToApply -
LibNotIncluded(name: String) -
CompoundError(errors: List(CigogneError))
The MigrationEngine contains all the data required to apply and roll back migrations.
When you get a MigrationEngine, you can be sure that your database is in a correct state to apply migrations.
You can get a MigrationEngine by using the create_engine function.
Example:
use config <- result.try(config.get("my_app"))
use engine <- result.try(cigogne.create_engine(config))
// Use the engine to apply or rollback migrations
pub opaque type MigrationEngine
Values
pub fn apply(
engine: MigrationEngine,
) -> Result(Nil, CigogneError)
Apply the next migration that wasn’t applied yet.
See create_engine for an example usage.
See apply_n to apply multiple migrations at once.
pub fn apply_all(
engine: MigrationEngine,
) -> Result(Nil, CigogneError)
Apply all unapplied migrations.
Example usage:
use engine <- result.try(cigogne.create_engine(config))
use _ <- result.try(cigogne.apply_all(engine))
pub fn apply_migration_if_not_applied(
engine: MigrationEngine,
migration: migration.Migration,
) -> Result(Nil, CigogneError)
Apply a migration to the database if it hasn’t been applied.
pub fn apply_migrations(
engine: MigrationEngine,
migrations: List(migration.Migration),
) -> Result(Nil, CigogneError)
Apply migrations to the database.
It is recommended to use apply or apply_n instead of this function directly.
It can still be useful if you want to apply specific migrations or zero migrations.
All migrations will be applied in a single transaction, so either all are applied or none is.
Be careful though as we don’t check if the migration has already been applied or not, see apply_migration_if_not_applied for this use case.
pub fn apply_n(
engine: MigrationEngine,
count: Int,
) -> Result(Nil, CigogneError)
Apply the next count migrations.
If count is less than or equal to 0, this function does nothing.
If there is no migration to apply, it returns an error.
If there are less than count migrations to apply, it applies all of them.
Example usage:
use engine <- result.try(cigogne.create_engine(config))
use _ <- result.try(cigogne.apply_n(engine, 3)) // Apply the next 3 migrations
pub fn create_engine(
config: config.Config,
) -> Result(MigrationEngine, CigogneError)
Creates a MigrationEngine from a configuration. This function will try to connect to the database, create the migrations table if it doesn’t exist. Then it will fetch the applied migrations and the existing migration files and check that hashes do match.
Example usage:
import cigogne/config
import cigogne/cigogne
pub fn main() {
use app_name <- result.try(config.get_app_name())
use config <- result.try(config.get(app_name))
use engine <- result.try(cigogne.create_engine(config))
// Now you can use the engine to apply or rollback migrations
use _ <- result.try(cigogne.apply(engine))
use _ <- result.try(cigogne.rollback(engine))
// Rest of your application logic
}
pub fn get_all_migrations(
engine: MigrationEngine,
) -> List(migration.Migration)
Get all defined migrations in your project.
pub fn get_applied_migrations(
engine: MigrationEngine,
) -> List(migration.Migration)
Get applied migrations in your project.
pub fn get_unapplied_migrations(
engine: MigrationEngine,
) -> List(migration.Migration)
Get unapplied migrations in your project.
pub fn include_lib(
migration_engine: MigrationEngine,
lib_name: String,
) -> Result(Nil, CigogneError)
Include a library’s migrations into your project. This will create a new migration that applies all migrations from the library that haven’t been applied yet. This will fail if you haven’t added the library to your project.
Example usage:
gleam add my_lib
gleam run -m cigogne include --lib-name my_lib
# There will be a new migration file created in priv/migrations with name `<timestamp>-my_lib`
pub fn main() -> Nil
The main entry point of the Cigogne CLI.
Example usage:
# List all possible actions
gleam run -m cigogne help
# Create a new migration
gleam run -m cigogne new --name AddUsers
# Apply a single migration
gleam run -m cigogne up
# Apply the next 3 migrations
gleam run -m cigogne up --count 3
# Apply all unapplied migrations
gleam run -m cigogne all
# Rollback a single migration
gleam run -m cigogne down
pub fn new_migration(
migrations_config: config.MigrationsConfig,
name: String,
) -> Result(Nil, CigogneError)
Create a new migration file in the folder specified by the configuration with the provided name.
Note: Migrations are always created in the priv/<migrations_folder> folder.
pub fn print_error(error: CigogneError) -> Nil
Print a MigrateError to the standard error stream.
pub fn read_migrations(
config: config.Config,
) -> Result(List(migration.Migration), CigogneError)
Read all migration files from the folder specified in the configuration .
It is recommended to use create_engine and get_all_migrations instead of this function.
It can still be useful if you want to read migrations without connecting to the database.
pub fn remove_lib(
migration_engine: MigrationEngine,
lib_name: String,
) -> Result(Nil, CigogneError)
Remove a library’s migrations from your project. This will create a new migration that applies all the down migrations from the migrations created by include for this library. This will fail if you haven’t included the library yet.
Example usage:
gleam add my_lib
gleam run -m cigogne include --lib-name my_lib
gleam run -m cigogne remove --lib-name my_lib
# There will be a new migration file created in priv/migrations with name `<timestamp>-remove_my_lib`
pub fn rollback(
engine: MigrationEngine,
) -> Result(Nil, CigogneError)
Roll back the last applied migration.
See create_engine for an example usage.
See rollback_n to rollback multiple migrations at once.
pub fn rollback_migrations(
engine: MigrationEngine,
migrations: List(migration.Migration),
) -> Result(Nil, CigogneError)
Roll back migrations from the database.
It is recommended to use rollback or rollback_n instead of this function directly.
It can still be useful if you want to roll back specific migrations or a zero migrations.
All migrations will be rolled back in a single transaction, so either all are rolled back or none is.
Be careful though as we don’t check if the migration has been applied or not.
pub fn rollback_n(
engine: MigrationEngine,
count: Int,
) -> Result(Nil, CigogneError)
Roll back count migrations.
If count is less than or equal to 0, this function does nothing.
If there is no migration to rollback, it returns an error.
If there are less than count migrations to rollback, it rolls all of them back.
Example usage:
use engine <- result.try(cigogne.create_engine(config))
use _ <- result.try(cigogne.rollback_n(engine, 3)) // Rollback the next 3 migrations
pub fn update_config(
config: config.Config,
) -> Result(Nil, CigogneError)
Update the configuration file at priv/cigogne.toml with the provided configuration.
If the file doesn’t exist, it will be created.
If it already exists, it will be overwritten.