0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/core/server/data/migrations/init/1-create-tables.js
Daniel Lockyer ddaa4ee5e1 Removed unnecessary checks during initial DB init
refs https://github.com/TryGhost/Toolbox/issues/202

- during DB init, we have to create all the tables
- right now we loop over all tables and call the `createTable` command
  - this command checks if the table exists and if not, creates the table
  - this works fine but it means we query the database for every table
  - in MySQL, we query the information_schema table, which we've seen
    issues with before because it doesn't have indexes
- the smarter thing to do here is to get all the tables that already exist,
  remove them from the list, and just straight up create them without
  further checks
- this entire thing should be protected by the migration lock so we
  shouldn't encounter issues from multiple processes initializing the DB
  and tables existing after the initial check
- this commit also removes the check from `createTable` because this isn't
  really needed. We should be using the migration utils, which do
  check for existing tables. I've added a note to the function and
  audited anywhere we still call the function
- this commit removes (- 49 tables + 1 initial check) 48 queries from
  the initial DB init
2022-02-21 08:44:40 +01:00

32 lines
1.1 KiB
JavaScript

const Promise = require('bluebird');
const commands = require('../../schema').commands;
const schema = require('../../schema').tables;
const logging = require('@tryghost/logging');
const schemaTables = Object.keys(schema);
module.exports.up = async (options) => {
const connection = options.connection;
const existingTables = await commands.getTables(connection);
const missingTables = schemaTables.filter(t => !existingTables.includes(t));
await Promise.mapSeries(missingTables, async (table) => {
logging.info('Creating table: ' + table);
await commands.createTable(table, connection);
});
};
/**
@TODO: This works, but is very dangerous in the current state of the knex-migrator v3.
@TODO: Decide if we should enable or delete this
module.exports.down = async (options) => {
var connection = options.connection;
// Reference between tables!
schemaTables.reverse();
await Promise.mapSeries(schemaTables, async (table) => {
logging.info('Drop table: ' + table);
await commands.deleteTable(table, connection);
});
};
*/