Add columns for referral code and referrer

Also made sure that these columns are auto-added to old
databases 😉
This commit is contained in:
Badri 2022-02-20 15:46:01 +05:30
parent c4341772c6
commit 7cca56eaca

View file

@ -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')
}
})