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

Error updating to Ghost 5.80 from 5.23, blog down, please help #19839

Open
1 task done
DigitalLeaves opened this issue Mar 12, 2024 · 5 comments
Open
1 task done

Error updating to Ghost 5.80 from 5.23, blog down, please help #19839

DigitalLeaves opened this issue Mar 12, 2024 · 5 comments
Labels
OSS P3 - Medium [triage] Bugs which we'll fix soon

Comments

@DigitalLeaves
Copy link

DigitalLeaves commented Mar 12, 2024

Issue Summary

Hello, I just updated my Ghost blog from 5.23 to 5.80.
After the update, I tried to restart Ghost, but it fails with this message:

Ghost instance is not running! Starting...
✖ Starting Ghost
A GhostError occurred.

Message: Ghost was able to start, but errored during boot with: insert into `collections_posts` (`collection_id`, `id`, `post_id`, `sort_order`) select '65f02f0dafb7eeb96c4c90a6' as `collection_id`, '65f02f0dafb7eeb96c4c90a8' as `id`, '637a812a81cfb759fad27265' as `post_id`, 0 as `sort_order` union all select '65f02f0dafb7eeb96c4c90a6' as `collection_id`,
... (many more rows) ...
, '65314decb0c970c6a6eca545' as `post_id`, 0 as `sort_order` - SQLITE_ERROR: too many terms in compound SELECT
Context: [object Object]
Help: Error occurred while executing the following migration: 2023-07-10-05-16-55-add-built-in-collection-posts.js

Debug Information:
OS: Debian GNU/Linux, v11
Node Version: v18.19.1
Ghost Version: 5.80.2
Ghost-CLI Version: 1.25.3
Environment: development
Command: 'ghost restart'
mysql Ver 8.0.27 for Linux on x86_64 (MySQL Community Server - GPL)

Steps to Reproduce

Start with an install of v5.23 or below 5.80
Apparently the number of posts can be a cause, so get more than 500 posts.
Run ghost update and ghost restart

Ghost Version

5.80.2

Node.js Version

v18.19.1

How did you install Ghost?

Command line

Database type

MySQL 5.7

Browser & OS version

Chrome, MacOS

Relevant log / error output

Ghost instance is not running! Starting...
✖ Starting Ghost
A GhostError occurred.
 
Message: Ghost was able to start, but errored during boot with: insert into `collections_posts` (`collection_id`, `id`, `post_id`, `sort_order`) select '65f03420dd3af6bb83e5ad3d' as `collection_id`, '65f03420dd3af6bb83e5ad3f' as `id`, '637a812a81cfb759fad27265' as `post_id`, 0 as `sort_order` union all select '65f03420dd3af6bb83e5ad3d' as `collection_id`, '65f03420dd3af6bb83e5ad40' as `id`, '637a812a81cfb759fad27266' as `post_id`, 0 as `sort_order` union all select '65f03420dd3af6bb83e5ad3d' as `collection_id`, '65f03420dd3af6bb83e5ad41' as `id`, '637a812a81cfb759fad27267'
... (rows ommited)...
, '65f03420dd3af6bb83e5afe3' as `id`, '65314decb0c970c6a6eca545' as `post_id`, 0 as `sort_order` - SQLITE_ERROR: too many terms in compound SELECT
Context: [object Object]
Help: Error occurred while executing the following migration: 2023-07-10-05-16-55-add-built-in-collection-posts.js

Code of Conduct

  • I agree to be friendly and polite to people in this repository
@github-actions github-actions bot added the needs:triage [triage] this needs to be triaged by the Ghost team label Mar 12, 2024
@DigitalLeaves
Copy link
Author

Ok, I managed to solve it manually. The issue is in the script 2023-07-10-05-16-55-add-built-in-collection-posts.js.

Specifically, in this function:

const insertPostCollections = async (knex, collectionId, postIds) => {
    logging.warn(`Batch inserting ${postIds.length} collection posts for collection ${collectionId}`);

    const collectionPosts = postIds.map((postId) => {
        return {
            id: (new ObjectID()).toHexString(),
            collection_id: collectionId,
            post_id: postId,
            sort_order: 0
        };
    });

    await knex.batchInsert('collections_posts', collectionPosts, 1000);
};

The problem is the batch size. I guess it does not make sense to have it set to 1000 if the maximum size for batch inserts in MySQL is 500. So lowering to 100 for example does the trick.

Now, can you kindly confirm that even by reducing the batch size to 100, all posts would be updated in the database as per the new Ghost 5.80 requirements? (looking at the code, it seems that is the case, but just want to be sure).

Thank you. A PR here would be quite easy I guess, just decreasing the limit to 100.

@daniellockyer daniellockyer added the P2 label Mar 12, 2024 — with Linear
@daniellockyer daniellockyer removed the needs:triage [triage] this needs to be triaged by the Ghost team label Mar 12, 2024
@ErisDS ErisDS removed the P2 label Jun 14, 2024
@ErisDS ErisDS added the P2 label Jun 14, 2024 — with Linear
@ErisDS ErisDS added P3 - Medium [triage] Bugs which we'll fix soon and removed P2 labels Jun 27, 2024
@allouis
Copy link
Contributor

allouis commented Oct 25, 2024

In order to fix this issue we need a few things:

  1. A replication of the error
  2. A batchSize which fixes the error
  3. A new helper which wraps knex.batchInsert and forces it to use our safe batch size
  4. Updating all uses of the knex.batchInsert to use the new helper

@markstos
Copy link
Contributor

@allouis It's possible to take a "good now, better later" approach here.

If the PR #21063 is adjusted down to use a value of 100, it will solve some known, reported case were people are running into this SQLite limit and the code quality will be no worse than the current code which hardcodes a limit of 1,000 rather than 100. That's a good solution that can help now. Per above, the value of 100 has been tested.

What you describe here is a more ideal solution with a test-suite-ready replication and a helper method. Although, the helper method will only help if people remember to use it instead of knex.batchInsert.

An alternate path forward is to merge the PR adjusted to a batch size of 100 while also leaving this issue open for a more ideal approach later.

@allouis
Copy link
Contributor

allouis commented Oct 28, 2024

@markstos Absolutely! If this is still an issue for people and we have a PR that solves it - I'm happy to merge 😁

FWIW I wasn't asking for a test suite replication - a manual one would have been fine - the problem is that the original PR didn't fix the issue - so I'd like to be sure that anything we merge does fix an issue!

I'll reopen that PR for you now - thank you for coming back and taking an interest - it's appreciated!

markstos added a commit to markstos/Ghost that referenced this issue Oct 29, 2024
SQLite has limit of 500 items in a compound select statement.

This limit could be hit when a complex SELECT statement was being generated
as part of a batch insert statement.

Lowering the batch size will have minimal impact on migration performance
while improving SQLite compatibility.

Ref: https://www.sqlite.org/limits.html
Issue: TryGhost#19839
allouis pushed a commit that referenced this issue Nov 5, 2024
refs #19839
refs https://www.sqlite.org/limits.html

SQLite has limit of 500 items in a compound select statement.

This limit could be hit when a complex select statement was being
generated as part of a batch insert statement.

Lowering the batch size will have minimal impact on migration
performance while improving SQLite compatibility.

One of these bulk inserts is confirmed to be affected through the linked
issue.

I didn't confirm if the other two cases would trigger it, but the change
won't hurt there either.
@markstos
Copy link
Contributor

markstos commented Nov 6, 2024

This can be closed, it was resolved by #21063 in the next release after 5.99.

@ErisDS ErisDS added the OSS label Nov 8, 2024 — with Linear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OSS P3 - Medium [triage] Bugs which we'll fix soon
Projects
None yet
Development

No branches or pull requests

5 participants