mirror of
https://github.com/withastro/astro.git
synced 2025-02-03 22:29:08 -05:00
feat: add references checks to migratiosn
This commit is contained in:
parent
5731926f75
commit
0920af1ad5
1 changed files with 9 additions and 2 deletions
|
@ -22,6 +22,7 @@ import {
|
||||||
getCreateIndexQueries,
|
getCreateIndexQueries,
|
||||||
getCreateTableQuery,
|
getCreateTableQuery,
|
||||||
getModifiers,
|
getModifiers,
|
||||||
|
getReferencesConfig,
|
||||||
hasDefault,
|
hasDefault,
|
||||||
schemaTypeToSqlType,
|
schemaTypeToSqlType,
|
||||||
} from '../queries.js';
|
} from '../queries.js';
|
||||||
|
@ -103,8 +104,12 @@ export async function getCollectionChangeQueries({
|
||||||
const updated = getUpdatedFields(oldCollection.fields, newCollection.fields);
|
const updated = getUpdatedFields(oldCollection.fields, newCollection.fields);
|
||||||
let added = getAdded(oldCollection.fields, newCollection.fields);
|
let added = getAdded(oldCollection.fields, newCollection.fields);
|
||||||
let dropped = getDropped(oldCollection.fields, newCollection.fields);
|
let dropped = getDropped(oldCollection.fields, newCollection.fields);
|
||||||
|
/** Any foreign key changes require a full table recreate */
|
||||||
|
const hasForeignKeyChanges = Boolean(
|
||||||
|
deepDiff(oldCollection.foreignKeys, newCollection.foreignKeys)
|
||||||
|
);
|
||||||
|
|
||||||
if (isEmpty(updated) && isEmpty(added) && isEmpty(dropped)) {
|
if (!hasForeignKeyChanges && isEmpty(updated) && isEmpty(added) && isEmpty(dropped)) {
|
||||||
return {
|
return {
|
||||||
queries: getChangeIndexQueries({
|
queries: getChangeIndexQueries({
|
||||||
collectionName,
|
collectionName,
|
||||||
|
@ -114,13 +119,14 @@ export async function getCollectionChangeQueries({
|
||||||
confirmations,
|
confirmations,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (!isEmpty(added) && !isEmpty(dropped)) {
|
if (!hasForeignKeyChanges && !isEmpty(added) && !isEmpty(dropped)) {
|
||||||
const resolved = await resolveFieldRenames(collectionName, added, dropped, ambiguityResponses);
|
const resolved = await resolveFieldRenames(collectionName, added, dropped, ambiguityResponses);
|
||||||
added = resolved.added;
|
added = resolved.added;
|
||||||
dropped = resolved.dropped;
|
dropped = resolved.dropped;
|
||||||
queries.push(...getFieldRenameQueries(collectionName, resolved.renamed));
|
queries.push(...getFieldRenameQueries(collectionName, resolved.renamed));
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
|
!hasForeignKeyChanges &&
|
||||||
isEmpty(updated) &&
|
isEmpty(updated) &&
|
||||||
Object.values(dropped).every(canAlterTableDropColumn) &&
|
Object.values(dropped).every(canAlterTableDropColumn) &&
|
||||||
Object.values(added).every(canAlterTableAddColumn)
|
Object.values(added).every(canAlterTableAddColumn)
|
||||||
|
@ -432,6 +438,7 @@ function canAlterTableAddColumn(field: DBField) {
|
||||||
if (hasRuntimeDefault(field)) return false;
|
if (hasRuntimeDefault(field)) return false;
|
||||||
if (!field.optional && !hasDefault(field)) return false;
|
if (!field.optional && !hasDefault(field)) return false;
|
||||||
if (hasPrimaryKey(field)) return false;
|
if (hasPrimaryKey(field)) return false;
|
||||||
|
if (getReferencesConfig(field)) return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue