4ff467794f
refs: https://github.com/TryGhost/DevOps/issues/11 This is a pretty huge commit, but the relevant points are: * Each importer no longer needs to be passed a set of data, it just gets the data it needs * Each importer specifies its dependencies, so that the order of import can be determined at runtime using a topological sort * The main data generator function can just tell each importer to import the data it has This makes working on the data generator much easier. Some other benefits are: * Batched importing, massively speeding up the whole process * `--tables` to set the exact tables you want to import, and specify the quantity of each
48 lines
2.1 KiB
JavaScript
48 lines
2.1 KiB
JavaScript
const TableImporter = require('./TableImporter');
|
|
const {faker} = require('@faker-js/faker');
|
|
const {luck} = require('../utils/random');
|
|
|
|
class MembersSubscriptionCreatedEventsImporter extends TableImporter {
|
|
static table = 'members_subscription_created_events';
|
|
static dependencies = ['members_stripe_customers_subscriptions', 'subscriptions', 'posts'];
|
|
|
|
constructor(knex, transaction) {
|
|
super(MembersSubscriptionCreatedEventsImporter.table, knex, transaction);
|
|
}
|
|
|
|
async import(quantity) {
|
|
const membersStripeCustomersSubscriptions = await this.transaction.select('id', 'ghost_subscription_id').from('members_stripe_customers_subscriptions');
|
|
this.subscriptions = await this.transaction.select('id', 'created_at', 'member_id').from('subscriptions');
|
|
this.posts = await this.transaction.select('id', 'published_at', 'visibility', 'type', 'slug').from('posts').orderBy('published_at', 'desc');
|
|
|
|
await this.importForEach(membersStripeCustomersSubscriptions, quantity ? quantity / membersStripeCustomersSubscriptions.length : 1);
|
|
}
|
|
|
|
generate() {
|
|
const subscription = this.subscriptions.find(s => s.id === this.model.ghost_subscription_id);
|
|
let attribution = {};
|
|
if (luck(10)) {
|
|
const post = this.posts.find(p => p.visibility === 'public' && new Date(p.published_at) < new Date(subscription.created_at));
|
|
if (post) {
|
|
attribution = {
|
|
attribution_id: post.id,
|
|
attribution_type: post.type,
|
|
attribution_url: post.slug
|
|
};
|
|
}
|
|
}
|
|
return Object.assign({}, {
|
|
id: faker.database.mongodbObjectId(),
|
|
created_at: subscription.created_at,
|
|
member_id: subscription.member_id,
|
|
subscription_id: this.model.id,
|
|
// TODO: Implement referrers
|
|
referrer_source: null,
|
|
referrer_medium: null,
|
|
referrer_url: null
|
|
}, attribution);
|
|
}
|
|
}
|
|
|
|
module.exports = MembersSubscriptionCreatedEventsImporter;
|