From 69651f259dc5e5efec47fd788b75313e9781b8e2 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Wed, 24 Jul 2024 22:25:48 -0400 Subject: [PATCH] Add a materialized view tracking collection price history --- .../007-card-price-history-materialized-view.sql | 13 +++++++++++++ tutor/cli.py | 1 + 2 files changed, 14 insertions(+) create mode 100644 postgres/007-card-price-history-materialized-view.sql diff --git a/postgres/007-card-price-history-materialized-view.sql b/postgres/007-card-price-history-materialized-view.sql new file mode 100644 index 0000000..384fc37 --- /dev/null +++ b/postgres/007-card-price-history-materialized-view.sql @@ -0,0 +1,13 @@ +DROP MATERIALIZED VIEW IF EXISTS "collection_price_history"; +CREATE MATERIALIZED VIEW "collection_price_history" AS +SELECT "card_prices"."date"::timestamp AS "date" + , SUM(CASE WHEN "copies"."isFoil" + THEN "card_prices"."usd_foil" + ELSE "card_prices"."usd" + END) AS "value" +FROM "cards" +JOIN "copies" USING ("scryfall_id") +JOIN "card_prices" USING ("scryfall_id") +WHERE "copies"."created_date" <= "card_prices"."date"::timestamp +GROUP BY "card_prices"."date" +ORDER BY "card_prices"."date"; diff --git a/tutor/cli.py b/tutor/cli.py index bc731bc..fbf0fe3 100644 --- a/tutor/cli.py +++ b/tutor/cli.py @@ -329,6 +329,7 @@ def update_scryfall(ctx, filename): print("Refreshing materialized views") await cursor.execute('REFRESH MATERIALIZED VIEW "cards"') await cursor.execute('REFRESH MATERIALIZED VIEW "oracle_latest"') + await cursor.execute('REFRESH MATERIALIZED VIEW "collection_price_history"') await tutor.database.store_var(cursor, "last_update", str(today)) await conn.commit()