Skip to content

Commit

Permalink
atomic creation
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Dec 5, 2024
1 parent cf3dd36 commit 44a360d
Showing 1 changed file with 68 additions and 59 deletions.
127 changes: 68 additions & 59 deletions src/__tests__/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 44a360d

Please sign in to comment.