File tree 1 file changed +26
-5
lines changed
packages/adapter-postgres/src
1 file changed +26
-5
lines changed Original file line number Diff line number Diff line change @@ -185,12 +185,33 @@ export class PostgresDatabaseAdapter
185
185
async init ( ) {
186
186
await this . testConnection ( ) ;
187
187
188
- const schema = fs . readFileSync (
189
- path . resolve ( __dirname , "../schema.sql" ) ,
190
- "utf8"
191
- ) ;
188
+ const client = await this . pool . connect ( ) ;
189
+ try {
190
+ await client . query ( "BEGIN" ) ;
191
+
192
+ // Check if schema already exists (check for a core table)
193
+ const { rows } = await client . query ( `
194
+ SELECT EXISTS (
195
+ SELECT FROM information_schema.tables
196
+ WHERE table_name = 'rooms'
197
+ );
198
+ ` ) ;
192
199
193
- await this . query ( schema ) ;
200
+ if ( ! rows [ 0 ] . exists ) {
201
+ const schema = fs . readFileSync (
202
+ path . resolve ( __dirname , "../schema.sql" ) ,
203
+ "utf8"
204
+ ) ;
205
+ await client . query ( schema ) ;
206
+ }
207
+
208
+ await client . query ( "COMMIT" ) ;
209
+ } catch ( error ) {
210
+ await client . query ( "ROLLBACK" ) ;
211
+ throw error ;
212
+ } finally {
213
+ client . release ( ) ;
214
+ }
194
215
}
195
216
196
217
async close ( ) {
You can’t perform that action at this time.
0 commit comments