From 4e326123d35cbb38b2c4e632dc45376b55a9ef46 Mon Sep 17 00:00:00 2001 From: Fabien 'egg' O'Carroll Date: Wed, 20 Oct 2021 14:22:37 +0200 Subject: [PATCH] Updated Offers related tables (#13609) no-issue * Removed NOT_NULL constraint from stripe_coupon_id When handling disconnecting from Stripe - we remove all Stripe data from our database to ensure we do not have bad/invalid data stored. Removing this constraint will allow us to set the value to NULL. * Added created_at column to offer_redemptions Offer Redemptions are not just a joining table, but an event. A created_at date allows them to be ordered Because this is in alpha it is simpler to just drop the tables and re-add them, due to offer_redemptions depending on offers, we also drop this table and re-add it. --- .../4.20/01-remove-offer-redemptions-table.js | 19 ++++++++++++ .../versions/4.20/02-remove-offers-table.js | 30 +++++++++++++++++++ .../versions/4.20/03-add-offers-table.js | 21 +++++++++++++ .../4.20/04-add-offer-redemptions-table.js | 9 ++++++ core/server/data/schema/schema.js | 5 ++-- .../unit/server/data/schema/integrity.test.js | 2 +- 6 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 core/server/data/migrations/versions/4.20/01-remove-offer-redemptions-table.js create mode 100644 core/server/data/migrations/versions/4.20/02-remove-offers-table.js create mode 100644 core/server/data/migrations/versions/4.20/03-add-offers-table.js create mode 100644 core/server/data/migrations/versions/4.20/04-add-offer-redemptions-table.js diff --git a/core/server/data/migrations/versions/4.20/01-remove-offer-redemptions-table.js b/core/server/data/migrations/versions/4.20/01-remove-offer-redemptions-table.js new file mode 100644 index 0000000000..2bc1803423 --- /dev/null +++ b/core/server/data/migrations/versions/4.20/01-remove-offer-redemptions-table.js @@ -0,0 +1,19 @@ +const utils = require('../../utils'); + +const migration = utils.addTable('offer_redemptions', { + id: {type: 'string', maxlength: 24, nullable: false, primary: true}, + offer_id: {type: 'string', maxlength: 24, nullable: false, references: 'offers.id', cascadeDelete: true}, + member_id: {type: 'string', maxlength: 24, nullable: false, references: 'members.id', cascadeDelete: true}, + subscription_id: {type: 'string', maxlength: 24, nullable: false, references: 'members_stripe_customers_subscriptions.id', cascadeDelete: true} +}); + +// This reverses an "addTable" migration so that we +// drop the table going forwards and re-add it going back +const up = migration.down; +const down = migration.up; + +migration.up = up; +migration.down = down; + +module.exports = migration; + diff --git a/core/server/data/migrations/versions/4.20/02-remove-offers-table.js b/core/server/data/migrations/versions/4.20/02-remove-offers-table.js new file mode 100644 index 0000000000..ff4b1b6266 --- /dev/null +++ b/core/server/data/migrations/versions/4.20/02-remove-offers-table.js @@ -0,0 +1,30 @@ +const utils = require('../../utils'); + +const migration = utils.addTable('offers', { + id: {type: 'string', maxlength: 24, nullable: false, primary: true}, + active: {type: 'boolean', nullable: false, defaultTo: true}, + name: {type: 'string', maxlength: 191, nullable: false, unique: true}, + code: {type: 'string', maxlength: 191, nullable: false, unique: true}, + product_id: {type: 'string', maxlength: 24, nullable: false, references: 'products.id'}, + stripe_coupon_id: {type: 'string', maxlength: 255, nullable: false, unique: true}, + interval: {type: 'string', maxlength: 50, nullable: false}, + currency: {type: 'string', maxlength: 50, nullable: true}, + discount_type: {type: 'string', maxlength: 50, nullable: false}, + discount_amount: {type: 'integer', nullable: false}, + duration: {type: 'string', maxlength: 50, nullable: false}, + duration_in_months: {type: 'integer', nullable: true}, + portal_title: {type: 'string', maxlength: 191, nullable: false}, + portal_description: {type: 'string', maxlength: 2000, nullable: true}, + created_at: {type: 'dateTime', nullable: false}, + updated_at: {type: 'dateTime', nullable: true} +}); + +// This reverses an "addTable" migration so that we +// drop the table going forwards and re-add it going back +const up = migration.down; +const down = migration.up; + +migration.up = up; +migration.down = down; + +module.exports = migration; diff --git a/core/server/data/migrations/versions/4.20/03-add-offers-table.js b/core/server/data/migrations/versions/4.20/03-add-offers-table.js new file mode 100644 index 0000000000..6f0f892c43 --- /dev/null +++ b/core/server/data/migrations/versions/4.20/03-add-offers-table.js @@ -0,0 +1,21 @@ +const utils = require('../../utils'); + +module.exports = utils.addTable('offers', { + id: {type: 'string', maxlength: 24, nullable: false, primary: true}, + active: {type: 'boolean', nullable: false, defaultTo: true}, + name: {type: 'string', maxlength: 191, nullable: false, unique: true}, + code: {type: 'string', maxlength: 191, nullable: false, unique: true}, + product_id: {type: 'string', maxlength: 24, nullable: false, references: 'products.id'}, + stripe_coupon_id: {type: 'string', maxlength: 255, nullable: true, unique: true}, + interval: {type: 'string', maxlength: 50, nullable: false}, + currency: {type: 'string', maxlength: 50, nullable: true}, + discount_type: {type: 'string', maxlength: 50, nullable: false}, + discount_amount: {type: 'integer', nullable: false}, + duration: {type: 'string', maxlength: 50, nullable: false}, + duration_in_months: {type: 'integer', nullable: true}, + portal_title: {type: 'string', maxlength: 191, nullable: false}, + portal_description: {type: 'string', maxlength: 2000, nullable: true}, + created_at: {type: 'dateTime', nullable: false}, + updated_at: {type: 'dateTime', nullable: true} +}); + diff --git a/core/server/data/migrations/versions/4.20/04-add-offer-redemptions-table.js b/core/server/data/migrations/versions/4.20/04-add-offer-redemptions-table.js new file mode 100644 index 0000000000..af29719442 --- /dev/null +++ b/core/server/data/migrations/versions/4.20/04-add-offer-redemptions-table.js @@ -0,0 +1,9 @@ +const {addTable} = require('../../utils'); + +module.exports = addTable('offer_redemptions', { + id: {type: 'string', maxlength: 24, nullable: false, primary: true}, + offer_id: {type: 'string', maxlength: 24, nullable: false, references: 'offers.id', cascadeDelete: true}, + member_id: {type: 'string', maxlength: 24, nullable: false, references: 'members.id', cascadeDelete: true}, + subscription_id: {type: 'string', maxlength: 24, nullable: false, references: 'members_stripe_customers_subscriptions.id', cascadeDelete: true}, + created_at: {type: 'dateTime', nullable: false} +}); diff --git a/core/server/data/schema/schema.js b/core/server/data/schema/schema.js index f3d3386af9..291f278d87 100644 --- a/core/server/data/schema/schema.js +++ b/core/server/data/schema/schema.js @@ -390,7 +390,7 @@ module.exports = { name: {type: 'string', maxlength: 191, nullable: false, unique: true}, code: {type: 'string', maxlength: 191, nullable: false, unique: true}, product_id: {type: 'string', maxlength: 24, nullable: false, references: 'products.id'}, - stripe_coupon_id: {type: 'string', maxlength: 255, nullable: false, unique: true}, + stripe_coupon_id: {type: 'string', maxlength: 255, nullable: true, unique: true}, interval: {type: 'string', maxlength: 50, nullable: false, validations: {isIn: [['month', 'year']]}}, currency: {type: 'string', maxlength: 50, nullable: true}, discount_type: {type: 'string', maxlength: 50, nullable: false, validations: {isIn: [['percent', 'amount']]}}, @@ -533,7 +533,8 @@ module.exports = { id: {type: 'string', maxlength: 24, nullable: false, primary: true}, offer_id: {type: 'string', maxlength: 24, nullable: false, references: 'offers.id', cascadeDelete: true}, member_id: {type: 'string', maxlength: 24, nullable: false, references: 'members.id', cascadeDelete: true}, - subscription_id: {type: 'string', maxlength: 24, nullable: false, references: 'members_stripe_customers_subscriptions.id'} + subscription_id: {type: 'string', maxlength: 24, nullable: false, references: 'members_stripe_customers_subscriptions.id', cascadeDelete: true}, + created_at: {type: 'dateTime', nullable: false} }, members_subscribe_events: { id: {type: 'string', maxlength: 24, nullable: false, primary: true}, diff --git a/test/unit/server/data/schema/integrity.test.js b/test/unit/server/data/schema/integrity.test.js index 9d633a05d4..0e25873f1e 100644 --- a/test/unit/server/data/schema/integrity.test.js +++ b/test/unit/server/data/schema/integrity.test.js @@ -32,7 +32,7 @@ const defaultSettings = require('../../../../../core/server/data/schema/default- */ describe('DB version integrity', function () { // Only these variables should need updating - const currentSchemaHash = '33ec13330fc7384b849ae4b0e506017d'; + const currentSchemaHash = '06c1007b471faba9bb82d053f6ba6cc1'; const currentFixturesHash = 'c064a1b57c594e6a8d36f9e884df0a2a'; const currentSettingsHash = 'aa3fcbc8ab119b630aeda7366ead5493'; const currentRoutesHash = '3d180d52c663d173a6be791ef411ed01';