Skip to main content

PostgreSQL Database Migrations for Flask with Psycopg

Synopsis

flask-migratepg is a simple migrations tool for Flask and Psycopg 3.

  1. Install and setup the Flask extension.
  2. Place SQL migrations under the subdirectory database/migrations/.
  3. Execute migrations with the command flask migrate execute.

Installation

$ pip install flask-migratepg

Setup in application:

from flask import Flask
from flask_migratepg import MigratePg
import os

app = Flask(__name__)
app.config.from_mapping(
    MIGRATIONS_PATH=os.path.abspath('database/migrations'),
    PSYCOPG_CONNINFO="dbname=example host=localhost user=example password=secret"
)
MigratePg(app)

Usage

Create a new migration SQL file:

$ flask migrate --help
$ flask migrate new --help
$ flask migrate new migration_name

Then to run migrations:

$ flask migrate execute --help
$ flask migrate execute

This will run migrations in alphabetical order and track them in a migrations table.

If there is a failure, the transaction will be rolled back.

Migration Files

Migrations are placed under database/migrations/ as an SQL file (that is, with an .sql filename extension.)

The recommended filename format is YYMMDD_NNN_migration_name.sql, for example, 20240219_001_add_table_accounts.sql.

Statements are separated as per standard SQL conventions with a semicolon.

These are just plain-text standard SQL files. Comments (lines beginning with -- ) will be ignored.

Metadata

Release files for flask-migratepg 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-migratepg 1.0.3
File Size Uploaded Provenance Actions
flask_migratepg-1.0.3.tar.gz 4.2 kB Without provenance Details for flask_migratepg-1.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flask-migratepg 1.0.3
File Interpreter ABI Platform Actions
flask_migratepg-1.0.3-py3-none-any.whl Python 3 none any Details for flask_migratepg-1.0.3-py3-none-any.whl

Total release size: 8.8 kB

Release files / flask_migratepg-1.0.3.tar.gz

Download URL flask_migratepg-1.0.3.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
fd5399c2cf822144afe5ab4224cf736a18aa59ec84c92b07c173777b98dd5666
BLAKE2b-256 checksum
How to use checksums
d5e75694681e549ad00077e22067356df9edb1189a54521e04921ba287e6c5f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / flask_migratepg-1.0.3-py3-none-any.whl

Download URL flask_migratepg-1.0.3-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e1b74b1e18d43f10d98419d245aef59c1b3939e300698c6000dddd581513942
BLAKE2b-256 checksum
How to use checksums
5dfa39634a34a443316840f5ba9aa20b27b36b0a3263da3aa8e616ca096d7975
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Security for flask-migratepg 1.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page