### Yoyo Configuration File Example Source: https://ollycope.com/software/yoyo/latest Example `yoyo.ini` configuration file. Specifies migration sources, database connection, verbosity, batch mode, editor, post-create command, and filename prefix. ```ini [DEFAULT] # List of migration source directories. "% (here)s" is expanded to the # full path of the directory containing this ini file. sources = %(here)s/migrations %(here)s/lib/module/migrations # Target database database = postgresql://scott:tiger@localhost/mydb # Verbosity level. Goes from 0 (least verbose) to 3 (most verbose) verbosity = 3 # Disable interactive features batch_mode = on # Editor to use when starting new migrations # "{}" is expanded to the filename of the new migration editor = /usr/local/bin/vim -f {} # An arbitrary command to run after a migration has been created # "{}" is expanded to the filename of the new migration post_create_command = hg add {} # A prefix to use for generated migration filenames prefix = myproject_ ``` -------------------------------- ### SQL Migration Content Example Source: https://ollycope.com/software/yoyo/latest Example content for a SQL migration file, including a comment and SQL commands. ```sql -- Create table foo -- depends: CREATE TABLE foo ( a int ); ``` -------------------------------- ### MySQL Network Connection Example Source: https://ollycope.com/software/yoyo/latest Example configuration for a MySQL network database connection using a connection URL. ```ini database = mysql://scott:tiger@localhost/mydatabase ``` -------------------------------- ### Install yoyo-migrations Source: https://ollycope.com/software/yoyo/latest Install the yoyo-migrations package using pip. ```bash pip install yoyo-migrations ``` -------------------------------- ### Connect to a PostgreSQL Database Source: https://ollycope.com/software/yoyo/latest Example of specifying a PostgreSQL database connection URL for yoyo commands. ```bash yoyo list --database postgresql://scott:tiger@localhost/mydatabase ``` -------------------------------- ### Migration Sources Configuration Source: https://ollycope.com/software/yoyo/latest Configures migration sources in `yoyo.ini` using glob patterns for local directories or `package:` format for migrations within installed Python packages. ```ini [DEFAULT] sources = %(here)s/migrations %(here)s/src/*/migrations ``` ```ini [DEFAULT] sources = package:myapplication:data/migrations ``` -------------------------------- ### Use Custom Backend via Command Line Source: https://ollycope.com/software/yoyo/latest Specify your custom backend using its registered name as the protocol in the `--database` argument when running Yoyo commands. For example, `yoyo apply --database my_backend://...`. ```sh yoyo apply --database my_backend://... ``` -------------------------------- ### Import Step and Transaction in Migration Scripts Source: https://ollycope.com/software/yoyo/latest To prevent linters from throwing errors on undefined names in migration scripts, start your migration files with the import statement `from yoyo import step, transaction`. ```python from yoyo import step, transaction ``` -------------------------------- ### Initialize Yoyo Project Source: https://ollycope.com/software/yoyo/latest Initialize yoyo for your project, specifying the database connection string and migrations directory. ```bash yoyo init --database sqlite:///mydb.sqlite3 migrations ``` -------------------------------- ### Create a Basic Python Migration Source: https://ollycope.com/software/yoyo/latest Define migration steps using Python and the `step` function. Each step includes SQL for applying and rolling back changes. ```python from yoyo import step steps = [ step( "CREATE TABLE foo (id INT, bar VARCHAR(20), PRIMARY KEY (id))", "DROP TABLE foo" ) ] ``` -------------------------------- ### Configure Custom Backend Entry Point Source: https://ollycope.com/software/yoyo/latest Define custom Yoyo backends by subclassing `yoyo.backends.base.DatabaseBackend` and registering them using `entry_points` in your package's `setup.cfg`. This allows you to use your custom backend with the `yoyo` command-line tool. ```ini [options.entry_points] yoyo.backends = mybackend = mypackage:MyBackend ``` -------------------------------- ### Configuration File Inheritance and Includes Source: https://ollycope.com/software/yoyo/latest Demonstrates `%inherit` and `%include` directives in Yoyo configuration files for managing settings across multiple files. `%inherit` processes settings first, while `%include` processes later. ```ini # # file: yoyo-defaults.ini # [DEFAULT] sources = %(here)s/migrations # # file: yoyo.ini # [DEFAULT] ; Inherit settings from yoyo-defaults.ini ; ; Settings in inherited files are processed first and may be overridden by ; settings in this file %inherit = yoyo-defaults.ini ; Include settings from yoyo-local.ini ; ; Included files are processed after this file and may override the settings ; in this file %include = yoyo-local.ini ; Use '?' to avoid raising an error if the file does not exist %inherit = ?yoyo-defaults.ini database = sqlite:///%(here)s/mydb.sqlite ``` -------------------------------- ### Create a New Migration with Message Source: https://ollycope.com/software/yoyo/latest Create a new migration file with a descriptive message. ```bash yoyo new -m "Add column to foo" ``` -------------------------------- ### Create a New SQL Migration Source: https://ollycope.com/software/yoyo/latest Generate a new migration file in SQL format using the `yoyo new --sql` command. ```bash yoyo new --sql ``` -------------------------------- ### Yoyo Command Help Source: https://ollycope.com/software/yoyo/latest View the list of available yoyo commands. ```bash $ yoyo --help ``` -------------------------------- ### Migration Steps as Python Functions Source: https://ollycope.com/software/yoyo/latest Use Python functions for migration steps when SQL is insufficient. Each function receives a database connection object. ```python # # file: migrations/0001_create_foo.py # from yoyo import step def apply_step(conn): cursor = conn.cursor() cursor.execute( # query to perform the migration ) def rollback_step(conn): cursor = conn.cursor() cursor.execute( # query to undo the above ) steps = [ step(apply_step, rollback_step) ] ``` -------------------------------- ### PostgreSQL Connection Strings Source: https://ollycope.com/software/yoyo/latest Set up PostgreSQL connections, supporting unix sockets, custom schemas, and the psycopg 3 driver. ```python database = postgresql://scott:tiger@localhost/mydatabase ``` ```python database = postgresql://scott:tiger@/mydatabase ``` ```python database = postgresql://scott:tiger@/mydatabase?host=/var/run/postgresql&port=5434 ``` ```python database = postgresql+psycopg://scott:tiger@localhost/mydatabase ``` ```python database = postgresql://scott:tiger@/mydatabase?schema=some_schema ``` -------------------------------- ### List Migrations Source: https://ollycope.com/software/yoyo/latest Display the list of available migrations, indicating applied (A) and unapplied (U) statuses. ```bash $ yoyo list ``` -------------------------------- ### Apply Migrations Source: https://ollycope.com/software/yoyo/latest Apply all unapplied migrations to the target database. Batch mode can be enabled to disable prompting. ```bash $ yoyo apply ``` -------------------------------- ### MySQL Connection Strings Source: https://ollycope.com/software/yoyo/latest Configure MySQL connections using different drivers and options like unix sockets and SSL/TLS. ```python database = mysql://scott:tiger@/mydatabase?unix_socket=/tmp/mysql.sock ``` ```python database = mysql+mysqldb://scott:tiger@localhost/mydatabase ``` ```python database = mysql+mysqldb://scott:tiger@localhost/mydatabase?ssl=yes&sslca=/path/to/cert ``` -------------------------------- ### Apply and Rollback Migrations from Python Source: https://ollycope.com/software/yoyo/latest Integrate Yoyo migrations into your Python code by obtaining a backend instance and reading migration files. Use `backend.lock()` to ensure exclusive access, then apply or rollback migrations as needed. ```python from yoyo import read_migrations from yoyo import get_backend backend = get_backend('postgresql://myuser@localhost/mydatabase') migrations = read_migrations('path/to/migrations') with backend.lock(): # Apply any outstanding migrations backend.apply_migrations(backend.to_apply(migrations)) # Rollback all migrations backend.rollback_migrations(backend.to_rollback(migrations)) ``` -------------------------------- ### SQLite Connection Strings Source: https://ollycope.com/software/yoyo/latest Configure SQLite connections, specifying database files or in-memory databases. Use three slashes for relative paths and four for absolute paths. ```python sqlite:///mydb.sqlite ``` ```python sqlite:////home/user/mydb.sqlite ``` -------------------------------- ### Post-Apply Hook Script Source: https://ollycope.com/software/yoyo/latest A special migration file named `post-apply.py` or `post-apply.sql` that runs after every successful migration. Useful for tasks like updating permissions or re-creating views. ```python # This is a placeholder for a post-apply hook script. # Actual content would be Python code to execute after migrations. pass ``` -------------------------------- ### SQL Migration Script Source: https://ollycope.com/software/yoyo/latest Standard SQL script for applying a database migration. Save as `.sql`. ```sql -- -- file: migrations/0001.create-foo.sql -- CREATE TABLE foo (id INT, bar VARCHAR(20), PRIMARY KEY (id)); ``` -------------------------------- ### Grouped Migration Steps with Error Handling Source: https://ollycope.com/software/yoyo/latest Use `group` to nest migration steps and control rollback behavior. `ignore_errors='all'` will roll back only the failed step, allowing execution to continue with the next step. If `ignore_errors` is not set, the entire migration rolls back on error. ```python group([ step("ALTER TABLE employees ADD tax_code TEXT"), step("CREATE INDEX tax_code_idx ON employees (tax_code)") ], ignore_errors='all') step("UPDATE employees SET tax_code='C' WHERE pay_grade < 4") step("UPDATE employees SET tax_code='B' WHERE pay_grade >= 6") step("UPDATE employees SET tax_code='A' WHERE pay_grade >= 8") ``` -------------------------------- ### Python Migration Script Structure Source: https://ollycope.com/software/yoyo/latest Define database migrations using Python scripts with steps for applying and rolling back changes. Supports SQL queries or Python functions. ```python # # file: migrations/0001_create_foo.py # from yoyo import step __depends__ = {"0000.initial-schema"} steps = [ step( "CREATE TABLE foo (id INT, bar VARCHAR(20), PRIMARY KEY (id))", "DROP TABLE foo", ), step( "ALTER TABLE foo ADD COLUMN baz INT NOT NULL" ) ] ``` -------------------------------- ### SQL Migration with Dependencies Source: https://ollycope.com/software/yoyo/latest SQL script specifying dependencies on other migrations using a structured comment. The `depends:` directive lists space-separated migration names. ```sql -- depends: 0000.initial-schema 0001.create-foo ALTER TABLE foo ADD baz INT; ``` -------------------------------- ### SQL Rollback Script Source: https://ollycope.com/software/yoyo/latest SQL script for rolling back a database migration. Save as `.rollback.sql`. ```sql -- -- file: migrations/0001.create-foo.rollback.sql -- DROP TABLE foo; ``` -------------------------------- ### Configure Mercurial for Email Commits Source: https://ollycope.com/software/yoyo/latest Set up your Mercurial configuration (`~/.hgrc`) to use the `patchbomb` extension and configure SMTP settings for sending commits via email. This is part of the contribution workflow for Yoyo. ```ini [extensions] patchbomb = [email] from = Your Name method = smtp [smtp] host = mail.example.org port = 587 tls = smtps username = you@example.org ``` -------------------------------- ### Migration Dependencies Source: https://ollycope.com/software/yoyo/latest Declare dependencies between migration scripts using the `__depends__` attribute to ensure correct execution order. ```python # # file: migrations/0002.modify-foo.py # __depends__ = {'0000.initial-schema', '0001.create-foo'} steps = [ # migration steps ] ``` -------------------------------- ### Update Yoyo Imports for Namespace Package Removal Source: https://ollycope.com/software/yoyo/latest When migrating from older versions of Yoyo, update your import statements to reflect the removal of the yoyo.migrate namespace package. This ensures compatibility with newer versions. ```python from yoyo.migrate import read_migrations from yoyo.migrate.connections import connect ``` ```python from yoyo import read_migrations from yoyo.connections import connect ``` -------------------------------- ### Set Local Mercurial Email Recipient Source: https://ollycope.com/software/yoyo/latest Configure the local repository's Mercurial settings (`.hg/hgrc`) to specify the recipient address for sending commits via email. This ensures your commits are sent to the correct mailing list for review. ```ini [email] to = <~olly/yoyo@lists.sr.ht> ``` -------------------------------- ### Disable Transactions in SQL Migration Source: https://ollycope.com/software/yoyo/latest Use the `-- transactional: false` directive at the beginning of an SQL migration file to disable transaction handling for that migration. This is necessary for certain SQL statements that are not permitted within transaction blocks. ```sql -- transactional: false CREATE DATABASE mydb ``` -------------------------------- ### Disable Transactions in Python Migration Source: https://ollycope.com/software/yoyo/latest Set `__transactional__ = False` within a Python migration script to disable transaction handling for all steps in that migration. This is useful for statements that cannot be run inside a transaction block, like `CREATE DATABASE`. ```python __transactional__ = False step("CREATE DATABASE mydb", "DROP DATABASE mydb") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.