@@ -4,6 +4,7 @@ import type {
4
4
MessageQueueListenOptions ,
5
5
} from "@fedify/fedify" ;
6
6
import type { Sql } from "postgres" ;
7
+ import postgres from "postgres" ;
7
8
8
9
/**
9
10
* Options for the PostgreSQL message queue.
@@ -94,6 +95,7 @@ export class PostgresMessageQueue implements MessageQueue {
94
95
handler : ( message : any ) => void | Promise < void > ,
95
96
options : MessageQueueListenOptions = { } ,
96
97
) : Promise < void > {
98
+ await this . initialize ( ) ;
97
99
const { signal } = options ;
98
100
const poll = async ( ) => {
99
101
if ( signal ?. aborted ) return ;
@@ -155,14 +157,23 @@ export class PostgresMessageQueue implements MessageQueue {
155
157
*/
156
158
async initialize ( ) : Promise < void > {
157
159
if ( this . #initialized) return ;
158
- await this . #sql`
160
+ try {
161
+ await this . #sql`
159
162
CREATE TABLE IF NOT EXISTS ${ this . #sql( this . #tableName) } (
160
163
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
161
164
message jsonb NOT NULL,
162
165
delay interval DEFAULT '0 seconds',
163
166
created timestamp with time zone DEFAULT CURRENT_TIMESTAMP
164
167
);
165
168
` ;
169
+ } catch ( e ) {
170
+ if (
171
+ ! ( e instanceof postgres . PostgresError &&
172
+ e . constraint_name === "pg_type_typname_nsp_index" )
173
+ ) {
174
+ throw e ;
175
+ }
176
+ }
166
177
this . #initialized = true ;
167
178
}
168
179
@@ -173,3 +184,5 @@ export class PostgresMessageQueue implements MessageQueue {
173
184
await this . #sql`DROP TABLE IF EXISTS ${ this . #sql( this . #tableName) } ;` ;
174
185
}
175
186
}
187
+
188
+ // cSpell: ignore typname
0 commit comments