Skip to content

Commit

Permalink
use describe to check if a column exists in the table before adding it
Browse files Browse the repository at this point in the history
  • Loading branch information
shriramshankar committed Aug 14, 2017
1 parent 9600e32 commit c03bbc8
Showing 1 changed file with 69 additions and 26 deletions.
95 changes: 69 additions & 26 deletions migrations/20170811194929-add-profile-imc-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,84 @@
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/

'use strict'; // eslint-disable-line strict
const TBL = 'Profiles';

module.exports = {
up(qi /* , Sequelize */) {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
let attr;
return qi.sequelize.transaction(() => qi.describeTable(TBL)
.then((attributes) => {
attr = attributes;
if (attr.hasOwnProperty('botAccess')) {
return true;
}

return qi.sequelize.query('ALTER TABLE ONLY "Profiles" ' +
'ADD COLUMN "botAccess" "enum_Profiles_botAccess"', {
type: qi.sequelize.QueryTypes.ALTER,
});
})
.then(() => {
if (attr.hasOwnProperty('eventAccess')) {
return true;
}

return qi.sequelize.query('ALTER TABLE ONLY "Profiles" ' +
'ADD COLUMN "eventAccess" "enum_Profiles_eventAccess"', {
type: qi.sequelize.QueryTypes.ALTER,
});
})
.then(() => {
if (attr.hasOwnProperty('roomAccess')) {
return true;
}

return qi.sequelize.query('ALTER TABLE ONLY "Profiles" ' +
'ADD COLUMN "roomAccess" "enum_Profiles_roomAccess"', {
type: qi.sequelize.QueryTypes.ALTER,
});
})
.then(() => {
if (attr.hasOwnProperty('roomTypeAccess')) {
return true;
}

Example:
return qi.createTable('users', { id: Sequelize.INTEGER });
*/
return qi.sequelize.transaction(() =>
qi.sequelize.query('ALTER TABLE ONLY "Profiles" ' +
'ADD COLUMN "botAccess" "enum_Profiles_botAccess", ' +
'ADD COLUMN "eventAccess" "enum_Profiles_eventAccess", ' +
'ADD COLUMN "roomAccess" "enum_Profiles_roomAccess", ' +
return qi.sequelize.query('ALTER TABLE ONLY "Profiles" ' +
'ADD COLUMN "roomTypeAccess" "enum_Profiles_roomTypeAccess"', {
type: qi.sequelize.QueryTypes.ALTER,
})
);
});
}));
},

down(qi /* , Sequelize*/) {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return qi.dropTable('users');
*/
return qi.sequelize.transaction(() =>
qi.removeColumn(TBL, 'botAccess'))
.then(() => qi.removeColumn(TBL, 'eventAccess'))
.then(() => qi.removeColumn(TBL, 'roomAccess'))
.then(() => qi.removeColumn(TBL, 'roomTypeAccess'));
down(qi/* , Sequelize */) {
let attr;
return qi.sequelize.transaction(() => qi.describeTable(TBL)
.then((attributes) => {
attr = attributes;
if (attr.hasOwnProperty('botAccess')) {
return qi.removeColumn(TBL, 'botAccess');
}
return true;
})
.then(() => {
if (attr.hasOwnProperty('eventAccess')) {
return qi.removeColumn(TBL, 'eventAccess');
}
return true;
})
.then(() => {
if (attr.hasOwnProperty('roomAccess')) {
return qi.removeColumn(TBL, 'roomAccess');
}
return true;
})
.then(() => {
if (attr.hasOwnProperty('roomTypeAccess')) {
return qi.removeColumn(TBL, 'roomTypeAccess');
}
return true;
}));
},
};

0 comments on commit c03bbc8

Please sign in to comment.