Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Migrations not working on a fresh DatoCMS instance as a result of mismatched IDs #245

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
50 changes: 41 additions & 9 deletions config/datocms/migrations/1730405111_linkRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Client } from '@datocms/cli/lib/cma-client-node';

export default async function (client: Client) {
console.log('Update existing fields/fieldsets');
const home = await client.itemTypes.find('home_page');
const page = await client.itemTypes.find('page');

console.log(
'Update Structured text field "Text" (`text`) in block model "\uD83D\uDCDD \uD83D\uDDBC\uFE0F Text Image Block" (`text_image_block`)'
Expand All @@ -20,15 +22,16 @@ export default async function (client: Client) {
on_publish_with_unpublished_references_strategy: 'fail',
on_reference_unpublish_strategy: 'delete_references',
on_reference_delete_strategy: 'delete_references',
item_types: ['LjXdkuCdQxCFT4hv8_ayew', 'X_tZn3TxQY28ltSyjZUGHQ'],
item_types: [page.id, home.id],
},
},
});

console.log(
'Update Slug field "Slug" (`slug`) in model "\uD83D\uDCD1 Page" (`page`)'
);
await client.fields.update('SFfpl7xiQaGe5-jy6tlPfg', { position: 2 });
const slug = await client.fields.find('page::slug');
await client.fields.update(slug.id, { position: 2 });

console.log(
'Update Structured text field "Text" (`text`) in block model "\uD83D\uDCDD Text Block" (`text_block`)'
Expand All @@ -48,7 +51,7 @@ export default async function (client: Client) {
on_publish_with_unpublished_references_strategy: 'fail',
on_reference_unpublish_strategy: 'delete_references',
on_reference_delete_strategy: 'delete_references',
item_types: ['LjXdkuCdQxCFT4hv8_ayew', 'X_tZn3TxQY28ltSyjZUGHQ'],
item_types: [page.id, home.id],
},
},
});
Expand Down Expand Up @@ -77,7 +80,8 @@ export default async function (client: Client) {
console.log(
'Update model "\u2934\uFE0F Schema migration" (`schema_migration`)'
);
await client.itemTypes.update('EkWwcYfPQSGXskAZKO_tTg', {
const schemaMigrationItemType = await client.itemTypes.find('schema_migration');
await client.itemTypes.update(schemaMigrationItemType.id, {
name: '\u2934\uFE0F Schema migration',
});

Expand All @@ -101,22 +105,50 @@ export default async function (client: Client) {
console.log('Manage menu items');

console.log('Delete menu item "Schema migration"');
await client.menuItems.destroy('Pl3NAPTcQVmC0ben2g6V8Q');
const menuItems = await client.menuItems.list();
const schemaMigrationMenuItem = menuItems.find(item => item.label === 'Schema migration'),
translationMenuItem = menuItems.find(item => item.label === '\uD83C\uDF10 Translation'),
pagesMenuItem = menuItems.find(item => item.label === '\uD83D\uDCD1 Pages'),
homeMenuItem = menuItems.find(item => item.label === '\uD83C\uDFE0 Home'),
notFoundMenuItem = menuItems.find(item => item.label === '\uD83E\uDD37 404 Page');

if (
!schemaMigrationMenuItem ||
!translationMenuItem ||
!pagesMenuItem ||
!homeMenuItem ||
!notFoundMenuItem
)
throw new Error(`Expected menu items to consist of: [
"Schema Migration",
"Translation",
"Pages",
"Home",
"404 Page",
"Redirect Rules",
"Image Block",
"Table Block", and
"Video Embed Block",
] but received: [
${menuItems.map(item => ` "${item.label}"`).join(',\n')}
]`);

await client.menuItems.destroy(schemaMigrationMenuItem.id);

console.log('Update menu item "\uD83C\uDF10 Translations"');
await client.menuItems.update('Rl4MJgiIR2akoRpTSmqHHg', {
await client.menuItems.update(translationMenuItem.id, {
label: '\uD83C\uDF10 Translations',
position: 5,
});

console.log('Update menu item "\uD83D\uDCD1 Pages"');
await client.menuItems.update('B_78LT8CQ12Cyj27FKLw-Q', { position: 3 });
await client.menuItems.update(pagesMenuItem.id, { position: 3 });

console.log('Update menu item "\uD83C\uDFE0 Home"');
await client.menuItems.update('C2TTNIPgSZapZlrQpxEdSQ', { position: 2 });
await client.menuItems.update(homeMenuItem.id, { position: 2 });

console.log('Update menu item "\uD83E\uDD37 404 Page"');
await client.menuItems.update('LNNzMSjXQ5CDv1JmXT0XOg', { position: 4 });
await client.menuItems.update(notFoundMenuItem.id, { position: 4 });

console.log('Manage schema menu items');

Expand Down
10 changes: 6 additions & 4 deletions config/datocms/migrations/1730927867_fileModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Client } from '@datocms/cli/lib/cma-client-node';

export default async function (client: Client) {
console.log('Manage upload filters');
const home = await client.itemTypes.find('home_page');
const page = await client.itemTypes.find('page');

console.log('Install plugin "Computed Fields"');
await client.plugins.create({
Expand Down Expand Up @@ -113,8 +115,8 @@ export default async function (client: Client) {
on_reference_delete_strategy: 'delete_references',
item_types: [
'GjWw8t-hTFaYYWyc53FeIg',
'LjXdkuCdQxCFT4hv8_ayew',
'X_tZn3TxQY28ltSyjZUGHQ',
page.id,
home.id
],
},
},
Expand All @@ -140,8 +142,8 @@ export default async function (client: Client) {
on_reference_delete_strategy: 'delete_references',
item_types: [
'GjWw8t-hTFaYYWyc53FeIg',
'LjXdkuCdQxCFT4hv8_ayew',
'X_tZn3TxQY28ltSyjZUGHQ',
page.id,
home.id
],
},
},
Expand Down
14 changes: 8 additions & 6 deletions config/datocms/migrations/1734363388_actionBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export const actionStyleField: SimpleSchemaTypes.FieldCreateSchema = {

export default async function (client: Client) {
console.log('Create new models/block models');

const home = await client.itemTypes.find('home_page');
const page = await client.itemTypes.find('page');

console.log(
'Create block model "\uD83C\uDF9B\uFE0F Action Block" (`action_block`)'
);
Expand Down Expand Up @@ -111,7 +113,7 @@ export default async function (client: Client) {
on_publish_with_unpublished_references_strategy: 'fail',
on_reference_unpublish_strategy: 'delete_references',
on_reference_delete_strategy: 'delete_references',
item_types: ['LjXdkuCdQxCFT4hv8_ayew', 'X_tZn3TxQY28ltSyjZUGHQ'],
item_types: [page.id, home.id],
},
required: {},
},
Expand Down Expand Up @@ -149,8 +151,8 @@ export default async function (client: Client) {
on_reference_delete_strategy: 'delete_references',
item_types: [
'GjWw8t-hTFaYYWyc53FeIg',
'LjXdkuCdQxCFT4hv8_ayew',
'X_tZn3TxQY28ltSyjZUGHQ',
page.id,
home.id
],
},
},
Expand Down Expand Up @@ -216,8 +218,8 @@ export default async function (client: Client) {
on_reference_delete_strategy: 'delete_references',
item_types: [
'GjWw8t-hTFaYYWyc53FeIg',
'LjXdkuCdQxCFT4hv8_ayew',
'X_tZn3TxQY28ltSyjZUGHQ',
page.id,
home.id
],
},
},
Expand Down
6 changes: 4 additions & 2 deletions config/datocms/migrations/1734713241_basicMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Client } from '@datocms/cli/lib/cma-client-node';

export default async function (client: Client) {
console.log('Create new models/block models');

const home = await client.itemTypes.find('home_page');
const page = await client.itemTypes.find('page');

console.log('Create model "\uD83D\uDDA5\uFE0F Website" (`app`)');
await client.itemTypes.create(
{
Expand Down Expand Up @@ -88,7 +90,7 @@ export default async function (client: Client) {
on_publish_with_unpublished_references_strategy: 'fail',
on_reference_unpublish_strategy: 'delete_references',
on_reference_delete_strategy: 'delete_references',
item_types: ['LjXdkuCdQxCFT4hv8_ayew', 'X_tZn3TxQY28ltSyjZUGHQ'],
item_types: [page.id, home.id],
},
required: {},
},
Expand Down
Loading