### Install Flask-Migrate Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Install Flask-Migrate using pip. This is the initial setup step. ```bash pip install Flask-Migrate ``` -------------------------------- ### Initialize Flask-Migrate with init_app Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Initialize the Flask-Migrate extension using the application-factory pattern with the `init_app` method. This approach is useful for more complex application setups. ```python from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate db = SQLAlchemy() migrate = Migrate() def create_app(): """Application-factory pattern""" ... ... db.init_app(app) migrate.init_app(app, db) ... ... return app ``` -------------------------------- ### Display Flask-Migrate Help Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/README.md Shows all available commands and options for the Flask-Migrate extension. ```bash $ flask db --help ``` -------------------------------- ### Initialize Flask Application with Flask-Migrate Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/README.md Sets up a basic Flask application with SQLAlchemy and Flask-Migrate. Ensure SQLALCHEMY_DATABASE_URI is configured. ```python from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db' db = SQLAlchemy(app) migrate = Migrate(app, db) class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128)) ``` -------------------------------- ### Flask DB Init Command Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Initializes migration support for the application. Use `--multidb` for multiple databases, `--template` to specify a repository template, and `--package` to add `__init__.py` files. ```bash flask db init [--multidb] [--template TEMPLATE] [--package] ``` -------------------------------- ### init Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Initializes migration support for the application. ```APIDOC ## init ### Description Initializes migration support for the application. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature `init(directory='migrations', multidb=False)` ### Parameters - **directory** (str) - Optional - The directory to store migration scripts. - **multidb** (bool) - Optional - Whether to support multiple databases. ``` -------------------------------- ### Initialize Flask-Migrate with Standard CLI Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Initialize the Flask-Migrate extension with the standard Flask command-line interface. This is the most common way to set up migrations. ```python from flask_migrate import Migrate migrate = Migrate(app, db) ``` -------------------------------- ### Initialize Multi-Database Migration Repository Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Use the --multidb argument when initializing your migration repository to enable tracking for multiple databases defined in SQLALCHEMY_BINDS. ```bash $ flask db init --multidb ``` -------------------------------- ### Flask DB History Command Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Shows the list of database migrations. If a revision range is not specified, the entire migration history is displayed. Use `--verbose` for more details. ```bash flask db history [--rev-range REV_RANGE] [--verbose] ``` -------------------------------- ### Flask DB Show Command Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Displays the details of a specific database revision identified by a symbol. ```bash flask db show ``` -------------------------------- ### Initialize Database Migrations with Flask-Migrate Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/README.md Initializes the migrations environment for a Flask application. This command creates a 'migrations' folder and must be run before generating migration scripts. Ensure FLASK_APP environment variable is set. ```bash $ flask db init ``` -------------------------------- ### Flask DB Branches Command Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Shows the current branch points in the migration history. Use `--verbose` for more detailed output. ```bash flask db branches [--verbose] ``` -------------------------------- ### Flask DB Heads Command Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Displays the current available heads (latest revisions) in the migration script directory. Use `--verbose` for more details and `--resolve-dependencies` to resolve dependency issues. ```bash flask db heads [--verbose] [--resolve-dependencies] ``` -------------------------------- ### Flask DB Current Command Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Displays the current revision of the database. Use the `--verbose` flag for more detailed output. ```bash flask db current [--verbose] ``` -------------------------------- ### Apply Database Migrations with Flask-Migrate Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/README.md Applies the pending database migrations to the database. This command should be run after generating and reviewing migration scripts. To sync the database on another system, refresh the 'migrations' folder from source control and run this command. ```bash $ flask db upgrade ``` -------------------------------- ### Manually Configure Alembic Options Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Pass Alembic configuration options as keyword arguments to the Migrate constructor to override default behaviors or enable specific features. ```python migrate = Migrate(app, db, render_as_batch=False) ``` -------------------------------- ### Generate Database Migration Script Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Automatically generate a migration script based on changes to your models. Review and edit the script before committing it to version control. ```bash $ flask db migrate -m "Initial migration." ``` -------------------------------- ### migrate Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Creates an automatic revision script. ```APIDOC ## migrate ### Description Creates an automatic revision script. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature `migrate(directory='migrations', message=None, sql=False, head='head', splice=False, branch_label=None, version_path=None, rev_id=None)` ### Parameters - **directory** (str) - Optional - The directory to store migration scripts. - **message** (str) - Optional - A message describing the revision. - **sql** (bool) - Optional - Whether to generate a SQL script. - **head** (str) - Optional - The head revision to base the new revision on. - **splice** (bool) - Optional - Whether to splice revisions. - **branch_label** (str) - Optional - A label for the branch. - **version_path** (str) - Optional - The path to the version file. - **rev_id** (str) - Optional - The revision ID. ``` -------------------------------- ### Flask DB Upgrade Command Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Upgrades the database to a specified revision. If no revision is given, it defaults to 'head'. The `--sql` option prints SQL statements instead of executing them. ```bash flask db upgrade [--sql] [--tag TAG] ``` -------------------------------- ### upgrade Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Upgrades the database. ```APIDOC ## upgrade ### Description Upgrades the database. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature `upgrade(directory='migrations', revision='head', sql=False, tag=None)` ### Parameters - **directory** (str) - Optional - The directory to store migration scripts. - **revision** (str) - Optional - The revision to upgrade to. - **sql** (bool) - Optional - Whether to generate a SQL script. - **tag** (str) - Optional - A tag for the upgrade. ``` -------------------------------- ### Flask DB Revision Command Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Creates an empty migration script that needs manual editing. An optional message can be included. See Alembic's documentation for writing migration scripts. ```bash flask db revision [--message MESSAGE] [--autogenerate] [--sql] [--head HEAD] [--splice] [--branch-label BRANCH_LABEL] [--version-path VERSION_PATH] [--rev-id REV_ID] ``` -------------------------------- ### show Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Show the revision denoted by the given symbol. ```APIDOC ## show ### Description Show the revision denoted by the given symbol. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature `show(directory='migrations', revision='head')` ### Parameters - **directory** (str) - Optional - The directory to store migration scripts. - **revision** (str) - Optional - The revision to show. ``` -------------------------------- ### history Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Shows the list of migrations. If a range isn’t given then the entire history is shown. ```APIDOC ## history ### Description Shows the list of migrations. If a range isn’t given then the entire history is shown. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature `history(directory='migrations', rev_range=None, verbose=False)` ### Parameters - **directory** (str) - Optional - The directory to store migration scripts. - **rev_range** (str) - Optional - The revision range to show. - **verbose** (bool) - Optional - Whether to show verbose output. ``` -------------------------------- ### revision Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Creates an empty revision script. ```APIDOC ## revision ### Description Creates an empty revision script. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature `revision(directory='migrations', message=None, autogenerate=False, sql=False, head='head', splice=False, branch_label=None, version_path=None, rev_id=None)` ### Parameters - **directory** (str) - Optional - The directory to store migration scripts. - **message** (str) - Optional - A message describing the revision. - **autogenerate** (bool) - Optional - Whether to automatically generate the revision script. - **sql** (bool) - Optional - Whether to generate a SQL script. - **head** (str) - Optional - The head revision to base the new revision on. - **splice** (bool) - Optional - Whether to splice revisions. - **branch_label** (str) - Optional - A label for the branch. - **version_path** (str) - Optional - The path to the version file. - **rev_id** (str) - Optional - The revision ID. ``` -------------------------------- ### Flask DB Stamp Command Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Sets the database revision to a specified revision without performing any migration operations. The `--sql` option prints SQL statements instead of executing them. ```bash flask db stamp [--sql] [--tag TAG] ``` -------------------------------- ### Flask DB Downgrade Command Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Downgrades the database to a specified revision. If no revision is given, it defaults to '-1'. The `--sql` option prints SQL statements instead of executing them. ```bash flask db downgrade [--sql] [--tag TAG] ``` -------------------------------- ### downgrade Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Downgrades the database. ```APIDOC ## downgrade ### Description Downgrades the database. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature `downgrade(directory='migrations', revision='-1', sql=False, tag=None)` ### Parameters - **directory** (str) - Optional - The directory to store migration scripts. - **revision** (str) - Optional - The revision to downgrade to. - **sql** (bool) - Optional - Whether to generate a SQL script. - **tag** (str) - Optional - A tag for the downgrade. ``` -------------------------------- ### heads Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Show current available heads in the script directory. ```APIDOC ## heads ### Description Show current available heads in the script directory. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature `heads(directory='migrations', verbose=False, resolve_dependencies=False)` ### Parameters - **directory** (str) - Optional - The directory to store migration scripts. - **verbose** (bool) - Optional - Whether to show verbose output. - **resolve_dependencies** (bool) - Optional - Whether to resolve dependencies. ``` -------------------------------- ### current Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Shows the current revision of the database. ```APIDOC ## current ### Description Shows the current revision of the database. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature `current(directory='migrations', verbose=False, head_only=False)` ### Parameters - **directory** (str) - Optional - The directory to store migration scripts. - **verbose** (bool) - Optional - Whether to show verbose output. - **head_only** (bool) - Optional - Whether to show only the head revision. ``` -------------------------------- ### Generate Database Migration Script with Flask-Migrate Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/README.md Generates a new migration script based on changes to your SQLAlchemy models. The generated script needs manual review and editing, especially for indexes, before being added to version control. ```bash $ flask db migrate ``` -------------------------------- ### Flask DB Check Command Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Checks if a `migrate` command would generate any changes. Exits with a non-zero status code if pending changes are detected. ```bash flask db check ``` -------------------------------- ### Define Alembic Configuration Callback Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Use the @migrate.configure decorator to define a function that modifies the Alembic configuration object before it is applied. Multiple callbacks can be defined. ```python @migrate.configure def configure_alembic(config): # modify config object return config ``` -------------------------------- ### branches Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Show current branch points. ```APIDOC ## branches ### Description Show current branch points. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature `branches(directory='migrations', verbose=False)` ### Parameters - **directory** (str) - Optional - The directory to store migration scripts. - **verbose** (bool) - Optional - Whether to show verbose output. ``` -------------------------------- ### Flask DB Migrate Command Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Automatically generates a migration script based on detected changes. The script should be reviewed and edited as not all changes are detected automatically. This command only creates the script, it does not modify the database. ```bash flask db migrate [--message MESSAGE] [--sql] [--head HEAD] [--splice] [--branch-label BRANCH_LABEL] [--version-path VERSION_PATH] [--rev-id REV_ID] ``` -------------------------------- ### merge Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Merge two revisions together. Creates a new migration file. ```APIDOC ## merge ### Description Merge two revisions together. Creates a new migration file. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature `merge(directory='migrations', revisions='', message=None, branch_label=None, rev_id=None)` ### Parameters - **directory** (str) - Optional - The directory to store migration scripts. - **revisions** (str) - Required - The revisions to merge. - **message** (str) - Optional - A message describing the merge. - **branch_label** (str) - Optional - A label for the branch. - **rev_id** (str) - Optional - The revision ID. ``` -------------------------------- ### Flask DB Edit Command Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Edits a specified revision script using the default text editor defined by the $EDITOR environment variable. ```bash flask db edit ``` -------------------------------- ### edit Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Edit revision script(s) using $EDITOR. ```APIDOC ## edit ### Description Edit revision script(s) using $EDITOR. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature `edit(directory='migrations', revision='head')` ### Parameters - **directory** (str) - Optional - The directory to store migration scripts. - **revision** (str) - Optional - The revision to edit. ``` -------------------------------- ### Flask DB Merge Command Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Merges two or more specified revisions together, creating a new revision file. An optional message and branch label can be provided. ```bash flask db merge [--message MESSAGE] [--branch-label BRANCH_LABEL] [--rev-id REV_ID] ``` -------------------------------- ### stamp Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Sets the revision in the database to the one given as an argument, without performing any migrations. ```APIDOC ## stamp ### Description Sets the revision in the database to the one given as an argument, without performing any migrations. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature `stamp(directory='migrations', revision='head', sql=False, tag=None)` ### Parameters - **directory** (str) - Optional - The directory to store migration scripts. - **revision** (str) - Optional - The revision to stamp. - **sql** (bool) - Optional - Whether to generate a SQL script. - **tag** (str) - Optional - A tag for the stamp. ``` -------------------------------- ### Change Flask-Migrate Command Group Name Source: https://github.com/miguelgrinberg/flask-migrate/blob/main/docs/index.md Customize the command group name for Flask-Migrate by passing the 'command' argument to the Migrate constructor. ```python migrate = Migrate(app, db, command='migrate') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.