Skip to content

Commit 4ad7b90

Browse files
committed
[DATABASE] Add ability to auto-migrate default peewee Models
1 parent caa9d4a commit 4ad7b90

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

unmanic/libs/db_migrate.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import inspect
3434
import os
3535

36-
from peewee import SqliteDatabase, Field
36+
from peewee import Model, SqliteDatabase, Field
3737
from peewee_migrate import Router
3838

3939
from unmanic.libs import unlogger
@@ -97,10 +97,19 @@ def update_schema(self):
9797
"""
9898
# Fetch all model classes
9999
all_models = []
100+
all_base_models = []
100101
for model in list_all_models():
101102
imported_model = getattr(importlib.import_module("unmanic.libs.unmodels"), model)
102103
if inspect.isclass(imported_model) and issubclass(imported_model, BaseModel):
104+
# Add this model to both the 'all_models' list and our list of base models
103105
all_models.append(imported_model)
106+
all_base_models.append(imported_model)
107+
elif inspect.isclass(imported_model) and issubclass(imported_model, Model):
108+
# If the model is not one of the base models, it is an in-build model from peewee.
109+
# For, this list of models we will not run a migration, but we will still ensure that the
110+
# table is created in the DB
111+
all_models.append(imported_model)
112+
pass
104113

105114
# Start by creating all models
106115
self.__log("Initialising database tables")
@@ -120,7 +129,7 @@ def update_schema(self):
120129
# Newly added fields can be auto added with this function... no need for a migration script
121130
# Ensure all files are also present for each of the model classes
122131
self.__log("Updating database fields")
123-
for model in all_models:
132+
for model in all_base_models:
124133
# Fetch all peewee fields for the model class
125134
# https://stackoverflow.com/questions/22573558/peewee-determining-meta-data-about-model-at-run-time
126135
fields = model._meta.fields

0 commit comments

Comments
 (0)