From 71e79faac4c2d2fde5912ed0ac6e1a0062ab57cd Mon Sep 17 00:00:00 2001 From: Hellow2 Date: Wed, 15 Feb 2023 15:44:13 +0100 Subject: [PATCH] modified data moddel, to support non static use of the model --- src/music_kraken/database/data_models.py | 57 ++++++++++++++++-------- src/music_kraken/database/database.py | 19 +++++++- 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/src/music_kraken/database/data_models.py b/src/music_kraken/database/data_models.py index 7d60318..4b8e1be 100644 --- a/src/music_kraken/database/data_models.py +++ b/src/music_kraken/database/data_models.py @@ -1,4 +1,4 @@ -from typing import List, Union, Type +from typing import List, Union, Type, Optional from peewee import ( SqliteDatabase, PostgresqlDatabase, @@ -11,6 +11,18 @@ from peewee import ( 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 Meta: @@ -24,7 +36,6 @@ class BaseModel(Model): def use(self, database: Union[SqliteDatabase, PostgresqlDatabase, MySQLDatabase]) -> Model: self._meta.database = database return self - class Album(BaseModel): @@ -120,25 +131,35 @@ class AlbumArtist(BaseModel): artist: ForeignKeyField = ForeignKeyField(Artist, backref='album_artists') +ALL_MODELS = [ + Song, + Album, + Artist, + Source, + Lyrics, + AlbumArtist, + Target, + SongArtist +] -database_1 = SqliteDatabase(":memory:") -database_1.create_tables([Song.Use(database_1)]) -database_2 = SqliteDatabase(":memory:") -database_2.create_tables([Song.Use(database_2)]) +if __name__ == "__main__": + database_1 = SqliteDatabase(":memory:") + database_1.create_tables([Song.Use(database_1)]) + 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 -for i in range(100): - song = Song(name=str(i) + "hs") + # creating songs, adding it to db_2 if i is even, else to db_1 + for i in range(100): + song = Song(name=str(i) + "hs") - db_to_use = database_2 if i % 2 == 0 else database_1 - song.use(db_to_use).save() + db_to_use = database_2 if i % 2 == 0 else database_1 + song.use(db_to_use).save() -print("database 1") -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) + print("database 1") + 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) diff --git a/src/music_kraken/database/database.py b/src/music_kraken/database/database.py index f77ff60..002fad8 100644 --- a/src/music_kraken/database/database.py +++ b/src/music_kraken/database/database.py @@ -75,5 +75,22 @@ class Database: create tables if they don't exist. """ self.database = self.create_database() - 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()