Track external ID and use when importing

This commit is contained in:
Correl Roush 2024-03-12 21:08:07 -04:00
parent fc7671efd1
commit 82868b28c6
4 changed files with 28 additions and 2 deletions

View File

@ -0,0 +1,4 @@
ALTER TABLE "copies"
ADD COLUMN IF NOT EXISTS "external_id" TEXT UNIQUE;
CREATE INDEX IF NOT EXISTS "copies_external_id" ON "copies" ("external_id");

View File

@ -36,6 +36,7 @@ async def load(
for row in reader:
is_foil = "Foil" in row and row["Foil"].lower() == "foil"
quantity = int(row.get("Quantity", row.get("Count", 1)))
external_id = row.get("Delver Local ID")
if "Creation Date" in row:
created_date = arrow.get(
row["Creation Date"].replace("_", " ")
@ -68,6 +69,7 @@ async def load(
language=row["Language"] or "English",
collection=row.get("List name") or "Default",
created_date=created_date,
external_id=external_id,
)
logging.info((quantity, card))
for i in range(quantity):

View File

@ -351,8 +351,26 @@ async def store_set(
async def store_copy(db: psycopg.AsyncCursor, copy: tutor.models.CardCopy) -> None:
await db.execute(
"""
INSERT INTO copies ("scryfall_id", "isFoil", "collection", "condition", "created_date")
VALUES (%(scryfall_id)s, %(foil)s, %(collection)s, %(condition)s, %(created_date)s)
INSERT INTO copies ( "scryfall_id"
, "isFoil"
, "collection"
, "condition"
, "created_date"
, "external_id"
)
VALUES ( %(scryfall_id)s
, %(foil)s
, %(collection)s
, %(condition)s
, %(created_date)s
, %(external_id)s
)
ON CONFLICT ("external_id") DO UPDATE
SET "scryfall_id" = %(scryfall_id)s
, "isFoil" = %(foil)s
, "collection" = %(collection)s
, "condition" = %(condition)s
, "created_date" = %(created_date)s
""",
{
"scryfall_id": str(copy.card.scryfall_id),
@ -360,6 +378,7 @@ async def store_copy(db: psycopg.AsyncCursor, copy: tutor.models.CardCopy) -> No
"collection": copy.collection,
"condition": copy.condition,
"created_date": str(copy.created_date),
"external_id": copy.external_id,
},
)

View File

@ -108,6 +108,7 @@ class CardCopy:
foil: bool
language: str = "English"
collection: str = "Default"
external_id: typing.Optional[str] = None
condition: typing.Optional[str] = None
created_date: datetime.datetime = dataclasses.field(
default_factory=datetime.datetime.now