feat: show table definition (#3061)
* feat: show table definition * chore: test
This commit is contained in:
parent
d7d2eb634e
commit
57d6a82fcc
@ -76,4 +76,14 @@ export default class MysqlQueryInterface extends QueryInterface {
|
|||||||
const sql = match[0];
|
const sql = match[0];
|
||||||
return sql;
|
return sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async showTableDefinition(tableInfo: { name: string; schema?: string }): Promise<any> {
|
||||||
|
const { name } = tableInfo;
|
||||||
|
|
||||||
|
const sql = `SHOW CREATE TABLE ${name}`;
|
||||||
|
|
||||||
|
const results = await this.db.sequelize.query(sql, { type: 'SELECT' });
|
||||||
|
|
||||||
|
return results[0]['Create Table'];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -112,4 +112,34 @@ export default class PostgresQueryInterface extends QueryInterface {
|
|||||||
return {};
|
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'];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,8 @@ export default abstract class QueryInterface {
|
|||||||
|
|
||||||
abstract parseSQL(sql: string): any;
|
abstract parseSQL(sql: string): any;
|
||||||
|
|
||||||
|
abstract showTableDefinition(tableInfo: { name: string; schema?: string }): Promise<any>;
|
||||||
|
|
||||||
async dropAll(options) {
|
async dropAll(options) {
|
||||||
if (options.drop !== true) return;
|
if (options.drop !== true) return;
|
||||||
|
|
||||||
|
@ -86,4 +86,8 @@ export default class SqliteQueryInterface extends QueryInterface {
|
|||||||
|
|
||||||
return sql;
|
return sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
showTableDefinition(tableInfo: { name: string; schema?: string }): Promise<any> {
|
||||||
|
return Promise.resolve(undefined);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user