From 44a360d97020daf925c6a9b7a18c5a337ef6f301 Mon Sep 17 00:00:00 2001 From: 0age <37939117+0age@users.noreply.github.com> Date: Wed, 4 Dec 2024 20:13:05 -0800 Subject: [PATCH] atomic creation --- src/__tests__/setup.ts | 127 ++++++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 59 deletions(-) diff --git a/src/__tests__/setup.ts b/src/__tests__/setup.ts index 8e461b7..8e5f93a 100644 --- a/src/__tests__/setup.ts +++ b/src/__tests__/setup.ts @@ -30,65 +30,74 @@ class DatabaseManager { this.db = new PGlite('memory://'); await this.db.ready; - // Create sessions table - await this.db.query(` - CREATE TABLE IF NOT EXISTS sessions ( - id TEXT PRIMARY KEY, - address TEXT NOT NULL, - nonce TEXT NOT NULL, - created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, - expires_at TIMESTAMP WITH TIME ZONE NOT NULL, - domain TEXT NOT NULL - ) - `); - - // Create nonces table - await this.db.query(` - CREATE TABLE IF NOT EXISTS nonces ( - id TEXT PRIMARY KEY, - chain_id TEXT NOT NULL, - nonce TEXT NOT NULL, - consumed_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, - UNIQUE(chain_id, nonce) - ) - `); - - // Create compacts table - await this.db.query(` - CREATE TABLE IF NOT EXISTS compacts ( - id TEXT PRIMARY KEY, - chain_id TEXT NOT NULL, - claim_hash TEXT NOT NULL, - arbiter TEXT NOT NULL, - sponsor TEXT NOT NULL, - nonce TEXT NOT NULL, - expires BIGINT NOT NULL, - compact_id TEXT NOT NULL, - amount TEXT NOT NULL, - witness_type_string TEXT, - witness_hash TEXT, - signature TEXT NOT NULL, - created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, - UNIQUE(chain_id, claim_hash) - ) - `); - - // Create indexes - await this.db.query( - 'CREATE INDEX IF NOT EXISTS idx_sessions_address ON sessions(address)' - ); - await this.db.query( - 'CREATE INDEX IF NOT EXISTS idx_sessions_expires_at ON sessions(expires_at)' - ); - await this.db.query( - 'CREATE INDEX IF NOT EXISTS idx_compacts_sponsor ON compacts(sponsor)' - ); - await this.db.query( - 'CREATE INDEX IF NOT EXISTS idx_compacts_chain_claim ON compacts(chain_id, claim_hash)' - ); - await this.db.query( - 'CREATE INDEX IF NOT EXISTS idx_nonces_chain_nonce ON nonces(chain_id, nonce)' - ); + // Wrap table creation in a transaction + await this.db.query('BEGIN'); + try { + // Create sessions table + await this.db.query(` + CREATE TABLE IF NOT EXISTS sessions ( + id TEXT PRIMARY KEY, + address TEXT NOT NULL, + nonce TEXT NOT NULL, + created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, + expires_at TIMESTAMP WITH TIME ZONE NOT NULL, + domain TEXT NOT NULL + ) + `); + + // Create nonces table + await this.db.query(` + CREATE TABLE IF NOT EXISTS nonces ( + id TEXT PRIMARY KEY, + chain_id TEXT NOT NULL, + nonce TEXT NOT NULL, + consumed_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, + UNIQUE(chain_id, nonce) + ) + `); + + // Create compacts table + await this.db.query(` + CREATE TABLE IF NOT EXISTS compacts ( + id TEXT PRIMARY KEY, + chain_id TEXT NOT NULL, + claim_hash TEXT NOT NULL, + arbiter TEXT NOT NULL, + sponsor TEXT NOT NULL, + nonce TEXT NOT NULL, + expires BIGINT NOT NULL, + compact_id TEXT NOT NULL, + amount TEXT NOT NULL, + witness_type_string TEXT, + witness_hash TEXT, + signature TEXT NOT NULL, + created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, + UNIQUE(chain_id, claim_hash) + ) + `); + + // Create indexes + await this.db.query( + 'CREATE INDEX IF NOT EXISTS idx_sessions_address ON sessions(address)' + ); + await this.db.query( + 'CREATE INDEX IF NOT EXISTS idx_sessions_expires_at ON sessions(expires_at)' + ); + await this.db.query( + 'CREATE INDEX IF NOT EXISTS idx_compacts_sponsor ON compacts(sponsor)' + ); + await this.db.query( + 'CREATE INDEX IF NOT EXISTS idx_compacts_chain_claim ON compacts(chain_id, claim_hash)' + ); + await this.db.query( + 'CREATE INDEX IF NOT EXISTS idx_nonces_chain_nonce ON nonces(chain_id, nonce)' + ); + + await this.db.query('COMMIT'); + } catch (error) { + await this.db.query('ROLLBACK'); + throw error; + } } return; }