From b367cd338086e5429b61aae84c547249261f5b98 Mon Sep 17 00:00:00 2001
From: Gao Sun <gao@silverhand.io>
Date: Thu, 22 Sep 2022 13:36:57 +0800
Subject: [PATCH] refactor(core): add migration scripts for #1973

---
 packages/core/src/migration/index.ts          |  9 ++++--
 packages/schemas/src/migrations/next.ts       | 31 +++++++++++++++++++
 packages/schemas/src/types/index.ts           |  1 +
 .../src/types/migration.ts}                   |  0
 4 files changed, 39 insertions(+), 2 deletions(-)
 create mode 100644 packages/schemas/src/migrations/next.ts
 rename packages/{core/src/migration/types.ts => schemas/src/types/migration.ts} (100%)

diff --git a/packages/core/src/migration/index.ts b/packages/core/src/migration/index.ts
index 090e7748c..6a1fe09d7 100644
--- a/packages/core/src/migration/index.ts
+++ b/packages/core/src/migration/index.ts
@@ -2,7 +2,13 @@ import { existsSync } from 'fs';
 import { readdir, readFile } from 'fs/promises';
 import path from 'path';
 
-import { LogtoConfig, LogtoConfigs } from '@logto/schemas';
+import {
+  LogtoConfig,
+  LogtoConfigs,
+  DatabaseVersion,
+  databaseVersionGuard,
+  MigrationScript,
+} from '@logto/schemas';
 import { conditionalString } from '@silverhand/essentials';
 import chalk from 'chalk';
 import { DatabasePool, sql } from 'slonik';
@@ -15,7 +21,6 @@ import {
   logtoConfigsTableFilePath,
   migrationFilesDirectory,
 } from './constants';
-import { DatabaseVersion, databaseVersionGuard, MigrationScript } from './types';
 import { compareVersion, getVersionFromFileName, migrationFileNameRegex } from './utils';
 
 const { table, fields } = convertToIdentifiers(LogtoConfigs);
diff --git a/packages/schemas/src/migrations/next.ts b/packages/schemas/src/migrations/next.ts
new file mode 100644
index 000000000..58c69092b
--- /dev/null
+++ b/packages/schemas/src/migrations/next.ts
@@ -0,0 +1,31 @@
+import { sql } from 'slonik';
+
+import { MigrationScript } from '../types';
+
+const migration: MigrationScript = {
+  up: async (pool) => {
+    // [Pull] feat(core): machine to machine apps #1973
+    await pool.query(sql`
+      alter type application_type add value 'MachineToMachine';
+      alter table applications add role_names jsonb not null default '[]'::jsonb;
+    `);
+  },
+  down: async (pool) => {
+    // [Pull] feat(core): machine to machine apps #1973
+    await pool.query(sql`
+      -- Drop role_names
+      alter table applications drop role_names;
+
+      -- Drop enum 'MachineToMachine'
+      create type application_type_new as enum ('Native', 'SPA', 'Traditional');
+      delete from applications where "type"='MachineToMachine';
+      alter table applications
+        alter column "type" type application_type_new
+          using ("type"::text::application_type_new);
+      drop type application_type;
+      alter type application_type_new rename to application_type;
+    `);
+  },
+};
+
+export default migration;
diff --git a/packages/schemas/src/types/index.ts b/packages/schemas/src/types/index.ts
index c22dd0bc7..59147c51c 100644
--- a/packages/schemas/src/types/index.ts
+++ b/packages/schemas/src/types/index.ts
@@ -2,3 +2,4 @@ export * from './connector';
 export * from './log';
 export * from './oidc-config';
 export * from './user';
+export * from './migration';
diff --git a/packages/core/src/migration/types.ts b/packages/schemas/src/types/migration.ts
similarity index 100%
rename from packages/core/src/migration/types.ts
rename to packages/schemas/src/types/migration.ts