A framework for managing SQLite database migrations, inspired by Django.
Project description
sqlite-shift
Description
sqlite-shift is a framework for managing SQLite database migrations, inspired by Django's methodology.
Table of Contents
Installation
You can install sqlite-shift via pip:
pip install sqlite-shift
Features
- Automatic Dependency Tracking: Automatically track and manage migration dependencies based on the order of execution.
- Configuration Options: Customize migration paths, database connections, and other settings via a configuration file.
- CLI Support: Command-line interface for convenient migration management.
- Structured: A systematic and developer friendly way to write and maintain migrations.
Basic Usage
1. Create a configuration file
SQLite-Shift utilizes a configuration file to manage settings such as database connection details and migration paths. To create a configuration file:
-
Create a new
.iniconfiguration file in your project directory, below is a sample configuration.[test_db] # Database name db_path = /path/to/test_db.sql # Database path migrations_path = /path/to/schema_migrations # The directory path where migration files are to be stored for this database.Refer to Configuration Structure on how to implement a migration file.
-
Generate a Migration Use the create_migration command from the SQLite-Shift CLI
sqlite-shift create test_db --migration_name add_users_tableRefer to Migration Structure on how to implement a migration file.
-
Apply a Migration
sqlite-shift apply test_db -
Revert a Migration
sqlite-shift revert test_db
Configuration Structure
The configuration file in sqlite-shift allows developers to configure one or more databases and their details. Below is the structure of the configuration file and its available options:
[<database name>]
db_path = <path to the sqlite database>
migrations_path = <path to the migrations folder that holds all the migration files >
Note:
- One or more databases can be configured and the same database name should be used while applying / reverting migrations.
- Migrations folder should be named as
schema_migrations.
Examples
-
Configuration with one database
[orders_db] db_path = /src/db/orders.sql migrations_path = /src/schema_migrations -
Configuration with two database
[orders_db] db_path = /src/db/orders.sql migrations_path = /src/orders/schema_migrations [users_db] db_path = /src/db/users.sql migrations_path = /src/users/schema_migrations
Migration Structure
- Migrations folder should always be named
schema_migrations. - To apply a migration, sqlite-shift uses the
upgrademethod defined in each migration file. Whenapply_all_migrationsis called, S sqlite-shift iterates through all migration files in the specified directory and applies them in the correct order based on their dependencies. - To revert a migration, sqlite-shift uses the
downgrademethod defined in each migration file. Whenrevert_last_migrationis called, sqlite-shift reverts the last applied migration by executing the downgrade method of the corresponding migration file. - All migrations will be executed within a transaction
from sqlite_shift.core.base_migration import BaseMigration
class Migration(BaseMigration): # All migration classes should extend "BaseMigration" class
def upgrade(self, conn): # class method that will be used while applying a migration
# Add 'users' table to the database schema
conn.execute("""
CREATE TABLE users (
id INTEGER PRIMARY KEY,
username TEXT NOT NULL,
email TEXT NOT NULL
);
""")
def downgrade(self, conn): # class method that will be used while revertinf a migration
# Remove 'users' table from the database schema
conn.execute("DROP TABLE users;")
Migration Manager
The Migration Manager class in sqlite-shift provides a set of methods to manage database migrations.
Initialisation
from sqlite_shift import MigrationManager
# Initialize Migration Manager
manager = MigrationManager(db_name="example_db", config_file_path="migrations.ini")
Accepts the following parameters on initialisation
- db_name: The database to be used
- config_file_path: The path for the configuration file. By default, it tries to find the config file with the name
migrations.iniin the project root directory.
Methods
manager.apply_all_migrations() # Applies all unapplied migrations in the specified migrations directory to the database.
manager.revert_last_migration() # Reverts the last applied migration from the database.
Contribution
Thank you for considering contributing to SQLite-Shift! Here are some guidelines to get started:
-
Reporting Issues:
If you encounter a bug or have a suggestion for improvement, please open an issue.
-
Code Contributions:
Fork the repository, make your changes, and submit a pull request. Follow existing code style and add tests for new features or fixes.
-
Code Review:
All pull requests undergo code review. Be open to feedback and suggestions from maintainers.
-
Testing:
Ensure that your changes include appropriate tests to maintain code quality.
-
Documentation:
Update documentation for any new features or modifications.
Your contributions are appreciated! Thank you for helping improve sqlite-shift.
License
Licensed under the Apache License 2.0.
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 sqlite-shift-0.2.0.tar.gz.
File metadata
- Download URL: sqlite-shift-0.2.0.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.18 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a45c4ff38a2e9b78a2cf71cc930ed3d63798c272c2cf46e37a1804a5fde89370
|
|
| MD5 |
9a66d361b31a3b1d8b725e7f9a3e08a6
|
|
| BLAKE2b-256 |
c76877baba7e46c5f6f045443355f2688c93ed1726bff02c2c5d1998d4a777de
|
File details
Details for the file sqlite_shift-0.2.0-py3-none-any.whl.
File metadata
- Download URL: sqlite_shift-0.2.0-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.18 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aab2134212ff76be4e04022bbdc96c216b1dbe1928cae11a68d656b6e185f079
|
|
| MD5 |
517c73f2e60683a0ff555322fbd39f34
|
|
| BLAKE2b-256 |
c836af33b8e0bdc666837794b905eedee8128998085d7af1ac2e911b22f26154
|