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

Consolidate content versioning #22413

Merged
merged 31 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
1ca7a89
Add migration
licitdev Apr 17, 2024
0215517
Use the new delta field
licitdev Apr 17, 2024
7ff19fb
Add cast-json flag
licitdev Apr 17, 2024
ca86cb0
Fix typing
licitdev Apr 17, 2024
da645fa
Fetch existing deltas if version created during migration
licitdev Apr 17, 2024
f2f757d
Add changeset
licitdev Apr 17, 2024
d50f17a
Add version delta field into sdk schema
licitdev Apr 24, 2024
06a97d3
Update migration timestamp
licitdev May 9, 2024
c0ad03f
Merge branch 'main' into consolidate-content-versioning
paescuj May 9, 2024
4fed821
Merge branch 'main' into consolidate-content-versioning
licitdev Jun 5, 2024
1c529f8
Update versions.save() to return finalVersionDelta
licitdev Jun 5, 2024
bb1c08c
Merge branch 'main' into consolidate-content-versioning
paescuj Jul 19, 2024
f232bb2
Sort on DB level
paescuj Jul 19, 2024
a66c3c5
Update migration date
paescuj Jul 19, 2024
77687b0
Disallow passing delta via create/update
paescuj Jul 22, 2024
b0bdb75
Update docs & specs
paescuj Jul 22, 2024
33777db
Fix save response
paescuj Jul 22, 2024
3aac8d2
Remove unnecessary access check
paescuj Jul 22, 2024
b98ba91
Update changeset
paescuj Jul 22, 2024
c957341
Don't require update perms on versions for save
paescuj Jul 22, 2024
5372522
Optimize validateCreateData
paescuj Jul 22, 2024
37e20cf
Merge branch 'main' into consolidate-content-versioning
ComfortablyCoding Aug 7, 2024
ea39c04
update to new validateAccess
ComfortablyCoding Aug 7, 2024
78bac94
Update docs/reference/system/versions.md
ComfortablyCoding Aug 8, 2024
7f25407
Merge branch 'main' into consolidate-content-versioning
licitdev Aug 21, 2024
2e2f50f
Remove migration of delta
licitdev Aug 22, 2024
8065a1c
Rename to legacy
licitdev Aug 22, 2024
3c3083a
Add missed changes for Remove migration of delta in 2e2f50fa67bb82130…
licitdev Aug 22, 2024
ee971cb
Merge branch 'main' into consolidate-content-versioning
ComfortablyCoding Aug 28, 2024
0c74124
Merge branch 'main' into consolidate-content-versioning
ComfortablyCoding Sep 3, 2024
52026a0
Update docs/reference/system/versions.md
ComfortablyCoding Sep 6, 2024
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
7 changes: 7 additions & 0 deletions .changeset/loud-avocados-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---

Check warning on line 1 in .changeset/loud-avocados-serve.md

View workflow job for this annotation

GitHub Actions / Lint

File ignored by default.

Check warning on line 1 in .changeset/loud-avocados-serve.md

View workflow job for this annotation

GitHub Actions / Lint

File ignored by default.
'@directus/system-data': patch
'@directus/types': patch
'@directus/api': patch
---

Consolidated content versioning with a new `delta` field in `directus_versions`
14 changes: 10 additions & 4 deletions api/src/controllers/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,15 @@ router.get(

const { outdated, mainHash } = await service.verifyHash(version['collection'], version['item'], version['hash']);

const saves = await service.getVersionSavesById(version['id']);
let current;

const current = assign({}, ...saves);
if (version['delta']) {
current = version['delta'];
} else {
const saves = await service.getVersionSavesById(version['id']);

current = assign({}, ...saves);
}

const main = await service.getMainItem(version['collection'], version['item']);

Expand All @@ -238,9 +244,9 @@ router.post(

await service.save(req.params['pk']!, req.body);

const saves = await service.getVersionSavesById(req.params['pk']!);
const updatedVersion = await service.readOne(req.params['pk']!);
licitdev marked this conversation as resolved.
Show resolved Hide resolved

const result = assign(mainItem, ...saves);
const result = assign(mainItem, updatedVersion['delta']);

res.locals['payload'] = { data: result || null };

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { Knex } from 'knex';
import { assign, sortBy } from 'lodash-es';

export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_versions', (table) => {
table.json('delta');
});

const versions = await knex.select('id').from('directus_versions');

for (const version of versions) {
const deltas = sortBy(
await knex.select('id', 'delta').from('directus_revisions').where('version', '=', version.id),
'id',
).map((revision) => (typeof revision.delta === 'string' ? JSON.parse(revision.delta) : revision.delta));
paescuj marked this conversation as resolved.
Show resolved Hide resolved

const finalVersionDelta = assign({}, ...deltas);

await knex('directus_versions')
.update({ delta: JSON.stringify(finalVersionDelta) })
.where('id', '=', version.id);
}
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_versions', (table) => {
table.dropColumn('delta');
});
}
31 changes: 27 additions & 4 deletions api/src/services/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ export class VersionsService extends ItemsService {

if (!versions?.[0]) return null;

if (versions[0]['delta']) {
return [versions[0]['delta']];
}

const saves = await this.getVersionSavesById(versions[0]['id']);
licitdev marked this conversation as resolved.
Show resolved Hide resolved

return saves;
Expand Down Expand Up @@ -167,10 +171,11 @@ export class VersionsService extends ItemsService {
}

override async updateMany(keys: PrimaryKey[], data: Partial<Item>, opts?: MutationOptions): Promise<PrimaryKey[]> {
// Only allow updates on "key" and "name" fields
// Only allow updates on "key", "name" and "delta" fields
paescuj marked this conversation as resolved.
Show resolved Hide resolved
const versionUpdateSchema = Joi.object({
key: Joi.string(),
name: Joi.string().allow(null).optional(),
delta: Joi.object().optional(),
});

const { error } = versionUpdateSchema.validate(data);
Expand Down Expand Up @@ -253,6 +258,18 @@ export class VersionsService extends ItemsService {
delta: revisionDelta,
});

let existingDelta = version['delta'];

if (!existingDelta) {
const saves = await this.getVersionSavesById(key);

existingDelta = assign({}, ...saves);
}

const finalVersionDelta = assign({}, existingDelta, revisionDelta ? JSON.parse(revisionDelta) : null);

await this.updateOne(key, { delta: finalVersionDelta });
paescuj marked this conversation as resolved.
Show resolved Hide resolved

const { cache } = getCache();

if (shouldClearCache(cache, undefined, collection)) {
Expand All @@ -263,7 +280,7 @@ export class VersionsService extends ItemsService {
}

async promote(version: PrimaryKey, mainHash: string, fields?: string[]) {
const { id, collection, item } = (await this.readOne(version)) as ContentVersion;
const { id, collection, item, delta } = (await this.readOne(version)) as ContentVersion;

// will throw an error if the accountability does not have permission to update the item
await this.authorizationService.checkAccess('update', collection, item);
Expand All @@ -276,9 +293,15 @@ export class VersionsService extends ItemsService {
});
}

const saves = await this.getVersionSavesById(id);
let versionResult;

const versionResult = assign({}, ...saves);
if (delta) {
versionResult = delta;
} else {
const saves = await this.getVersionSavesById(id);

versionResult = assign({}, ...saves);
}

const payloadToUpdate = fields ? pick(versionResult, fields) : versionResult;

Expand Down
4 changes: 4 additions & 0 deletions packages/system-data/src/fields/versions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ fields:
- field: user_updated
special:
- user-updated

- field: delta
special:
- cast-json
1 change: 1 addition & 0 deletions packages/types/src/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export type ContentVersion = {
date_updated: string | null;
user_created: string | null;
user_updated: string | null;
delta: Record<string, any> | null;
};
1 change: 1 addition & 0 deletions sdk/src/schema/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ export type DirectusVersion<Schema extends object> = MergeCoreCollection<
date_updated: 'datetime' | null;
user_created: DirectusUser<Schema> | string | null;
user_updated: DirectusUser<Schema> | string | null;
delta: Record<string, any> | null;
}
>;
Loading