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..e8757771 100644 --- a/databases/catdat/scripts/deduce-category-properties.ts +++ b/databases/catdat/scripts/deduce-category-properties.ts @@ -281,20 +281,21 @@ 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) - VALUES ${value_fragments.join(',\n')}` - : `INSERT INTO category_property_assignments - (category_id, property_id, is_satisfied, reason, position, is_deduced) - VALUES ${value_fragments.join(',\n')} - ON CONFLICT (category_id, property_id) DO NOTHING` + 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')} + ${conflict_clause} + ` try { await tx.execute({ sql: insert_sql, args: values }) @@ -390,20 +391,21 @@ 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) - VALUES ${value_fragments.join(',\n')}` - : `INSERT INTO category_property_assignments - (category_id, property_id, is_satisfied, reason, position, is_deduced) - VALUES ${value_fragments.join(',\n')} - ON CONFLICT (category_id, property_id) DO NOTHING` + 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')} + ${conflict_clause} + ` try { await tx.execute({ sql: insert_query, args: values }) 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`