Simplify fields name in the database
This commit is contained in:
parent
0beb1147b9
commit
cf6d5f43c0
27
bricktracker/migrations/0007.py
Normal file
27
bricktracker/migrations/0007.py
Normal file
@ -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
|
||||
])
|
||||
}
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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;
|
@ -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;
|
32
bricktracker/sql/migrations/0009.sql
Normal file
32
bricktracker/sql/migrations/0009.sql
Normal file
@ -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;
|
@ -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 %}
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
INSERT INTO "bricktracker_minifigures" (
|
||||
"bricktracker_set_id",
|
||||
"rebrickable_figure",
|
||||
"id",
|
||||
"figure",
|
||||
"quantity"
|
||||
) VALUES (
|
||||
:bricktracker_set_id,
|
||||
:id,
|
||||
:figure,
|
||||
:quantity
|
||||
)
|
||||
|
@ -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 %}
|
||||
|
||||
|
@ -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 %}
|
||||
|
@ -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 %}
|
||||
|
@ -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 %}
|
||||
|
@ -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 %}
|
||||
|
@ -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 %}
|
||||
|
@ -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 %}
|
||||
|
@ -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 %}
|
||||
|
@ -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 %}
|
||||
|
@ -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 %}
|
||||
|
||||
|
@ -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 %}
|
@ -1,6 +1,6 @@
|
||||
SELECT
|
||||
"bricktracker_sets"."id",
|
||||
"bricktracker_sets"."rebrickable_set" AS "set"
|
||||
"bricktracker_sets"."set"
|
||||
FROM "bricktracker_sets"
|
||||
|
||||
{% block join %}{% endblock %}
|
||||
|
@ -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 }}';
|
||||
|
@ -1,6 +1,6 @@
|
||||
INSERT OR IGNORE INTO "bricktracker_sets" (
|
||||
"id",
|
||||
"rebrickable_set"
|
||||
"set"
|
||||
) VALUES (
|
||||
:id,
|
||||
:set
|
||||
|
@ -2,5 +2,5 @@
|
||||
|
||||
{% block group %}
|
||||
GROUP BY
|
||||
"bricktracker_sets"."rebrickable_set"
|
||||
"bricktracker_sets"."set"
|
||||
{% endblock %}
|
||||
|
@ -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 %}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user