Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ const initializeContentModel = async (config) => {
};

/**
* Add migration entry to contentful
* Save migration entry to contentful (add or update)
* @param {*} data
* @param {*} config
*/
const addMigrationEntry = async (data, config) => {
const saveMigrationEntry = async (data, config) => {
const { version, name, state, message } = data;

let { defaultLocale, migrationContentTypeId } = config;
Expand All @@ -112,7 +112,10 @@ const addMigrationEntry = async (data, config) => {

let entry;
try {
entry = await client.getEntry(version);
const entries = await client.getEntries({ content_type: migrationContentTypeId, 'fields.version': version });
if (entries.items && entries.items.length > 0) {
entry = entries.items[0];
}
} catch {}

if (!entry) {
Expand All @@ -136,6 +139,7 @@ const addMigrationEntry = async (data, config) => {
entry.fields.name = { [defaultLocale]: name };
entry.fields.state = { [defaultLocale]: state };
entry.fields.message = { [defaultLocale]: message || '' };
entry = await entry.update();
}

try {
Expand All @@ -144,7 +148,9 @@ const addMigrationEntry = async (data, config) => {
} else {
await entry.unpublish();
}
} catch {}
} catch (error) {
console.log('The publish state of the migration entry cou');
}
};

/**
Expand Down Expand Up @@ -178,7 +184,7 @@ const storeMigration = (data, config) => {
const { storage = STORAGE_TAG } = config;

if (storage === STORAGE_CONTENT) {
return addMigrationEntry(data, config);
return saveMigrationEntry(data, config);
}

if (state === STATE_SUCCESS) {
Expand Down Expand Up @@ -223,7 +229,7 @@ const migrateToContentStorage = async (config) => {
const name = path.basename(file);
const [, num] = /^(\d+)-/.exec(name);

await addMigrationEntry({ version: num, name, state: STATE_SUCCESS }, config);
await saveMigrationEntry({ version: num, name, state: STATE_SUCCESS }, config);
progress++;
bar.update(progress);
}
Expand Down
18 changes: 16 additions & 2 deletions lib/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,24 @@ const executeMigration = async (file, config) => {
}
try {
await runMigration(options);
await storeMigration({ version, name, state: STATE_SUCCESS }, config);
try {
await storeMigration({ version, name, state: STATE_SUCCESS }, config);
} catch (storeError) {
console.log(
'\nThe migration run had no errors, but there was an error storing the migration result!',
storeError
);
}
} catch (error) {
const message = (error.errors || [error]).map((error) => error.message).join('\n');
await storeMigration({ version, name, state: STATE_FAILURE, message }, config);
try {
await storeMigration({ version, name, state: STATE_FAILURE, message }, config);
} catch (storeError) {
console.log(
'\nThe migration run had errors and additionally there was an error storing the migration result!',
storeError
);
}

throw error;
}
Expand Down
Loading