### Perform Database Migrations with Feather in Gleam Source: https://hexdocs.pm/feather/index This Gleam code demonstrates how to initialize feather, connect to a database (using sqlight in this example), retrieve migration scripts, and apply them. It sets up the necessary environment for running database migrations. ```Gleam import feather import gleam/result import gleam/erlang import sqlight pub fn main() { let assert Ok(priv_dir) = erlang.priv_directory("my_module_name") use migrations <- result.try(feather.get_migrations(priv_dir <> "/migrations")) use connection <- feather.connect(feather.Config(..feather.default_config(), file: "./database.db")) feather.migrate(migrations, on: connection) } ``` -------------------------------- ### Start Connection Pool in Gleam Source: https://hexdocs.pm/feather/feather/pool Initializes and starts a connection pool in Gleam with a given configuration and connection count. It returns a `Subject` for managing messages from the pool or a `StartError` if initialization fails. ```Gleam pub fn start( config: Config, count: Int, ) -> Result(Subject(ManagerMessage(Connection, a)), StartError) ``` -------------------------------- ### Get Database Migrations from Directory (Gleam) Source: https://hexdocs.pm/feather/feather/migrate Retrieves a list of migration files from a specified directory. Migration files must be SQL, start with an integer ID, and end with `.sql`. ```Gleam pub fn get_migrations( in directory: String, ) -> Result(List(Migration), MigrationError) ``` -------------------------------- ### Retrieve Default Feather Configuration Source: https://hexdocs.pm/feather/feather Returns a default `Config` object, providing a convenient starting point for database connection settings. This function simplifies setup by offering a pre-configured set of common parameters. ```Gleam pub fn default_config() -> Config ``` -------------------------------- ### feather/migrate.main Function API Source: https://hexdocs.pm/feather/feather/migrate API documentation for the `main` function, the CLI entry point. ```APIDOC fn main() -> Nil ``` -------------------------------- ### Common Development Commands for Gleam Projects Source: https://hexdocs.pm/feather/index These are standard command-line instructions for developing Gleam projects. `gleam run` executes the project, `gleam test` runs unit tests, and `gleam shell` opens an Erlang shell for interactive debugging. ```Shell gleam run # Run the project gleam test # Run the tests gleam shell # Run an Erlang shell ``` -------------------------------- ### Run Feather CLI (Gleam) Source: https://hexdocs.pm/feather/feather/migrate The main entry point for the feather CLI, used to generate new migrations and dump the schema. Typically run via `gleam run -m feather`. ```Gleam pub fn main() -> Nil ``` -------------------------------- ### Execute Feather CLI Main Entry Point Source: https://hexdocs.pm/feather/feather The main entry point for the feather command-line interface (CLI), primarily used for generating new migrations and dumping the database schema. This function is typically invoked via `gleam run -m feather` and is not intended for direct programmatic use. ```Gleam pub fn main() -> Nil ``` -------------------------------- ### Apply Database Migrations (Gleam) Source: https://hexdocs.pm/feather/feather/migrate Applies a given list of migrations to a database using a `sqlight` connection. ```Gleam pub fn migrate( migrations: List(Migration), on connection: Connection, ) -> Result(Nil, MigrationError) ``` -------------------------------- ### Add Feather Dependency to Gleam Project Source: https://hexdocs.pm/feather/index This command adds the 'feather' library as a dependency to your Gleam project. It allows your project to utilize feather's database migration capabilities. ```Shell gleam add feather ``` -------------------------------- ### Configure Feather Migration Paths in gleam.toml Source: https://hexdocs.pm/feather/index These lines define the directory for migration files and the output path for the generated schema file within your `gleam.toml` configuration. Adjust these paths to match your project's structure. ```TOML # this can of course be anything you like migrations_dir = "./priv/migrations" schemafile = "./schema.sql" ``` -------------------------------- ### Connect to a Feather SQLite Database Source: https://hexdocs.pm/feather/feather Establishes a connection to an SQLite database using the provided `Config` object. This function is the primary entry point for initiating database interactions and returns a `Result` indicating success or failure. ```Gleam pub fn connect(config: Config) -> Result(Connection, Error) ``` -------------------------------- ### feather/migrate.migrate Function API Source: https://hexdocs.pm/feather/feather/migrate API documentation for the `migrate` function. ```APIDOC fn migrate(migrations: List(Migration), connection: Connection) -> Result(Nil, MigrationError) migrations: List(Migration) - A list of migrations to apply. connection: Connection - The database connection to use. ``` -------------------------------- ### Optimize an SQLite Database Connection Source: https://hexdocs.pm/feather/feather Runs the `PRAGMA optimize;` command on the given database connection. This operation can significantly improve database performance, especially for long-lived connections, by optimizing internal structures and freeing resources. ```Gleam pub fn optimize(connection: Connection) -> Result(Nil, Error) ``` -------------------------------- ### Manage Short-Lived SQLite Connection with Callback Source: https://hexdocs.pm/feather/feather Creates a temporary database connection, executes a provided callback function (`fxn`) with that connection, and then automatically disconnects. This pattern ensures proper resource management and is particularly well-suited for use with Gleam's 'use' expressions for concise and safe database operations. ```Gleam pub fn with_connection( config: Config, fxn: fn(Connection) -> a, ) -> Result(a, Error) ``` ```Gleam use connection <- with_connection(default_config()) let result = sqlight.query(...) ``` -------------------------------- ### feather/migrate.Migration Type API Source: https://hexdocs.pm/feather/feather/migrate API documentation for the `Migration` type, representing a database migration. ```APIDOC type Migration: Migration(id: Int, up: String) ``` -------------------------------- ### feather/migrate.get_migrations Function API Source: https://hexdocs.pm/feather/feather/migrate API documentation for the `get_migrations` function. ```APIDOC fn get_migrations(directory: String) -> Result(List(Migration), MigrationError) directory: String - The directory to scan for migration files. ``` -------------------------------- ### Execute Function with Connection in Gleam Source: https://hexdocs.pm/feather/feather/pool Executes a given function `fxn` with a connection obtained from the pool, applying a specified timeout. This function ensures that a connection is acquired, used, and then potentially returned to the pool. It returns the result of the function or `Nil` on failure. ```Gleam pub fn with_connection( pool: Subject(ManagerMessage(Connection, a)), timeout: Int, fxn: fn(Connection) -> a, ) -> Result(a, Nil) ``` -------------------------------- ### feather/migrate.MigrationError Type API Source: https://hexdocs.pm/feather/feather/migrate API documentation for the `MigrationError` type, enumerating possible errors during migration operations. ```APIDOC type MigrationError: DirectoryNotExist(String) - Folder that you gave does not exist InvalidMigrationName(String) - The migration script file name is not valid InvalidMigrationId(String) - The migration script file has a non-integer id TransactionError(sqlight.Error) - Error starting or comitting the migration transaction MigrationsTableError(sqlight.Error) - Error reading/writing/creating the transactions table MigrationScriptError(Int, sqlight.Error) - Error applying the migrations script ``` -------------------------------- ### Define Config Type for Feather Database Connections Source: https://hexdocs.pm/feather/feather Defines the `Config` record type used for configuring database connections in the feather library. It includes fields for file path, journal mode, synchronization, temporary storage, memory-mapped size, page size, and foreign key enforcement. ```Gleam pub type Config { Config( file: String, journal_mode: JournalMode, synchronous: SyncMode, temp_store: TempStore, mmap_size: Option(Int), page_size: Option(Int), foreign_keys: Bool, ) } ``` -------------------------------- ### Disconnect from a Feather SQLite Database Source: https://hexdocs.pm/feather/feather Closes an existing database connection, first running `PRAGMA optimize;` to improve performance. This function is crucial for releasing system resources and ensuring proper database state after operations. ```Gleam pub fn disconnect(connection: Connection) -> Result(Nil, Error) ``` -------------------------------- ### Define TempStore Enum for SQLite Temporary Data Source: https://hexdocs.pm/feather/feather Defines the `TempStore` enum type, specifying where temporary tables and indices are stored for SQLite connections. Options include default behavior, file-based storage, or in-memory storage, affecting performance and resource usage. ```Gleam pub type TempStore { TempStoreDefault TempStoreFile TempStoreMemory } ``` -------------------------------- ### Define Migration Type in Gleam Source: https://hexdocs.pm/feather/feather/migrate Defines the `Migration` record type used to represent database migrations, including an ID and the SQL script. ```Gleam pub type Migration { Migration(id: Int, up: String) } ``` -------------------------------- ### Define JournalMode Enum for SQLite Connections Source: https://hexdocs.pm/feather/feather Defines the `JournalMode` enum type, specifying different journaling modes for SQLite connections. These modes control how transactions are handled and written to the database, impacting performance and data integrity. ```Gleam pub type JournalMode { JournalDelete JournalTruncate JournalPersist JournalMemory JournalWal JournalOff } ``` -------------------------------- ### Define SyncMode Enum for SQLite Synchronization Source: https://hexdocs.pm/feather/feather Defines the `SyncMode` enum type, which controls the level of synchronization for SQLite connections. It determines how often data is flushed to disk to ensure durability and prevent data loss in case of system failure. ```Gleam pub type SyncMode { SyncExtra SyncFull SyncNormal SyncOff } ``` -------------------------------- ### Execute Function within Transaction in Gleam Source: https://hexdocs.pm/feather/feather/pool Executes a given function `fxn` within a transaction using a connection from the pool, applying a specified timeout. This ensures atomicity for database operations. It returns the result of the function or `Nil` on failure. Note: This function will panic if the transaction is manually ended within the provided `fxn`. ```Gleam pub fn with_transaction( pool: Subject(ManagerMessage(Connection, Result(a, Nil))), timeout: Int, fxn: fn(Connection) -> Result(a, Nil), ) -> Result(a, Nil) ``` -------------------------------- ### Define MigrationError Type in Gleam Source: https://hexdocs.pm/feather/feather/migrate Defines the `MigrationError` enum type, listing various error conditions that can occur during database migrations. ```Gleam pub type MigrationError { DirectoryNotExist(String) InvalidMigrationName(String) InvalidMigrationId(String) TransactionError(sqlight.Error) MigrationsTableError(sqlight.Error) MigrationScriptError(Int, sqlight.Error) } ``` -------------------------------- ### Define Pool Type in Gleam Source: https://hexdocs.pm/feather/feather/pool Defines a generic `Pool` type in Gleam, representing a subject that manages `puddle.ManagerMessage` for connections. This type is fundamental for handling connection pool operations. ```Gleam pub type Pool(a) = Subject(puddle.ManagerMessage(Connection, a)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.