From 17dbe6effbfd1ddf51cff5e45798b1b38edf5ff4 Mon Sep 17 00:00:00 2001 From: Siyuan Wang Date: Sat, 15 Feb 2025 18:04:43 +0800 Subject: [PATCH] refactor(migration): change migration api step by step (#1062) --- apps/telegram-monitor/src/migration.ts | 32 +++++++++---------- apps/twitter-monitor/src/migrations.ts | 32 +++++++++---------- .../2025-02-15-09-52.json | 10 ++++++ .../app-twitter-monitor/2025-02-15-09-52.json | 10 ++++++ .../changes/@yuants/sql/2025-02-15-09-52.json | 10 ++++++ libraries/sql/etc/sql.api.md | 9 ++++-- libraries/sql/src/index.ts | 20 ++++++++++-- 7 files changed, 83 insertions(+), 40 deletions(-) create mode 100644 common/changes/@yuants/app-telegram-monitor/2025-02-15-09-52.json create mode 100644 common/changes/@yuants/app-twitter-monitor/2025-02-15-09-52.json create mode 100644 common/changes/@yuants/sql/2025-02-15-09-52.json diff --git a/apps/telegram-monitor/src/migration.ts b/apps/telegram-monitor/src/migration.ts index 2d9dda06..e846da35 100644 --- a/apps/telegram-monitor/src/migration.ts +++ b/apps/telegram-monitor/src/migration.ts @@ -1,12 +1,11 @@ -import { SetupMigration } from '@yuants/sql'; +import { AddMigration, ExecuteMigrations } from '@yuants/sql'; import { terminal } from './terminal'; -SetupMigration(terminal, [ - { - id: 'eb62f3a4-11fd-4386-a86d-1841c3beee13', - name: 'create-table-telegram_messages', - dependencies: [], - statement: ` +AddMigration({ + id: 'eb62f3a4-11fd-4386-a86d-1841c3beee13', + name: 'create-table-telegram_messages', + dependencies: [], + statement: ` CREATE TABLE IF NOT EXISTS public.telegram_messages ( id serial4 NOT NULL, message text NOT NULL, @@ -21,15 +20,13 @@ CREATE TABLE IF NOT EXISTS public.telegram_messages ( CONSTRAINT telegram_messages_pkey PRIMARY KEY (id) ); `, - }, -]); +}); -SetupMigration(terminal, [ - { - id: '15b955fc-96bd-4c3f-8dd9-589456ae3bcc', - name: 'create-table-telegram_monitor_accounts', - dependencies: [], - statement: ` +AddMigration({ + id: '15b955fc-96bd-4c3f-8dd9-589456ae3bcc', + name: 'create-table-telegram_monitor_accounts', + dependencies: [], + statement: ` CREATE TABLE IF NOT EXISTS public.telegram_monitor_accounts ( id serial4 NOT NULL, phone_number text NOT NULL, @@ -40,5 +37,6 @@ SetupMigration(terminal, [ frozen_at timestamptz NULL ); `, - }, -]); +}); + +ExecuteMigrations(terminal); diff --git a/apps/twitter-monitor/src/migrations.ts b/apps/twitter-monitor/src/migrations.ts index 5b7dc36a..1b3bf09b 100644 --- a/apps/twitter-monitor/src/migrations.ts +++ b/apps/twitter-monitor/src/migrations.ts @@ -1,12 +1,11 @@ -import { SetupMigration } from '@yuants/sql'; +import { AddMigration, ExecuteMigrations } from '@yuants/sql'; import { terminal } from './terminal'; -SetupMigration(terminal, [ - { - id: '73ce7f3e-f359-4968-b57a-8ecf8deb71c7', - name: 'create-table-twitter_messages', - dependencies: [], - statement: ` +AddMigration({ + id: '73ce7f3e-f359-4968-b57a-8ecf8deb71c7', + name: 'create-table-twitter_messages', + dependencies: [], + statement: ` CREATE TABLE IF NOT EXISTS twitter_messages ( id TEXT PRIMARY KEY, content TEXT NOT NULL, @@ -21,15 +20,13 @@ SetupMigration(terminal, [ raw_data JSONB ); `, - }, -]); +}); -SetupMigration(terminal, [ - { - id: '0a2023d4-c817-4af1-a2b8-00686cb2bdfe', - name: 'create-table-twitter_monitor_users', - dependencies: [], - statement: ` +AddMigration({ + id: '0a2023d4-c817-4af1-a2b8-00686cb2bdfe', + name: 'create-table-twitter_monitor_users', + dependencies: [], + statement: ` CREATE TABLE IF NOT EXISTS twitter_monitor_users ( id TEXT PRIMARY KEY, user_id TEXT NOT NULL, @@ -38,5 +35,6 @@ SetupMigration(terminal, [ frozen_at TIMESTAMPTZ ); `, - }, -]); +}); + +ExecuteMigrations(terminal); diff --git a/common/changes/@yuants/app-telegram-monitor/2025-02-15-09-52.json b/common/changes/@yuants/app-telegram-monitor/2025-02-15-09-52.json new file mode 100644 index 00000000..24f086a6 --- /dev/null +++ b/common/changes/@yuants/app-telegram-monitor/2025-02-15-09-52.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@yuants/app-telegram-monitor", + "comment": "adapt migration api change", + "type": "patch" + } + ], + "packageName": "@yuants/app-telegram-monitor" +} \ No newline at end of file diff --git a/common/changes/@yuants/app-twitter-monitor/2025-02-15-09-52.json b/common/changes/@yuants/app-twitter-monitor/2025-02-15-09-52.json new file mode 100644 index 00000000..819ce9fd --- /dev/null +++ b/common/changes/@yuants/app-twitter-monitor/2025-02-15-09-52.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@yuants/app-twitter-monitor", + "comment": "adapt migration api change", + "type": "patch" + } + ], + "packageName": "@yuants/app-twitter-monitor" +} \ No newline at end of file diff --git a/common/changes/@yuants/sql/2025-02-15-09-52.json b/common/changes/@yuants/sql/2025-02-15-09-52.json new file mode 100644 index 00000000..f572303e --- /dev/null +++ b/common/changes/@yuants/sql/2025-02-15-09-52.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@yuants/sql", + "comment": "refactor change migration api step by step", + "type": "minor" + } + ], + "packageName": "@yuants/sql" +} \ No newline at end of file diff --git a/libraries/sql/etc/sql.api.md b/libraries/sql/etc/sql.api.md index e57516c6..bb806e15 100644 --- a/libraries/sql/etc/sql.api.md +++ b/libraries/sql/etc/sql.api.md @@ -6,6 +6,12 @@ import { Terminal } from '@yuants/protocol'; +// @public +export const AddMigration: (migration: ISQLMigration) => void; + +// @public +export const ExecuteMigrations: (terminal: Terminal) => Promise; + // @public (undocumented) export interface ISQLMigration { dependencies: string[]; @@ -14,9 +20,6 @@ export interface ISQLMigration { statement: string; } -// @public (undocumented) -export const SetupMigration: (terminal: Terminal, migrations: ISQLMigration[]) => Promise; - // (No @packageDocumentation comment for this package) ``` diff --git a/libraries/sql/src/index.ts b/libraries/sql/src/index.ts index fb391a73..e474a2c1 100644 --- a/libraries/sql/src/index.ts +++ b/libraries/sql/src/index.ts @@ -46,15 +46,29 @@ BEGIN END $$; `; +const allMigrations: ISQLMigration[] = []; + +/** + * Add a migration to the list of migrations to run. + * + * @public + * @param migration - The migration to add + */ +export const AddMigration = (migration: ISQLMigration) => { + allMigrations.push(migration); +}; + /** + * Execute all migrations in the list. + * * @public - * @param migrations - The list of migrations to run. + * @param terminal - The terminal to use for running the migrations */ -export const SetupMigration = async (terminal: Terminal, migrations: ISQLMigration[]) => { +export const ExecuteMigrations = async (terminal: Terminal) => { console.info(formatTime(Date.now()), `SetupMigrationStart`); await lastValueFrom( - from(migrations).pipe( + from(allMigrations).pipe( // concatMap((migration) => defer(async () => {