Zero-boilerplate schema migrations —straight from your own SQL files.
Project description
SQLStride
Zero-boilerplate schema migrations — straight from your own SQL files.
SQLStride is a simple, yet powerful database migration tool that allows you to manage your database schema changes using plain SQL files organized in a specific directory structure.
Installation
pip install sqlstride
Requirements:
- Python 3.8 or higher
- Dependencies: etl-utilities, pymysql, click, pyodbc, pyyaml, jinja2
Configuration
SQLStride uses a YAML configuration file named sqlstride.yaml (for backward compatibility) in your project root
directory. Here's an example of what
this file should look like:
# Required
sql_dialect: postgres # Options: postgres, mssql, mariadb
host: localhost
# Optional with no defaults
port: 5432
instance: mssql_db_instance
database: my_database
username: db_user
password: db_password
default_schema: public
# Optional with defaults
trusted_auth: false
log_table: sqlstride_log
lock_table: sqlstride_lock
# Jinja template variables
jinja_vars:
environment: production
schema_prefix: app_
# Add any variables you want to use in your SQL templates
Configuration Options
| Option | Description | Default |
|---|---|---|
| sql_dialect | SQL dialect to use | postgres |
| host | Database host (required) | - |
| port | Database port | - |
| database | Database name | - |
| username | Database username | - |
| password | Database password | - |
| instance | Instance name (for MSSQL) | - |
| default_schema | Default schema the log and lock tables will save to | - |
| trusted_auth | Use trusted authentication (for MSSQL) | false |
| log_table | Name of the log table | sqlstride_log |
| lock_table | Name of the lock table | sqlstride_lock |
| jinja_vars | Variables to use in Jinja SQL templates | {} |
Folder Structure and Execution Order
SQLStride executes SQL files in a specific order based on the directory structure. The tool processes directories in the following order:
-
Infrastructure/Runtime
extensions(for modules)roles(for users)
-
Namespaces & Custom Types
schemastypes(domains, enums, composite types)sequences
-
Core Relational Objects
tablesindexes
-
Reference/Seed Data
seed_data(for data)
-
Relational Constraints
constraints
-
Programmable Objects
functionsprocedurestriggers
-
Wrapper/Presentation Objects
viewsmaterialized_viewssynonyms
-
Security & Automation
grants(for permissions)jobs(for tasks)
-
Clean-up Scripts
retire
Within each directory, SQL files are processed in alphabetical order.
SQL File Format
Each SQL file can contain multiple migration steps. A step is identified by a special comment line:
-- step author:step_id
For example:
-- step john:create_users_table
CREATE TABLE users
(
id SERIAL PRIMARY KEY,
username VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
-- step john:add_user_index
CREATE INDEX idx_users_username ON users (username);
Jinja Templating
SQLStride supports Jinja2 templating in SQL files. To use this feature, name your SQL files with a .j2 extension (
e.g., users.sql.j2). You can then use Jinja2 syntax in your SQL files:
-- step john:create_users_table
CREATE TABLE {{ schema_prefix }} users
(
id
SERIAL
PRIMARY
KEY,
username
VARCHAR
(
100
) NOT NULL,
email VARCHAR
(
255
) NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL DEFAULT NOW
(
)
);
{% if environment == 'development' %}
-- step john:add_test_user
INSERT INTO {{ schema_prefix }} users (username, email)
VALUES ('test_user', 'test@example.com');
{% endif %}
You can define variables in the jinja_vars section of your configuration file or pass them via the --jinja-vars
command-line option as a JSON string.
CLI Usage
Sync Command
The main command is sqlstride sync, which synchronizes your database schema with your SQL files.
sqlstride sync [OPTIONS]
Create Repository Structure
The create_repo command creates the recommended directory structure for your project with blank __init__.py files in
each directory. It also interactively prompts you for configuration values to generate a customized configuration file.
sqlstride create_repo [OPTIONS]
Sync Command Options
| Option | Description |
|---|---|
| --project, -p | Path to schema repo containing the configuration file & schema/ (default: current directory) |
| --host | Database host |
| --port | Database port |
| --instance | Instance used for connecting to MSSQL Database |
| --database, -db | Desired database to connect to on host |
| --username, -u | Username used for authenticating with the database |
| --password, -pw | Password used for authenticating with the database |
| --trusted-auth | Use trusted authentication for connecting to MSSQL Database |
| --sql-dialect | SQL dialect to use for connecting to database |
| --default-schema | Schema that the log and lock tables will be created in |
| --log-table | Name of the table to use to keep track of changes |
| --lock-table | Name of the table to use to lock the database during sync |
| --dry-run | Parse & list SQL without executing anything |
| --same-checksums | Checks the current checksums against the existing checksums and raises an error if they are different |
| --jinja-vars | JSON string of variables to use in Jinja templates |
Create Repository Structure Options
| Option | Description |
|---|---|
| --project, -p | Path to create the repository structure (default: current directory) |
Examples
Basic Usage
# Navigate to your project directory containing the configuration file
cd my_project
# Run the sync command
sqlstride sync
# Create the repository structure in the current directory
sqlstride create_repo
Using Command-line Options
# Override configuration options
sqlstride sync --host db.example.com --port 5432 --database my_db --username admin --password secret
# Dry run to see what would be executed without making changes
sqlstride sync --dry-run
# Check if any applied migrations have changed
sqlstride sync --same-checksums
# Create repository structure in a specific directory
sqlstride create_repo --project /path/to/my_new_project
# Use Jinja template variables
sqlstride sync --jinja-vars '{"environment": "development", "schema_prefix": "dev_"}'
Project Structure Example
my_project/
├── sqlstride.yaml # Configuration file (name kept for backward compatibility)
├── schema/
├── tables/
│ ├── users.sql
│ └── posts.sql.j2 # Jinja template
├── constraints/
│ └── foreign_keys.sql
├── functions/
│ └── user_functions.sql
└── views/
└── user_posts_view.sql
How It Works
- SQLStride scans your project directory for SQL files in the specified order.
- It parses each file to extract migration steps.
- For files with a
.j2extension, it processes them with Jinja2 templating using the provided variables. - It checks which steps have already been applied to the database.
- It applies any pending steps in the correct order.
- It records each applied step in the log table with a checksum to ensure idempotency.
This approach allows you to manage your database schema using plain SQL files without having to write boilerplate migration code, with the added flexibility of using templates when needed.
Continuous Deployment
This project uses GitHub Actions to automatically publish new versions to PyPI whenever changes are pushed to the main branch.
Setting up PyPI Deployment
To enable automatic PyPI deployment:
-
Generate a PyPI API token:
- Go to https://pypi.org/manage/account/token/
- Create a new API token with scope limited to the
sqlstrideproject - Copy the token value (you'll only see it once)
-
Add the token to your GitHub repository secrets:
- Go to your GitHub repository
- Navigate to Settings > Secrets and variables > Actions
- Click "New repository secret"
- Name:
PYPI_API_TOKEN - Value: Paste the PyPI token you generated
- Click "Add secret"
-
Now, whenever you push changes to the main branch, the package will be automatically built and published to PyPI with the version specified in
src/sqlstride/__about__.py.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlstride-0.1.9.tar.gz.
File metadata
- Download URL: sqlstride-0.1.9.tar.gz
- Upload date:
- Size: 29.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6fd166583540a7505ce68d31e644cffbafb30c931f2800a2a15ba4a7f146d1a
|
|
| MD5 |
92341fc6363bccf47904ba5c4aece136
|
|
| BLAKE2b-256 |
bab891927978f3fc24b8bfc2ea17585732d3be1984cc1f9aea8977bcc1e676ef
|
Provenance
The following attestation bundles were made for sqlstride-0.1.9.tar.gz:
Publisher:
publish.yml on magicjedi90/sqlstride
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlstride-0.1.9.tar.gz -
Subject digest:
f6fd166583540a7505ce68d31e644cffbafb30c931f2800a2a15ba4a7f146d1a - Sigstore transparency entry: 231225736
- Sigstore integration time:
-
Permalink:
magicjedi90/sqlstride@d9b12803349b0bcb70ea3cccb1ba4882e1839381 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/magicjedi90
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d9b12803349b0bcb70ea3cccb1ba4882e1839381 -
Trigger Event:
push
-
Statement type:
File details
Details for the file sqlstride-0.1.9-py3-none-any.whl.
File metadata
- Download URL: sqlstride-0.1.9-py3-none-any.whl
- Upload date:
- Size: 24.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c6203d8cc30954cf2c329e304e00b260395ab48b9d026fe5ec8a911117b89dc
|
|
| MD5 |
4099708cc0b7ec99d8f3cf243d6ec3c3
|
|
| BLAKE2b-256 |
01dbadf1dc25e8a8ae06a4a318dbc815bc336257acc11ec86f534433b027ee62
|
Provenance
The following attestation bundles were made for sqlstride-0.1.9-py3-none-any.whl:
Publisher:
publish.yml on magicjedi90/sqlstride
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlstride-0.1.9-py3-none-any.whl -
Subject digest:
8c6203d8cc30954cf2c329e304e00b260395ab48b9d026fe5ec8a911117b89dc - Sigstore transparency entry: 231225748
- Sigstore integration time:
-
Permalink:
magicjedi90/sqlstride@d9b12803349b0bcb70ea3cccb1ba4882e1839381 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/magicjedi90
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d9b12803349b0bcb70ea3cccb1ba4882e1839381 -
Trigger Event:
push
-
Statement type: