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

Added index for redirects lookup #21783

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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();
});
}
);
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}`);
}
}
);
2 changes: 1 addition & 1 deletion ghost/core/core/server/data/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ module.exports = {
},
redirects: {
id: {type: 'string', maxlength: 24, nullable: false, primary: true},
from: {type: 'string', maxlength: 2000, nullable: false},
from: {type: 'string', maxlength: 191, nullable: false, index: true},
to: {type: 'string', maxlength: 2000, nullable: false},
post_id: {type: 'string', maxlength: 24, nullable: true, unique: false, references: 'posts.id', setNullDelete: true},
created_at: {type: 'dateTime', nullable: false},
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/unit/server/data/schema/integrity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const validateRouteSettings = require('../../../../../core/server/services/route
*/
describe('DB version integrity', function () {
// Only these variables should need updating
const currentSchemaHash = 'f12341a0c74998eeb4628322fd0982fb';
const currentSchemaHash = 'b26690fb57ffd0edbddb4cd9e02b17d6';
const currentFixturesHash = '80e79d1efd5da275e19cb375afb4ad04';
const currentSettingsHash = '47a75e8898fab270174a0c905cb3e914';
const currentRoutesHash = '3d180d52c663d173a6be791ef411ed01';
Expand Down
Loading