From 966aef241f58f7718d4c23e1280cf025a16cb576 Mon Sep 17 00:00:00 2001 From: Ashley Date: Wed, 11 Oct 2023 23:49:41 +0100 Subject: [PATCH] feat: Add migrator create script - Implement a script that creates a migration file based on a template. - The script reads the current version from a file and generates the filename using the current date and time. - The template file is filled with the current date, filename, and version. - The modified content is written to the new file. - Finally, the script prints the filename of the created migration file. This commit introduces the `create.py` script and the `template.py` file that is used as a placeholder for the generated migrations. --- backend/app/migrator/create.py | 41 ++++++++++++++++++++++++++++++++ backend/app/migrator/template.py | 17 +++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 backend/app/migrator/create.py create mode 100644 backend/app/migrator/template.py diff --git a/backend/app/migrator/create.py b/backend/app/migrator/create.py new file mode 100644 index 000000000..1bcdb11c0 --- /dev/null +++ b/backend/app/migrator/create.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +from datetime import datetime +from os import path +from packaging.version import parse + +def get_current_version(): + # File path to the version file + version_file = path.join(path.dirname(path.realpath(__file__)), "../", "../", "../", "latest") + + # Read the current version + with open(version_file, "r") as f: + current_version = parse(f.read()) + + return current_version + +# Get the current script's directory +script_dir = path.dirname(path.abspath(__file__ if '__file__' in locals() else path.abspath(path.realpath(__file__)))) + +# Define the path to the template file relative to the script's directory +template_file = path.join(script_dir, "template.py") + +# Get the current date and time in YYYY-MM-DD_HH-MM-SS format +current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + +# Create the filename with the current date in the current directory +filename = path.join(script_dir, "migrations", f"{current_time}.py") + +# Read the content of the template file +with open(template_file, "r") as template: + template_content = template.read() + +# Replace {data} with Tue Oct 10 2023 date format and replace {name} with the filename +template_content = template_content.replace("{date}", datetime.now().strftime("%a %b %d %Y")) +template_content = template_content.replace("{name}", current_time) +template_content = template_content.replace("{version}", str(get_current_version())) + +# Write the modified content to the new file +with open(filename, "w") as new_file: + new_file.write(template_content) + +print(f"MIGRATION: {filename} created") diff --git a/backend/app/migrator/template.py b/backend/app/migrator/template.py new file mode 100644 index 000000000..5ff68b626 --- /dev/null +++ b/backend/app/migrator/template.py @@ -0,0 +1,17 @@ +# +# CREATED ON VERSION: V{version} +# MIGRATION: {name} +# CREATED: {date} +# + +from peewee import * +from playhouse.migrate import * + +from app import db + +# Do not change the name of this file, +# migrations are run in order of their filenames date and time + +def run(): + # Use migrator to perform actions on the database + migrator = SqliteMigrator(db)