diff --git a/bricktracker/migrations/0007.py b/bricktracker/migrations/0007.py new file mode 100644 index 0000000..fb5b723 --- /dev/null +++ b/bricktracker/migrations/0007.py @@ -0,0 +1,27 @@ +from typing import Any, TYPE_CHECKING + +if TYPE_CHECKING: + from ..sql import BrickSQL + + +# Grab the list of checkboxes to create a list of SQL columns +def migration_0007(self: 'BrickSQL') -> dict[str, Any]: + records = self.fetchall('checkbox/list') + + return { + 'sources': ', '.join([ + '"bricktracker_set_statuses_old"."status_{id}"'.format(id=record['id']) # noqa: E501 + for record + in records + ]), + 'targets': ', '.join([ + '"status_{id}"'.format(id=record['id']) + for record + in records + ]), + 'structure': ', '.join([ + '"status_{id}" BOOLEAN NOT NULL DEFAULT 0'.format(id=record['id']) + for record + in records + ]) + } diff --git a/bricktracker/minifigure_list.py b/bricktracker/minifigure_list.py index 81affa6..24b9933 100644 --- a/bricktracker/minifigure_list.py +++ b/bricktracker/minifigure_list.py @@ -134,7 +134,7 @@ class BrickMinifigureList(BrickRecordList[BrickMinifigure]): parameters: dict[str, Any] = super().sql_parameters() if self.brickset is not None: - parameters['bricktracker_set_id'] = self.brickset.fields.id + parameters['id'] = self.brickset.fields.id return parameters diff --git a/bricktracker/rebrickable_minifigure.py b/bricktracker/rebrickable_minifigure.py index 28d3d75..e296356 100644 --- a/bricktracker/rebrickable_minifigure.py +++ b/bricktracker/rebrickable_minifigure.py @@ -75,9 +75,8 @@ class RebrickableMinifigure(BrickRecord): parameters = super().sql_parameters() # Supplement from the brickset - if self.brickset is not None: - if 'bricktracker_set_id' not in parameters: - parameters['bricktracker_set_id'] = self.brickset.fields.id + if self.brickset is not None and 'id' not in parameters: + parameters['id'] = self.brickset.fields.id return parameters diff --git a/bricktracker/sql/migrations/0007.sql b/bricktracker/sql/migrations/0007.sql index 09830c4..036f12e 100644 --- a/bricktracker/sql/migrations/0007.sql +++ b/bricktracker/sql/migrations/0007.sql @@ -1,30 +1,53 @@ --- description: Creation of the deduplicated table of Rebrickable minifigures +-- description: Renaming various complicated field names to something simpler + +PRAGMA foreign_keys = ON; BEGIN TRANSACTION; --- Create a Rebrickable minifigures table: each unique minifigure imported from Rebrickable -CREATE TABLE "rebrickable_minifigures" ( - "figure" TEXT NOT NULL, - "number" INTEGER NOT NULL, - "name" TEXT NOT NULL, - "image" TEXT, - PRIMARY KEY("figure") +-- Rename sets table +ALTER TABLE "bricktracker_sets" RENAME TO "bricktracker_sets_old"; + +-- Re-Create a Bricktable set table with the simplified name +CREATE TABLE "bricktracker_sets" ( + "id" TEXT NOT NULL, + "set" TEXT NOT NULL, + PRIMARY KEY("id"), + FOREIGN KEY("set") REFERENCES "rebrickable_sets"("set") ); -- Insert existing sets into the new table -INSERT INTO "rebrickable_minifigures" ( - "figure", - "number", - "name", - "image" +INSERT INTO "bricktracker_sets" ( + "id", + "set" ) SELECT - "minifigures"."fig_num", - CAST(SUBSTR("minifigures"."fig_num", 5) AS INTEGER), - "minifigures"."name", - "minifigures"."set_img_url" -FROM "minifigures" -GROUP BY - "minifigures"."fig_num"; + "bricktracker_sets_old"."id", + "bricktracker_sets_old"."rebrickable_set" +FROM "bricktracker_sets_old"; + +-- Rename status table +ALTER TABLE "bricktracker_set_statuses" RENAME TO "bricktracker_set_statuses_old"; + +-- Re-create a table for the status of each checkbox +CREATE TABLE "bricktracker_set_statuses" ( + "id" TEXT NOT NULL, + {% if structure %}{{ structure }},{% endif %} + PRIMARY KEY("id"), + FOREIGN KEY("id") REFERENCES "bricktracker_sets"("id") +); + +-- Insert existing status into the new table +INSERT INTO "bricktracker_set_statuses" ( + {% if targets %}{{ targets }},{% endif %} + "id" +) +SELECT + {% if sources %}{{ sources }},{% endif %} + "bricktracker_set_statuses_old"."bricktracker_set_id" +FROM "bricktracker_set_statuses_old"; + +-- Delete the original tables +DROP TABLE "bricktracker_set_statuses_old"; +DROP TABLE "bricktracker_sets_old"; COMMIT; \ No newline at end of file diff --git a/bricktracker/sql/migrations/0008.sql b/bricktracker/sql/migrations/0008.sql index 48b905a..09830c4 100644 --- a/bricktracker/sql/migrations/0008.sql +++ b/bricktracker/sql/migrations/0008.sql @@ -1,32 +1,30 @@ --- description: Migrate the Bricktracker minifigures - -PRAGMA foreign_keys = ON; +-- description: Creation of the deduplicated table of Rebrickable minifigures BEGIN TRANSACTION; --- Create a Bricktable minifigures table: an amount of minifigures linked to a Bricktracker set -CREATE TABLE "bricktracker_minifigures" ( - "bricktracker_set_id" TEXT NOT NULL, - "rebrickable_figure" TEXT NOT NULL, - "quantity" INTEGER NOT NULL, - PRIMARY KEY("bricktracker_set_id", "rebrickable_figure"), - FOREIGN KEY("bricktracker_set_id") REFERENCES "bricktracker_sets"("id"), - FOREIGN KEY("rebrickable_figure") REFERENCES "rebrickable_minifigures"("figure") +-- Create a Rebrickable minifigures table: each unique minifigure imported from Rebrickable +CREATE TABLE "rebrickable_minifigures" ( + "figure" TEXT NOT NULL, + "number" INTEGER NOT NULL, + "name" TEXT NOT NULL, + "image" TEXT, + PRIMARY KEY("figure") ); -- Insert existing sets into the new table -INSERT INTO "bricktracker_minifigures" ( - "bricktracker_set_id", - "rebrickable_figure", - "quantity" +INSERT INTO "rebrickable_minifigures" ( + "figure", + "number", + "name", + "image" ) SELECT - "minifigures"."u_id", "minifigures"."fig_num", - "minifigures"."quantity" -FROM "minifigures"; - --- Rename the original table (don't delete it yet?) -ALTER TABLE "minifigures" RENAME TO "minifigures_old"; + CAST(SUBSTR("minifigures"."fig_num", 5) AS INTEGER), + "minifigures"."name", + "minifigures"."set_img_url" +FROM "minifigures" +GROUP BY + "minifigures"."fig_num"; COMMIT; \ No newline at end of file diff --git a/bricktracker/sql/migrations/0009.sql b/bricktracker/sql/migrations/0009.sql new file mode 100644 index 0000000..135f95d --- /dev/null +++ b/bricktracker/sql/migrations/0009.sql @@ -0,0 +1,32 @@ +-- description: Migrate the Bricktracker minifigures + +PRAGMA foreign_keys = ON; + +BEGIN TRANSACTION; + +-- Create a Bricktable minifigures table: an amount of minifigures linked to a Bricktracker set +CREATE TABLE "bricktracker_minifigures" ( + "id" TEXT NOT NULL, + "figure" TEXT NOT NULL, + "quantity" INTEGER NOT NULL, + PRIMARY KEY("id", "figure"), + FOREIGN KEY("id") REFERENCES "bricktracker_sets"("id"), + FOREIGN KEY("figure") REFERENCES "rebrickable_minifigures"("figure") +); + +-- Insert existing sets into the new table +INSERT INTO "bricktracker_minifigures" ( + "id", + "figure", + "quantity" +) +SELECT + "minifigures"."u_id", + "minifigures"."fig_num", + "minifigures"."quantity" +FROM "minifigures"; + +-- Rename the original table (don't delete it yet?) +ALTER TABLE "minifigures" RENAME TO "minifigures_old"; + +COMMIT; \ No newline at end of file diff --git a/bricktracker/sql/minifigure/base/base.sql b/bricktracker/sql/minifigure/base/base.sql index bfaf10d..c580b38 100644 --- a/bricktracker/sql/minifigure/base/base.sql +++ b/bricktracker/sql/minifigure/base/base.sql @@ -17,7 +17,7 @@ SELECT FROM "bricktracker_minifigures" INNER JOIN "rebrickable_minifigures" -ON "bricktracker_minifigures"."rebrickable_figure" IS NOT DISTINCT FROM "rebrickable_minifigures"."figure" +ON "bricktracker_minifigures"."figure" IS NOT DISTINCT FROM "rebrickable_minifigures"."figure" {% block join %}{% endblock %} diff --git a/bricktracker/sql/minifigure/insert.sql b/bricktracker/sql/minifigure/insert.sql index cd7c413..0a2679e 100644 --- a/bricktracker/sql/minifigure/insert.sql +++ b/bricktracker/sql/minifigure/insert.sql @@ -1,9 +1,9 @@ INSERT INTO "bricktracker_minifigures" ( - "bricktracker_set_id", - "rebrickable_figure", + "id", + "figure", "quantity" ) VALUES ( - :bricktracker_set_id, + :id, :figure, :quantity ) diff --git a/bricktracker/sql/minifigure/list/all.sql b/bricktracker/sql/minifigure/list/all.sql index 82e61a2..498062d 100644 --- a/bricktracker/sql/minifigure/list/all.sql +++ b/bricktracker/sql/minifigure/list/all.sql @@ -9,7 +9,7 @@ SUM(IFNULL("bricktracker_minifigures"."quantity", 0)) AS "total_quantity", {% endblock %} {% block total_sets %} -COUNT("bricktracker_minifigures"."bricktracker_set_id") AS "total_sets" +COUNT("bricktracker_minifigures"."id") AS "total_sets" {% endblock %} {% block join %} @@ -24,7 +24,7 @@ LEFT JOIN ( "missing"."set_num", "missing"."u_id" ) missing_join -ON "bricktracker_minifigures"."bricktracker_set_id" IS NOT DISTINCT FROM "missing_join"."u_id" +ON "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "missing_join"."u_id" AND "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "missing_join"."set_num" {% endblock %} diff --git a/bricktracker/sql/minifigure/list/from_set.sql b/bricktracker/sql/minifigure/list/from_set.sql index 65b4e69..e22ee95 100644 --- a/bricktracker/sql/minifigure/list/from_set.sql +++ b/bricktracker/sql/minifigure/list/from_set.sql @@ -1,5 +1,5 @@ {% extends 'minifigure/base/base.sql' %} {% block where %} -WHERE "bricktracker_minifigures"."bricktracker_set_id" IS NOT DISTINCT FROM :bricktracker_set_id +WHERE "bricktracker_minifigures"."id" IS NOT DISTINCT FROM :id {% endblock %} diff --git a/bricktracker/sql/minifigure/list/last.sql b/bricktracker/sql/minifigure/list/last.sql index 266b7c0..cacc2f7 100644 --- a/bricktracker/sql/minifigure/list/last.sql +++ b/bricktracker/sql/minifigure/list/last.sql @@ -7,11 +7,11 @@ SUM(IFNULL("missing"."quantity", 0)) AS "total_missing", {% block join %} LEFT JOIN "missing" ON "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "missing"."set_num" -AND "bricktracker_minifigures"."bricktracker_set_id" IS NOT DISTINCT FROM "missing"."u_id" +AND "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "missing"."u_id" {% endblock %} {% block group %} GROUP BY "rebrickable_minifigures"."figure", - "bricktracker_minifigures"."bricktracker_set_id" + "bricktracker_minifigures"."id" {% endblock %} diff --git a/bricktracker/sql/minifigure/list/missing_part.sql b/bricktracker/sql/minifigure/list/missing_part.sql index 660da6d..3fe4210 100644 --- a/bricktracker/sql/minifigure/list/missing_part.sql +++ b/bricktracker/sql/minifigure/list/missing_part.sql @@ -7,7 +7,7 @@ SUM(IFNULL("missing"."quantity", 0)) AS "total_missing", {% block join %} LEFT JOIN "missing" ON "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "missing"."set_num" -AND "bricktracker_minifigures"."bricktracker_set_id" IS NOT DISTINCT FROM "missing"."u_id" +AND "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "missing"."u_id" {% endblock %} {% block where %} diff --git a/bricktracker/sql/minifigure/select/generic.sql b/bricktracker/sql/minifigure/select/generic.sql index 966c022..16dc56d 100644 --- a/bricktracker/sql/minifigure/select/generic.sql +++ b/bricktracker/sql/minifigure/select/generic.sql @@ -9,13 +9,13 @@ SUM(IFNULL("bricktracker_minifigures"."quantity", 0)) AS "total_quantity", {% endblock %} {% block total_sets %} -COUNT(DISTINCT "bricktracker_minifigures"."bricktracker_set_id") AS "total_sets" +COUNT(DISTINCT "bricktracker_minifigures"."id") AS "total_sets" {% endblock %} {% block join %} LEFT JOIN "missing" ON "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "missing"."set_num" -AND "bricktracker_minifigures"."bricktracker_set_id" IS NOT DISTINCT FROM "missing"."u_id" +AND "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "missing"."u_id" {% endblock %} {% block where %} diff --git a/bricktracker/sql/minifigure/select/specific.sql b/bricktracker/sql/minifigure/select/specific.sql index 479c9e5..00a66af 100644 --- a/bricktracker/sql/minifigure/select/specific.sql +++ b/bricktracker/sql/minifigure/select/specific.sql @@ -2,5 +2,5 @@ {% block where %} WHERE "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM :figure -AND "bricktracker_minifigures"."bricktracker_set_id" IS NOT DISTINCT FROM :bricktracker_set_id +AND "bricktracker_minifigures"."id" IS NOT DISTINCT FROM :id {% endblock %} diff --git a/bricktracker/sql/part/list/all.sql b/bricktracker/sql/part/list/all.sql index 9d73fcc..c5bbf69 100644 --- a/bricktracker/sql/part/list/all.sql +++ b/bricktracker/sql/part/list/all.sql @@ -9,7 +9,7 @@ SUM("inventory"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)) A {% endblock %} {% block total_sets %} -COUNT(DISTINCT "bricktracker_minifigures"."bricktracker_set_id") AS "total_sets", +COUNT(DISTINCT "bricktracker_minifigures"."id") AS "total_sets", {% endblock %} {% block total_minifigures %} @@ -26,8 +26,8 @@ AND "inventory"."element_id" IS NOT DISTINCT FROM "missing"."element_id" AND "inventory"."u_id" IS NOT DISTINCT FROM "missing"."u_id" LEFT JOIN "bricktracker_minifigures" -ON "inventory"."set_num" IS NOT DISTINCT FROM "bricktracker_minifigures"."rebrickable_figure" -AND "inventory"."u_id" IS NOT DISTINCT FROM "bricktracker_minifigures"."bricktracker_set_id" +ON "inventory"."set_num" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure" +AND "inventory"."u_id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id" {% endblock %} {% block group %} diff --git a/bricktracker/sql/part/list/missing.sql b/bricktracker/sql/part/list/missing.sql index fc64e25..8f17ae3 100644 --- a/bricktracker/sql/part/list/missing.sql +++ b/bricktracker/sql/part/list/missing.sql @@ -5,7 +5,7 @@ SUM(IFNULL("missing"."quantity", 0)) AS "total_missing", {% endblock %} {% block total_sets %} -COUNT("inventory"."u_id") - COUNT("bricktracker_minifigures"."bricktracker_set_id") AS "total_sets", +COUNT("inventory"."u_id") - COUNT("bricktracker_minifigures"."id") AS "total_sets", {% endblock %} {% block total_minifigures %} @@ -22,8 +22,8 @@ AND "missing"."element_id" IS NOT DISTINCT FROM "inventory"."element_id" AND "missing"."u_id" IS NOT DISTINCT FROM "inventory"."u_id" LEFT JOIN "bricktracker_minifigures" -ON "inventory"."set_num" IS NOT DISTINCT FROM "bricktracker_minifigures"."rebrickable_figure" -AND "inventory"."u_id" IS NOT DISTINCT FROM "bricktracker_minifigures"."bricktracker_set_id" +ON "inventory"."set_num" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure" +AND "inventory"."u_id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id" {% endblock %} {% block group %} diff --git a/bricktracker/sql/part/select/generic.sql b/bricktracker/sql/part/select/generic.sql index 28b32a9..eb7e194 100644 --- a/bricktracker/sql/part/select/generic.sql +++ b/bricktracker/sql/part/select/generic.sql @@ -22,8 +22,8 @@ AND "inventory"."element_id" IS NOT DISTINCT FROM "missing"."element_id" AND "inventory"."u_id" IS NOT DISTINCT FROM "missing"."u_id" LEFT JOIN "bricktracker_minifigures" -ON "inventory"."set_num" IS NOT DISTINCT FROM "bricktracker_minifigures"."rebrickable_figure" -AND "inventory"."u_id" IS NOT DISTINCT FROM "bricktracker_minifigures"."bricktracker_set_id" +ON "inventory"."set_num" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure" +AND "inventory"."u_id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id" {% endblock %} {% block where %} diff --git a/bricktracker/sql/set/base/base.sql b/bricktracker/sql/set/base/base.sql index 2f4d683..8b1f4c8 100644 --- a/bricktracker/sql/set/base/base.sql +++ b/bricktracker/sql/set/base/base.sql @@ -21,7 +21,7 @@ SELECT FROM "bricktracker_sets" INNER JOIN "rebrickable_sets" -ON "bricktracker_sets"."rebrickable_set" IS NOT DISTINCT FROM "rebrickable_sets"."set" +ON "bricktracker_sets"."set" IS NOT DISTINCT FROM "rebrickable_sets"."set" {% block join %}{% endblock %} diff --git a/bricktracker/sql/set/base/full.sql b/bricktracker/sql/set/base/full.sql index 68333c2..092e487 100644 --- a/bricktracker/sql/set/base/full.sql +++ b/bricktracker/sql/set/base/full.sql @@ -15,7 +15,7 @@ IFNULL("minifigures_join"."total", 0) AS "total_minifigures" {% block join %} {% if statuses %} LEFT JOIN "bricktracker_set_statuses" -ON "bricktracker_sets"."id" IS NOT DISTINCT FROM "bricktracker_set_statuses"."bricktracker_set_id" +ON "bricktracker_sets"."id" IS NOT DISTINCT FROM "bricktracker_set_statuses"."id" {% endif %} -- LEFT JOIN + SELECT to avoid messing the total @@ -32,11 +32,11 @@ ON "bricktracker_sets"."id" IS NOT DISTINCT FROM "missing_join"."u_id" -- LEFT JOIN + SELECT to avoid messing the total LEFT JOIN ( SELECT - "bricktracker_minifigures"."bricktracker_set_id", + "bricktracker_minifigures"."id", SUM("bricktracker_minifigures"."quantity") AS "total" FROM "bricktracker_minifigures" {% block where_minifigures %}{% endblock %} - GROUP BY "bricktracker_minifigures"."bricktracker_set_id" + GROUP BY "bricktracker_minifigures"."id" ) "minifigures_join" -ON "bricktracker_sets"."id" IS NOT DISTINCT FROM "minifigures_join"."bricktracker_set_id" +ON "bricktracker_sets"."id" IS NOT DISTINCT FROM "minifigures_join"."id" {% endblock %} \ No newline at end of file diff --git a/bricktracker/sql/set/base/light.sql b/bricktracker/sql/set/base/light.sql index b599a87..12df8e2 100644 --- a/bricktracker/sql/set/base/light.sql +++ b/bricktracker/sql/set/base/light.sql @@ -1,6 +1,6 @@ SELECT "bricktracker_sets"."id", - "bricktracker_sets"."rebrickable_set" AS "set" + "bricktracker_sets"."set" FROM "bricktracker_sets" {% block join %}{% endblock %} diff --git a/bricktracker/sql/set/delete/set.sql b/bricktracker/sql/set/delete/set.sql index 93a51df..b477f81 100644 --- a/bricktracker/sql/set/delete/set.sql +++ b/bricktracker/sql/set/delete/set.sql @@ -7,10 +7,10 @@ DELETE FROM "bricktracker_sets" WHERE "bricktracker_sets"."id" IS NOT DISTINCT FROM '{{ id }}'; DELETE FROM "bricktracker_set_statuses" -WHERE "bricktracker_set_statuses"."bricktracker_set_id" IS NOT DISTINCT FROM '{{ id }}'; +WHERE "bricktracker_set_statuses"."id" IS NOT DISTINCT FROM '{{ id }}'; DELETE FROM "bricktracker_minifigures" -WHERE "bricktracker_minifigures"."bricktracker_set_id" IS NOT DISTINCT FROM '{{ id }}'; +WHERE "bricktracker_minifigures"."id" IS NOT DISTINCT FROM '{{ id }}'; DELETE FROM "missing" WHERE "missing"."u_id" IS NOT DISTINCT FROM '{{ id }}'; diff --git a/bricktracker/sql/set/insert.sql b/bricktracker/sql/set/insert.sql index 2462ac5..7dd6dec 100644 --- a/bricktracker/sql/set/insert.sql +++ b/bricktracker/sql/set/insert.sql @@ -1,6 +1,6 @@ INSERT OR IGNORE INTO "bricktracker_sets" ( "id", - "rebrickable_set" + "set" ) VALUES ( :id, :set diff --git a/bricktracker/sql/set/list/generic.sql b/bricktracker/sql/set/list/generic.sql index 0177c2b..d5b2da4 100644 --- a/bricktracker/sql/set/list/generic.sql +++ b/bricktracker/sql/set/list/generic.sql @@ -2,5 +2,5 @@ {% block group %} GROUP BY - "bricktracker_sets"."rebrickable_set" + "bricktracker_sets"."set" {% endblock %} diff --git a/bricktracker/sql/set/select/full.sql b/bricktracker/sql/set/select/full.sql index a89d2c9..80d1161 100644 --- a/bricktracker/sql/set/select/full.sql +++ b/bricktracker/sql/set/select/full.sql @@ -5,7 +5,7 @@ WHERE "missing"."u_id" IS NOT DISTINCT FROM :id {% endblock %} {% block where_minifigures %} -WHERE "bricktracker_minifigures"."bricktracker_set_id" IS NOT DISTINCT FROM :id +WHERE "bricktracker_minifigures"."id" IS NOT DISTINCT FROM :id {% endblock %} {% block where %} diff --git a/bricktracker/sql/set/update/status.sql b/bricktracker/sql/set/update/status.sql index d72616e..4fc78e4 100644 --- a/bricktracker/sql/set/update/status.sql +++ b/bricktracker/sql/set/update/status.sql @@ -1,10 +1,10 @@ INSERT INTO "bricktracker_set_statuses" ( - "bricktracker_set_id", + "id", "{{name}}" ) VALUES ( :id, :status ) -ON CONFLICT("bricktracker_set_id") +ON CONFLICT("id") DO UPDATE SET "{{name}}" = :status -WHERE "bricktracker_set_statuses"."bricktracker_set_id" IS NOT DISTINCT FROM :id +WHERE "bricktracker_set_statuses"."id" IS NOT DISTINCT FROM :id