From 92fb2a54aefdef29484aa966eba6ef09ca19c49c Mon Sep 17 00:00:00 2001 From: Naz Date: Tue, 8 Aug 2023 12:54:19 +0800 Subject: [PATCH] Fixed non-deretministic post ordering in content gating test suite refs https://github.com/TryGhost/Ghost/pull/17609 - The tests for content gating started to fail with introduction of the index on `published_at` data in for `posts` table. The reason for the failure was identical `published_at` date set during the fixture insertion, making the returned results change order non-deterministically. The problem is mostly in how the test is set up as it's quite unrealistic to have multiple posts in the system inserted at the same time down to millisecond. Maybe... by some coincidence, but thats not a problem we should care too much about imo. --- ...23-08-07-11-17-05-add-posts-published-at-index.js | 5 +++-- ghost/core/test/regression/api/content/posts.test.js | 12 ++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/ghost/core/core/server/data/migrations/versions/5.59/2023-08-07-11-17-05-add-posts-published-at-index.js b/ghost/core/core/server/data/migrations/versions/5.59/2023-08-07-11-17-05-add-posts-published-at-index.js index abf78fcaba..5784ac5d9b 100644 --- a/ghost/core/core/server/data/migrations/versions/5.59/2023-08-07-11-17-05-add-posts-published-at-index.js +++ b/ghost/core/core/server/data/migrations/versions/5.59/2023-08-07-11-17-05-add-posts-published-at-index.js @@ -1,4 +1,5 @@ const logging = require('@tryghost/logging'); +const DatabaseInfo = require('@tryghost/database-info'); const {createNonTransactionalMigration} = require('../../utils'); const INDEX_NAME = 'posts_published_at_index'; @@ -7,7 +8,7 @@ module.exports = createNonTransactionalMigration( async function up(knex) { let hasIndex = false; - if (knex.client.config.client === 'sqlite3') { + if (DatabaseInfo.isSQLite(knex)) { const result = await knex.raw(`select * from sqlite_master where type = 'index' and tbl_name = 'posts' and name = '${INDEX_NAME}'`); hasIndex = (result.length !== 0); } else { @@ -29,7 +30,7 @@ module.exports = createNonTransactionalMigration( async function down(knex) { let missingIndex = false; - if (knex.client.config.client === 'sqlite3') { + if (DatabaseInfo.isSQLite(knex)) { const result = await knex.raw(`select * from sqlite_master where type = 'index' and tbl_name = 'posts' and name = '${INDEX_NAME}'`); missingIndex = (result.length === 0); } else { diff --git a/ghost/core/test/regression/api/content/posts.test.js b/ghost/core/test/regression/api/content/posts.test.js index 7f86ae434f..c7296c46f6 100644 --- a/ghost/core/test/regression/api/content/posts.test.js +++ b/ghost/core/test/regression/api/content/posts.test.js @@ -302,24 +302,28 @@ describe('api/endpoints/content/posts', function () { before (function () { publicPost = testUtils.DataGenerator.forKnex.createPost({ slug: 'free-to-see', - visibility: 'public' + visibility: 'public', + published_at: new Date('2023-07-15T04:20:30.000+00:00') }); membersPost = testUtils.DataGenerator.forKnex.createPost({ slug: 'thou-shalt-not-be-seen', - visibility: 'members' + visibility: 'members', + published_at: new Date('2023-07-20T04:20:30.000+00:00') }); paidPost = testUtils.DataGenerator.forKnex.createPost({ slug: 'thou-shalt-be-paid-for', - visibility: 'paid' + visibility: 'paid', + published_at: new Date('2023-07-25T04:20:30.000+00:00') }); membersPostWithPaywallCard = testUtils.DataGenerator.forKnex.createPost({ slug: 'thou-shalt-have-a-taste', visibility: 'members', mobiledoc: '{"version":"0.3.1","markups":[],"atoms":[],"cards":[["paywall",{}]],"sections":[[1,"p",[[0,[],0,"Free content"]]],[10,0],[1,"p",[[0,[],0,"Members content"]]]]}', - html: '

Free content

Members content

' + html: '

Free content

Members content

', + published_at: new Date('2023-07-30T04:20:30.000+00:00') }); return testUtils.fixtures.insertPosts([