From 346c3fafc36ea024149b788a89ca0616d30745c5 Mon Sep 17 00:00:00 2001 From: Script Raccoon Date: Sat, 25 Apr 2026 12:57:55 +0200 Subject: [PATCH 1/2] order property assignments --- .../migrations/014_order_assignments.sql | 35 +++++++++++++++++++ .../scripts/deduce-category-properties.ts | 22 ++++++------ .../scripts/deduce-functor-properties.ts | 15 ++++---- src/routes/category/[id]/+page.server.ts | 2 +- src/routes/functor/[id]/+page.server.ts | 2 +- 5 files changed, 54 insertions(+), 22 deletions(-) create mode 100644 databases/catdat/migrations/014_order_assignments.sql diff --git a/databases/catdat/migrations/014_order_assignments.sql b/databases/catdat/migrations/014_order_assignments.sql new file mode 100644 index 00000000..d15ca95f --- /dev/null +++ b/databases/catdat/migrations/014_order_assignments.sql @@ -0,0 +1,35 @@ +DROP TABLE category_property_assignments; + +CREATE TABLE category_property_assignments ( + id INTEGER PRIMARY KEY, + category_id TEXT NOT NULL, + property_id TEXT NOT NULL, + is_satisfied INTEGER NOT NULL + CHECK (is_satisfied in (TRUE, FALSE)), + reason TEXT NOT NULL CHECK (length(reason) > 0), + is_deduced INTEGER NOT NULL DEFAULT FALSE + CHECK (is_deduced in (TRUE, FALSE)), + UNIQUE (category_id, property_id), + FOREIGN KEY (category_id) REFERENCES categories (id) ON DELETE CASCADE, + FOREIGN KEY (property_id) REFERENCES category_properties (id) ON DELETE CASCADE +); + +CREATE INDEX idx_property_category ON category_property_assignments (property_id); + +DROP TABLE functor_property_assignments; + +CREATE TABLE functor_property_assignments ( + id INTEGER PRIMARY KEY, + functor_id TEXT NOT NULL, + property_id TEXT NOT NULL, + is_satisfied INTEGER NOT NULL + CHECK (is_satisfied IN (TRUE, FALSE)), + reason TEXT NOT NULL CHECK (length(reason) > 0), + is_deduced INTEGER NOT NULL DEFAULT FALSE + CHECK (is_deduced in (TRUE, FALSE)), + UNIQUE (functor_id, property_id), + FOREIGN KEY (functor_id) REFERENCES functors (id) ON DELETE CASCADE, + FOREIGN KEY (property_id) REFERENCES functor_properties (id) ON DELETE CASCADE +); + +CREATE INDEX idx_property_functor ON functor_property_assignments (property_id); \ No newline at end of file diff --git a/databases/catdat/scripts/deduce-category-properties.ts b/databases/catdat/scripts/deduce-category-properties.ts index 7a7c3d88..d33259db 100644 --- a/databases/catdat/scripts/deduce-category-properties.ts +++ b/databases/catdat/scripts/deduce-category-properties.ts @@ -281,18 +281,17 @@ async function deduce_satisfied_category_properties( const value_fragments: string[] = [] const values: (string | number)[] = [] - for (let i = 0; i < deduced_satisfied_props.length; i++) { - const id = deduced_satisfied_props[i] - value_fragments.push(`(?, ?, TRUE, ?, ?, TRUE)`) - values.push(category_id, id, reasons[id], i + 1) + for (const id of deduced_satisfied_props) { + value_fragments.push(`(?, ?, TRUE, ?, TRUE)`) + values.push(category_id, id, reasons[id]) } const insert_sql = options.check_conflicts ? `INSERT INTO category_property_assignments - (category_id, property_id, is_satisfied, reason, position, is_deduced) + (category_id, property_id, is_satisfied, reason, is_deduced) VALUES ${value_fragments.join(',\n')}` : `INSERT INTO category_property_assignments - (category_id, property_id, is_satisfied, reason, position, is_deduced) + (category_id, property_id, is_satisfied, reason, is_deduced) VALUES ${value_fragments.join(',\n')} ON CONFLICT (category_id, property_id) DO NOTHING` @@ -390,18 +389,17 @@ async function deduce_unsatisfied_category_properties( const value_fragments: string[] = [] const values: (string | number)[] = [] - for (let i = 0; i < deduced_unsatisfied_props.length; i++) { - const id = deduced_unsatisfied_props[i] - value_fragments.push('(?, ?, FALSE, ?, ?, TRUE)') - values.push(category_id, id, reasons[id], i + 1) + for (const id of deduced_unsatisfied_props) { + value_fragments.push('(?, ?, FALSE, ?, TRUE)') + values.push(category_id, id, reasons[id]) } const insert_query = options.check_conflicts ? `INSERT INTO category_property_assignments - (category_id, property_id, is_satisfied, reason, position, is_deduced) + (category_id, property_id, is_satisfied, reason, is_deduced) VALUES ${value_fragments.join(',\n')}` : `INSERT INTO category_property_assignments - (category_id, property_id, is_satisfied, reason, position, is_deduced) + (category_id, property_id, is_satisfied, reason, is_deduced) VALUES ${value_fragments.join(',\n')} ON CONFLICT (category_id, property_id) DO NOTHING` diff --git a/databases/catdat/scripts/deduce-functor-properties.ts b/databases/catdat/scripts/deduce-functor-properties.ts index 656e2443..ef0c4c5a 100644 --- a/databases/catdat/scripts/deduce-functor-properties.ts +++ b/databases/catdat/scripts/deduce-functor-properties.ts @@ -270,15 +270,14 @@ async function deduce_satisfied_functor_properties( const value_fragments: string[] = [] const values: (string | number)[] = [] - for (let i = 0; i < deduced_satisfied_props.length; i++) { - const id = deduced_satisfied_props[i] - value_fragments.push(`(?, ?, TRUE, ?, ?, TRUE)`) - values.push(functor.id, id, reasons[id], i + 1) + for (const id of deduced_satisfied_props) { + value_fragments.push(`(?, ?, TRUE, ?, TRUE)`) + values.push(functor.id, id, reasons[id]) } const insert_sql = ` INSERT INTO functor_property_assignments ( - functor_id, property_id, is_satisfied, reason, position, is_deduced + functor_id, property_id, is_satisfied, reason, is_deduced ) VALUES ${value_fragments.join(',\n')} @@ -361,13 +360,13 @@ async function deduce_unsatisfied_functor_properties( for (let i = 0; i < deduced_unsatisfied_props.length; i++) { const id = deduced_unsatisfied_props[i] - value_fragments.push('(?, ?, FALSE, ?, ?, TRUE)') - values.push(functor.id, id, reasons[id], i + 1) + value_fragments.push('(?, ?, FALSE, ?, TRUE)') + values.push(functor.id, id, reasons[id]) } const insert_query = ` INSERT INTO functor_property_assignments ( - functor_id, property_id, is_satisfied, reason, position, is_deduced + functor_id, property_id, is_satisfied, reason, is_deduced ) VALUES ${value_fragments.join(',\n')}` diff --git a/src/routes/category/[id]/+page.server.ts b/src/routes/category/[id]/+page.server.ts index 650c6a4e..d3d0d825 100644 --- a/src/routes/category/[id]/+page.server.ts +++ b/src/routes/category/[id]/+page.server.ts @@ -82,7 +82,7 @@ export const load = async (event) => { INNER JOIN category_properties p ON p.id = cp.property_id INNER JOIN relations r ON r.relation = p.relation WHERE cp.category_id = ${id} - ORDER BY cp.position, lower(cp.property_id) + ORDER BY cp.id `, // unknown properties sql` diff --git a/src/routes/functor/[id]/+page.server.ts b/src/routes/functor/[id]/+page.server.ts index 51641b2f..fe9bfb76 100644 --- a/src/routes/functor/[id]/+page.server.ts +++ b/src/routes/functor/[id]/+page.server.ts @@ -51,7 +51,7 @@ export const load = async (event) => { INNER JOIN functor_properties p ON p.id = fp.property_id INNER JOIN relations r ON r.relation = p.relation WHERE fp.functor_id = ${id} - ORDER BY fp.position, lower(fp.property_id) + ORDER BY fp.id `, // unknown properties sql` From 835fd107861366898c8dba57b978182b7ff5d5e5 Mon Sep 17 00:00:00 2001 From: Script Raccoon Date: Sat, 25 Apr 2026 13:41:38 +0200 Subject: [PATCH 2/2] small refactor --- .../scripts/deduce-category-properties.ts | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/databases/catdat/scripts/deduce-category-properties.ts b/databases/catdat/scripts/deduce-category-properties.ts index d33259db..e8757771 100644 --- a/databases/catdat/scripts/deduce-category-properties.ts +++ b/databases/catdat/scripts/deduce-category-properties.ts @@ -286,14 +286,16 @@ async function deduce_satisfied_category_properties( values.push(category_id, id, reasons[id]) } - const insert_sql = options.check_conflicts - ? `INSERT INTO category_property_assignments - (category_id, property_id, is_satisfied, reason, is_deduced) - VALUES ${value_fragments.join(',\n')}` - : `INSERT INTO category_property_assignments + const conflict_clause = options.check_conflicts + ? '' + : 'ON CONFLICT (category_id, property_id) DO NOTHING' + + const insert_sql = ` + INSERT INTO category_property_assignments (category_id, property_id, is_satisfied, reason, is_deduced) - VALUES ${value_fragments.join(',\n')} - ON CONFLICT (category_id, property_id) DO NOTHING` + VALUES ${value_fragments.join(',\n')} + ${conflict_clause} + ` try { await tx.execute({ sql: insert_sql, args: values }) @@ -394,14 +396,16 @@ async function deduce_unsatisfied_category_properties( values.push(category_id, id, reasons[id]) } - const insert_query = options.check_conflicts - ? `INSERT INTO category_property_assignments - (category_id, property_id, is_satisfied, reason, is_deduced) - VALUES ${value_fragments.join(',\n')}` - : `INSERT INTO category_property_assignments + const conflict_clause = options.check_conflicts + ? '' + : 'ON CONFLICT (category_id, property_id) DO NOTHING' + + const insert_query = ` + INSERT INTO category_property_assignments (category_id, property_id, is_satisfied, reason, is_deduced) - VALUES ${value_fragments.join(',\n')} - ON CONFLICT (category_id, property_id) DO NOTHING` + VALUES ${value_fragments.join(',\n')} + ${conflict_clause} + ` try { await tx.execute({ sql: insert_query, args: values })