Only commit metadata changes of applied migrations
One of the errors that were caused by this was that everytime a new migration was applied, keys of previous migrations got deleted
This commit is contained in:
@@ -150,12 +150,12 @@ const applyMetadataDeleteKeysMigration = (
|
|||||||
return migratedMetadata;
|
return migratedMetadata;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getOutdatedMetadataKeys = (metadata?: Metadata): AppMetadataKeys[] => {
|
const getOutdatedMetadataKeys = (metadata: Metadata | undefined, migrationId: number): AppMetadataKeys[] => {
|
||||||
if (!metadata) {
|
if (!metadata) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const oldAppKeyPrefixes = METADATA_MIGRATIONS.reduce((acc, migration) => {
|
const oldAppKeyPrefixes = METADATA_MIGRATIONS.slice(migrationId).reduce((acc, migration) => {
|
||||||
const oldPrefix = migration.appKeyPrefix?.oldPrefix;
|
const oldPrefix = migration.appKeyPrefix?.oldPrefix;
|
||||||
if (!oldPrefix) {
|
if (!oldPrefix) {
|
||||||
return acc;
|
return acc;
|
||||||
@@ -164,7 +164,7 @@ const getOutdatedMetadataKeys = (metadata?: Metadata): AppMetadataKeys[] => {
|
|||||||
return [...acc, oldPrefix];
|
return [...acc, oldPrefix];
|
||||||
}, [] as string[]);
|
}, [] as string[]);
|
||||||
|
|
||||||
const keyToDeleteInMigrations = METADATA_MIGRATIONS.reduce(
|
const keyToDeleteInMigrations = METADATA_MIGRATIONS.slice(migrationId).reduce(
|
||||||
(acc, migration) => [...acc, ...(migration.deleteKeys ?? [])],
|
(acc, migration) => [...acc, ...(migration.deleteKeys ?? [])],
|
||||||
[] as string[],
|
[] as string[],
|
||||||
);
|
);
|
||||||
@@ -196,12 +196,13 @@ const getNewMetadataKeys = (
|
|||||||
metadata: Metadata | undefined,
|
metadata: Metadata | undefined,
|
||||||
migratedMetadata: Metadata,
|
migratedMetadata: Metadata,
|
||||||
metadataKeyToDelete: string[],
|
metadataKeyToDelete: string[],
|
||||||
|
migrationId: number,
|
||||||
): string[] => {
|
): string[] => {
|
||||||
if (!metadata) {
|
if (!metadata) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const newKeys = METADATA_MIGRATIONS.reduce((acc, migration) => {
|
const newKeys = METADATA_MIGRATIONS.slice(migrationId).reduce((acc, migration) => {
|
||||||
if (!migration.keys) {
|
if (!migration.keys) {
|
||||||
return acc;
|
return acc;
|
||||||
}
|
}
|
||||||
@@ -225,12 +226,13 @@ const getNewMetadataKeys = (
|
|||||||
const getMetadataKeysWithUpdatedValues = (
|
const getMetadataKeysWithUpdatedValues = (
|
||||||
metadata: Metadata | undefined,
|
metadata: Metadata | undefined,
|
||||||
newAndDeletedMetadataKeys: string[],
|
newAndDeletedMetadataKeys: string[],
|
||||||
|
migrationId: number,
|
||||||
): string[] => {
|
): string[] => {
|
||||||
if (!metadata) {
|
if (!metadata) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const keysWithUpdatedValues = METADATA_MIGRATIONS.reduce((acc, migration) => {
|
const keysWithUpdatedValues = METADATA_MIGRATIONS.slice(migrationId).reduce((acc, migration) => {
|
||||||
const keysWithUpdatedValuesOfMigration = Object.keys(metadata).filter((metadataKey) =>
|
const keysWithUpdatedValuesOfMigration = Object.keys(metadata).filter((metadataKey) =>
|
||||||
migration.values?.some(({ key: migrationKey, oldValue }) => {
|
migration.values?.some(({ key: migrationKey, oldValue }) => {
|
||||||
const isMigrationForAllKeys = !migrationKey;
|
const isMigrationForAllKeys = !migrationKey;
|
||||||
@@ -274,12 +276,15 @@ const commitMigratedMetadata = (
|
|||||||
): void => {
|
): void => {
|
||||||
const metadata = metadataHolder?.meta;
|
const metadata = metadataHolder?.meta;
|
||||||
|
|
||||||
const metadataKeysToDelete = getOutdatedMetadataKeys(metadata);
|
const migrationId = Number(metadata?.[getMetadataKey('migration')] ?? 1);
|
||||||
const newMetadataKeys = getNewMetadataKeys(metadata, migratedMetadata, metadataKeysToDelete);
|
|
||||||
const metadataKeysWithUpdatedValues = getMetadataKeysWithUpdatedValues(metadata, [
|
const metadataKeysToDelete = getOutdatedMetadataKeys(metadata, migrationId);
|
||||||
...metadataKeysToDelete,
|
const newMetadataKeys = getNewMetadataKeys(metadata, migratedMetadata, metadataKeysToDelete, migrationId);
|
||||||
...newMetadataKeys,
|
const metadataKeysWithUpdatedValues = getMetadataKeysWithUpdatedValues(
|
||||||
]);
|
metadata,
|
||||||
|
[...metadataKeysToDelete, ...newMetadataKeys],
|
||||||
|
migrationId,
|
||||||
|
);
|
||||||
const metadataToUpdate = [...newMetadataKeys, ...metadataKeysWithUpdatedValues].map((key) => [
|
const metadataToUpdate = [...newMetadataKeys, ...metadataKeysWithUpdatedValues].map((key) => [
|
||||||
key,
|
key,
|
||||||
migratedMetadata[key],
|
migratedMetadata[key],
|
||||||
@@ -297,8 +302,7 @@ const commitMigratedMetadata = (
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isMetadataAlreadyMigrated =
|
const isMetadataAlreadyMigrated = !metadata || migrationId === METADATA_MIGRATIONS.length;
|
||||||
!metadata || Number(metadata[getMetadataKey('migration')]) === METADATA_MIGRATIONS.length;
|
|
||||||
|
|
||||||
const isCommitRequired =
|
const isCommitRequired =
|
||||||
!isMetadataAlreadyMigrated && (!!metadataKeysToDelete.length || !!metadataToUpdate.length);
|
!isMetadataAlreadyMigrated && (!!metadataKeysToDelete.length || !!metadataToUpdate.length);
|
||||||
|
|||||||
Reference in New Issue
Block a user