db/events: rework tags, make event_id a FK to events again, drop value_2 and value_3
This commit is contained in:
parent
4708839fd6
commit
2ff40c8fc5
|
@ -29,9 +29,7 @@ interface EventFTSRow {
|
||||||
|
|
||||||
interface TagRow {
|
interface TagRow {
|
||||||
tag: string;
|
tag: string;
|
||||||
value_1: string | null;
|
value: string;
|
||||||
value_2: string | null;
|
|
||||||
value_3: string | null;
|
|
||||||
event_id: string;
|
event_id: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,17 +34,14 @@ function insertEvent(event: Event): Promise<void> {
|
||||||
}
|
}
|
||||||
|
|
||||||
const tagCounts: Record<string, number> = {};
|
const tagCounts: Record<string, number> = {};
|
||||||
const tags = event.tags.reduce<Insertable<TagRow>[]>((results, tag) => {
|
const tags = event.tags.reduce<Insertable<TagRow>[]>((results, [name, value]) => {
|
||||||
const tagName = tag[0];
|
tagCounts[name] = (tagCounts[name] || 0) + 1;
|
||||||
tagCounts[tagName] = (tagCounts[tagName] || 0) + 1;
|
|
||||||
|
|
||||||
if (tagConditions[tagName]?.({ event, count: tagCounts[tagName] - 1 })) {
|
if (value && tagConditions[name]?.({ event, count: tagCounts[name] - 1 })) {
|
||||||
results.push({
|
results.push({
|
||||||
event_id: event.id,
|
event_id: event.id,
|
||||||
tag: tagName,
|
tag: name,
|
||||||
value_1: tag[1] || null,
|
value,
|
||||||
value_2: tag[2] || null,
|
|
||||||
value_3: tag[3] || null,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,7 +108,7 @@ function getFilterQuery(filter: DittoFilter) {
|
||||||
query = query
|
query = query
|
||||||
.leftJoin('tags', 'tags.event_id', 'events.id')
|
.leftJoin('tags', 'tags.event_id', 'events.id')
|
||||||
.where('tags.tag', '=', tag)
|
.where('tags.tag', '=', tag)
|
||||||
.where('tags.value_1', 'in', value) as typeof query;
|
.where('tags.value', 'in', value) as typeof query;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,19 +154,11 @@ async function getFilters<K extends number>(
|
||||||
/** Delete events based on filters from the database. */
|
/** Delete events based on filters from the database. */
|
||||||
function deleteFilters<K extends number>(filters: DittoFilter<K>[]) {
|
function deleteFilters<K extends number>(filters: DittoFilter<K>[]) {
|
||||||
if (!filters.length) return Promise.resolve([]);
|
if (!filters.length) return Promise.resolve([]);
|
||||||
|
const query = getFiltersQuery(filters);
|
||||||
|
|
||||||
return db.transaction().execute(async (trx) => {
|
return db.deleteFrom('events')
|
||||||
const query = getFiltersQuery(filters).clearSelect().select('id');
|
.where('id', 'in', () => query.clearSelect().select('id'))
|
||||||
|
.execute();
|
||||||
await trx.deleteFrom('tags')
|
|
||||||
.where('event_id', 'in', () => query)
|
|
||||||
.where('tag', 'not in', ['d', 'proxy'])
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
return trx.deleteFrom('events')
|
|
||||||
.where('id', 'in', () => query)
|
|
||||||
.execute();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get number of events that would be returned by filters. */
|
/** Get number of events that would be returned by filters. */
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
import { Kysely, sql } from '@/deps.ts';
|
||||||
|
|
||||||
|
export async function up(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema
|
||||||
|
.createTable('tags_new')
|
||||||
|
.addColumn('tag', 'text', (col) => col.notNull())
|
||||||
|
.addColumn('value', 'text', (col) => col.notNull())
|
||||||
|
.addColumn('event_id', 'text', (col) => col.references('events.id').onDelete('cascade'))
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await sql`
|
||||||
|
INSERT INTO tags_new (tag, value, event_id)
|
||||||
|
SELECT tag, value_1 as value, event_id
|
||||||
|
FROM tags
|
||||||
|
WHERE value_1 IS NOT NULL
|
||||||
|
`.execute(db);
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.dropTable('tags')
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.alterTable('tags_new')
|
||||||
|
.renameTo('tags').execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex('idx_tags_tag')
|
||||||
|
.on('tags')
|
||||||
|
.column('tag')
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex('idx_tags_value')
|
||||||
|
.on('tags')
|
||||||
|
.column('value')
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex('idx_tags_event_id')
|
||||||
|
.on('tags')
|
||||||
|
.column('event_id')
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema.dropTable('tags').execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createTable('tags')
|
||||||
|
.addColumn('tag', 'text', (col) => col.notNull())
|
||||||
|
.addColumn('value_1', 'text')
|
||||||
|
.addColumn('value_2', 'text')
|
||||||
|
.addColumn('value_3', 'text')
|
||||||
|
.addColumn('event_id', 'text', (col) => col.notNull())
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex('idx_tags_tag')
|
||||||
|
.on('tags')
|
||||||
|
.column('tag')
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex('idx_tags_value_1')
|
||||||
|
.on('tags')
|
||||||
|
.column('value_1')
|
||||||
|
.execute();
|
||||||
|
}
|
Loading…
Reference in New Issue