diff --git a/packages/core/database/src/query-interface/mysql-query-interface.ts b/packages/core/database/src/query-interface/mysql-query-interface.ts index 522ba12c4..6fee45304 100644 --- a/packages/core/database/src/query-interface/mysql-query-interface.ts +++ b/packages/core/database/src/query-interface/mysql-query-interface.ts @@ -76,4 +76,14 @@ export default class MysqlQueryInterface extends QueryInterface { const sql = match[0]; return sql; } + + async showTableDefinition(tableInfo: { name: string; schema?: string }): Promise { + const { name } = tableInfo; + + const sql = `SHOW CREATE TABLE ${name}`; + + const results = await this.db.sequelize.query(sql, { type: 'SELECT' }); + + return results[0]['Create Table']; + } } diff --git a/packages/core/database/src/query-interface/postgres-query-interface.ts b/packages/core/database/src/query-interface/postgres-query-interface.ts index 5754af93f..6e920e828 100644 --- a/packages/core/database/src/query-interface/postgres-query-interface.ts +++ b/packages/core/database/src/query-interface/postgres-query-interface.ts @@ -112,4 +112,34 @@ export default class PostgresQueryInterface extends QueryInterface { return {}; } } + + async showTableDefinition(tableInfo: { name: string; schema?: string }) { + const showFunc = ` +CREATE OR REPLACE FUNCTION show_create_table(p_schema text, p_table_name text) +RETURNS text AS +$BODY$ +SELECT 'CREATE TABLE ' || p_schema || '.' || p_table_name || ' (' || E'\\n' || '' || + string_agg(column_list.column_expr, ', ' || E'\\n' || '') || + '' || E'\\n' || ');' +FROM ( + SELECT ' ' || column_name || ' ' || data_type || + coalesce('(' || character_maximum_length || ')', '') || + case when is_nullable = 'YES' then '' else ' NOT NULL' end as column_expr + FROM information_schema.columns + WHERE table_schema = p_schema AND table_name = p_table_name + ORDER BY ordinal_position) column_list; +$BODY$ + LANGUAGE SQL STABLE; + `; + await this.db.sequelize.query(showFunc, { type: 'RAW' }); + + const res = await this.db.sequelize.query( + `SELECT show_create_table('${tableInfo.schema}', '${tableInfo.name || 'public'}')`, + { + type: 'SELECT', + }, + ); + + return res[0]['show_create_table']; + } } diff --git a/packages/core/database/src/query-interface/query-interface.ts b/packages/core/database/src/query-interface/query-interface.ts index 3c21554bb..5033e3fb0 100644 --- a/packages/core/database/src/query-interface/query-interface.ts +++ b/packages/core/database/src/query-interface/query-interface.ts @@ -25,6 +25,8 @@ export default abstract class QueryInterface { abstract parseSQL(sql: string): any; + abstract showTableDefinition(tableInfo: { name: string; schema?: string }): Promise; + async dropAll(options) { if (options.drop !== true) return; diff --git a/packages/core/database/src/query-interface/sqlite-query-interface.ts b/packages/core/database/src/query-interface/sqlite-query-interface.ts index 5b99bc222..ee1d53adb 100644 --- a/packages/core/database/src/query-interface/sqlite-query-interface.ts +++ b/packages/core/database/src/query-interface/sqlite-query-interface.ts @@ -86,4 +86,8 @@ export default class SqliteQueryInterface extends QueryInterface { return sql; } + + showTableDefinition(tableInfo: { name: string; schema?: string }): Promise { + return Promise.resolve(undefined); + } }