Allowed relative paths for base data pack in data generation

refs: https://github.com/TryGhost/Toolbox/issues/453
This commit is contained in:
Sam Lord 2022-11-03 16:48:25 +00:00
parent 08250d44b4
commit 9cb95140fb
3 changed files with 84 additions and 12 deletions

View File

@ -24,9 +24,11 @@ const {
MembersSubscriptionCreatedEventsImporter,
MembersSubscribeEventsImporter
} = require('./tables');
const path = require('path');
const fs = require('fs/promises');
const {faker} = require('@faker-js/faker');
const JsonImporter = require('./utils/json-importer');
const {getProcessRoot} = require('@tryghost/root-utils');
/**
* @typedef {Object} DataGeneratorOptions
@ -85,15 +87,28 @@ class DataGenerator {
// Use an existant set of data for a more realisitic looking site
if (this.useBaseData) {
const baseData = JSON.parse(await (await fs.readFile(this.baseDataPack)).toString());
let baseDataPack = this.baseDataPack;
if (!path.isAbsolute(this.baseDataPack)) {
baseDataPack = path.join(getProcessRoot(), baseDataPack);
}
let baseData = {};
try {
baseData = JSON.parse(await (await fs.readFile(baseDataPack)).toString());
this.logger.info('Loaded data pack');
} catch (error) {
this.logger.error('Failed to load data pack: ', error);
throw error;
}
const jsonImporter = new JsonImporter(transaction);
// Must have at least 2 in base data set
await transaction('newsletters').delete();
newsletters = await jsonImporter.import({
name: 'newsletters',
data: baseData.newsletters
data: baseData.newsletters,
rows: ['sort_order']
});
newsletters.sort((a, b) => a.sort_order - b.sort_order);
await transaction('posts_authors').delete();
await transaction('posts_tags').delete();
@ -106,7 +121,7 @@ class DataGenerator {
name: 'posts',
data: baseData.posts
});
postsImporter.addNewsletters({posts});
await postsImporter.addNewsletters({posts});
posts = await transaction.select('id', 'newsletter_id').from('posts');
await transaction('tags').delete();
@ -151,10 +166,13 @@ class DataGenerator {
name: 'custom_theme_settings',
data: baseData.custom_theme_settings
});
this.logger.info('Completed JSON import');
} else {
const newslettersImporter = new NewslettersImporter(transaction);
// First newsletter is free, second is paid
newsletters = await newslettersImporter.import({amount: 2});
newsletters = await newslettersImporter.import({amount: 2, rows: ['sort_order']});
newsletters.sort((a, b) => a.sort_order - b.sort_order);
const postsImporter = new PostsImporter(transaction, {
newsletters
@ -176,7 +194,7 @@ class DataGenerator {
products = await productsImporter.import({amount: 4, rows: ['name', 'monthly_price', 'yearly_price']});
const stripeProductsImporter = new StripeProductsImporter(transaction);
stripeProducts = await stripeProductsImporter.importForEach(products.slice(1), {
stripeProducts = await stripeProductsImporter.importForEach(products.filter(product => product.name !== 'Free'), {
amount: 1,
rows: ['product_id', 'stripe_product_id']
});
@ -296,6 +314,8 @@ class DataGenerator {
// TODO: Feedback - members_feedback (relies on members and posts)
await transaction.commit();
this.logger.info('Completed random import');
}
}

View File

@ -26,6 +26,7 @@
},
"dependencies": {
"@faker-js/faker": "7.6.0",
"@tryghost/root-utils": "0.3.17",
"@tryghost/string": "0.2.1",
"probability-distributions": "0.9.1"
}

View File

@ -4302,7 +4302,7 @@
"@tryghost/root-utils" "^0.3.17"
debug "^4.3.1"
"@tryghost/elasticsearch@^3.0.4":
"@tryghost/elasticsearch@^3.0.2", "@tryghost/elasticsearch@^3.0.4":
version "3.0.4"
resolved "https://registry.yarnpkg.com/@tryghost/elasticsearch/-/elasticsearch-3.0.4.tgz#f8572122bb34a0192471226d2c5c9e61d049a821"
integrity sha512-ysExjEWI7VXWedmEbi757ub2avPRIZOAvzCWM5XC3vi9FyroW+invABmLfN9jsPX99eGpTWe1ENguslpLEvtdA==
@ -4384,7 +4384,7 @@
resolved "https://registry.yarnpkg.com/@tryghost/http-cache-utils/-/http-cache-utils-0.1.4.tgz#0bf143a0817c19658c54e34b4ad925058731b96c"
integrity sha512-Wa3XWkwOpgbjEHPZ0T+/lEPPKJXk1VNNtukpyS3Y58gGFhrt2u6h8ncAzWwml8A/cFTF6MxvCe/UGW6o/ZgOIw==
"@tryghost/http-stream@^0.1.13":
"@tryghost/http-stream@^0.1.10", "@tryghost/http-stream@^0.1.13":
version "0.1.13"
resolved "https://registry.yarnpkg.com/@tryghost/http-stream/-/http-stream-0.1.13.tgz#d7c60e0e6639a4d0c6f9446730ec4c2f8c7347a4"
integrity sha512-SjYFH9RJkU4lsrLwewdhjryBAo+ladUu1yydg6eWtBgdwGZ5z0RmYANxkFL0jkyaw+6U7ntCKMfm6GI8HT7w+Q==
@ -4500,7 +4500,24 @@
lodash "^4.17.21"
luxon "^1.26.0"
"@tryghost/logging@2.2.3", "@tryghost/logging@2.3.2", "@tryghost/logging@^2.2.3":
"@tryghost/logging@2.2.3":
version "2.2.3"
resolved "https://registry.yarnpkg.com/@tryghost/logging/-/logging-2.2.3.tgz#40575a42e18b907a49cee5dfdfa62deb820954aa"
integrity sha512-ACCm84U4HITt3mQhDSpyDLZetxzjYo4ux2MoSVGL3zxPfQBPFoI6hWEiSzYWX/4RGq2l2tR4di+5LWjIe8Ow6A==
dependencies:
"@tryghost/bunyan-rotating-filestream" "^0.0.7"
"@tryghost/elasticsearch" "^3.0.2"
"@tryghost/http-stream" "^0.1.10"
"@tryghost/pretty-stream" "^0.1.11"
"@tryghost/root-utils" "^0.3.15"
bunyan "^1.8.15"
bunyan-loggly "^1.4.2"
fs-extra "^10.0.0"
gelf-stream "^1.1.1"
json-stringify-safe "^5.0.1"
lodash "^4.17.21"
"@tryghost/logging@2.3.2", "@tryghost/logging@^2.2.3":
version "2.3.2"
resolved "https://registry.yarnpkg.com/@tryghost/logging/-/logging-2.3.2.tgz#31559c2f6eebb481a0ee8e9a958b2a2d8d55f82e"
integrity sha512-XIKSNwcG1ARNzSakXXk0jX2Pph3LMHSXQAKAm9q50fKgvX5kMdyHlunmu/NikkFsctOF8MTBGm6BJ97QW3F5IA==
@ -4605,7 +4622,7 @@
chalk "^4.1.0"
sywac "^1.3.0"
"@tryghost/pretty-stream@^0.1.12":
"@tryghost/pretty-stream@^0.1.11", "@tryghost/pretty-stream@^0.1.12":
version "0.1.13"
resolved "https://registry.yarnpkg.com/@tryghost/pretty-stream/-/pretty-stream-0.1.13.tgz#392330e57ddb06fd89205eb58726445d5e6c02c8"
integrity sha512-IpSdRxMRJqoy3WUbzwfz1UrBZADAoit24qnRREu71+/lC95ebk0cB2r6Gte2vwiFEuKJ2BFzu7iFRCZeH6jKvg==
@ -4632,7 +4649,7 @@
got "9.6.0"
lodash "^4.17.21"
"@tryghost/root-utils@^0.3.15", "@tryghost/root-utils@^0.3.16", "@tryghost/root-utils@^0.3.17":
"@tryghost/root-utils@0.3.17", "@tryghost/root-utils@^0.3.15", "@tryghost/root-utils@^0.3.16", "@tryghost/root-utils@^0.3.17":
version "0.3.17"
resolved "https://registry.yarnpkg.com/@tryghost/root-utils/-/root-utils-0.3.17.tgz#942dafe7175fc7e930d4d4e2a5de5c4d65bea1d4"
integrity sha512-mc/UkX6sNBe+Ela4nkYbxA/Vd8GzZpEvF2fAYdJM3KNDQMmUr70SnGoY1gUVeA0rh7xXHiJs1cxo7E/mIHdWvg==
@ -19381,18 +19398,52 @@ mock-knex@TryGhost/mock-knex#8ecb8c227bf463c991c3d820d33f59efc3ab9682:
lodash "^4.14.2"
semver "^5.3.0"
moment-timezone@0.5.23, moment-timezone@0.5.34, moment-timezone@^0.5.23, moment-timezone@^0.5.31, moment-timezone@^0.5.33:
moment-timezone@0.5.23, moment-timezone@^0.5.23:
version "0.5.23"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.23.tgz#7cbb00db2c14c71b19303cb47b0fb0a6d8651463"
integrity sha512-WHFH85DkCfiNMDX5D3X7hpNH3/PUhjTGcD0U1SgfBGZxJ3qUmJh5FdvaFjcClxOvB3rzdfj4oRffbI38jEnC1w==
dependencies:
moment ">= 2.9.0"
moment@2.24.0, moment@2.27.0, moment@2.29.1, moment@2.29.3, moment@2.29.4, "moment@>= 2.9.0", moment@^2.10.2, moment@^2.18.1, moment@^2.19.3, moment@^2.27.0, moment@^2.29.1:
moment-timezone@0.5.34:
version "0.5.34"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.34.tgz#a75938f7476b88f155d3504a9343f7519d9a405c"
integrity sha512-3zAEHh2hKUs3EXLESx/wsgw6IQdusOT8Bxm3D9UrHPQR7zlMmzwybC8zHEM1tQ4LJwP7fcxrWr8tuBg05fFCbg==
dependencies:
moment ">= 2.9.0"
moment-timezone@^0.5.31, moment-timezone@^0.5.33:
version "0.5.38"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.38.tgz#9674a5397b8be7c13de820fd387d8afa0f725aad"
integrity sha512-nMIrzGah4+oYZPflDvLZUgoVUO4fvAqHstvG3xAUnMolWncuAiLDWNnJZj6EwJGMGfb1ZcuTFE6GI3hNOVWI/Q==
dependencies:
moment ">= 2.9.0"
moment@2.24.0, "moment@>= 2.9.0", moment@^2.10.2, moment@^2.18.1, moment@^2.19.3:
version "2.24.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b"
integrity sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==
moment@2.27.0:
version "2.27.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.27.0.tgz#8bff4e3e26a236220dfe3e36de756b6ebaa0105d"
integrity sha512-al0MUK7cpIcglMv3YF13qSgdAIqxHTO7brRtaz3DlSULbqfazqkc5kEjNrLDOM7fsjshoFIihnU8snrP7zUvhQ==
moment@2.29.1:
version "2.29.1"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3"
integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==
moment@2.29.3:
version "2.29.3"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.3.tgz#edd47411c322413999f7a5940d526de183c031f3"
integrity sha512-c6YRvhEo//6T2Jz/vVtYzqBzwvPT95JBQ+smCytzf7c50oMZRsR/a4w88aD34I+/QVSfnoAnSBFPJHItlOMJVw==
moment@2.29.4, moment@^2.27.0, moment@^2.29.1:
version "2.29.4"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108"
integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==
moo@^0.5.0, moo@^0.5.1:
version "0.5.2"
resolved "https://registry.yarnpkg.com/moo/-/moo-0.5.2.tgz#f9fe82473bc7c184b0d32e2215d3f6e67278733c"