@@ -30,7 +30,25 @@ export async function deduce_category_properties(db: Client) {
3030 }
3131
3232 for ( const category of categories ) {
33+ const allowed =
34+ category . dual_category_id !== null &&
35+ category . name . toLowerCase ( ) . startsWith ( 'dual' ) // prevent circular deduction
36+
37+ if ( ! allowed ) continue
38+
3339 await deduce_dual_category_properties ( tx , category )
40+ await deduce_satisfied_category_properties (
41+ tx ,
42+ category . id ,
43+ implications ,
44+ true ,
45+ )
46+ await deduce_unsatisfied_category_properties (
47+ tx ,
48+ category . id ,
49+ implications ,
50+ true ,
51+ )
3452 }
3553
3654 await tx . commit ( )
@@ -123,6 +141,7 @@ async function deduce_satisfied_category_properties(
123141 tx : Transaction ,
124142 category_id : string ,
125143 implications : NormalizedCategoryImplication [ ] ,
144+ ignore_conflicts = false ,
126145) {
127146 const satisfied_res = await tx . execute ( {
128147 sql : `
@@ -175,13 +194,14 @@ async function deduce_satisfied_category_properties(
175194 values . push ( category_id , id , reasons [ id ] , i + 1 )
176195 }
177196
178- const insert_sql = `
179- INSERT INTO category_property_assignments (
180- category_id, property_id, is_satisfied, reason, position, is_deduced
181- )
182- VALUES
183- ${ value_fragments . join ( ',\n' ) }
184- `
197+ const insert_sql = ! ignore_conflicts
198+ ? `INSERT INTO category_property_assignments
199+ (category_id, property_id, is_satisfied, reason, position, is_deduced)
200+ VALUES ${ value_fragments . join ( ',\n' ) } `
201+ : `INSERT INTO category_property_assignments
202+ (category_id, property_id, is_satisfied, reason, position, is_deduced)
203+ VALUES ${ value_fragments . join ( ',\n' ) }
204+ ON CONFLICT (category_id, property_id) DO NOTHING`
185205
186206 await tx . execute ( { sql : insert_sql , args : values } )
187207 }
@@ -195,6 +215,7 @@ async function deduce_unsatisfied_category_properties(
195215 tx : Transaction ,
196216 category_id : string ,
197217 implications : NormalizedCategoryImplication [ ] ,
218+ ignore_conflicts = false ,
198219) {
199220 const satisfied_res = await tx . execute ( {
200221 sql : `
@@ -210,13 +231,21 @@ async function deduce_unsatisfied_category_properties(
210231 )
211232
212233 const unsatisfied_res = await tx . execute ( {
213- sql : `
234+ sql : ! ignore_conflicts
235+ ? `
214236 SELECT property_id
215237 FROM category_property_assignments
216238 WHERE
217239 category_id = ?
218240 AND is_satisfied = FALSE
219241 AND is_deduced = FALSE
242+ `
243+ : `
244+ SELECT property_id
245+ FROM category_property_assignments
246+ WHERE
247+ category_id = ?
248+ AND is_satisfied = FALSE
220249 ` ,
221250 args : [ category_id ] ,
222251 } )
@@ -278,12 +307,14 @@ async function deduce_unsatisfied_category_properties(
278307 values . push ( category_id , id , reasons [ id ] , i + 1 )
279308 }
280309
281- const insert_query = `
282- INSERT INTO category_property_assignments (
283- category_id, property_id, is_satisfied, reason, position, is_deduced
284- )
285- VALUES
286- ${ value_fragments . join ( ',\n' ) } `
310+ const insert_query = ! ignore_conflicts
311+ ? `INSERT INTO category_property_assignments
312+ (category_id, property_id, is_satisfied, reason, position, is_deduced)
313+ VALUES ${ value_fragments . join ( ',\n' ) } `
314+ : `INSERT INTO category_property_assignments
315+ (category_id, property_id, is_satisfied, reason, position, is_deduced)
316+ VALUES ${ value_fragments . join ( ',\n' ) }
317+ ON CONFLICT (category_id, property_id) DO NOTHING`
287318
288319 await tx . execute ( { sql : insert_query , args : values } )
289320 }
@@ -294,12 +325,6 @@ async function deduce_unsatisfied_category_properties(
294325}
295326
296327async function deduce_dual_category_properties ( tx : Transaction , category : CategoryMeta ) {
297- const allowed =
298- category . dual_category_id !== null &&
299- category . name . toLowerCase ( ) . startsWith ( 'dual' ) // prevent circular deduction
300-
301- if ( ! allowed ) return
302-
303328 const res = await tx . execute ( {
304329 sql : `
305330 INSERT OR REPLACE INTO category_property_assignments
@@ -318,7 +343,9 @@ async function deduce_dual_category_properties(tx: Transaction, category: Catego
318343 FROM category_property_assignments a
319344 INNER JOIN properties p ON p.id = a.property_id
320345 INNER JOIN relations r ON r.relation= p.relation
321- WHERE a.category_id = ? AND p.dual_property_id IS NOT NULL
346+ WHERE
347+ a.category_id = ?
348+ AND p.dual_property_id IS NOT NULL
322349 ORDER BY lower(p.dual_property_id)
323350 ` ,
324351 args : [ category . id , category . dual_category_id ] ,
0 commit comments