Skip to content

Commit

Permalink
refactor(migration): change migration api step by step (#1062)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrimbda authored Feb 15, 2025
1 parent 1a608b9 commit 17dbe6e
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 40 deletions.
32 changes: 15 additions & 17 deletions apps/telegram-monitor/src/migration.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -40,5 +37,6 @@ SetupMigration(terminal, [
frozen_at timestamptz NULL
);
`,
},
]);
});

ExecuteMigrations(terminal);
32 changes: 15 additions & 17 deletions apps/twitter-monitor/src/migrations.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -38,5 +35,6 @@ SetupMigration(terminal, [
frozen_at TIMESTAMPTZ
);
`,
},
]);
});

ExecuteMigrations(terminal);
10 changes: 10 additions & 0 deletions common/changes/@yuants/app-telegram-monitor/2025-02-15-09-52.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@yuants/app-telegram-monitor",
"comment": "adapt migration api change",
"type": "patch"
}
],
"packageName": "@yuants/app-telegram-monitor"
}
10 changes: 10 additions & 0 deletions common/changes/@yuants/app-twitter-monitor/2025-02-15-09-52.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@yuants/app-twitter-monitor",
"comment": "adapt migration api change",
"type": "patch"
}
],
"packageName": "@yuants/app-twitter-monitor"
}
10 changes: 10 additions & 0 deletions common/changes/@yuants/sql/2025-02-15-09-52.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@yuants/sql",
"comment": "refactor change migration api step by step",
"type": "minor"
}
],
"packageName": "@yuants/sql"
}
9 changes: 6 additions & 3 deletions libraries/sql/etc/sql.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

import { Terminal } from '@yuants/protocol';

// @public
export const AddMigration: (migration: ISQLMigration) => void;

// @public
export const ExecuteMigrations: (terminal: Terminal) => Promise<void>;

// @public (undocumented)
export interface ISQLMigration {
dependencies: string[];
Expand All @@ -14,9 +20,6 @@ export interface ISQLMigration {
statement: string;
}

// @public (undocumented)
export const SetupMigration: (terminal: Terminal, migrations: ISQLMigration[]) => Promise<void>;

// (No @packageDocumentation comment for this package)

```
20 changes: 17 additions & 3 deletions libraries/sql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 17dbe6e

Please sign in to comment.