use entity_id instead of entity_ref

Signed-off-by: Kiss Miklos <miklos@roadie.io>
This commit is contained in:
Kiss Miklos
2022-07-05 14:39:10 +02:00
parent 61aca33a25
commit e75bde1342
5 changed files with 22 additions and 24 deletions
@@ -23,9 +23,9 @@ exports.up = async function up(knex) {
'This table contains relations between entities and keys to trigger refreshes with',
);
table
.text('entity_ref')
.text('entity_id')
.notNullable()
.references('entity_ref')
.references('entity_id')
.inTable('refresh_state')
.onDelete('CASCADE')
.comment('A reference to the entity that the refresh key is tied to');
@@ -35,8 +35,7 @@ exports.up = async function up(knex) {
.comment(
'A reference to a key which should be used to trigger a refresh on this entity',
);
table.unique(['entity_ref', 'key']);
table.index('entity_ref', 'refresh_keys_entity_ref_idx');
table.index('entity_id', 'refresh_keys_entity_id_idx');
table.index('key', 'refresh_keys_key_idx');
});
};
@@ -46,7 +45,7 @@ exports.up = async function up(knex) {
*/
exports.down = async function down(knex) {
await knex.schema.alterTable('refresh_keys', table => {
table.dropIndex([], 'refresh_keys_entity_ref_idx');
table.dropIndex([], 'refresh_keys_entity_id_idx');
table.dropIndex([], 'refresh_keys_key_idx');
});
@@ -522,7 +522,7 @@ describe('Default Processing Database', () => {
);
const refreshKeys = await knex<DbRefreshKeysRow>('refresh_keys')
.where({ entity_ref: stringifyEntityRef(processedEntity) })
.where({ entity_id: id })
.select();
expect(refreshKeys[0]).toEqual({
@@ -79,6 +79,7 @@ export class DefaultProcessingDatabase implements ProcessingDatabase {
errors,
relations,
deferredEntities,
refreshKeys,
locationKey,
} = options;
const refreshResult = await tx<DbRefreshStateRow>('refresh_state')
@@ -141,17 +142,19 @@ export class DefaultProcessingDatabase implements ProcessingDatabase {
BATCH_SIZE,
);
// Delete old refresh keys
await tx<DbRefreshKeysRow>('refresh_keys')
.where({ entity_id: id })
.delete();
// Insert the refresh keys for the processed entity
await Promise.all(
options.refreshKeys.map(k => {
return tx<DbRefreshKeysRow>('refresh_keys')
.insert({
entity_ref: sourceEntityRef,
key: k.key,
})
.onConflict(['entity_ref', 'key'])
.ignore();
}),
await tx.batchInsert(
'refresh_keys',
refreshKeys.map(k => ({
entity_id: id,
key: k.key,
})),
BATCH_SIZE,
);
return {
@@ -540,18 +543,18 @@ export class DefaultProcessingDatabase implements ProcessingDatabase {
const { keys } = options;
const updateResult = await tx<DbRefreshStateRow>('refresh_state')
.whereIn('entity_ref', function selectEntityRefs(tx2) {
.whereIn('entity_id', function selectEntityRefs(tx2) {
tx2
.whereIn('key', keys)
.select({
entity_ref: 'refresh_keys.entity_ref',
entity_id: 'refresh_keys.entity_id',
})
.from('refresh_keys');
})
.update({ next_update_at: tx.fn.now() });
if (updateResult === 0) {
throw new NotFoundError(
this.options.logger.info(
`Failed to schedule ${JSON.stringify(keys)} for keys`,
);
}
@@ -44,7 +44,7 @@ export type DbRefreshStateRow = {
};
export type DbRefreshKeysRow = {
entity_ref: string;
entity_id: string;
key: string;
};
@@ -82,10 +82,6 @@ export type ReplaceUnprocessedEntitiesOptions =
type: 'delta';
};
export type RefreshKeyOptions = {
refreshKeys: RefreshKeyData[];
};
export type RefreshByKeyOptions = {
keys: string[];
};