Skip to main content

A simple migrator with no bs.

Project description

Simple Migrator

A simple tool written in python to run simple migrations.

Basic Usage

0. Installation

On Macos Install

brew install --cask mysql-connector-python
pip install simple_migrator

1. Setup

simple_migrator setup {DATABASE_URL}

Result:-

File_path migrations/.config
Checking if migration table exists
Migration table not exists
Creating Table __migrations_table__

2. Create

simple_migrator create {MIGRATION_NAME} 

Result:-

Migration 1704095392331687000_migration_uoi.sql created at migrations/1704095392331687000_migration_uoi.sql

3. Apply

i. There are two ways to apply the migration. One way is to apply all the latest pending migrations.

simple_migrator up  

Result:-

Going to run the following migrations:
 1704096783004737000_migration_3.sql
Migraiton 1704096783004737000_migration_3.sql: COMPLETED
The following migration runned Successfully: ['1704096783004737000_migration_3.sql']

ii. Another way to do this is to give the files name to the up command.

Note that this method will rerun the given migrations file.

simple_migrator up --f 1703509439.048595_temp

Result:-

Going to run the following migrations:
 1704097419937590000_migration_0.sql
Migraiton 1704097419937590000_migration_0.sql: COMPLETED
The following migration runned Successfully: ['1704097419937590000_migration_0.sql']

3. Rollback

i. Similar to "up" migration there are two ways to do this

simple_migrator down 

Result:-

Going to run the following migrations:
 ['1704097461646876000_migration_1.sql']
Rollback Migraiton 1704097461646876000_migration_1.sql completed
All migration rollback successfully.

ii. Another way to do this is to give the files name to the up command.

Note that this method will rollback every migration no matter if they were applied or not.

simple_migrator down --files 1703509439.048595_temp

Result:-

Going to run the following migrations:
 ['1704097419937590000_migration_0.sql']
Rollback Migraiton 1704097419937590000_migration_0.sql completed
All migration rollback successfully.

4. List

i. List All

This will list all the migrations present.

simple_migrator list all

Result:-

Name Status Applied At
1704097419937590000_migration_0.sql MigrationStatus.PENDING 2024-01-01 14:03:14.225402
1704097461646876000_migration_1.sql MigrationStatus.APPLIED 2024-01-01 14:00:21.011784

ii. List All Applied

This will list all the migrations present.

simple_migrator list applied 
Name Status Applied At
1704097461646876000_migration_1.sql MigrationStatus.APPLIED 2024-01-01 14:00:21.011784

iii. List All Applied

This will list the last applied migrations.

simple_migrator list last-applied 
Name Status Applied At
1704097461646876000_migration_1.sql MigrationStatus.APPLIED 2024-01-01 14:00:21.011784

iv. List Pending

This will list the all the pending migrations.

simple_migrator list pending 
Name Status Applied At
1704097419937590000_migration_0.sql MigrationStatus.PENDING 2024-01-01 14:03:14.225402

5. Update

This command is used when you want to update the migration status of certain migrations. You can set different migrations status with --files file_name_1 --files file_name_2 and --status can be set to pending|applied|failed

Result:-

Going to update the following migrations:['1704097461646876000_migration_1.sql'] to status pending
Migrations:['1704097461646876000_migration_1.sql'] updated to status pending

6. Scync

When the database and the migration files are not in scynced. Use this command to scync it.

poetry run python simple_migrator/migrator.py scync --status applied

Result:-

1704097461646876000_migration_1.sql
Scyncing completed

Visit:

  1. Github
  2. PyPi
  3. Website

TDOD:

  • Make it so that the project does not reset when the setup is called twice.(There must be a better way).
  • Add tests to ensure that it works.
  • Add tests with other databases to ensure that it works.
  • Add a decorator so that we can check if setup is correctly done and exists gracefully.
  • Make sql errors more visible.
  • Exist gracefully when database name is suppied properly.

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

simple_migrator-0.1.9.tar.gz (37.6 kB view hashes)

Uploaded Source

Built Distribution

simple_migrator-0.1.9-py3-none-any.whl (14.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page