From eeafc8603b960fefcc54f96c59aee51948ba8a0c Mon Sep 17 00:00:00 2001 From: Rishabh Garg Date: Mon, 31 Jan 2022 15:33:58 +0530 Subject: [PATCH] Added pivot table to store tiers on post (#14038) refs https://github.com/TryGhost/Team/issues/1071 We used `posts.visibility` originally to store visibility as `free|paid` with a character limit of 50. This same field was repurposed to store an NQL filter when member tiers is enabled. The NQL filter uses the slug of the tier name, which can easily create a filter longer than 50 characters, adding an unwanted limitation on number of tiers that can be added to post's visibility. Going forward, we'd like to store the visibility of posts for tiers in a separate pivot table and instead store the value of `visibility` as `tiers` when restricting post access to specific tiers. This change - - adds a new pivot table fixture for storing relation between posts and tiers - adds a migration for creating the new table - updates tests --- core/server/data/exporter/table-lists.js | 1 + .../4.35/2022-01-20-05-55-add-post-products-table.js | 8 ++++++++ core/server/data/schema/schema.js | 6 ++++++ test/integration/exporter/exporter.test.js | 1 + test/unit/server/data/schema/integrity.test.js | 2 +- 5 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 core/server/data/migrations/versions/4.35/2022-01-20-05-55-add-post-products-table.js diff --git a/core/server/data/exporter/table-lists.js b/core/server/data/exporter/table-lists.js index 20e114ca57..66a6f044a3 100644 --- a/core/server/data/exporter/table-lists.js +++ b/core/server/data/exporter/table-lists.js @@ -10,6 +10,7 @@ const BACKUP_TABLES = [ 'members', 'members_labels', 'members_products', + 'posts_products', 'members_stripe_customers', 'members_stripe_customers_subscriptions', 'migrations', diff --git a/core/server/data/migrations/versions/4.35/2022-01-20-05-55-add-post-products-table.js b/core/server/data/migrations/versions/4.35/2022-01-20-05-55-add-post-products-table.js new file mode 100644 index 0000000000..e34229a297 --- /dev/null +++ b/core/server/data/migrations/versions/4.35/2022-01-20-05-55-add-post-products-table.js @@ -0,0 +1,8 @@ +const {addTable} = require('../../utils'); + +module.exports = addTable('posts_products', { + id: {type: 'string', maxlength: 24, nullable: false, primary: true}, + post_id: {type: 'string', maxlength: 24, nullable: false, references: 'posts.id', cascadeDelete: true}, + product_id: {type: 'string', maxlength: 24, nullable: false, references: 'products.id', cascadeDelete: true}, + sort_order: {type: 'integer', nullable: false, unsigned: true, defaultTo: 0} +}); diff --git a/core/server/data/schema/schema.js b/core/server/data/schema/schema.js index a4981c7d25..02e7063d70 100644 --- a/core/server/data/schema/schema.js +++ b/core/server/data/schema/schema.js @@ -424,6 +424,12 @@ module.exports = { product_id: {type: 'string', maxlength: 24, nullable: false, references: 'products.id', cascadeDelete: true}, sort_order: {type: 'integer', nullable: false, unsigned: true, defaultTo: 0} }, + posts_products: { + id: {type: 'string', maxlength: 24, nullable: false, primary: true}, + post_id: {type: 'string', maxlength: 24, nullable: false, references: 'posts.id', cascadeDelete: true}, + product_id: {type: 'string', maxlength: 24, nullable: false, references: 'products.id', cascadeDelete: true}, + sort_order: {type: 'integer', nullable: false, unsigned: true, defaultTo: 0} + }, members_payment_events: { id: {type: 'string', maxlength: 24, nullable: false, primary: true}, member_id: {type: 'string', maxlength: 24, nullable: false, references: 'members.id', cascadeDelete: true}, diff --git a/test/integration/exporter/exporter.test.js b/test/integration/exporter/exporter.test.js index a160baaf91..165af24a16 100644 --- a/test/integration/exporter/exporter.test.js +++ b/test/integration/exporter/exporter.test.js @@ -55,6 +55,7 @@ describe('Exporter', function () { 'permissions_roles', 'permissions_users', 'posts', + 'posts_products', 'posts_authors', 'posts_meta', 'posts_tags', diff --git a/test/unit/server/data/schema/integrity.test.js b/test/unit/server/data/schema/integrity.test.js index 8c5b9c9ea7..abb7f82845 100644 --- a/test/unit/server/data/schema/integrity.test.js +++ b/test/unit/server/data/schema/integrity.test.js @@ -35,7 +35,7 @@ const validateRouteSettings = require('../../../../../core/server/services/route */ describe('DB version integrity', function () { // Only these variables should need updating - const currentSchemaHash = '690caeeb4b3b52591a204704aa15dec9'; + const currentSchemaHash = '95bef616ba2bf7b91a99c627bbc591a3'; const currentFixturesHash = 'beb040c0376a492c2a44767fdd825a3e'; const currentSettingsHash = 'd73b63e33153c9256bca42ebfd376779'; const currentRoutesHash = '3d180d52c663d173a6be791ef411ed01';