modified data moddel, to support non static use of the model
This commit is contained in:
parent
4719f46c49
commit
71e79faac4
@ -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,8 +131,19 @@ 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
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
database_1 = SqliteDatabase(":memory:")
|
database_1 = SqliteDatabase(":memory:")
|
||||||
database_1.create_tables([Song.Use(database_1)])
|
database_1.create_tables([Song.Use(database_1)])
|
||||||
database_2 = SqliteDatabase(":memory:")
|
database_2 = SqliteDatabase(":memory:")
|
||||||
@ -141,4 +163,3 @@ for song in Song.Use(database_1).select():
|
|||||||
print("database 2")
|
print("database 2")
|
||||||
for song in Song.Use(database_1).select():
|
for song in Song.Use(database_1).select():
|
||||||
print(song.name)
|
print(song.name)
|
||||||
|
|
||||||
|
@ -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()
|
||||||
|
Loading…
Reference in New Issue
Block a user