From 7cca56eacacff8400841f390961ee7ba3302cf35 Mon Sep 17 00:00:00 2001 From: Badri Date: Sun, 20 Feb 2022 15:46:01 +0530 Subject: [PATCH] Add columns for referral code and referrer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also made sure that these columns are auto-added to old databases 😉 --- server/index.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/server/index.js b/server/index.js index 03ff1a4..740fb92 100644 --- a/server/index.js +++ b/server/index.js @@ -106,6 +106,33 @@ let pledgeSchema = function(t) { t.integer('retry_times').defaultTo(10) t.boolean('get_newsletter').defaultTo(false) t.text('other_message') + + t.string('referral_code', 256) + t.string('referrer', 128) +} + +// function to make sure referral columns exist +// we need this because they were added later in the code, so +// if there are old databases they'd need to be updated. + +function ensureReferralColumns(table) { + knex.schema.hasColumn(table, 'referral_code').then(function(exists) { + if (!exists) { + console.debug(`No referral_code column in ${table}! Adding now...`) + knex.schema.alterTable(table, t => { + t.string('referral_code', 256) + }).then(r=> console.debug(`referral_code column added to ${table}:`, r)) + } + }) + + knex.schema.hasColumn(table, 'referrer').then(function(exists) { + if (!exists) { + console.debug(`No referrer column in ${table}! Adding it now...`) + knex.schema.alterTable(table, t => { + t.string('referrer', 128) + }).then(r=> console.debug(`referrer column added to ${table}:`, r)) + } + }) } // make sure pledges table exists @@ -113,6 +140,8 @@ knex.schema.hasTable('pledges').then(function(exists) { if (!exists) { if (DEBUG) console.debug('No pledge table exists! Creating one now...') return knex.schema.createTable('pledges', pledgeSchema) + } else { + ensureReferralColumns('pledges') } }) @@ -121,6 +150,8 @@ knex.schema.hasTable('unverified_pledges').then(function(exists) { if (!exists) { if (DEBUG) console.debug('No unverified pledge table exists! Creating one now...') return knex.schema.createTable('unverified_pledges', pledgeSchema) + } else { + ensureReferralColumns('unverified_pledges') } })