diff --git a/packages/server/src/system/migrations/20240222134235_seed_free_subscription_to_tenants.js b/packages/server/src/system/migrations/20240222134235_seed_free_subscription_to_tenants.js new file mode 100644 index 000000000..5368db3c0 --- /dev/null +++ b/packages/server/src/system/migrations/20240222134235_seed_free_subscription_to_tenants.js @@ -0,0 +1,7 @@ +exports.up = function (knex) { + return knex.seed.run({ + specific: 'seed_tenants_free_subscription.js', + }); +}; + +exports.down = function (knex) {}; diff --git a/packages/server/src/system/seeds/seed_tenants_free_subscription.js b/packages/server/src/system/seeds/seed_tenants_free_subscription.js new file mode 100644 index 000000000..0c08a41b1 --- /dev/null +++ b/packages/server/src/system/seeds/seed_tenants_free_subscription.js @@ -0,0 +1,26 @@ +exports.seed = (knex) => { + // Deletes ALL existing entries + return knex('subscription_plan_subscriptions') + .then(async () => { + const tenants = await knex('tenants'); + + for (const tenant of tenants) { + const existingSubscription = await knex('subscription_plan_subscriptions') + .where('tenantId', tenant.id) + .first(); + + if (!existingSubscription) { + const freePlan = await knex('subscription_plans').where('slug', 'free').first(); + + await knex('subscription_plan_subscriptions').insert({ + tenantId: tenant.id, + planId: freePlan.id, + slug: 'main', + startsAt: knex.fn.now(), + endsAt: null, + createdAt: knex.fn.now(), + }); + } + } + }); +};