### Install fastmigrate Source: https://github.com/answerdotai/fastmigrate/blob/main/README.md Install fastmigrate using pip or add it to your pyproject.toml if using uv. ```bash pip install fastmigrate ``` ```bash uv add fastmigrate ``` -------------------------------- ### Install FastMigrate with Dev Dependencies (uv) Source: https://github.com/answerdotai/fastmigrate/blob/main/README.md Installs fastmigrate in editable mode with development dependencies using uv. ```bash uv sync ``` -------------------------------- ### FastMigrate Configuration File Example Source: https://context7.com/answerdotai/fastmigrate/llms.txt Example of a .fastmigrate config file to set default paths for database and migrations. CLI arguments override these settings. ```ini # .fastmigrate [paths] db = data/production.db migrations = db/migrations ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/answerdotai/fastmigrate/blob/main/CLAUDE.md Use this command to install the project in editable mode, including its dependencies. ```bash pip install -e . ``` -------------------------------- ### SQL Migration Script Example Source: https://context7.com/answerdotai/fastmigrate/llms.txt Example of a SQL migration script to create a users table. Ensure SQL statements are valid. ```sql -- migrations/0001-create-users.sql -- Migration 0001: Create users table CREATE TABLE users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL UNIQUE, email TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE INDEX idx_users_username ON users(username); ``` -------------------------------- ### SQLAlchemy Backend Example Source: https://context7.com/answerdotai/fastmigrate/llms.txt Example of a config.py file for using a custom database backend with SQLAlchemy. Functions can be sync or async. ```python # Create a config.py in your migrations directory to use a custom database backend. Each function can be sync or async. ```python ``` -------------------------------- ### FastMigrate Python Configuration Source: https://github.com/answerdotai/fastmigrate/blob/main/README.md Example of how to create a SQLAlchemy engine for SQLite using pysqlite. ```python from sqlalchemy import create_engine, text def get_connection(db): return create_engine(f"sqlite+pysqlite:///{db}") def close_connection(engine): engine.dispose() def ensure_meta_table(engine): with engine.begin() as conn: conn.exec_driver_sql( "CREATE TABLE IF NOT EXISTS _meta (id INTEGER PRIMARY KEY, version INTEGER NOT NULL)" ) row = conn.exec_driver_sql("SELECT version FROM _meta WHERE id=1").fetchone() if row is None: conn.exec_driver_sql("INSERT INTO _meta (id, version) VALUES (1, 0)") def get_version(engine): with engine.connect() as conn: row = conn.exec_driver_sql("SELECT version FROM _meta WHERE id=1").fetchone() return int(row[0]) if row else 0 def set_version(engine, version: int): with engine.begin() as conn: conn.exec_driver_sql("DELETE FROM _meta WHERE id=1") conn.exec_driver_sql("INSERT INTO _meta (id, version) VALUES (1, ?)", (int(version),)) def execute_sql(engine, sql: str): with engine.begin() as conn: # Basic split; feel free to use something more robust for your DB for stmt in [s.strip() for s in sql.split(";") if s.strip()]: conn.exec_driver_sql(stmt) ``` -------------------------------- ### Install FastMigrate with Dev Dependencies (pip) Source: https://github.com/answerdotai/fastmigrate/blob/main/README.md Installs fastmigrate in editable mode with development dependencies using pip. ```bash pip install -e . --group dev ``` -------------------------------- ### Python Migration Script Example Source: https://context7.com/answerdotai/fastmigrate/llms.txt Python script to add sample data to the users table. Requires database path as a command-line argument and must return exit code 0 on success. ```python #!/usr/bin/env python # migrations/0003-add-sample-data.py """Migration 0003: Add sample data""" import sqlite3 import sys def main() -> int: if len(sys.argv) < 2: print("Error: Database path not provided") return 1 db_path = sys.argv[1] conn = sqlite3.connect(db_path) conn.executescript(""" INSERT INTO users (username, email) VALUES ('admin', 'admin@example.com'), ('user1', 'user1@example.com'); """) conn.commit() conn.close() return 0 if __name__ == "__main__": sys.exit(main()) ``` -------------------------------- ### Shell Migration Script Example Source: https://context7.com/answerdotai/fastmigrate/llms.txt Shell script to create tags and post_tags tables. The database path is passed as the first argument and the script must exit with code 0 on success. ```bash #!/bin/sh # migrations/0004-add-tags-table.sh # Migration 0004: Create tags table DB_PATH="$1" sqlite3 "$DB_PATH" < int: """Get current version.""" row = await conn.fetchrow("SELECT version FROM _meta WHERE id=1") return int(row['version']) if row else 0 async def set_version(conn, version: int): """Set schema version.""" await conn.execute("UPDATE _meta SET version=$1 WHERE id=1", version) async def execute_sql(conn, sql: str): """Execute SQL migration.""" await conn.execute(sql) ``` -------------------------------- ### Synchronous SQLite Connection and Migration Functions Source: https://context7.com/answerdotai/fastmigrate/llms.txt Provides functions for creating a SQLite database engine, managing connections, ensuring the meta table exists, getting and setting the schema version, and executing SQL migrations. ```Python from sqlalchemy import create_engine def get_connection(db): """Return database connection/engine.""" return create_engine(f"sqlite+pysqlite:///{db}") def close_connection(engine): """Clean up connection.""" engine.dispose() def ensure_meta_table(engine): """Create _meta table if it doesn't exist.""" with engine.begin() as conn: conn.exec_driver_sql( "CREATE TABLE IF NOT EXISTS _meta (id INTEGER PRIMARY KEY, version INTEGER NOT NULL)" ) row = conn.exec_driver_sql("SELECT version FROM _meta WHERE id=1").fetchone() if row is None: conn.exec_driver_sql("INSERT INTO _meta (id, version) VALUES (1, 0)") def get_version(engine) -> int: """Get current schema version.""" with engine.connect() as conn: row = conn.exec_driver_sql("SELECT version FROM _meta WHERE id=1").fetchone() return int(row[0]) if row else 0 def set_version(engine, version: int): """Update schema version.""" with engine.begin() as conn: conn.exec_driver_sql("DELETE FROM _meta WHERE id=1") conn.exec_driver_sql("INSERT INTO _meta (id, version) VALUES (1, ?)", (int(version),)) def execute_sql(engine, sql: str): """Execute SQL migration.""" with engine.begin() as conn: for stmt in [s.strip() for s in sql.split(";") if s.strip()]: conn.exec_driver_sql(stmt) ``` -------------------------------- ### Get Current Database Version Source: https://context7.com/answerdotai/fastmigrate/llms.txt Retrieve the current version number from the database's `_meta` table using `get_db_version`. Handles `FileNotFoundError` if the database does not exist and `sqlite3.Error` if the database is not managed by fastmigrate. ```python from fastmigrate import get_db_version from pathlib import Path import sqlite3 db_path = Path("data/myapp.db") try: version = get_db_version(db_path) print(f"Current database version: {version}") except FileNotFoundError: print("Database file does not exist") except sqlite3.Error as e: print(f"Database not managed by fastmigrate: {e}") ``` -------------------------------- ### get_db_version - Get current database version Source: https://context7.com/answerdotai/fastmigrate/llms.txt Returns the current version number stored in the database's `_meta` table. Raises FileNotFoundError if database doesn't exist, or sqlite3.Error if the database is not versioned. ```APIDOC ## get_db_version ### Description Returns the current version number stored in the database's `_meta` table. Raises FileNotFoundError if database doesn't exist, or sqlite3.Error if the database is not versioned. ### Method `get_db_version` ### Parameters #### Path Parameters - **db_path** (Path) - Required - Path to the SQLite database file. ### Request Example ```python from fastmigrate import get_db_version from pathlib import Path import sqlite3 db_path = Path("data/myapp.db") try: version = get_db_version(db_path) print(f"Current database version: {version}") except FileNotFoundError: print("Database file does not exist") except sqlite3.Error as e: print(f"Database not managed by fastmigrate: {e}") ``` ### Response #### Success Response (int) - **version** (int) - The current version number of the database. ``` -------------------------------- ### Full Application Database Initialization with FastMigrate Source: https://context7.com/answerdotai/fastmigrate/llms.txt Demonstrates a complete application startup pattern integrating FastMigrate. It includes creating/verifying the database, optionally backing it up, running pending migrations, and setting up logging. ```Python from pathlib import Path from fastmigrate import create_db, run_migrations, create_db_backup, setup_logging, get_db_version import sys def initialize_database( db_path: Path, migrations_dir: Path, backup_before_migrate: bool = True, verbose: bool = False ) -> int: """Initialize database with migrations. Returns the final database version, or exits on failure. """ if verbose: setup_logging(verbose=True) # Step 1: Create or verify versioned database try: initial_version = create_db(db_path) print(f"Database initialized at version {initial_version}") except Exception as e: print(f"Failed to create/verify database: {e}") sys.exit(1) # Step 2: Optionally backup before migration if backup_before_migrate and db_path.exists(): backup = create_db_backup(db_path) if backup is None: print("Backup failed, aborting migration") sys.exit(1) # Step 3: Run pending migrations if not run_migrations(db_path, migrations_dir): print("Migration failed!") sys.exit(1) # Step 4: Return final version final_version = get_db_version(db_path) print(f"Database ready at version {final_version}") return final_version # Application entry point if __name__ == "__main__": db_path = Path("data/myapp.db") migrations_dir = Path("migrations") version = initialize_database( db_path, migrations_dir, backup_before_migrate=True, verbose=True ) # Continue with application startup... print(f"Starting application with database schema v{version}") ``` -------------------------------- ### Initialize and Run Migrations in Python Source: https://github.com/answerdotai/fastmigrate/blob/main/README.md Use fastmigrate in your application startup to create or verify the database and apply pending migrations. ```python from fastmigrate import create_db, run_migrations, setup_logging # At application startup: db_path = "path/to/database.db" migrations_dir = "path/to/migrations" # Create/verify there is a versioned database current_version = create_db(db_path) # Optional: enable debug logs from fastmigrate setup_logging(verbose=True) # Apply any pending migrations if not run_migrations(db_path, migrations_dir): print("Database migration failed!") ``` -------------------------------- ### Run fastmigrate with Arguments Source: https://github.com/answerdotai/fastmigrate/blob/main/CLAUDE.md Run the fastmigrate module, specifying the database path and migrations directory. ```bash python -m fastmigrate --db path/to/db --migrations path/to/migrations ``` -------------------------------- ### Show Version Info CLI Command Source: https://context7.com/answerdotai/fastmigrate/llms.txt Displays the fastmigrate library version and the database schema version. Requires the database path. ```bash fastmigrate_check_version --db /path/to/myapp.db # Output: # FastMigrate version: 0.5.3 # Database version: 4 ``` -------------------------------- ### Run fastmigrate Source: https://github.com/answerdotai/fastmigrate/blob/main/CLAUDE.md Execute the fastmigrate module from the command line. ```bash python -m fastmigrate ``` -------------------------------- ### Create Database Command Source: https://github.com/answerdotai/fastmigrate/blob/main/README.md Creates an empty database with version 0 if it doesn't exist. Exits with an error if the database is unversioned or invalid. Equivalent to calling `fastmigrate.create_db()`. ```bash fastmigrate_create_db --db /path/to/data.db ``` -------------------------------- ### setup_logging - Configure debug logging Source: https://context7.com/answerdotai/fastmigrate/llms.txt Enables or disables debug-level logging for fastmigrate operations. Useful for troubleshooting migration issues. ```APIDOC ## setup_logging ### Description Enables or disables debug-level logging for fastmigrate operations. Useful for troubleshooting migration issues. ### Method `setup_logging` ### Parameters #### Query Parameters - **verbose** (bool) - Optional - If True, enables debug logging. Defaults to False. ### Request Example ```python from fastmigrate import setup_logging, run_migrations from pathlib import Path # Enable verbose logging before running migrations setup_logging(verbose=True) # Now run_migrations will output detailed debug information success = run_migrations( Path("data/myapp.db"), Path("migrations") ) ``` ``` -------------------------------- ### Enroll Existing Database Command Source: https://github.com/answerdotai/fastmigrate/blob/main/README.md Enrolls an existing SQLite database for versioning, adds an initial migration, and runs it, setting the version to 1. Equivalent to calling `fastmigrate.enroll_db()`. ```bash fastmigrate_enroll_db --db path/to/data.db ``` -------------------------------- ### Configure Fastmigrate Debug Logging Source: https://context7.com/answerdotai/fastmigrate/llms.txt Control the verbosity of fastmigrate operations with `setup_logging`. Set `verbose=True` to enable detailed debug logging, which is helpful for diagnosing migration issues. This should typically be called before running migrations. ```python from fastmigrate import setup_logging, run_migrations from pathlib import Path # Enable verbose logging before running migrations setup_logging(verbose=True) # Now run_migrations will output detailed debug information success = run_migrations( Path("data/myapp.db"), Path("migrations") ) # DEBUG: mode=sqlite db=data/myapp.db migrations_dir=migrations # DEBUG: db_exists=True # DEBUG: current_version=2 # DEBUG: migration_versions=[1, 2, 3, 4] # DEBUG: pending_versions=[3, 4] # DEBUG: apply version=3 script=0003-add-indexes.sql # DEBUG: updated_version=3 # ... ``` -------------------------------- ### Create Database Backup CLI Command Source: https://context7.com/answerdotai/fastmigrate/llms.txt Creates a timestamped backup of the database. Requires the database path. ```bash fastmigrate_backup_db --db /path/to/myapp.db # Output: # Database backup created: /path/to/myapp.db.20240115_143022.backup ``` -------------------------------- ### Enroll Existing Database CLI Command Source: https://context7.com/answerdotai/fastmigrate/llms.txt Enrolls an existing unversioned SQLite database into fastmigrate management. Adds version tracking and generates an initial migration script. Requires database and migrations paths. ```bash fastmigrate_enroll_db --db /path/to/existing.db --migrations /path/to/migrations # This will: # 1. Add a _meta table to the database with version=1 # 2. Generate migrations/0001-initialize.sql from the current schema ``` -------------------------------- ### Fastmigrate Configuration for Non-SQLite Databases Source: https://github.com/answerdotai/fastmigrate/blob/main/README.md Provide a backend adapter in config.py within your migrations directory to make fastmigrate database-independent. ```python def get_connection(db): ... def ensure_meta_table(conn): ... def get_version(conn) -> int: ... def set_version(conn, version: int): ... def execute_sql(conn, sql: str): ... # optional def close_connection(conn): ... ``` -------------------------------- ### Check Database Version Command Source: https://github.com/answerdotai/fastmigrate/blob/main/README.md Reports the version of both fastmigrate and the database. ```bash fastmigrate_check_version --db /path/to/data.db ``` -------------------------------- ### Create or Verify Versioned SQLite Database Source: https://context7.com/answerdotai/fastmigrate/llms.txt Use `create_db` to initialize a new versioned SQLite database or verify an existing one. If the database exists and is versioned, it returns the current version without changes. ```python from fastmigrate import create_db from pathlib import Path # Create a new versioned database db_path = Path("data/myapp.db") current_version = create_db(db_path) print(f"Database ready at version {current_version}") # Output: Database ready at version 0 # Verify an existing versioned database existing_version = create_db(db_path) # Returns current version, no modification print(f"Existing database at version {existing_version}") ``` -------------------------------- ### Backup Database Command Source: https://github.com/answerdotai/fastmigrate/blob/main/README.md Backs up the database with a timestamped filename ending in .backup. Equivalent to calling `fastmigrate.backup_db()`. ```bash fastmigrate_backup_db --db /path/to/data.db ``` -------------------------------- ### Run Migrations Command Source: https://github.com/answerdotai/fastmigrate/blob/main/README.md Runs all necessary migrations on the database. Fails if a migration fails or if no managed database exists at the specified path. Use `--verbose` for debug logs. Equivalent to calling `fastmigrate.run_migrations()`. ```bash fastmigrate_run_migrations --db path/to/data.db --verbose ``` -------------------------------- ### Run Pending Migrations CLI Command Source: https://context7.com/answerdotai/fastmigrate/llms.txt Applies all pending migration scripts to the database. Supports custom paths, verbose output, and configuration files. ```bash # Run with defaults (looks for ./migrations/ and ./data/database.db) fastmigrate_run_migrations # Run with custom paths and verbose output fastmigrate_run_migrations --db /path/to/myapp.db --migrations /path/to/migrations --verbose # Using config file (.fastmigrate) fastmigrate_run_migrations --config_path .fastmigrate ``` -------------------------------- ### Create New Database CLI Command Source: https://context7.com/answerdotai/fastmigrate/llms.txt Command to create a new versioned SQLite database file or verify an existing one. Supports default and custom database paths. ```bash # Create database at default path (data/database.db) fastmigrate_create_db # Create database at custom path fastmigrate_create_db --db /path/to/myapp.db # Output: # Creating database at /path/to/myapp.db # Created new versioned SQLite database with version=0 at: /path/to/myapp.db ``` -------------------------------- ### create_db - Create or verify a versioned database Source: https://context7.com/answerdotai/fastmigrate/llms.txt Creates a new versioned SQLite database with version 0, or verifies an existing database is properly versioned. If the database already exists with versioning, returns the current version without modification. ```APIDOC ## create_db ### Description Creates a new versioned SQLite database with version 0, or verifies an existing database is properly versioned. If the database already exists with versioning, returns the current version without modification. ### Method `create_db` ### Parameters #### Path Parameters - **db_path** (Path) - Required - Path to the SQLite database file. ### Request Example ```python from fastmigrate import create_db from pathlib import Path # Create a new versioned database db_path = Path("data/myapp.db") current_version = create_db(db_path) print(f"Database ready at version {current_version}") ``` ### Response #### Success Response (int) - **current_version** (int) - The current version of the database. ``` -------------------------------- ### Apply Pending Database Migrations Source: https://context7.com/answerdotai/fastmigrate/llms.txt Use `run_migrations` to apply all migration scripts with version numbers greater than the current database version. It returns `True` on success and `False` on failure, stopping at the first error. Ensure the database is created and logging is set up if needed. ```python from fastmigrate import create_db, run_migrations, setup_logging from pathlib import Path db_path = Path("data/myapp.db") migrations_dir = Path("migrations") # Create/verify database first create_db(db_path) # Enable debug logging (optional) setup_logging(verbose=True) # Run all pending migrations success = run_migrations(db_path, migrations_dir) if not success: print("Migration failed! Check logs for details.") exit(1) print("All migrations applied successfully") ``` -------------------------------- ### create_db_backup - Create timestamped backup Source: https://context7.com/answerdotai/fastmigrate/llms.txt Creates a backup of the database using SQLite's backup API, ensuring consistency even during transactions. Returns the backup path on success, None on failure. ```APIDOC ## create_db_backup ### Description Creates a backup of the database using SQLite's backup API, ensuring consistency even during transactions. Returns the backup path on success, None on failure. ### Method `create_db_backup` ### Parameters #### Path Parameters - **db_path** (Path) - Required - Path to the SQLite database file. ### Request Example ```python from fastmigrate import create_db_backup from pathlib import Path db_path = Path("data/myapp.db") backup_path = create_db_backup(db_path) if backup_path: print(f"Backup created: {backup_path}") # Output: Backup created: data/myapp.db.20240115_143022.backup else: print("Backup failed") ``` ### Response #### Success Response (str or None) - **backup_path** (str) - The path to the created backup file, or None if backup failed. ``` -------------------------------- ### Create Timestamped Database Backup Source: https://context7.com/answerdotai/fastmigrate/llms.txt Generate a consistent, timestamped backup of the database using `create_db_backup`. This function utilizes SQLite's backup API and returns the path to the backup file on success, or `None` if the backup fails. ```python from fastmigrate import create_db_backup from pathlib import Path db_path = Path("data/myapp.db") backup_path = create_db_backup(db_path) if backup_path: print(f"Backup created: {backup_path}") # Output: Backup created: data/myapp.db.20240115_143022.backup else: print("Backup failed") ``` -------------------------------- ### Run Project Tests Source: https://github.com/answerdotai/fastmigrate/blob/main/CLAUDE.md Execute all tests for the project using pytest. ```bash pytest ``` -------------------------------- ### run_migrations - Apply pending migrations Source: https://context7.com/answerdotai/fastmigrate/llms.txt Detects and runs all migration scripts with version numbers greater than the current database version. Returns True if all migrations succeed, False otherwise. Stops execution on the first failure. ```APIDOC ## run_migrations ### Description Detects and runs all migration scripts with version numbers greater than the current database version. Returns True if all migrations succeed, False otherwise. Stops execution on the first failure. ### Method `run_migrations` ### Parameters #### Path Parameters - **db_path** (Path) - Required - Path to the SQLite database file. - **migrations_dir** (Path) - Required - Directory containing migration scripts. ### Request Example ```python from fastmigrate import create_db, run_migrations, setup_logging from pathlib import Path db_path = Path("data/myapp.db") migrations_dir = Path("migrations") # Create/verify database first create_db(db_path) # Enable debug logging (optional) setup_logging(verbose=True) # Run all pending migrations success = run_migrations(db_path, migrations_dir) if not success: print("Migration failed! Check logs for details.") exit(1) print("All migrations applied successfully") ``` ### Response #### Success Response (bool) - **success** (bool) - True if all migrations succeeded, False otherwise. ``` -------------------------------- ### recreate_table - Recreate table with new schema Source: https://context7.com/answerdotai/fastmigrate/llms.txt Updates a table by recreating it with new column definitions and copying existing data. Requires the optional `apswutils` dependency. Implements SQLite's recommended pattern for table schema changes that can't be done with ALTER TABLE. ```APIDOC ## recreate_table ### Description Updates a table by recreating it with new column definitions and copying existing data. Requires the optional `apswutils` dependency. Implements SQLite's recommended pattern for table schema changes that can't be done with ALTER TABLE. ### Method `recreate_table` ### Parameters #### Path Parameters - **db_path** (Path) - Required - Path to the SQLite database file. - **table_name** (str) - Required - The name of the table to recreate. - **new_column_defs** (str) - Required - A string containing the new column definitions for the table. ### Request Example ```python from fastmigrate import recreate_table from pathlib import Path db_path = Path("data/myapp.db") # Add a new column with a NOT NULL constraint and default value new_column_defs = """ id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL UNIQUE, email TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, is_active INTEGER NOT NULL DEFAULT 1 """ recreate_table(db_path, "users", new_column_defs) ``` ``` -------------------------------- ### Perform Type Checking Source: https://github.com/answerdotai/fastmigrate/blob/main/CLAUDE.md Check the project for type errors using mypy. ```bash mypy . ``` -------------------------------- ### Run a Single Test File Source: https://github.com/answerdotai/fastmigrate/blob/main/CLAUDE.md Execute tests within a specific file using pytest. ```bash pytest tests/path/to/test.py::test_function ``` -------------------------------- ### Recreate Table with New Schema Source: https://context7.com/answerdotai/fastmigrate/llms.txt Modify a table's schema using `recreate_table`, which rebuilds the table with new column definitions and preserves existing data. This function requires the `apswutils` dependency and is suitable for schema changes not supported by `ALTER TABLE`. ```python from fastmigrate import recreate_table from pathlib import Path db_path = Path("data/myapp.db") # Add a new column with a NOT NULL constraint and default value new_column_defs = """ id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL UNIQUE, email TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, is_active INTEGER NOT NULL DEFAULT 1 """ recreate_table(db_path, "users", new_column_defs) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.