From be6b9e437f66b43f545ea2231316e1a03219377d Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Sat, 13 Jan 2024 17:35:33 +0100 Subject: [PATCH] Refactored fetching schema tables in data generator - we want to pass in the schema tables instead of cross requiring them from a different package because it means the package isn't standalone and moving the code structure around breaks the data generator --- ghost/core/core/cli/generate-data.js | 3 +++ ghost/data-generator/lib/DataGenerator.js | 5 +++-- ghost/data-generator/test/data-generator.test.js | 5 ++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/ghost/core/core/cli/generate-data.js b/ghost/core/core/cli/generate-data.js index 3039f017d6..496240f3b1 100644 --- a/ghost/core/core/cli/generate-data.js +++ b/ghost/core/core/cli/generate-data.js @@ -3,6 +3,8 @@ const Command = require('./command'); const DataGenerator = require('@tryghost/data-generator'); const config = require('../shared/config'); +const schemaTables = require('../server/data/schema').tables; + module.exports = class DataGeneratorCommand extends Command { setup() { this.help('Generates random data to populate the database for development & testing'); @@ -74,6 +76,7 @@ module.exports = class DataGeneratorCommand extends Command { baseUrl: config.getSiteUrl(), clearDatabase: argv['clear-database'], tables, + schemaTables, withDefault: argv['with-default'], printDependencies: argv['print-dependencies'], quantities, diff --git a/ghost/data-generator/lib/DataGenerator.js b/ghost/data-generator/lib/DataGenerator.js index ce35f01fd0..0e94911e37 100644 --- a/ghost/data-generator/lib/DataGenerator.js +++ b/ghost/data-generator/lib/DataGenerator.js @@ -13,7 +13,6 @@ const importers = require('./importers').reduce((acc, val) => { acc[val.table] = val; return acc; }, {}); -const schema = require('../../core/core/server/data/schema').tables; class DataGenerator { /** @@ -25,6 +24,7 @@ class DataGenerator { constructor({ knex, tables, + schemaTables, clearDatabase = false, baseDataPack = '', baseUrl, @@ -36,6 +36,7 @@ class DataGenerator { }) { this.knex = knex; this.tableList = tables || []; + this.schemaTables = schemaTables; this.willClearData = clearDatabase; this.useBaseDataPack = baseDataPack !== ''; this.baseDataPack = baseDataPack; @@ -53,7 +54,7 @@ class DataGenerator { table.importer = importers[table.name]; // eslint-disable-next-line no-unused-vars - table.dependencies = Object.entries(schema[table.name]).reduce((acc, [_col, data]) => { + table.dependencies = Object.entries(this.schemaTables[table.name]).reduce((acc, [_col, data]) => { if (data.references) { const referencedTable = data.references.split('.')[0]; // The ghost_subscriptions_id property has a foreign key to the subscriptions table, but we don't use that table yet atm, so don't add it as a dependency diff --git a/ghost/data-generator/test/data-generator.test.js b/ghost/data-generator/test/data-generator.test.js index a947969437..e70b17a33e 100644 --- a/ghost/data-generator/test/data-generator.test.js +++ b/ghost/data-generator/test/data-generator.test.js @@ -13,6 +13,8 @@ const DataGenerator = require('../index'); const schema = require('../../core/core/server/data/schema'); +const schemaTables = schema.tables; + describe('Data Generator', function () { let db; @@ -76,7 +78,8 @@ describe('Data Generator', function () { const dataGenerator = new DataGenerator({ eventsOnly: false, knex: db, - schema: schema, + schema, + schemaTables, logger: { info: () => { }, ok: () => { }