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
This commit is contained in:
Daniel Lockyer 2024-01-13 17:35:33 +01:00 committed by Daniel Lockyer
parent 09921fd2b4
commit be6b9e437f
3 changed files with 10 additions and 3 deletions

View File

@ -3,6 +3,8 @@ const Command = require('./command');
const DataGenerator = require('@tryghost/data-generator'); const DataGenerator = require('@tryghost/data-generator');
const config = require('../shared/config'); const config = require('../shared/config');
const schemaTables = require('../server/data/schema').tables;
module.exports = class DataGeneratorCommand extends Command { module.exports = class DataGeneratorCommand extends Command {
setup() { setup() {
this.help('Generates random data to populate the database for development & testing'); 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(), baseUrl: config.getSiteUrl(),
clearDatabase: argv['clear-database'], clearDatabase: argv['clear-database'],
tables, tables,
schemaTables,
withDefault: argv['with-default'], withDefault: argv['with-default'],
printDependencies: argv['print-dependencies'], printDependencies: argv['print-dependencies'],
quantities, quantities,

View File

@ -13,7 +13,6 @@ const importers = require('./importers').reduce((acc, val) => {
acc[val.table] = val; acc[val.table] = val;
return acc; return acc;
}, {}); }, {});
const schema = require('../../core/core/server/data/schema').tables;
class DataGenerator { class DataGenerator {
/** /**
@ -25,6 +24,7 @@ class DataGenerator {
constructor({ constructor({
knex, knex,
tables, tables,
schemaTables,
clearDatabase = false, clearDatabase = false,
baseDataPack = '', baseDataPack = '',
baseUrl, baseUrl,
@ -36,6 +36,7 @@ class DataGenerator {
}) { }) {
this.knex = knex; this.knex = knex;
this.tableList = tables || []; this.tableList = tables || [];
this.schemaTables = schemaTables;
this.willClearData = clearDatabase; this.willClearData = clearDatabase;
this.useBaseDataPack = baseDataPack !== ''; this.useBaseDataPack = baseDataPack !== '';
this.baseDataPack = baseDataPack; this.baseDataPack = baseDataPack;
@ -53,7 +54,7 @@ class DataGenerator {
table.importer = importers[table.name]; table.importer = importers[table.name];
// eslint-disable-next-line no-unused-vars // 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) { if (data.references) {
const referencedTable = data.references.split('.')[0]; 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 // 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

View File

@ -13,6 +13,8 @@ const DataGenerator = require('../index');
const schema = require('../../core/core/server/data/schema'); const schema = require('../../core/core/server/data/schema');
const schemaTables = schema.tables;
describe('Data Generator', function () { describe('Data Generator', function () {
let db; let db;
@ -76,7 +78,8 @@ describe('Data Generator', function () {
const dataGenerator = new DataGenerator({ const dataGenerator = new DataGenerator({
eventsOnly: false, eventsOnly: false,
knex: db, knex: db,
schema: schema, schema,
schemaTables,
logger: { logger: {
info: () => { }, info: () => { },
ok: () => { } ok: () => { }