Simplify fields name in the database

This commit is contained in:
Gregoo 2025-01-27 23:07:10 +01:00
parent 0beb1147b9
commit cf6d5f43c0
25 changed files with 159 additions and 80 deletions

View 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
])
}

View File

@ -134,7 +134,7 @@ class BrickMinifigureList(BrickRecordList[BrickMinifigure]):
parameters: dict[str, Any] = super().sql_parameters() parameters: dict[str, Any] = super().sql_parameters()
if self.brickset is not None: if self.brickset is not None:
parameters['bricktracker_set_id'] = self.brickset.fields.id parameters['id'] = self.brickset.fields.id
return parameters return parameters

View File

@ -75,9 +75,8 @@ class RebrickableMinifigure(BrickRecord):
parameters = super().sql_parameters() parameters = super().sql_parameters()
# Supplement from the brickset # Supplement from the brickset
if self.brickset is not None: if self.brickset is not None and 'id' not in parameters:
if 'bricktracker_set_id' not in parameters: parameters['id'] = self.brickset.fields.id
parameters['bricktracker_set_id'] = self.brickset.fields.id
return parameters return parameters

View File

@ -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; BEGIN TRANSACTION;
-- Create a Rebrickable minifigures table: each unique minifigure imported from Rebrickable -- Rename sets table
CREATE TABLE "rebrickable_minifigures" ( ALTER TABLE "bricktracker_sets" RENAME TO "bricktracker_sets_old";
"figure" TEXT NOT NULL,
"number" INTEGER NOT NULL, -- Re-Create a Bricktable set table with the simplified name
"name" TEXT NOT NULL, CREATE TABLE "bricktracker_sets" (
"image" TEXT, "id" TEXT NOT NULL,
PRIMARY KEY("figure") "set" TEXT NOT NULL,
PRIMARY KEY("id"),
FOREIGN KEY("set") REFERENCES "rebrickable_sets"("set")
); );
-- Insert existing sets into the new table -- Insert existing sets into the new table
INSERT INTO "rebrickable_minifigures" ( INSERT INTO "bricktracker_sets" (
"figure", "id",
"number", "set"
"name",
"image"
) )
SELECT SELECT
"minifigures"."fig_num", "bricktracker_sets_old"."id",
CAST(SUBSTR("minifigures"."fig_num", 5) AS INTEGER), "bricktracker_sets_old"."rebrickable_set"
"minifigures"."name", FROM "bricktracker_sets_old";
"minifigures"."set_img_url"
FROM "minifigures" -- Rename status table
GROUP BY ALTER TABLE "bricktracker_set_statuses" RENAME TO "bricktracker_set_statuses_old";
"minifigures"."fig_num";
-- 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; COMMIT;

View File

@ -1,32 +1,30 @@
-- description: Migrate the Bricktracker minifigures -- description: Creation of the deduplicated table of Rebrickable minifigures
PRAGMA foreign_keys = ON;
BEGIN TRANSACTION; BEGIN TRANSACTION;
-- Create a Bricktable minifigures table: an amount of minifigures linked to a Bricktracker set -- Create a Rebrickable minifigures table: each unique minifigure imported from Rebrickable
CREATE TABLE "bricktracker_minifigures" ( CREATE TABLE "rebrickable_minifigures" (
"bricktracker_set_id" TEXT NOT NULL, "figure" TEXT NOT NULL,
"rebrickable_figure" TEXT NOT NULL, "number" INTEGER NOT NULL,
"quantity" INTEGER NOT NULL, "name" TEXT NOT NULL,
PRIMARY KEY("bricktracker_set_id", "rebrickable_figure"), "image" TEXT,
FOREIGN KEY("bricktracker_set_id") REFERENCES "bricktracker_sets"("id"), PRIMARY KEY("figure")
FOREIGN KEY("rebrickable_figure") REFERENCES "rebrickable_minifigures"("figure")
); );
-- Insert existing sets into the new table -- Insert existing sets into the new table
INSERT INTO "bricktracker_minifigures" ( INSERT INTO "rebrickable_minifigures" (
"bricktracker_set_id", "figure",
"rebrickable_figure", "number",
"quantity" "name",
"image"
) )
SELECT SELECT
"minifigures"."u_id",
"minifigures"."fig_num", "minifigures"."fig_num",
"minifigures"."quantity" CAST(SUBSTR("minifigures"."fig_num", 5) AS INTEGER),
FROM "minifigures"; "minifigures"."name",
"minifigures"."set_img_url"
-- Rename the original table (don't delete it yet?) FROM "minifigures"
ALTER TABLE "minifigures" RENAME TO "minifigures_old"; GROUP BY
"minifigures"."fig_num";
COMMIT; COMMIT;

View 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;

View File

@ -17,7 +17,7 @@ SELECT
FROM "bricktracker_minifigures" FROM "bricktracker_minifigures"
INNER JOIN "rebrickable_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 %} {% block join %}{% endblock %}

View File

@ -1,9 +1,9 @@
INSERT INTO "bricktracker_minifigures" ( INSERT INTO "bricktracker_minifigures" (
"bricktracker_set_id", "id",
"rebrickable_figure", "figure",
"quantity" "quantity"
) VALUES ( ) VALUES (
:bricktracker_set_id, :id,
:figure, :figure,
:quantity :quantity
) )

View File

@ -9,7 +9,7 @@ SUM(IFNULL("bricktracker_minifigures"."quantity", 0)) AS "total_quantity",
{% endblock %} {% endblock %}
{% block total_sets %} {% block total_sets %}
COUNT("bricktracker_minifigures"."bricktracker_set_id") AS "total_sets" COUNT("bricktracker_minifigures"."id") AS "total_sets"
{% endblock %} {% endblock %}
{% block join %} {% block join %}
@ -24,7 +24,7 @@ LEFT JOIN (
"missing"."set_num", "missing"."set_num",
"missing"."u_id" "missing"."u_id"
) missing_join ) 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" AND "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "missing_join"."set_num"
{% endblock %} {% endblock %}

View File

@ -1,5 +1,5 @@
{% extends 'minifigure/base/base.sql' %} {% extends 'minifigure/base/base.sql' %}
{% block where %} {% 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 %} {% endblock %}

View File

@ -7,11 +7,11 @@ SUM(IFNULL("missing"."quantity", 0)) AS "total_missing",
{% block join %} {% block join %}
LEFT JOIN "missing" LEFT JOIN "missing"
ON "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "missing"."set_num" 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 %} {% endblock %}
{% block group %} {% block group %}
GROUP BY GROUP BY
"rebrickable_minifigures"."figure", "rebrickable_minifigures"."figure",
"bricktracker_minifigures"."bricktracker_set_id" "bricktracker_minifigures"."id"
{% endblock %} {% endblock %}

View File

@ -7,7 +7,7 @@ SUM(IFNULL("missing"."quantity", 0)) AS "total_missing",
{% block join %} {% block join %}
LEFT JOIN "missing" LEFT JOIN "missing"
ON "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "missing"."set_num" 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 %} {% endblock %}
{% block where %} {% block where %}

View File

@ -9,13 +9,13 @@ SUM(IFNULL("bricktracker_minifigures"."quantity", 0)) AS "total_quantity",
{% endblock %} {% endblock %}
{% block total_sets %} {% block total_sets %}
COUNT(DISTINCT "bricktracker_minifigures"."bricktracker_set_id") AS "total_sets" COUNT(DISTINCT "bricktracker_minifigures"."id") AS "total_sets"
{% endblock %} {% endblock %}
{% block join %} {% block join %}
LEFT JOIN "missing" LEFT JOIN "missing"
ON "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "missing"."set_num" 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 %} {% endblock %}
{% block where %} {% block where %}

View File

@ -2,5 +2,5 @@
{% block where %} {% block where %}
WHERE "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM :figure 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 %} {% endblock %}

View File

@ -9,7 +9,7 @@ SUM("inventory"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)) A
{% endblock %} {% endblock %}
{% block total_sets %} {% block total_sets %}
COUNT(DISTINCT "bricktracker_minifigures"."bricktracker_set_id") AS "total_sets", COUNT(DISTINCT "bricktracker_minifigures"."id") AS "total_sets",
{% endblock %} {% endblock %}
{% block total_minifigures %} {% 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" AND "inventory"."u_id" IS NOT DISTINCT FROM "missing"."u_id"
LEFT JOIN "bricktracker_minifigures" LEFT JOIN "bricktracker_minifigures"
ON "inventory"."set_num" IS NOT DISTINCT FROM "bricktracker_minifigures"."rebrickable_figure" ON "inventory"."set_num" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure"
AND "inventory"."u_id" IS NOT DISTINCT FROM "bricktracker_minifigures"."bricktracker_set_id" AND "inventory"."u_id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id"
{% endblock %} {% endblock %}
{% block group %} {% block group %}

View File

@ -5,7 +5,7 @@ SUM(IFNULL("missing"."quantity", 0)) AS "total_missing",
{% endblock %} {% endblock %}
{% block total_sets %} {% 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 %} {% endblock %}
{% block total_minifigures %} {% 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" AND "missing"."u_id" IS NOT DISTINCT FROM "inventory"."u_id"
LEFT JOIN "bricktracker_minifigures" LEFT JOIN "bricktracker_minifigures"
ON "inventory"."set_num" IS NOT DISTINCT FROM "bricktracker_minifigures"."rebrickable_figure" ON "inventory"."set_num" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure"
AND "inventory"."u_id" IS NOT DISTINCT FROM "bricktracker_minifigures"."bricktracker_set_id" AND "inventory"."u_id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id"
{% endblock %} {% endblock %}
{% block group %} {% block group %}

View File

@ -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" AND "inventory"."u_id" IS NOT DISTINCT FROM "missing"."u_id"
LEFT JOIN "bricktracker_minifigures" LEFT JOIN "bricktracker_minifigures"
ON "inventory"."set_num" IS NOT DISTINCT FROM "bricktracker_minifigures"."rebrickable_figure" ON "inventory"."set_num" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure"
AND "inventory"."u_id" IS NOT DISTINCT FROM "bricktracker_minifigures"."bricktracker_set_id" AND "inventory"."u_id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id"
{% endblock %} {% endblock %}
{% block where %} {% block where %}

View File

@ -21,7 +21,7 @@ SELECT
FROM "bricktracker_sets" FROM "bricktracker_sets"
INNER JOIN "rebrickable_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 %} {% block join %}{% endblock %}

View File

@ -15,7 +15,7 @@ IFNULL("minifigures_join"."total", 0) AS "total_minifigures"
{% block join %} {% block join %}
{% if statuses %} {% if statuses %}
LEFT JOIN "bricktracker_set_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 %} {% endif %}
-- LEFT JOIN + SELECT to avoid messing the total -- 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 to avoid messing the total
LEFT JOIN ( LEFT JOIN (
SELECT SELECT
"bricktracker_minifigures"."bricktracker_set_id", "bricktracker_minifigures"."id",
SUM("bricktracker_minifigures"."quantity") AS "total" SUM("bricktracker_minifigures"."quantity") AS "total"
FROM "bricktracker_minifigures" FROM "bricktracker_minifigures"
{% block where_minifigures %}{% endblock %} {% block where_minifigures %}{% endblock %}
GROUP BY "bricktracker_minifigures"."bricktracker_set_id" GROUP BY "bricktracker_minifigures"."id"
) "minifigures_join" ) "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 %} {% endblock %}

View File

@ -1,6 +1,6 @@
SELECT SELECT
"bricktracker_sets"."id", "bricktracker_sets"."id",
"bricktracker_sets"."rebrickable_set" AS "set" "bricktracker_sets"."set"
FROM "bricktracker_sets" FROM "bricktracker_sets"
{% block join %}{% endblock %} {% block join %}{% endblock %}

View File

@ -7,10 +7,10 @@ DELETE FROM "bricktracker_sets"
WHERE "bricktracker_sets"."id" IS NOT DISTINCT FROM '{{ id }}'; WHERE "bricktracker_sets"."id" IS NOT DISTINCT FROM '{{ id }}';
DELETE FROM "bricktracker_set_statuses" 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" 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" DELETE FROM "missing"
WHERE "missing"."u_id" IS NOT DISTINCT FROM '{{ id }}'; WHERE "missing"."u_id" IS NOT DISTINCT FROM '{{ id }}';

View File

@ -1,6 +1,6 @@
INSERT OR IGNORE INTO "bricktracker_sets" ( INSERT OR IGNORE INTO "bricktracker_sets" (
"id", "id",
"rebrickable_set" "set"
) VALUES ( ) VALUES (
:id, :id,
:set :set

View File

@ -2,5 +2,5 @@
{% block group %} {% block group %}
GROUP BY GROUP BY
"bricktracker_sets"."rebrickable_set" "bricktracker_sets"."set"
{% endblock %} {% endblock %}

View File

@ -5,7 +5,7 @@ WHERE "missing"."u_id" IS NOT DISTINCT FROM :id
{% endblock %} {% endblock %}
{% block where_minifigures %} {% block where_minifigures %}
WHERE "bricktracker_minifigures"."bricktracker_set_id" IS NOT DISTINCT FROM :id WHERE "bricktracker_minifigures"."id" IS NOT DISTINCT FROM :id
{% endblock %} {% endblock %}
{% block where %} {% block where %}

View File

@ -1,10 +1,10 @@
INSERT INTO "bricktracker_set_statuses" ( INSERT INTO "bricktracker_set_statuses" (
"bricktracker_set_id", "id",
"{{name}}" "{{name}}"
) VALUES ( ) VALUES (
:id, :id,
:status :status
) )
ON CONFLICT("bricktracker_set_id") ON CONFLICT("id")
DO UPDATE SET "{{name}}" = :status 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