Files
bricktracker
sql
views
__init__.py
app.py
config.py
configuration.py
configuration_list.py
exceptions.py
fields.py
instructions.py
instructions_list.py
login.py
minifigure.py
minifigure_list.py
navbar.py
parser.py
part.py
part_list.py
rebrickable.py
rebrickable_image.py
rebrickable_minifigures.py
rebrickable_parts.py
rebrickable_set.py
rebrickable_set_list.py
record.py
record_list.py
reload.py
retired.py
retired_list.py
set.py
set_checkbox.py
set_checkbox_list.py
set_list.py
socket.py
sql.py
sql_counter.py
sql_migration.py
sql_migration_list.py
sql_stats.py
theme.py
theme_list.py
version.py
wish.py
wish_list.py
docs
static
templates
.dockerignore
.env.sample
.gitignore
CHANGELOG.md
Dockerfile
LICENSE
README.md
__init__.py
app.py
compose.legacy.yml
compose.local.yaml
compose.yaml
entrypoint.sh
requirements.txt
test-server.sh
BrickTracker/bricktracker/sql_migration.py

53 lines
1.4 KiB
Python

import os
from .version import __database_version__
class BrickSQLMigration(object):
description: str | None
name: str
file: str
version: int
# Description marker
description_marker: str = '-- description:'
def __init__(self, file: str):
self.file = file
self.name, _ = os.path.splitext(os.path.basename(self.file))
self.version = int(self.name)
self.description = None
# Read the description from the migration file if it exists
def get_description(self) -> str:
if self.description is None:
self.description = ''
# First line or ignored
with open(self.file, 'r') as file:
line = file.readline()
# Extract a description (only the first one)
if line.startswith(self.description_marker):
self.description = line.strip()[
len(self.description_marker):
]
return self.description
# Tells whether the migration is need
def is_needed(self, current: int, /):
return self.version > current and self.version <= __database_version__
# Query name for the SQL loader
def get_query(self) -> str:
relative, _ = os.path.splitext(
os.path.relpath(self.file, os.path.join(
os.path.dirname(__file__),
'sql/'
))
)
return relative