Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bezborodow/flask-migratepg
Simple SQL migration command for Flask and Psycopg 3.
https://github.com/bezborodow/flask-migratepg
database flask migrations postgresql psycopg sql-migration
Last synced: 17 days ago
JSON representation
Simple SQL migration command for Flask and Psycopg 3.
- Host: GitHub
- URL: https://github.com/bezborodow/flask-migratepg
- Owner: bezborodow
- License: bsd-3-clause
- Created: 2023-06-05T15:05:23.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-03-04T06:22:45.000Z (8 months ago)
- Last Synced: 2024-10-01T02:49:48.619Z (about 1 month ago)
- Topics: database, flask, migrations, postgresql, psycopg, sql-migration
- Language: Python
- Homepage: https://pypi.org/project/flask-migratepg/
- Size: 32.2 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PostgreSQL Database Migrations for Flask with Psycopg
## Synopsis
`flask-migratepg` is a simple migrations tool for Flask and [Psycopg 3](https://www.psycopg.org/psycopg3/).
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:
````python
from flask import Flask
from flask_migratepg import MigratePg
import osapp = 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.