An open API service indexing awesome lists of open source software.

https://github.com/diversen/python-mysql-migrations

Simple python migrations for MySQL without using ORM
https://github.com/diversen/python-mysql-migrations

mysql-migrations python-mysql-migrations

Last synced: over 1 year ago
JSON representation

Simple python migrations for MySQL without using ORM

Awesome Lists containing this project

README

          

# python mysql migration

Simple Python MySQL migration tool.

It executes files with SQL statements in e.g. `migrations/up` and `migrations/down` directories.

The main usage is probably if you don't use a ORM, but query the MySQL database directly - maybe using `mysql.connector`.

## Install mysql-migration

pip install git+https://github.com/diversen/python-mysql-migrations

Or using a tag:

pip install git+https://github.com/diversen/python-mysql-migrations@v0.0.2

## Create migrations dir

mkdir -p migrations/up migrations/down

Add SQL files into `migrations/up` and `migrations/down` directories, which will then be executed.

E.g.:

migrations/up/0001.sql
migrations/up/0002.sql
migrations/down/0001.sql
migrations/down/0002.sql

## Usage

```python
from mysql_migrations import MySQLMigrations

# The 'migration_file' should be in a .gitignore file if using git
# This file holds the current version of the database
m = MySQLMigrations(migration_dir='migrations', migration_file='.migration')
m.connect(host='localhost', user='root', password='password', database='mysql_migration_test')

# Executes 0001.sql and 0002.sql up files. The .migration version is 2
m.migrate_up(2)

# Excutes 0002.sql down. Now version the .migration version is 1
m.migrate_down(1)

# Executes 0002.sql up. The version .migration is 2
m.migrate_up()

# Executes 0002.sql and then 0001.sql down files, The .migration version is 0
m.migrate_down()

# Get current version
m.get_current_version() # -> 0

```

## Tests

The test uses a docker container with MySQL and a database named 'mysql_migration_test'.

These are the connection parameters:

```python
m = MySQLMigrations(migration_dir='migrations', migration_file='.migration')
m.connect(host='localhost', user='root', password='password', database='mysql_migration_test')
```

To run the tests:

python -m unittest discover -s tests

## License

[MIT](LICENSE)