@risingwave/wavelet-server 0.2.1 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/dist/__tests__/cursor-parsing.test.d.ts +2 -0
  2. package/dist/__tests__/cursor-parsing.test.d.ts.map +1 -0
  3. package/dist/__tests__/cursor-parsing.test.js +64 -0
  4. package/dist/__tests__/cursor-parsing.test.js.map +1 -0
  5. package/dist/__tests__/http-api.test.d.ts +2 -0
  6. package/dist/__tests__/http-api.test.d.ts.map +1 -0
  7. package/dist/__tests__/http-api.test.js +135 -0
  8. package/dist/__tests__/http-api.test.js.map +1 -0
  9. package/dist/__tests__/integration.test.d.ts +2 -0
  10. package/dist/__tests__/integration.test.d.ts.map +1 -0
  11. package/dist/__tests__/integration.test.js +229 -0
  12. package/dist/__tests__/integration.test.js.map +1 -0
  13. package/dist/__tests__/jwt.test.d.ts +2 -0
  14. package/dist/__tests__/jwt.test.d.ts.map +1 -0
  15. package/dist/__tests__/jwt.test.js +86 -0
  16. package/dist/__tests__/jwt.test.js.map +1 -0
  17. package/dist/__tests__/ws-fanout.test.d.ts +2 -0
  18. package/dist/__tests__/ws-fanout.test.d.ts.map +1 -0
  19. package/dist/__tests__/ws-fanout.test.js +127 -0
  20. package/dist/__tests__/ws-fanout.test.js.map +1 -0
  21. package/dist/config-loader.d.ts +3 -0
  22. package/dist/config-loader.d.ts.map +1 -0
  23. package/dist/config-loader.js +25 -0
  24. package/dist/config-loader.js.map +1 -0
  25. package/dist/cursor-manager.d.ts +54 -0
  26. package/dist/cursor-manager.d.ts.map +1 -0
  27. package/dist/cursor-manager.js +263 -0
  28. package/dist/cursor-manager.js.map +1 -0
  29. package/dist/ddl-manager.d.ts +33 -0
  30. package/dist/ddl-manager.d.ts.map +1 -0
  31. package/dist/ddl-manager.js +364 -0
  32. package/dist/ddl-manager.js.map +1 -0
  33. package/dist/http-api.d.ts +21 -0
  34. package/dist/http-api.d.ts.map +1 -0
  35. package/dist/http-api.js +242 -0
  36. package/dist/http-api.js.map +1 -0
  37. package/dist/index.d.ts +5 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +32 -0
  40. package/dist/index.js.map +1 -0
  41. package/dist/jwt.d.ts +16 -0
  42. package/dist/jwt.d.ts.map +1 -0
  43. package/dist/jwt.js +87 -0
  44. package/dist/jwt.js.map +1 -0
  45. package/dist/server.d.ts +24 -0
  46. package/dist/server.d.ts.map +1 -0
  47. package/dist/server.js +82 -0
  48. package/dist/server.js.map +1 -0
  49. package/dist/webhook.d.ts +9 -0
  50. package/dist/webhook.d.ts.map +1 -0
  51. package/dist/webhook.js +63 -0
  52. package/dist/webhook.js.map +1 -0
  53. package/dist/ws-fanout.d.ts +24 -0
  54. package/dist/ws-fanout.d.ts.map +1 -0
  55. package/dist/ws-fanout.js +198 -0
  56. package/dist/ws-fanout.js.map +1 -0
  57. package/package.json +16 -3
  58. package/src/__tests__/cursor-parsing.test.ts +0 -68
  59. package/src/__tests__/http-api.test.ts +0 -130
  60. package/src/__tests__/integration.test.ts +0 -217
  61. package/src/__tests__/jwt.test.ts +0 -62
  62. package/src/config-loader.ts +0 -28
  63. package/src/cursor-manager.ts +0 -209
  64. package/src/ddl-manager.ts +0 -408
  65. package/src/http-api.ts +0 -278
  66. package/src/index.ts +0 -31
  67. package/src/jwt.ts +0 -56
  68. package/src/server.ts +0 -89
  69. package/src/webhook.ts +0 -67
  70. package/src/ws-fanout.ts +0 -171
  71. package/tsconfig.json +0 -8
@@ -1,209 +0,0 @@
1
- import pg from 'pg'
2
- import type { SqlFragment, QueryDef } from '@risingwave/wavelet'
3
-
4
- const { Client } = pg
5
-
6
- export interface DiffRow {
7
- op: 'insert' | 'update_insert' | 'update_delete' | 'delete'
8
- row: Record<string, unknown>
9
- rw_timestamp: string
10
- }
11
-
12
- export interface ViewDiff {
13
- cursor: string
14
- inserted: Record<string, unknown>[]
15
- updated: Record<string, unknown>[]
16
- deleted: Record<string, unknown>[]
17
- }
18
-
19
- type DiffCallback = (queryName: string, diff: ViewDiff) => void
20
-
21
- /**
22
- * Manages persistent subscription cursors against RisingWave.
23
- *
24
- * Each query gets its own dedicated pg connection and a persistent cursor.
25
- * Uses blocking FETCH (WITH timeout) so there is no polling interval -
26
- * diffs are dispatched as soon as RisingWave produces them.
27
- */
28
- export class CursorManager {
29
- // Shared connection for DDL (CREATE SUBSCRIPTION) and ad-hoc queries
30
- private client: InstanceType<typeof Client> | null = null
31
-
32
- // Per-query dedicated connections for blocking FETCH
33
- private queryConnections: Map<string, InstanceType<typeof Client>> = new Map()
34
- private cursorNames: Map<string, string> = new Map()
35
- private subscriptions: Map<string, string> = new Map()
36
- private running = false
37
-
38
- constructor(
39
- private connectionString: string,
40
- private queries: Record<string, QueryDef | SqlFragment>
41
- ) {}
42
-
43
- async initialize(): Promise<void> {
44
- this.client = new Client({ connectionString: this.connectionString })
45
- await this.client.connect()
46
- console.log('Connected to RisingWave')
47
-
48
- for (const [queryName] of Object.entries(this.queries)) {
49
- const subName = `wavelet_sub_${queryName}`
50
-
51
- // Create subscription if not exists (idempotent)
52
- try {
53
- await this.client.query(
54
- `CREATE SUBSCRIPTION ${subName} FROM ${queryName} WITH (retention = '24h')`
55
- )
56
- console.log(`Created subscription: ${subName}`)
57
- } catch (err: any) {
58
- if (err.message?.includes('exists')) {
59
- console.log(`Subscription exists: ${subName}`)
60
- } else {
61
- throw err
62
- }
63
- }
64
-
65
- this.subscriptions.set(queryName, subName)
66
-
67
- // Create dedicated connection and persistent cursor for this query
68
- const conn = new Client({ connectionString: this.connectionString })
69
- await conn.connect()
70
-
71
- const cursorName = `wavelet_cur_${queryName}`
72
- await conn.query(`DECLARE ${cursorName} SUBSCRIPTION CURSOR FOR ${subName}`)
73
- console.log(`Opened persistent cursor: ${cursorName}`)
74
-
75
- this.queryConnections.set(queryName, conn)
76
- this.cursorNames.set(queryName, cursorName)
77
- }
78
- }
79
-
80
- /**
81
- * Start listening for diffs on all queries.
82
- * Each query runs its own async loop with blocking FETCH.
83
- * No polling interval - FETCH blocks until data arrives or timeout.
84
- */
85
- startPolling(callback: DiffCallback): void {
86
- this.running = true
87
-
88
- for (const [queryName] of this.subscriptions.entries()) {
89
- this.listenLoop(queryName, callback)
90
- }
91
- }
92
-
93
- stopPolling(): void {
94
- this.running = false
95
- }
96
-
97
- private async listenLoop(queryName: string, callback: DiffCallback): Promise<void> {
98
- const conn = this.queryConnections.get(queryName)
99
- const cursorName = this.cursorNames.get(queryName)
100
- if (!conn || !cursorName) return
101
-
102
- while (this.running) {
103
- try {
104
- // Blocking FETCH: waits up to 5s for new data, returns immediately when data arrives
105
- const result = await conn.query(
106
- `FETCH NEXT FROM ${cursorName} WITH (timeout = '5s')`
107
- )
108
-
109
- if (result.rows.length === 0) continue
110
-
111
- // Got at least one row. Drain any remaining rows with timeout.
112
- const allRows = [...result.rows]
113
- let more = true
114
- while (more) {
115
- const batch = await conn.query(
116
- `FETCH 100 FROM ${cursorName} WITH (timeout = '1s')`
117
- )
118
- if (batch.rows.length > 0) {
119
- allRows.push(...batch.rows)
120
- } else {
121
- more = false
122
- }
123
- }
124
-
125
- const diff = this.parseDiffs(allRows)
126
-
127
- if (diff.inserted.length > 0 || diff.updated.length > 0 || diff.deleted.length > 0) {
128
- callback(queryName, diff)
129
- }
130
- } catch (err: any) {
131
- if (!this.running) return
132
- console.error(`[cursor-manager] Error fetching ${queryName}:`, err.message)
133
- // Back off on error, then retry
134
- await new Promise(r => setTimeout(r, 1000))
135
- }
136
- }
137
- }
138
-
139
- parseDiffs(rows: any[]): ViewDiff {
140
- const diff: ViewDiff = {
141
- cursor: '',
142
- inserted: [],
143
- updated: [],
144
- deleted: [],
145
- }
146
-
147
- for (const row of rows) {
148
- const { op, rw_timestamp, ...data } = row
149
- diff.cursor = rw_timestamp ?? diff.cursor
150
-
151
- // RisingWave returns op as a string: "Insert", "Delete", "UpdateInsert", "UpdateDelete"
152
- const opStr = String(op)
153
- switch (opStr) {
154
- case 'Insert':
155
- case '1':
156
- diff.inserted.push(data)
157
- break
158
- case 'Delete':
159
- case '2':
160
- diff.deleted.push(data)
161
- break
162
- case 'UpdateDelete':
163
- case '3':
164
- diff.deleted.push(data)
165
- break
166
- case 'UpdateInsert':
167
- case '4':
168
- diff.updated.push(data)
169
- break
170
- }
171
- }
172
-
173
- return diff
174
- }
175
-
176
- async query(sql: string): Promise<any[]> {
177
- if (!this.client) throw new Error('Not connected')
178
- const result = await this.client.query(sql)
179
- return result.rows
180
- }
181
-
182
- async execute(sql: string): Promise<void> {
183
- if (!this.client) throw new Error('Not connected')
184
- await this.client.query(sql)
185
- }
186
-
187
- async close(): Promise<void> {
188
- this.running = false
189
-
190
- // Close per-query connections
191
- for (const [queryName, conn] of this.queryConnections) {
192
- const cursorName = this.cursorNames.get(queryName)
193
- try {
194
- if (cursorName) await conn.query(`CLOSE ${cursorName}`)
195
- } catch {}
196
- try {
197
- await conn.end()
198
- } catch {}
199
- }
200
- this.queryConnections.clear()
201
- this.cursorNames.clear()
202
-
203
- // Close shared connection
204
- try {
205
- await this.client?.end()
206
- } catch {}
207
- this.client = null
208
- }
209
- }
@@ -1,408 +0,0 @@
1
- import pg from 'pg'
2
- import type { WaveletConfig, EventDef, QueryDef, SqlFragment, PostgresCdcSource } from '@risingwave/wavelet'
3
-
4
- const { Client } = pg
5
-
6
- export interface DdlAction {
7
- type: 'create' | 'update' | 'delete' | 'unchanged'
8
- resource: 'event' | 'source' | 'query' | 'subscription'
9
- name: string
10
- detail?: string
11
- }
12
-
13
- const COLUMN_TYPE_MAP: Record<string, string> = {
14
- string: 'VARCHAR',
15
- int: 'INT',
16
- float: 'DOUBLE',
17
- boolean: 'BOOLEAN',
18
- timestamp: 'TIMESTAMPTZ',
19
- json: 'JSONB',
20
- }
21
-
22
- function normalizeSql(sql: string): string {
23
- // RisingWave stores definitions as "CREATE MATERIALIZED VIEW name AS SELECT ..."
24
- // Strip the prefix to compare just the query part
25
- const stripped = sql.replace(/^create\s+materialized\s+view\s+\S+\s+as\s+/i, '')
26
- return stripped.replace(/\s+/g, ' ').trim().toLowerCase()
27
- }
28
-
29
- function getQuerySql(queryDef: QueryDef | SqlFragment): string {
30
- if ('_tag' in queryDef && queryDef._tag === 'sql') return queryDef.text
31
- return (queryDef as QueryDef).query.text
32
- }
33
-
34
- function buildCreateTableSql(name: string, eventDef: EventDef): string {
35
- const cols = Object.entries(eventDef.columns)
36
- .map(([colName, colType]) => {
37
- const sqlType = COLUMN_TYPE_MAP[colType]
38
- if (!sqlType) throw new Error(`Unknown column type "${colType}" for column "${colName}"`)
39
- return `${colName} ${sqlType}`
40
- })
41
- .join(', ')
42
- return `CREATE TABLE ${name} (${cols})`
43
- }
44
-
45
- export class DdlManager {
46
- private client: InstanceType<typeof Client> | null = null
47
-
48
- constructor(private connectionString: string) {}
49
-
50
- async connect(): Promise<void> {
51
- this.client = new Client({ connectionString: this.connectionString })
52
- await this.client.connect()
53
- console.log('[ddl-manager] Connected to RisingWave')
54
- }
55
-
56
- async close(): Promise<void> {
57
- await this.client?.end()
58
- this.client = null
59
- console.log('[ddl-manager] Connection closed')
60
- }
61
-
62
- /**
63
- * Sync all events, queries, and subscriptions to match the config.
64
- * Returns a list of actions taken.
65
- * Idempotent - safe to call multiple times.
66
- */
67
- async sync(config: WaveletConfig): Promise<DdlAction[]> {
68
- if (!this.client) throw new Error('Not connected - call connect() first')
69
-
70
- const actions: DdlAction[] = []
71
-
72
- // 1. Fetch existing state from RisingWave
73
- const existingTables = await this.getExistingTables()
74
- const existingMVs = await this.getExistingMaterializedViews()
75
- const existingSubscriptions = await this.getExistingSubscriptions()
76
-
77
- const desiredEvents = config.events ?? config.streams ?? {}
78
- const desiredSources = config.sources ?? {}
79
- const desiredQueries = config.queries ?? config.views ?? {}
80
-
81
- // 2. Determine which events (tables) to create or remove
82
- const desiredEventNames = new Set(Object.keys(desiredEvents))
83
- const desiredQueryNames = new Set(Object.keys(desiredQueries))
84
-
85
- // 3. Sync events - create missing tables
86
- for (const [eventName, eventDef] of Object.entries(desiredEvents)) {
87
- if (existingTables.has(eventName)) {
88
- actions.push({ type: 'unchanged', resource: 'event', name: eventName })
89
- } else {
90
- await this.createTable(eventName, eventDef)
91
- actions.push({ type: 'create', resource: 'event', name: eventName })
92
- }
93
- }
94
-
95
- // 3b. Sync CDC sources - create Postgres CDC tables
96
- const existingSources = await this.getExistingSources()
97
- for (const [sourceName, sourceDef] of Object.entries(desiredSources)) {
98
- if (sourceDef.type === 'postgres') {
99
- for (const tableName of sourceDef.tables) {
100
- const cdcTableName = `${sourceName}_${tableName}`
101
- if (existingSources.has(cdcTableName) || existingTables.has(cdcTableName)) {
102
- actions.push({ type: 'unchanged', resource: 'source', name: cdcTableName })
103
- } else {
104
- await this.createCdcSource(sourceName, tableName, sourceDef)
105
- actions.push({ type: 'create', resource: 'source', name: cdcTableName })
106
- }
107
- }
108
- }
109
- }
110
-
111
- // 4. Sync queries - create, update, or leave unchanged
112
- for (const [queryName, queryDef] of Object.entries(desiredQueries)) {
113
- const subName = `wavelet_sub_${queryName}`
114
- const desiredSql = getQuerySql(queryDef)
115
- const existingSql = existingMVs.get(queryName)
116
-
117
- if (existingSql === undefined) {
118
- // MV does not exist - create MV and subscription
119
- await this.createMaterializedView(queryName, desiredSql)
120
- actions.push({ type: 'create', resource: 'query', name: queryName })
121
-
122
- await this.createSubscription(subName, queryName)
123
- actions.push({ type: 'create', resource: 'subscription', name: subName })
124
- } else if (normalizeSql(existingSql) !== normalizeSql(desiredSql)) {
125
- // Query SQL changed - drop subscription, drop MV, recreate
126
- if (existingSubscriptions.has(subName)) {
127
- await this.dropSubscription(subName)
128
- actions.push({ type: 'delete', resource: 'subscription', name: subName, detail: 'dropped for query update' })
129
- }
130
-
131
- await this.dropMaterializedView(queryName)
132
- actions.push({ type: 'delete', resource: 'query', name: queryName, detail: 'dropped for update' })
133
-
134
- await this.createMaterializedView(queryName, desiredSql)
135
- actions.push({ type: 'create', resource: 'query', name: queryName, detail: 'recreated with updated SQL' })
136
-
137
- await this.createSubscription(subName, queryName)
138
- actions.push({ type: 'create', resource: 'subscription', name: subName, detail: 'recreated after query update' })
139
- } else {
140
- // Query SQL unchanged
141
- actions.push({ type: 'unchanged', resource: 'query', name: queryName })
142
-
143
- // Ensure subscription exists even if the query is unchanged
144
- if (!existingSubscriptions.has(subName)) {
145
- await this.createSubscription(subName, queryName)
146
- actions.push({ type: 'create', resource: 'subscription', name: subName })
147
- } else {
148
- actions.push({ type: 'unchanged', resource: 'subscription', name: subName })
149
- }
150
- }
151
- }
152
-
153
- // 5. Remove queries that are no longer in the config
154
- for (const [existingMVName] of existingMVs) {
155
- if (!desiredQueryNames.has(existingMVName)) {
156
- const subName = `wavelet_sub_${existingMVName}`
157
-
158
- // Drop subscription first
159
- if (existingSubscriptions.has(subName)) {
160
- await this.dropSubscription(subName)
161
- actions.push({ type: 'delete', resource: 'subscription', name: subName, detail: 'query removed from config' })
162
- }
163
-
164
- await this.dropMaterializedView(existingMVName)
165
- actions.push({ type: 'delete', resource: 'query', name: existingMVName, detail: 'removed from config' })
166
- }
167
- }
168
-
169
- // 6. Remove orphaned subscriptions that are no longer needed
170
- for (const existingSubName of existingSubscriptions) {
171
- // Only manage wavelet-prefixed subscriptions
172
- if (!existingSubName.startsWith('wavelet_sub_')) continue
173
-
174
- const queryName = existingSubName.slice('wavelet_sub_'.length)
175
- if (!desiredQueryNames.has(queryName)) {
176
- // Already handled in step 5 if the MV existed, but handle dangling subs too
177
- if (!existingMVs.has(queryName)) {
178
- await this.dropSubscription(existingSubName)
179
- actions.push({ type: 'delete', resource: 'subscription', name: existingSubName, detail: 'orphaned subscription' })
180
- }
181
- }
182
- }
183
-
184
- // 7. Remove events (tables) that are no longer in the config
185
- for (const existingTableName of existingTables) {
186
- if (!desiredEventNames.has(existingTableName)) {
187
- // Only drop if no MV depends on it
188
- const hasDependents = await this.tableHasDependentMVs(existingTableName)
189
- if (hasDependents) {
190
- console.log(`[ddl-manager] Skipping drop of table "${existingTableName}" - materialized views depend on it`)
191
- actions.push({
192
- type: 'unchanged',
193
- resource: 'event',
194
- name: existingTableName,
195
- detail: 'kept because dependent materialized views exist',
196
- })
197
- } else {
198
- await this.dropTable(existingTableName)
199
- actions.push({ type: 'delete', resource: 'event', name: existingTableName, detail: 'removed from config' })
200
- }
201
- }
202
- }
203
-
204
- return actions
205
- }
206
-
207
- // ── Query helpers ─────────────────────────────────────────────────────
208
-
209
- private async getExistingTables(): Promise<Set<string>> {
210
- const result = await this.client!.query(
211
- `SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE'`
212
- )
213
- return new Set(result.rows.map((r: any) => r.table_name))
214
- }
215
-
216
- private async getExistingMaterializedViews(): Promise<Map<string, string>> {
217
- const result = await this.client!.query(
218
- `SELECT name, definition FROM rw_catalog.rw_materialized_views WHERE schema_id = (SELECT id FROM rw_catalog.rw_schemas WHERE name = 'public')`
219
- )
220
- const views = new Map<string, string>()
221
- for (const row of result.rows) {
222
- views.set(row.name, row.definition)
223
- }
224
- return views
225
- }
226
-
227
- private async getExistingSources(): Promise<Set<string>> {
228
- try {
229
- const result = await this.client!.query(
230
- `SELECT name FROM rw_catalog.rw_tables WHERE schema_id = (SELECT id FROM rw_catalog.rw_schemas WHERE name = 'public') AND is_index = false`
231
- )
232
- return new Set(result.rows.map((r: any) => r.name))
233
- } catch {
234
- // Fallback: if catalog query fails, return empty set
235
- return new Set()
236
- }
237
- }
238
-
239
- private async getExistingSubscriptions(): Promise<Set<string>> {
240
- const result = await this.client!.query(
241
- `SELECT name FROM rw_catalog.rw_subscriptions WHERE schema_id = (SELECT id FROM rw_catalog.rw_schemas WHERE name = 'public')`
242
- )
243
- return new Set(result.rows.map((r: any) => r.name))
244
- }
245
-
246
- private async tableHasDependentMVs(tableName: string): Promise<boolean> {
247
- const result = await this.client!.query(
248
- `SELECT name FROM rw_catalog.rw_materialized_views WHERE schema_id = (SELECT id FROM rw_catalog.rw_schemas WHERE name = 'public') AND definition ILIKE $1`,
249
- [`%${tableName}%`]
250
- )
251
- return result.rows.length > 0
252
- }
253
-
254
- // ── DDL operations ────────────────────────────────────────────────────
255
-
256
- private async createTable(name: string, eventDef: EventDef): Promise<void> {
257
- const sql = buildCreateTableSql(name, eventDef)
258
- try {
259
- await this.client!.query(sql)
260
- console.log(`[ddl-manager] Created table: ${name}`)
261
- } catch (err: any) {
262
- if (err.message?.includes('already exists')) {
263
- console.log(`[ddl-manager] Table already exists: ${name}`)
264
- } else {
265
- throw err
266
- }
267
- }
268
- }
269
-
270
- private async dropTable(name: string): Promise<void> {
271
- try {
272
- await this.client!.query(`DROP TABLE ${name}`)
273
- console.log(`[ddl-manager] Dropped table: ${name}`)
274
- } catch (err: any) {
275
- if (err.message?.includes('does not exist')) {
276
- console.log(`[ddl-manager] Table already gone: ${name}`)
277
- } else {
278
- throw err
279
- }
280
- }
281
- }
282
-
283
- private async createMaterializedView(name: string, sql: string): Promise<void> {
284
- try {
285
- await this.client!.query(`CREATE MATERIALIZED VIEW ${name} AS ${sql}`)
286
- console.log(`[ddl-manager] Created materialized view: ${name}`)
287
- } catch (err: any) {
288
- if (err.message?.includes('already exists')) {
289
- console.log(`[ddl-manager] Materialized view already exists: ${name}`)
290
- } else {
291
- throw err
292
- }
293
- }
294
- }
295
-
296
- private async dropMaterializedView(name: string): Promise<void> {
297
- try {
298
- await this.client!.query(`DROP MATERIALIZED VIEW ${name}`)
299
- console.log(`[ddl-manager] Dropped materialized view: ${name}`)
300
- } catch (err: any) {
301
- if (err.message?.includes('does not exist')) {
302
- console.log(`[ddl-manager] Materialized view already gone: ${name}`)
303
- } else {
304
- throw err
305
- }
306
- }
307
- }
308
-
309
- private async createSubscription(subName: string, viewName: string): Promise<void> {
310
- try {
311
- await this.client!.query(
312
- `CREATE SUBSCRIPTION ${subName} FROM ${viewName} WITH (retention = '24h')`
313
- )
314
- console.log(`[ddl-manager] Created subscription: ${subName}`)
315
- } catch (err: any) {
316
- if (err.message?.includes('already exists')) {
317
- console.log(`[ddl-manager] Subscription already exists: ${subName}`)
318
- } else {
319
- throw err
320
- }
321
- }
322
- }
323
-
324
- private async dropSubscription(subName: string): Promise<void> {
325
- try {
326
- await this.client!.query(`DROP SUBSCRIPTION ${subName}`)
327
- console.log(`[ddl-manager] Dropped subscription: ${subName}`)
328
- } catch (err: any) {
329
- if (err.message?.includes('does not exist')) {
330
- console.log(`[ddl-manager] Subscription already gone: ${subName}`)
331
- } else {
332
- throw err
333
- }
334
- }
335
- }
336
-
337
- private async createCdcSource(
338
- sourceName: string,
339
- tableName: string,
340
- source: PostgresCdcSource
341
- ): Promise<void> {
342
- const cdcTableName = `${sourceName}_${tableName}`
343
- const slotName = source.slotName ?? `wavelet_${sourceName}`
344
- const pubName = source.publicationName ?? `wavelet_${sourceName}_pub`
345
-
346
- // RisingWave CDC source syntax:
347
- // CREATE TABLE table_name ( ... ) WITH (
348
- // connector = 'postgres-cdc',
349
- // hostname = '...',
350
- // port = '...',
351
- // username = '...',
352
- // password = '...',
353
- // database.name = '...',
354
- // table.name = '...',
355
- // slot.name = '...',
356
- // publication.name = '...'
357
- // )
358
- // We parse the connection string to extract components.
359
-
360
- const parsed = parsePostgresUrl(source.connection)
361
-
362
- const esc = (s: string) => s.replace(/'/g, "''")
363
- try {
364
- await this.client!.query(`
365
- CREATE TABLE IF NOT EXISTS ${cdcTableName} (*)
366
- WITH (
367
- connector = 'postgres-cdc',
368
- hostname = '${esc(parsed.host)}',
369
- port = '${esc(parsed.port)}',
370
- username = '${esc(parsed.user)}',
371
- password = '${esc(parsed.password)}',
372
- database.name = '${esc(parsed.database)}',
373
- schema.name = '${esc(parsed.schema)}',
374
- table.name = '${esc(tableName)}',
375
- slot.name = '${esc(slotName)}',
376
- publication.name = '${esc(pubName)}'
377
- )
378
- `)
379
- console.log(`[ddl-manager] Created CDC source: ${cdcTableName} (from ${parsed.host}/${parsed.database}.${tableName})`)
380
- } catch (err: any) {
381
- if (err.message?.includes('already exists')) {
382
- console.log(`[ddl-manager] CDC source already exists: ${cdcTableName}`)
383
- } else {
384
- throw err
385
- }
386
- }
387
- }
388
- }
389
-
390
- function parsePostgresUrl(url: string): {
391
- host: string
392
- port: string
393
- user: string
394
- password: string
395
- database: string
396
- schema: string
397
- } {
398
- // Parse postgresql://user:password@host:port/database?schema=xxx
399
- const u = new URL(url)
400
- return {
401
- host: u.hostname,
402
- port: u.port || '5432',
403
- user: decodeURIComponent(u.username),
404
- password: decodeURIComponent(u.password),
405
- database: u.pathname.replace(/^\//, '') || 'postgres',
406
- schema: u.searchParams.get('schema') ?? 'public',
407
- }
408
- }