File tree Expand file tree Collapse file tree 1 file changed +17
-8
lines changed
packages/verrou/src/drivers Expand file tree Collapse file tree 1 file changed +17
-8
lines changed Original file line number Diff line number Diff line change @@ -32,14 +32,23 @@ export class KnexAdapter implements DatabaseAdapter {
3232 }
3333
3434 async createTableIfNotExists ( ) {
35- const hasTable = await this . #connection. schema . hasTable ( this . #tableName)
36- if ( hasTable ) return
37-
38- await this . #connection. schema . createTable ( this . #tableName, ( table ) => {
39- table . string ( 'key' , 255 ) . notNullable ( ) . primary ( )
40- table . string ( 'owner' ) . notNullable ( )
41- table . bigint ( 'expiration' ) . unsigned ( ) . nullable ( )
42- } )
35+ try {
36+ await this . #connection. schema . createTable ( this . #tableName, ( table ) => {
37+ table . string ( 'key' , 255 ) . notNullable ( ) . primary ( )
38+ table . string ( 'owner' ) . notNullable ( )
39+ table . bigint ( 'expiration' ) . unsigned ( ) . nullable ( )
40+ } )
41+ } catch {
42+ /**
43+ * If table creation fails, verify the table actually exists.
44+ * This handles weird race conditions where multiple instances try to create
45+ * the table simultaneously
46+ *
47+ * See https://github.com/Julien-R44/verrou/pull/15
48+ */
49+ const hasTable = await this . #connection. schema . hasTable ( this . #tableName)
50+ if ( ! hasTable ) throw new Error ( `Failed to create table "${ this . #tableName} "` )
51+ }
4352 }
4453
4554 async insertLock ( lock : { key : string ; owner : string ; expiration : number | null } ) {
You can’t perform that action at this time.
0 commit comments