Skip to content

Commit

Permalink
Server: Add unique constraint on name and owner ID of items table
Browse files Browse the repository at this point in the history
In theory, the server enforces uniquness because when doing a PUT
operation it either creates the items if it doesn't exist, or overwrite
it. However, there's race condition that makes it possible for multiple
items with the same name being created per user. So we add this
constraint to ensure that any additional query would fail (which can be
recovered by repeating the request).
  • Loading branch information
laurent22 committed Oct 30, 2021
1 parent bb06e56 commit f7a18ba
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Knex } from 'knex';
import { DbConnection } from '../db';

export async function up(db: DbConnection): Promise<any> {
await db.schema.alterTable('items', (table: Knex.CreateTableBuilder) => {
table.unique(['name', 'owner_id']);
});
}

export async function down(db: DbConnection): Promise<any> {
await db.schema.alterTable('items', (table: Knex.CreateTableBuilder) => {
table.dropUnique(['name', 'owner_id']);
});
}

0 comments on commit f7a18ba

Please sign in to comment.