Simple CLI migration tool for SQL databases
Project description
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
You can install SQLift using pip:
pip install SQLift
By default, SQLift uses SQLite as the database. If you want to use PostgreSQL, you need to install the appropriate database driver. You can do this using the following command:
pip install SQLift[postgres]
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
-
First, clone the repository:
git clone git@github.com:SpaceShaman/SQLift.git
-
Install poetry if you don't have, here you can find the instructions
-
Create a virtual environment and install the dependencies:
cd SQLift poetry install --no-root
-
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
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 sqlift-0.2.3.tar.gz.
File metadata
- Download URL: sqlift-0.2.3.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.11 Linux/6.8.0-1031-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75d21cb32e071a196bd4ae696bfd8d6d0d961f7e9ca271158d9578a7cd51fd32
|
|
| MD5 |
05628e96db269f7e0528ed3133ae20c9
|
|
| BLAKE2b-256 |
7f3d80c1006922f679e0ad94ac57151cc25c4fce9f731b9cd61d003d7628e5cb
|
File details
Details for the file sqlift-0.2.3-py3-none-any.whl.
File metadata
- Download URL: sqlift-0.2.3-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.11 Linux/6.8.0-1031-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52bc0d7abb82091a20b4c1611ea769105a6876d26ef8758e31360e8571dfeb09
|
|
| MD5 |
c8f73d733ec41be9ce7a99aaade9da5b
|
|
| BLAKE2b-256 |
76e111352ed6b51d25804b6be135404253bd4b6ae6c8c7f5f1ad31e246ea3968
|