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 (
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)

View File

@ -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()