Skip to main content

cozo-migrate

A simple utility for migrations for cozo db

Features

  • It uses typer and pycozo as its only dependencies.
  • Loosely modeled after hasura-migrate which is one of the simplest migration tools I have used.
  • Auto-rollback if something goes wrong during the migrations
  • Has a programmatic API for using inside tests etc.

CLI Usage

Example

$ # Install cozo-migrate
$ pip install cozo-migrate

$ # Let's start a cozo server in the background
$ cozo server -e rocksdb &

$ # Let's create a migration
$ cozo-migrate -e http -d ./migrations create demo
  Writing 'migrations/migrate_1704803704_demo.py' Confirm? [y/N]: y
  ✔ Created migration at: migrations/migrate_1704803704_demo.py

$ cat migrations/migrate_1704803704_demo.py
  #/usr/bin/env python3

  MIGRATION_ID = "demo"
  CREATED_AT = 1704803704.762879

  def up(client):
      pass

  def down(client):
      pass

$ # Edit the migration to add schema change queries and then apply!
$ cozo-migrate -e http -d ./migrations apply -a
  • Migrate path: [NONE] → demo
  Are you sure you want to apply these migrations? [y/N]: y
  ✔ Database migrated.

Core options

 Usage: cozo-migrate [OPTIONS] COMMAND [ARGS]...

 A simple migration tool for Cozo databases.

╭─ Options ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --migrations-dir      -d      PATH                       Directory to use for looking up migration files. [env var: COZO_MIGRATIONS_DIR] [default: migrations] │
│ --engine              -e      [sqlite|rocksdb|http|mem]  Engine to use [env var: COZO_ENGINE] [default: EngineType.sqlite]                                     │
│ --path                -f      PATH                       Database file (not applicable for mem or http engines) [env var: COZO_PATH] [default: None]           │
│ --host                -h      TEXT                       Host to connect to (http engine only) [env var: COZO_HOST] [default: http://127.0.0.1:9070]           │
│ --auth                        TEXT                       Auth header (http engine only) [default: None]                                                        │
│ --verbose             -v                                                                                                                                       │
│ --install-completion                                     Install completion for the current shell.                                                             │
│ --show-completion                                        Show completion for the current shell, to copy it or customize the installation.                      │
│ --help                                                   Show this message and exit.                                                                           │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ apply        Apply migrations to the database. You can specify the number of steps to apply and the direction.            │
│ create       Create a new migration file in the migrations directory. Format: {migration_dir}/migrate_<timestamp>_<id>.py │
│ history      Display the migration history in the database as a pretty table.                                             │
│ init         Initialize the database with the migration manager table.                                                    │
│ status       Display the current migration status.                                                                        │
│ version      Display the current version.                                                                                 │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

Init

 Usage: cozo-migrate init [OPTIONS]

 Initialize the database with the migration manager table.

╭─ Options ───────────────────────────────────╮
│ --help          Show this message and exit. │
╰─────────────────────────────────────────────╯

Create

 Usage: cozo-migrate create [OPTIONS] ID

 Create a new migration file in the migrations directory. Format: {migration_dir}/migrate_<timestamp>_<id>.py

╭─ Arguments ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ *    id      TEXT  A short, descriptive, alphanumeric id for the migration. Only letters, numbers, and underscores are allowed. [required]│
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --yes   -y                                                                                                                                │
│ --help            Show this message and exit.                                                                                             │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

Apply

 Usage: cozo-migrate apply [OPTIONS] [STEPS]

 Apply migrations to the database. You can specify the number of steps to apply and the direction.

╭─ Arguments ────────────────────────╮
│   steps      [STEPS]  [default: 1] │
╰────────────────────────────────────╯
╭─ Options ──────────────────────────────────────╮
│ --down                                         │
│ --all   -a                                     │
│ --yes   -y                                     │
│ --help            Show this message and exit.  │
╰────────────────────────────────────────────────╯

Show

 Usage: cozo-migrate show

 Show the current schema in the database.

╭─ Options ────────────────────────────────────╮
│ --help          Show this message and exit.  │
╰──────────────────────────────────────────────╯

Status

 Usage: cozo-migrate status [OPTIONS]

 Display the current migration status.

╭─ Options ────────────────────────────────────╮
│ --help          Show this message and exit.  │
╰──────────────────────────────────────────────╯

History

 Usage: cozo-migrate history [OPTIONS]

 Display the migration history in the database as a pretty table.

╭─ Options ────────────────────────────────────╮
│ --help          Show this message and exit.  │
╰──────────────────────────────────────────────╯

API Usage

from cozo_migrate.api import init, apply
from pycozo import Client

migrations_dir: str = "./migrations"

# Create a new client for testing
# and initialize the schema.
client = Client()

# Initialize the migration schema
init(client)

# Apply migrations from the migration directory
apply(client, migrations_dir=migrations_dir, all_=True)

Metadata

Release files for cozo-migrate 0.2.4

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

Source distribution (sdist)

Source distribution for cozo-migrate 0.2.4
File Size Uploaded
cozo_migrate-0.2.4.tar.gz 14.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cozo-migrate 0.2.4
File Interpreter ABI Platform
cozo_migrate-0.2.4-py3-none-any.whl Python 3 none any Details

Total release size: 35.8 kB

Release files / cozo_migrate-0.2.4.tar.gz

Download URL cozo_migrate-0.2.4.tar.gz
Size 14.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ccb852f00bb25ff7c431dc8fa8a81e8f9f10198ad76aa34d1239d67f1613b899
BLAKE2b-256 checksum
How to use checksums
b13af66a88c50c5dd7bb7cb98d84f4d3e45bb2cfe1dba524f775f88b065b563b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.11.7 Linux/5.15.0-91-generic

Release files / cozo_migrate-0.2.4-py3-none-any.whl

Download URL cozo_migrate-0.2.4-py3-none-any.whl
Size 21.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
518151d65c81968e42402470418f42c8580e972f0b949df6c5c499cc2b098c1b
BLAKE2b-256 checksum
How to use checksums
26ce2dc5dc2be88ab79ed24b1412b7745c690e7f684e1665eb4feeb6300056bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.11.7 Linux/5.15.0-91-generic

Release history Release notifications | RSS feed

This release

0.2.4 This release

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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