Skip to main content

Python SDK for Flowgrate — Laravel-style database migrations with a fluent API

Project description

flowgrate/python

Python SDK for Flowgrate — Laravel-style database migrations with a fluent API.

How it works

Define migrations in Python using the fluent Blueprint API. The SDK serializes them to JSON and pipes to the Flowgrate CLI, which compiles and executes the SQL.

Requirements

  • Python 3.10+
  • Flowgrate CLI — download the binary for your platform and put it on your PATH
# Linux (amd64)
curl -L https://github.com/flowgrate/core/releases/latest/download/flowgrate-linux-amd64 -o flowgrate
chmod +x flowgrate
sudo mv flowgrate /usr/local/bin/

# macOS (Apple Silicon)
curl -L https://github.com/flowgrate/core/releases/latest/download/flowgrate-darwin-arm64 -o flowgrate
chmod +x flowgrate
sudo mv flowgrate /usr/local/bin/

# macOS (Intel)
curl -L https://github.com/flowgrate/core/releases/latest/download/flowgrate-darwin-amd64 -o flowgrate
chmod +x flowgrate
sudo mv flowgrate /usr/local/bin/

# Or build from source
go install github.com/flowgrate/core@latest

Setup

1. Install the SDK:

pip install flowgrate

2. Create Program.py (entry point for the CLI to invoke):

from flowgrate import FlowgrateRunner
import os

FlowgrateRunner.run(os.path.dirname(__file__))

3. Create flowgrate.yml next to your migrations:

Generate it with the CLI (recommended):

flowgrate init --db=postgres://user:pass@localhost/mydb

Or create manually:

database:
  url: postgres://user:pass@localhost/mydb

migrations:
  project: ./migrations
  sdk: python
  run: python Program.py

4. Generate and run migrations:

flowgrate make CreateUsersTable
flowgrate up

Migration anatomy

from flowgrate import Migration, Schema


class CreateUsersTable(Migration):
    def up(self):
        with Schema.create("users") as t:
            t.id()
            t.string("name")
            t.string("email", 100)
            t.timestamps()

    def down(self):
        Schema.drop_if_exists("users")

Migration files must follow the naming convention: YYYYMMDD_HHMMSS_MigrationName.py

Blueprint API reference

Create / drop table

Schema.create("users")          # CREATE TABLE
Schema.table("users")           # ALTER TABLE
Schema.drop("users")            # DROP TABLE
Schema.drop_if_exists("users")  # DROP TABLE IF EXISTS

Column types

t.id()                          # BIGSERIAL PRIMARY KEY
t.small_integer("level")        # SMALLINT
t.integer("views")              # INTEGER
t.big_integer("score")          # BIGINT
t.decimal("price", 10, 2)       # NUMERIC(10, 2)
t.float("rating")               # REAL
t.double("latitude")            # DOUBLE PRECISION
t.boolean("active")             # BOOLEAN
t.string("name")                # VARCHAR(255)
t.string("code", 10)            # VARCHAR(10)
t.text("bio")                   # TEXT
t.uuid("public_id")             # UUID
t.json("settings")              # JSON
t.jsonb("metadata")             # JSONB
t.binary("avatar")              # BYTEA
t.date("birthday")              # DATE
t.time("opens_at")              # TIME
t.timestamp("verified_at")      # TIMESTAMP

Column modifiers (chainable)

.nullable()                     # NULL
.default(value)                 # DEFAULT value
.default_expression("NOW()")    # DEFAULT NOW()  — raw SQL
.generated_uuid()               # DEFAULT gen_random_uuid()
.comment("description")
.unique()                       # single-column unique index

Helpers

t.timestamps()                  # created_at + updated_at TIMESTAMP DEFAULT NOW()
t.soft_deletes()                # deleted_at TIMESTAMP NULL
t.remember_token()              # remember_token VARCHAR(100) NULL
t.polymorphic("commentable")    # commentable_id BIGINT + commentable_type VARCHAR(255) + index
t.nullable_polymorphic("taggable")

Foreign keys

t.foreign_id("role_id") \
 .constrained("roles") \
 .on_delete("cascade") \
 .on_update("cascade")

Indexes

t.unique("email", "tenant_id", name="uq_users_email_tenant")
t.index("created_at")
t.index("email", "name", name="idx_users_search")

ALTER TABLE

with Schema.table("users") as t:
    t.add_column("phone").string(20).nullable()
    t.change_column("name").string(500)
    t.drop_column("avatar")

Running in Docker

docker compose exec sdk python Program.py | flowgrate up

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

flowgrate-0.1.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

flowgrate-0.1.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file flowgrate-0.1.0.tar.gz.

File metadata

  • Download URL: flowgrate-0.1.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for flowgrate-0.1.0.tar.gz
Algorithm Hash digest
SHA256 28199bb89e5ec1475a2f31831f2214bfeb4847c347a75de3cca144beb85afbc2
MD5 a406dd65b6c2363e069416d97ab333af
BLAKE2b-256 cdf5ec0a3b726abe4ecad353d12f45e8fe99954293d00790f97a7ce4a8072748

See more details on using hashes here.

File details

Details for the file flowgrate-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: flowgrate-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for flowgrate-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 25f94ee51d174e9d58cdda618e50c7c5c5718b3e126e0d1c5a8935e51fb8ca90
MD5 e69bec68bf34d7139af1c862900a92c4
BLAKE2b-256 ae1d6978690d27ae3807be5566bc99cf6f016cb50a814ba4be9bd85bf08e4c33

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