Skip to content

Commit

Permalink
feat: Add migrator create script
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
realashleybailey committed Oct 11, 2023
1 parent e58d03e commit 966aef2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
41 changes: 41 additions & 0 deletions backend/app/migrator/create.py
Original file line number Diff line number Diff line change
@@ -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")
17 changes: 17 additions & 0 deletions backend/app/migrator/template.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 966aef2

Please sign in to comment.