Migrated links, lyrics and properties
This commit is contained in:
53
scripts/00_migrate_links.py
Normal file
53
scripts/00_migrate_links.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import json
|
||||
from collections.abc import Iterable
|
||||
from typing import TypedDict
|
||||
|
||||
from folkugat_web.dal.sql import get_connection
|
||||
from folkugat_web.dal.sql.temes import links as links_dal
|
||||
from folkugat_web.model.temes import ContentType, Link, LinkType
|
||||
from folkugat_web.utils import map_none
|
||||
|
||||
query = """ SELECT id, links FROM temes """
|
||||
LinkRowTuple = tuple[int, str]
|
||||
|
||||
|
||||
class OldLinkRowDict(TypedDict):
|
||||
content_type: str
|
||||
link_type: str | None
|
||||
title: str
|
||||
url: str
|
||||
|
||||
|
||||
def _build_link(tema_id: int, link_dict: OldLinkRowDict) -> Link:
|
||||
return Link(
|
||||
id=None,
|
||||
tema_id=tema_id,
|
||||
content_type=ContentType(link_dict["content_type"]),
|
||||
link_type=map_none(LinkType, link_dict.get("link_type")),
|
||||
url=link_dict["url"],
|
||||
title=link_dict["title"],
|
||||
)
|
||||
|
||||
|
||||
def _build_links(row: LinkRowTuple) -> list[Link]:
|
||||
tema_id = row[0]
|
||||
link_dicts: list[OldLinkRowDict] = json.loads(row[1])
|
||||
return [_build_link(tema_id, link_dict) for link_dict in link_dicts]
|
||||
|
||||
|
||||
with get_connection() as con:
|
||||
cur = con.cursor()
|
||||
_ = cur.execute(query)
|
||||
iter_rows: Iterable[LinkRowTuple] = cur.fetchall()
|
||||
rows = list(map(_build_links, iter_rows))
|
||||
|
||||
_ = cur.execute("DELETE FROM tema_links")
|
||||
for links in rows:
|
||||
for link in links:
|
||||
_ = links_dal.insert_link(link=link, con=con)
|
||||
|
||||
drop_query = """ ALTER TABLE temes DROP COLUMN links"""
|
||||
_ = cur.execute(drop_query)
|
||||
|
||||
|
||||
print("DONE!")
|
||||
48
scripts/01_migrate_lyrics.py
Normal file
48
scripts/01_migrate_lyrics.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import json
|
||||
from collections.abc import Iterable
|
||||
from typing import TypedDict
|
||||
|
||||
from folkugat_web.dal.sql import get_connection
|
||||
from folkugat_web.dal.sql.temes import lyrics as lyrics_dal
|
||||
from folkugat_web.model.temes import Lyrics
|
||||
|
||||
query = """ SELECT id, lyrics FROM temes """
|
||||
LyricRowTuple = tuple[int, str]
|
||||
|
||||
|
||||
class OldLyricRowDict(TypedDict):
|
||||
title: str
|
||||
content: str
|
||||
|
||||
|
||||
def _build_lyric(tema_id: int, lyric_dict: OldLyricRowDict) -> Lyrics:
|
||||
return Lyrics(
|
||||
id=None,
|
||||
tema_id=tema_id,
|
||||
title=lyric_dict["title"],
|
||||
content=lyric_dict["content"],
|
||||
)
|
||||
|
||||
|
||||
def _build_lyrics(row: LyricRowTuple) -> list[Lyrics]:
|
||||
tema_id = row[0]
|
||||
lyric_dicts: list[OldLyricRowDict] = json.loads(row[1])
|
||||
return [_build_lyric(tema_id, lyric_dict) for lyric_dict in lyric_dicts]
|
||||
|
||||
|
||||
with get_connection() as con:
|
||||
cur = con.cursor()
|
||||
_ = cur.execute(query)
|
||||
iter_rows: Iterable[LyricRowTuple] = cur.fetchall()
|
||||
rows = list(map(_build_lyrics, iter_rows))
|
||||
|
||||
_ = cur.execute("DELETE FROM tema_lyrics")
|
||||
for lyrics in rows:
|
||||
for lyric in lyrics:
|
||||
_ = lyrics_dal.insert_lyric(lyric=lyric, con=con)
|
||||
|
||||
drop_query = """ ALTER TABLE temes DROP COLUMN lyrics"""
|
||||
_ = cur.execute(drop_query)
|
||||
|
||||
|
||||
print("DONE!")
|
||||
48
scripts/02_migrate_properties.py
Normal file
48
scripts/02_migrate_properties.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import json
|
||||
from collections.abc import Iterable
|
||||
from typing import TypedDict
|
||||
|
||||
from folkugat_web.dal.sql import get_connection
|
||||
from folkugat_web.dal.sql.temes import properties as properties_dal
|
||||
from folkugat_web.model.temes import Property, PropertyField
|
||||
|
||||
query = """ SELECT id, properties FROM temes """
|
||||
PropertyRowTuple = tuple[int, str]
|
||||
|
||||
|
||||
class OldPropertyRowDict(TypedDict):
|
||||
field: str
|
||||
value: str
|
||||
|
||||
|
||||
def _build_property(tema_id: int, property_dict: OldPropertyRowDict) -> Property:
|
||||
return Property(
|
||||
id=None,
|
||||
tema_id=tema_id,
|
||||
field=PropertyField(property_dict["field"]),
|
||||
value=property_dict["value"],
|
||||
)
|
||||
|
||||
|
||||
def _build_properties(row: PropertyRowTuple) -> list[Property]:
|
||||
tema_id = row[0]
|
||||
property_dicts: list[OldPropertyRowDict] = json.loads(row[1])
|
||||
return [_build_property(tema_id, property_dict) for property_dict in property_dicts]
|
||||
|
||||
|
||||
with get_connection() as con:
|
||||
cur = con.cursor()
|
||||
_ = cur.execute(query)
|
||||
iter_rows: Iterable[PropertyRowTuple] = cur.fetchall()
|
||||
rows = list(map(_build_properties, iter_rows))
|
||||
|
||||
_ = cur.execute("DELETE FROM tema_properties")
|
||||
for properties in rows:
|
||||
for property in properties:
|
||||
_ = properties_dal.insert_property(property=property, con=con)
|
||||
|
||||
drop_query = """ ALTER TABLE temes DROP COLUMN properties"""
|
||||
_ = cur.execute(drop_query)
|
||||
|
||||
|
||||
print("DONE!")
|
||||
Reference in New Issue
Block a user