Skip to main content

Simple CLI migration tool for SQL databases

Project description

SQLift

GitHub License Tests Codecov PyPI - Python Version PyPI - Version Code style: black Linting: Ruff SQLite PostgreSQL Pytest

SQLift is a simple CLI migration tool for SQL databases. It is designed to be easy to use and now supports SQLite and PostgreSQL databases.

Installation

pip install SQLift

Usage

First you need to create a 'migrations' directory where you will store your migration files. Migrations are simple SQL files that contain the SQL commands to be executed for up and down migrations like below. This file needs two sections, one for the up migration and one for the down migration, separated by --DOWN.

Example migration file

migrations/001_create_table.sql 
--UP
CREATE TABLE IF NOT EXISTS test (
    id INTEGER PRIMARY KEY
);

--DOWN
DROP TABLE IF EXISTS test;

You can find more examples of migration files in the migrations directory of this repository.

After you have created your migration files, you can run the following command to apply the migrations to your database. (It is recommended to start the migration files with a number to keep them in order)

sqlift up

This will apply all the migrations that have not been applied yet. If you want migrate to a specific version, you can pass the version as an argument.

sqlift up 001_create_table

To rollback all migrations, you can run the following command.

sqlift down

To rollback to a specific version, you can pass the version

sqlift down 001_create_table

You can also select specific path for migrations directory

sqlift up --path /path/to/migrations

Configuration

SQLift uses environment variables to configure the database connection. You can set the following environment variables to configure the database connection.

DB_URL=postgresql://user:password@localhost:5432/database # PostgreSQL
DB_URL=sqlite:///path/to/database.db                      # SQLite

If you don't set the DB_URL environment variable, SQLift will default to using SQLite with a database file named db.sqlite in the current directory.

Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request. I would like to keep the library to be safe as possible, so i would appreciate if you cover any new feature with tests to maintain 100% coverage.

Install in a development environment

  1. First, clone the repository:

    git clone git@github.com:SpaceShaman/SQLift.git
    
  2. Install poetry if you don't have, here you can find the instructions

  3. Create a virtual environment and install the dependencies:

    cd SQLift
    poetry install --no-root
    
  4. Activate the virtual environment:

    poetry shell
    

Run tests

First, you need to run databases and you can do it simply with docker-compose:

docker-compose up -d

Then you can run the tests with pytest:

pytest

You can also run the tests with coverage:

pytest --cov=sqlift

Database clients

If you want to add support for a new database, you need to create a new client class that is consistent with the Client protocol class from sqlift.clients and implement the execute method. Look at the SQLiteClient and PostgreSQLClient classes for reference.

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

sqlift-0.2.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

sqlift-0.2.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file sqlift-0.2.1.tar.gz.

File metadata

  • Download URL: sqlift-0.2.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.8 Linux/6.5.0-1025-azure

File hashes

Hashes for sqlift-0.2.1.tar.gz
Algorithm Hash digest
SHA256 bf9c40a9619214ac00d2bbf5c8db9f0e9dc71159ef42cef3b030dcdc2b57e7c5
MD5 9479d6f1801b873a985fbb47b38ba19c
BLAKE2b-256 771122ff9a9e56442c428422013a45e504e89777a4a037b4f62a3a826f8c4769

See more details on using hashes here.

File details

Details for the file sqlift-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: sqlift-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.8 Linux/6.5.0-1025-azure

File hashes

Hashes for sqlift-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7713eb4270f3a6db83030aa6498079eebf3146d4a5126bc873294207598c3af8
MD5 e7e5c719075c5833da04802436ce1bb9
BLAKE2b-256 5dcd74cd0b7e4ef7f849870553a231ae1f6a505e305228b87095e645cb55d233

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