getTableChangeQueries() — astro Function Reference
Architecture documentation for the getTableChangeQueries() function in migration-queries.ts from the astro codebase.
Entity Profile
Dependency Diagram
graph TD d8e591b7_f3dd_5f49_beb9_c9047d4d1e02["getTableChangeQueries()"] d1459290_7e42_1f92_05bd_dcc3aeda9fd3["migration-queries.ts"] d8e591b7_f3dd_5f49_beb9_c9047d4d1e02 -->|defined in| d1459290_7e42_1f92_05bd_dcc3aeda9fd3 8d0e7bb2_007f_5d88_64a2_3b0646b309a1["getMigrationQueries()"] 8d0e7bb2_007f_5d88_64a2_3b0646b309a1 -->|calls| d8e591b7_f3dd_5f49_beb9_c9047d4d1e02 4cd3c863_fa29_c34a_a49a_467807e47651["getUpdatedColumns()"] d8e591b7_f3dd_5f49_beb9_c9047d4d1e02 -->|calls| 4cd3c863_fa29_c34a_a49a_467807e47651 93b83f68_8c01_67cb_fc4b_e06a7e7ec6cd["getAdded()"] d8e591b7_f3dd_5f49_beb9_c9047d4d1e02 -->|calls| 93b83f68_8c01_67cb_fc4b_e06a7e7ec6cd d8831ca9_b159_cb01_9a7a_d14ff371f3bc["getDropped()"] d8e591b7_f3dd_5f49_beb9_c9047d4d1e02 -->|calls| d8831ca9_b159_cb01_9a7a_d14ff371f3bc 0855ded3_c0df_3359_b016_c3dc5542cc9c["isEmpty()"] d8e591b7_f3dd_5f49_beb9_c9047d4d1e02 -->|calls| 0855ded3_c0df_3359_b016_c3dc5542cc9c 60f71256_8537_0bef_2828_e36165f8890c["getChangeIndexQueries()"] d8e591b7_f3dd_5f49_beb9_c9047d4d1e02 -->|calls| 60f71256_8537_0bef_2828_e36165f8890c 655b1226_3b31_6e67_5211_7e7cd4c505e3["getAlterTableQueries()"] d8e591b7_f3dd_5f49_beb9_c9047d4d1e02 -->|calls| 655b1226_3b31_6e67_5211_7e7cd4c505e3 e15cd5dc_37f0_2b94_a984_85714025b10b["canRecreateTableWithoutDataLoss()"] d8e591b7_f3dd_5f49_beb9_c9047d4d1e02 -->|calls| e15cd5dc_37f0_2b94_a984_85714025b10b 69526ea7_dab8_b768_2cf0_95b6d89a88de["getRecreateTableQueries()"] d8e591b7_f3dd_5f49_beb9_c9047d4d1e02 -->|calls| 69526ea7_dab8_b768_2cf0_95b6d89a88de style d8e591b7_f3dd_5f49_beb9_c9047d4d1e02 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/db/src/core/cli/migration-queries.ts lines 110–183
export async function getTableChangeQueries({
tableName,
oldTable,
newTable,
}: {
tableName: string;
oldTable: ResolvedDBTable;
newTable: ResolvedDBTable;
}): Promise<{ queries: string[]; confirmations: string[] }> {
const queries: string[] = [];
const confirmations: string[] = [];
const updated = getUpdatedColumns(oldTable.columns, newTable.columns);
const added = getAdded(oldTable.columns, newTable.columns);
const dropped = getDropped(oldTable.columns, newTable.columns);
/** Any foreign key changes require a full table recreate */
const hasForeignKeyChanges = Boolean(deepDiff(oldTable.foreignKeys, newTable.foreignKeys));
if (!hasForeignKeyChanges && isEmpty(updated) && isEmpty(added) && isEmpty(dropped)) {
return {
queries: getChangeIndexQueries({
tableName,
oldIndexes: oldTable.indexes,
newIndexes: newTable.indexes,
}),
confirmations,
};
}
if (
!hasForeignKeyChanges &&
isEmpty(updated) &&
Object.values(dropped).every(canAlterTableDropColumn) &&
Object.values(added).every(canAlterTableAddColumn)
) {
queries.push(
...getAlterTableQueries(tableName, added, dropped),
...getChangeIndexQueries({
tableName,
oldIndexes: oldTable.indexes,
newIndexes: newTable.indexes,
}),
);
return { queries, confirmations };
}
const dataLossCheck = canRecreateTableWithoutDataLoss(added, updated);
if (dataLossCheck.dataLoss) {
const { reason, columnName } = dataLossCheck;
const reasonMsgs: Record<DataLossReason, string> = {
'added-required': `You added new required column '${color.bold(
tableName + '.' + columnName,
)}' with no default value.\n This cannot be executed on an existing table.`,
'updated-type': `Updating existing column ${color.bold(
tableName + '.' + columnName,
)} to a new type that cannot be handled automatically.`,
};
confirmations.push(reasonMsgs[reason]);
}
const primaryKeyExists = Object.entries(newTable.columns).find(([, column]) =>
hasPrimaryKey(column),
);
const droppedPrimaryKey = Object.entries(dropped).find(([, column]) => hasPrimaryKey(column));
const recreateTableQueries = getRecreateTableQueries({
tableName,
newTable,
added,
hasDataLoss: dataLossCheck.dataLoss,
migrateHiddenPrimaryKey: !primaryKeyExists && !droppedPrimaryKey,
});
queries.push(...recreateTableQueries, ...getCreateIndexQueries(tableName, newTable));
return { queries, confirmations };
}
Domain
Subdomains
Calls
Called By
Source
Frequently Asked Questions
What does getTableChangeQueries() do?
getTableChangeQueries() is a function in the astro codebase, defined in packages/db/src/core/cli/migration-queries.ts.
Where is getTableChangeQueries() defined?
getTableChangeQueries() is defined in packages/db/src/core/cli/migration-queries.ts at line 110.
What does getTableChangeQueries() call?
getTableChangeQueries() calls 8 function(s): canRecreateTableWithoutDataLoss, getAdded, getAlterTableQueries, getChangeIndexQueries, getDropped, getRecreateTableQueries, getUpdatedColumns, isEmpty.
What calls getTableChangeQueries()?
getTableChangeQueries() is called by 1 function(s): getMigrationQueries.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free