-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added index for redirects lookup (#21783)
ref https://linear.app/ghost/issue/ENG-1811/ - added first migration for truncating column length; required in order to be able to add an index - added second migration for creating the index After sending a newsletter, Ghost can struggle with the amount of incoming traffic (particularly link scanners) with particularly large pools of recipients. Part of this is link lookup, which an index has shown to help with when added manually on the db. Given that, this change is to make it native. I ran a stats service query to gather data on usage, as well as did code review, and I've not seen any use of this table that isn't using the 8 digit slug, i.e. /r/12345678, or 11 characters total. I made this 191 in case that use case changes, while still allowing us the value of an index. Note: SQLite does not support altering columns to my knowledge, so we have to create a temp table with the new column data to push back in. This should be ok because use of SQLite should be limited. In the past, I've seen migrations skipped for SQLite and I'd rather not do that here if able.
- Loading branch information
Showing
4 changed files
with
52 additions
and
2 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
.../server/data/migrations/versions/5.102/2024-12-02-17-32-40-alter-length-redirects-from.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// For information on writing migrations, see https://www.notion.so/ghost/Database-migrations-eb5b78c435d741d2b34a582d57c24253 | ||
|
||
const logging = require('@tryghost/logging'); | ||
const {createNonTransactionalMigration} = require('../../utils'); | ||
|
||
module.exports = createNonTransactionalMigration( | ||
async function up(knex) { | ||
logging.info('Altering length of redirects.from'); | ||
await knex.schema.alterTable('redirects', function (table) { | ||
table.string('from', 191).notNullable().alter(); | ||
}); | ||
}, | ||
async function down(knex) { | ||
logging.info('Reverting length of redirects.from'); | ||
await knex.schema.alterTable('redirects', function (table) { | ||
table.string('from', 191).notNullable().alter(); | ||
}); | ||
} | ||
); |
31 changes: 31 additions & 0 deletions
31
...ore/server/data/migrations/versions/5.102/2024-12-02-17-48-40-add-index-redirects-from.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// For information on writing migrations, see https://www.notion.so/ghost/Database-migrations-eb5b78c435d741d2b34a582d57c24253 | ||
|
||
const logging = require('@tryghost/logging'); | ||
const {createNonTransactionalMigration} = require('../../utils'); | ||
const {addIndex, dropIndex} = require('../../../schema/commands'); | ||
const errors = require('@tryghost/errors'); | ||
|
||
module.exports = createNonTransactionalMigration( | ||
async function up(knex) { | ||
logging.info('Adding index to redirects.from'); | ||
|
||
const columnInfo = await knex('redirects').columnInfo('from'); | ||
// knex is wrong; it's returning a string not a number so we re-cast it to satisfy the type checker | ||
if (columnInfo.maxLength.toString() !== '191') { | ||
logging.error(`Column length is not 191. Ensure the previous migration has been applied successfully. Column info: ${JSON.stringify(columnInfo)}`); | ||
throw new errors.MigrationError({ | ||
message: 'Column length is not 191. Ensure the previous migration has been applied successfully.' | ||
}); | ||
} | ||
|
||
await addIndex('redirects', ['from'], knex); | ||
}, | ||
async function down(knex) { | ||
logging.info('Removing index from redirects.from'); | ||
try { | ||
await dropIndex('redirects', ['from'], knex); | ||
} catch (error) { | ||
logging.error(`Error removing index from redirects.from: ${error}`); | ||
} | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters