MCPcopy
hub / github.com/drizzle-team/drizzle-orm / pgSuggestions

Function pgSuggestions

drizzle-kit/src/cli/commands/pgPushUtils.ts:59–269  ·  view source on GitHub ↗
(db: DB, statements: JsonStatement[])

Source from the content-addressed store, hash-verified

57}
58
59export const pgSuggestions = async (db: DB, statements: JsonStatement[]) => {
60 let shouldAskForApprove = false;
61 const statementsToExecute: string[] = [];
62 const infoToPrint: string[] = [];
63
64 const tablesToRemove: string[] = [];
65 const columnsToRemove: string[] = [];
66 const schemasToRemove: string[] = [];
67 const tablesToTruncate: string[] = [];
68 const matViewsToRemove: string[] = [];
69
70 let renamedSchemas: Record<string, string> = {};
71 let renamedTables: Record<string, string> = {};
72
73 for (const statement of statements) {
74 if (statement.type === 'rename_schema') {
75 renamedSchemas[statement.to] = statement.from;
76 } else if (statement.type === 'rename_table') {
77 renamedTables[concatSchemaAndTableName(statement.toSchema, statement.tableNameTo)] = statement.tableNameFrom;
78 } else if (statement.type === 'drop_table') {
79 const res = await db.query(
80 `select count(*) as count from ${
81 tableNameWithSchemaFrom(statement.schema, statement.tableName, renamedSchemas, renamedTables)
82 }`,
83 );
84 const count = Number(res[0].count);
85 if (count > 0) {
86 infoToPrint.push(`· You're about to delete ${chalk.underline(statement.tableName)} table with ${count} items`);
87 // statementsToExecute.push(
88 // `truncate table ${tableNameWithSchemaFrom(statement)} cascade;`
89 // );
90 tablesToRemove.push(statement.tableName);
91 shouldAskForApprove = true;
92 }
93 } else if (statement.type === 'drop_view' && statement.materialized) {
94 const res = await db.query(`select count(*) as count from "${statement.schema ?? 'public'}"."${statement.name}"`);
95 const count = Number(res[0].count);
96 if (count > 0) {
97 infoToPrint.push(
98 `· You're about to delete "${chalk.underline(statement.name)}" materialized view with ${count} items`,
99 );
100
101 matViewsToRemove.push(statement.name);
102 shouldAskForApprove = true;
103 }
104 } else if (statement.type === 'alter_table_drop_column') {
105 const res = await db.query(
106 `select count(*) as count from ${
107 tableNameWithSchemaFrom(statement.schema, statement.tableName, renamedSchemas, renamedTables)
108 }`,
109 );
110 const count = Number(res[0].count);
111 if (count > 0) {
112 infoToPrint.push(
113 `· You're about to delete ${
114 chalk.underline(statement.columnName)
115 } column in ${statement.tableName} table with ${count} items`,
116 );

Callers 5

pushSchemaFunction · 0.90
pgPushFunction · 0.90
diffTestSchemasPushFunction · 0.90
addNotNullFunction · 0.90

Calls 4

fromJsonFunction · 0.90
concatSchemaAndTableNameFunction · 0.85
tableNameWithSchemaFromFunction · 0.85
queryMethod · 0.45

Tested by 2

addNotNullFunction · 0.72