This project is a database migration tool that helps manage and apply database schema changes. It uses a JSON file to define the changes and applies them to a MySQL database.
Project description
Tern🐧 - MySQL Versioning for Python
This project is a database migration tool that helps manage and apply database schema changes. It uses a JSON file to define the changes and applies them to a MySQL database. It is named after a bird called Arctic Tern which can migrate the longest distance. Yearly migration can range from 44,000 miles to 59,000 miles.
Files
changes.json: Contains the list of database changes to be applied.connection.py: Manages the database connection pool.main.py: Entry point for running the migration scripts.Makefile: Contains commands to run the project.migration.py: Contains functions to process migrations, calculate checksums, and insert data into the database.requirement.txt: Lists the Python dependencies for the project.venv/: Virtual environment directory.
Setup
-
Create a Virtual Environment:
python -m venv venv
-
Activate the Virtual Environment:
- On Windows:
.\venv\Scripts\activate
- On macOS/Linux:
source venv/bin/activate
- On Windows:
-
Install Dependencies:
pip install -r requirement.txt
Usage
Running Migrations
To run the migrations, use the main.py script with the appropriate arguments.
-
Migrate Up:
python main.py --migrate up
-
Migrate Down:
python main.py --migrate down
Makefile
You can also use the Makefile to run the project:
- Run the Project:
make run
Code Overview
connection.py
get_connection_pool(): Returns a MySQL connection pool.create_internal_db(): Creates the internal database and thedb_changelogtable if it doesn't exist.
migration.py
process_migration_down(cnx_pool): Processes the down migration by one change.process_migration(cnx_pool): Processes the up migration by applying all changes from the changes.json file.insert_data(cnx_pool, change): Inserts a change record into thedb_changelogtable.calculate_checksum(changeText): Calculates the SHA-256 checksum of a change text.compare_checksum(checksum, changeText): Compares the given checksum with the calculated checksum of a change text.read_migration_from_file(file_name): Reads migration data from a YAML file.
main.py
- Entry point for the migration tool.
- Parses command-line arguments to determine whether to migrate up or down.
- Calls the appropriate functions from migration.py to process the migrations.
Example changes.json
{
"changeSet": [
{
"id": "1",
"author": "John Doe",
"description": "Initial version",
"date": "2019-01-01",
"comment": "Initial version",
"migrateUp": "CREATE TABLE my_table (id INT PRIMARY KEY, name VARCHAR(255));",
"migrateDown": "DROP TABLE my_table;"
},
{
"id": "2",
"author": "John Doe",
"description": "Add column",
"date": "2019-01-02",
"comment": "Add column",
"migrateUp": "ALTER TABLE my_table ADD COLUMN age INT;",
"migrateDown": "ALTER TABLE my_table DROP COLUMN age;"
}
]
}
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 qwik_tern-0.0.2.tar.gz.
File metadata
- Download URL: qwik_tern-0.0.2.tar.gz
- Upload date:
- Size: 19.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e95c76edcd9354a256122a3458f6695c60760b794655c9eae56db894870933ad
|
|
| MD5 |
56da339c2bfa0d8d25881387a728b699
|
|
| BLAKE2b-256 |
6fab63bcda81b7513bd55dd55e75f0aed7fc59cf9f37e6f007c13252aa3c473e
|
File details
Details for the file qwik_tern-0.0.2-py3-none-any.whl.
File metadata
- Download URL: qwik_tern-0.0.2-py3-none-any.whl
- Upload date:
- Size: 19.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9fb440192641e289f14b41fdc2fe36a712b6fda693ef591f3e5c0e741027f992
|
|
| MD5 |
f6e343f18d57a55e31c30436f8579b40
|
|
| BLAKE2b-256 |
d7d0540fab1a85a12c704254cff3873d7d5db4479c7cb042431af831a48968a4
|