0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-08 02:52:39 -05:00

Fixed collections ordering

refs https://github.com/TryGhost/Arch/issues/25

- When run against different DB Engines the returned order of collections belonging to a post is not consistent (SQLite vs MySQL). Having a primitive ordering by slug allows to keep the order compatible
This commit is contained in:
Naz 2023-07-12 20:14:28 +08:00 committed by naz
parent 9bfb197a85
commit c733424cc5
4 changed files with 35 additions and 22 deletions

View file

@ -401,7 +401,11 @@ export class CollectionsService {
filter: `posts:${postId}`
});
return collections.map(collection => this.toDTO(collection));
return collections.map(collection => this.toDTO(collection))
.sort((a, b) => {
// NOTE: sorting is here to keep DB engine ordering consistent
return a.slug.localeCompare(b.slug);
});
}
async destroy(id: string): Promise<Collection | null> {

View file

@ -117,15 +117,24 @@ describe('CollectionsService', function () {
it('Can get collections for a post', async function () {
const collection = await collectionsService.createCollection({
title: 'testing collections',
slug: 'testing-collections',
type: 'manual'
});
const collection2 = await collectionsService.createCollection({
title: 'testing collections 1',
slug: '1-testing-collections',
type: 'manual'
});
await collectionsService.addPostToCollection(collection.id, posts[0]);
await collectionsService.addPostToCollection(collection2.id, posts[0]);
const collections = await collectionsService.getCollectionsForPost(posts[0].id);
assert.equal(collections.length, 1, 'There should be one collection');
assert.equal(collections[0].id, collection.id, 'Collection should be the correct one');
assert.equal(collections.length, 2, 'There should be one collection');
assert.equal(collections[0].id, collection2.id, 'Collections should be sorted by slug');
assert.equal(collections[1].id, collection.id, 'Collections should be sorted by slug');
});
});

View file

@ -1440,6 +1440,23 @@ Object {
"codeinjection_foot": null,
"codeinjection_head": null,
"collections": Array [
Object {
"created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z/,
"description": null,
"feature_image": null,
"filter": null,
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"posts": Array [
Object {
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"sort_order": 0,
},
],
"slug": "collection-to-add",
"title": "Collection to add.",
"type": "manual",
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z/,
},
Object {
"created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z/,
"description": "Collection with all posts",
@ -1525,23 +1542,6 @@ Object {
"type": "automatic",
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z/,
},
Object {
"created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z/,
"description": null,
"feature_image": null,
"filter": null,
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"posts": Array [
Object {
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"sort_order": 0,
},
],
"slug": "collection-to-add",
"title": "Collection to add.",
"type": "manual",
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.\\\\d\\{3\\}Z/,
},
],
"comment_id": Any<String>,
"count": Object {

View file

@ -511,8 +511,8 @@ describe('Posts API', function () {
.matchBodySnapshot({
posts: [
Object.assign({}, matchPostShallowIncludes, {published_at: null}, {collections: [
buildCollectionMatcher(18),
collectionMatcher
collectionMatcher,
buildCollectionMatcher(18)
]}
)
]