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

feat: discard drafts when enabling draft and publish on ct #20257

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Knex = Parameters<Migration['up']>[0];
* Versions with only a draft version will be ignored.
* Only versions with a published version (which always have a draft version) will be discarded.
*/
async function* getBatchToDiscard({
export async function* getBatchToDiscard({
db,
trx,
uid,
Expand Down
90 changes: 31 additions & 59 deletions packages/core/core/src/migrations/draft-publish.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { contentTypes as contentTypesUtils } from '@strapi/utils';
import { contentTypes as contentTypesUtils, async } from '@strapi/utils';
import { Schema } from '@strapi/types';

import { getBatchToDiscard } from './database/5.0.0-discard-drafts';

interface Input {
oldContentTypes: Record<string, Schema.ContentType>;
contentTypes: Record<string, Schema.ContentType>;
Expand All @@ -20,66 +22,36 @@ const enableDraftAndPublish = async ({ oldContentTypes, contentTypes }: Input) =
}

// run the after content types migrations

for (const uid in contentTypes) {
if (!oldContentTypes[uid]) {
continue;
}

const oldContentType = oldContentTypes[uid];
const contentType = contentTypes[uid];

// if d&p was enabled set publishedAt to eq createdAt
if (
!contentTypesUtils.hasDraftAndPublish(oldContentType) &&
contentTypesUtils.hasDraftAndPublish(contentType)
) {
const metadata = strapi.db.metadata.get(uid);

// Extract all scalar attributes to use in the insert query
const attributes = Object.values(metadata.attributes).reduce((acc, attribute: any) => {
if (['id'].includes(attribute.columnName)) {
return acc;
}

if (contentTypesUtils.isScalarAttribute(attribute)) {
acc.push(attribute.columnName);
return strapi.db.transaction(async (trx) => {
for (const uid in contentTypes) {
if (!oldContentTypes[uid]) {
continue;
}

const oldContentType = oldContentTypes[uid];
const contentType = contentTypes[uid];

// if d&p was enabled set publishedAt to eq createdAt
if (
!contentTypesUtils.hasDraftAndPublish(oldContentType) &&
contentTypesUtils.hasDraftAndPublish(contentType)
) {
const discardDraft = async (entry: { documentId: string; locale: string }) =>
strapi
.documents(uid as any)
// Discard draft by referencing the documentId and locale
.discardDraft({ documentId: entry.documentId, locale: entry.locale });

/**
* Load a batch of entries (batched to prevent loading millions of rows at once ),
* and discard them using the document service.
*/
for await (const batch of getBatchToDiscard({ db: strapi.db, trx, uid })) {
await async.map(batch, discardDraft, { concurrency: 10 });
}

return acc;
}, [] as string[]);

/**
* INSERT INTO tableName (columnName1, columnName2, columnName3, ...)
* SELECT columnName1, columnName2, columnName3, ...
* FROM tableName
*/
const qb = strapi.db?.getConnection();
await qb
// INSERT INTO tableName (columnName1, columnName2, columnName3, ...)
.into(qb.raw(`${metadata.tableName} (${attributes.join(', ')})`))
.insert((subQb: typeof qb) => {
// SELECT columnName1, columnName2, columnName3, ...
subQb
.select(
...attributes.map((att) => {
// Override 'publishedAt' and 'updatedAt' attributes
if (att === 'published_at') {
return qb.raw('NULL as published_at');
}

if (att === 'updated_at') {
return qb.raw(`? as updated_at`, [new Date()]);
}

return att;
})
)
.from(metadata.tableName)
.whereNotNull('published_at');
});
}
}
}
});
};

const disableDraftAndPublish = async ({ oldContentTypes, contentTypes }: Input) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ describe('Migration - draft and publish', () => {

// Published dog should have a publishedAt value
expect(draftDog.publishedAt).toBe(null);
// Updated at value should be different
expect(draftDog.updatedAt).not.toBe(publishedDog.updatedAt);
// Updated at value should be the same
expect(draftDog.updatedAt).toBe(publishedDog.updatedAt);
});

data.dogs = sortDogs(dogs.filter((dog) => dog.publishedAt));
Expand Down