Skip to main content

Simple migration command for Flask and Psycopg 3.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_migratepg-1.0.3.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_migratepg-1.0.3-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file flask_migratepg-1.0.3.tar.gz.

File metadata

  • Download URL: flask_migratepg-1.0.3.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for flask_migratepg-1.0.3.tar.gz
Algorithm Hash digest
SHA256 fd5399c2cf822144afe5ab4224cf736a18aa59ec84c92b07c173777b98dd5666
MD5 a19e466ebc1ae8c3e7ea522d0cb56b95
BLAKE2b-256 d5e75694681e549ad00077e22067356df9edb1189a54521e04921ba287e6c5f1

See more details on using hashes here.

File details

Details for the file flask_migratepg-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_migratepg-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5e1b74b1e18d43f10d98419d245aef59c1b3939e300698c6000dddd581513942
MD5 2b0d7318220086c5710b73842bfe0ba4
BLAKE2b-256 5dfa39634a34a443316840f5ba9aa20b27b36b0a3263da3aa8e616ca096d7975

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page