Skip to content

Commit

Permalink
add back in schema check removed by accident
Browse files Browse the repository at this point in the history
  • Loading branch information
augchan42 committed Dec 3, 2024
1 parent c2521de commit 418eb26
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions packages/adapter-postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,33 @@ export class PostgresDatabaseAdapter
async init() {
await this.testConnection();

const schema = fs.readFileSync(
path.resolve(__dirname, "../schema.sql"),
"utf8"
);
const client = await this.pool.connect();
try {
await client.query("BEGIN");

// Check if schema already exists (check for a core table)
const { rows } = await client.query(`
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_name = 'rooms'
);
`);

await this.query(schema);
if (!rows[0].exists) {
const schema = fs.readFileSync(
path.resolve(__dirname, "../schema.sql"),
"utf8"
);
await client.query(schema);
}

await client.query("COMMIT");
} catch (error) {
await client.query("ROLLBACK");
throw error;
} finally {
client.release();
}
}

async close() {
Expand Down

0 comments on commit 418eb26

Please sign in to comment.