modified data moddel, to support non static use of the model

This commit is contained in:
Hellow2 2023-02-15 15:44:13 +01:00
parent 4719f46c49
commit 71e79faac4
2 changed files with 57 additions and 19 deletions

View File

@ -1,4 +1,4 @@
from typing import List, Union, Type from typing import List, Union, Type, Optional
from peewee import ( from peewee import (
SqliteDatabase, SqliteDatabase,
PostgresqlDatabase, PostgresqlDatabase,
@ -11,6 +11,18 @@ from peewee import (
TextField TextField
) )
"""
**IMPORTANT**:
never delete, modify the datatype or add constrains to ANY existing collumns,
between the versions, that gets pushed out to the users.
Else my function can't update legacy databases, to new databases,
while keeping the data of the old ones.
EVEN if that means to for example keep decimal values stored in strings.
(not in my codebase though.)
"""
class BaseModel(Model): class BaseModel(Model):
class Meta: class Meta:
@ -26,7 +38,6 @@ class BaseModel(Model):
return self return self
class Album(BaseModel): class Album(BaseModel):
"""A class representing an album in the music database.""" """A class representing an album in the music database."""
@ -120,25 +131,35 @@ class AlbumArtist(BaseModel):
artist: ForeignKeyField = ForeignKeyField(Artist, backref='album_artists') artist: ForeignKeyField = ForeignKeyField(Artist, backref='album_artists')
ALL_MODELS = [
Song,
Album,
Artist,
Source,
Lyrics,
AlbumArtist,
Target,
SongArtist
]
database_1 = SqliteDatabase(":memory:") if __name__ == "__main__":
database_1.create_tables([Song.Use(database_1)]) database_1 = SqliteDatabase(":memory:")
database_2 = SqliteDatabase(":memory:") database_1.create_tables([Song.Use(database_1)])
database_2.create_tables([Song.Use(database_2)]) database_2 = SqliteDatabase(":memory:")
database_2.create_tables([Song.Use(database_2)])
# creating songs, adding it to db_2 if i is even, else to db_1 # creating songs, adding it to db_2 if i is even, else to db_1
for i in range(100): for i in range(100):
song = Song(name=str(i) + "hs") song = Song(name=str(i) + "hs")
db_to_use = database_2 if i % 2 == 0 else database_1 db_to_use = database_2 if i % 2 == 0 else database_1
song.use(db_to_use).save() song.use(db_to_use).save()
print("database 1") print("database 1")
for song in Song.Use(database_1).select(): for song in Song.Use(database_1).select():
print(song.name) print(song.name)
print("database 2")
for song in Song.Use(database_1).select():
print(song.name)
print("database 2")
for song in Song.Use(database_1).select():
print(song.name)

View File

@ -75,5 +75,22 @@ class Database:
create tables if they don't exist. create tables if they don't exist.
""" """
self.database = self.create_database() self.database = self.create_database()
self.database.connect() self.database.connect()
self.database.create_tables(data_models.ALL_MODELS, safe=True)
"""
upgrade old databases.
If a collumn has been added in a new version this adds it to old Tables,
without deleting the data in legacy databases
"""
for model in data_models.ALL_MODELS:
for field_name, field_obj in model._meta.fields.items():
# check if the field exists in the database
if not self.database.table_column_exists(model._meta.db_table, field_name):
# add the missing column to the table
self.database.add_column(model._meta.db_table, field_name, field_obj)
def __del__(self):
self.database.close()