mirror of
https://github.com/283375/arcaea-offline.git
synced 2025-04-20 22:40:17 +00:00
feat(db): Database.check_init()
This commit is contained in:
parent
d979b6cd10
commit
a6c1e594c4
@ -1,4 +1,4 @@
|
|||||||
from sqlalchemy import Engine, select
|
from sqlalchemy import Engine, inspect, select
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
from .models.config import *
|
from .models.config import *
|
||||||
@ -46,3 +46,17 @@ class Database(metaclass=Singleton):
|
|||||||
if not checkfirst or not result:
|
if not checkfirst or not result:
|
||||||
session.add(Property(key="version", value="2"))
|
session.add(Property(key="version", value="2"))
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
def check_init(self) -> bool:
|
||||||
|
# check table exists
|
||||||
|
expect_tables = (
|
||||||
|
list(SongsBase.metadata.tables.keys())
|
||||||
|
+ list(ScoresBase.metadata.tables.keys())
|
||||||
|
+ list(ConfigBase.metadata.tables.keys())
|
||||||
|
+ [
|
||||||
|
Calculated.__tablename__,
|
||||||
|
Best.__tablename__,
|
||||||
|
CalculatedPotential.__tablename__,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
return all(inspect(self.engine).has_table(t) for t in expect_tables)
|
||||||
|
@ -48,6 +48,8 @@ class ScoresViewBase(DeclarativeBase, ReprHelper):
|
|||||||
|
|
||||||
|
|
||||||
class Calculated(ScoresViewBase):
|
class Calculated(ScoresViewBase):
|
||||||
|
__tablename__ = "calculated"
|
||||||
|
|
||||||
score_id: Mapped[str]
|
score_id: Mapped[str]
|
||||||
song_id: Mapped[str]
|
song_id: Mapped[str]
|
||||||
rating_class: Mapped[int]
|
rating_class: Mapped[int]
|
||||||
@ -62,7 +64,7 @@ class Calculated(ScoresViewBase):
|
|||||||
potential: Mapped[float]
|
potential: Mapped[float]
|
||||||
|
|
||||||
__table__ = create_view(
|
__table__ = create_view(
|
||||||
name="calculated",
|
name=__tablename__,
|
||||||
selectable=select(
|
selectable=select(
|
||||||
Score.id.label("score_id"),
|
Score.id.label("score_id"),
|
||||||
Chart.song_id,
|
Chart.song_id,
|
||||||
@ -110,6 +112,8 @@ class Calculated(ScoresViewBase):
|
|||||||
|
|
||||||
|
|
||||||
class Best(ScoresViewBase):
|
class Best(ScoresViewBase):
|
||||||
|
__tablename__ = "best"
|
||||||
|
|
||||||
score_id: Mapped[str]
|
score_id: Mapped[str]
|
||||||
song_id: Mapped[str]
|
song_id: Mapped[str]
|
||||||
rating_class: Mapped[int]
|
rating_class: Mapped[int]
|
||||||
@ -124,7 +128,7 @@ class Best(ScoresViewBase):
|
|||||||
potential: Mapped[float]
|
potential: Mapped[float]
|
||||||
|
|
||||||
__table__ = create_view(
|
__table__ = create_view(
|
||||||
name="best",
|
name=__tablename__,
|
||||||
selectable=select(
|
selectable=select(
|
||||||
*[col for col in inspect(Calculated).columns if col.name != "potential"],
|
*[col for col in inspect(Calculated).columns if col.name != "potential"],
|
||||||
func.max(Calculated.potential).label("potential"),
|
func.max(Calculated.potential).label("potential"),
|
||||||
@ -138,6 +142,8 @@ class Best(ScoresViewBase):
|
|||||||
|
|
||||||
|
|
||||||
class CalculatedPotential(ScoresViewBase):
|
class CalculatedPotential(ScoresViewBase):
|
||||||
|
__tablename__ = "calculated_potential"
|
||||||
|
|
||||||
b30: Mapped[float]
|
b30: Mapped[float]
|
||||||
|
|
||||||
_select_bests_subquery = (
|
_select_bests_subquery = (
|
||||||
@ -147,7 +153,7 @@ class CalculatedPotential(ScoresViewBase):
|
|||||||
.subquery()
|
.subquery()
|
||||||
)
|
)
|
||||||
__table__ = create_view(
|
__table__ = create_view(
|
||||||
name="calculated_potential",
|
name=__tablename__,
|
||||||
selectable=select(func.avg(_select_bests_subquery.c.b30_sum).label("b30")),
|
selectable=select(func.avg(_select_bests_subquery.c.b30_sum).label("b30")),
|
||||||
metadata=ScoresViewBase.metadata,
|
metadata=ScoresViewBase.metadata,
|
||||||
cascade_on_drop=False,
|
cascade_on_drop=False,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user