Skip to content

Commit

Permalink
Slug migration to change allownull (coding-blocks#118)
Browse files Browse the repository at this point in the history
* Slug migration to change allownull

* Updated Migration

* migration to allow slug to not null

* Final Corrections in File Name
  • Loading branch information
kunal202 authored Jun 9, 2020
1 parent 5cbe166 commit 0d8dc66
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return Promise.all([
queryInterface.addColumn('calendar_events', 'is_open', {
allowNull: false,
type: Sequelize.BOOLEAN,
defaultValue: false,
}),
queryInterface.addColumn('calendar_events', 'is_public', {
allowNull: false,
type: Sequelize.BOOLEAN,
defaultValue: false,
}),
]);
},

down: (queryInterface, Sequelize) => {
return Promise.all([
queryInterface.removeColumn('calendar_events', 'is_open'),
queryInterface.removeColumn('calendar_events', 'is_public'),
]);
},
};
18 changes: 18 additions & 0 deletions api/migrations/20200526035531-add-slug-to-calendar-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return Promise.all([
queryInterface.addColumn('calendar_events', 'slug', {
allowNull: true,
type: Sequelize.STRING,
}),
]);
},

down: (queryInterface, Sequelize) => {
return Promise.all([
queryInterface.removeColumn('calendar_events', 'slug'),
]);
},
};
39 changes: 39 additions & 0 deletions api/migrations/20200603194408-add-slug-to-not-null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.sequelize
.query(
`
UPDATE calendar_events SET slug = BTRIM(REGEXP_REPLACE(lower(title), '[^a-z0-9\\-_]+', '-', 'gi'), '-')
WHERE slug is NULL;
`,
)
.then(() => {
return [
queryInterface.changeColumn('calendar_events', 'slug', {
type: Sequelize.STRING,
allowNull: false,
}),
queryInterface.addIndex('calendar_events', {
unique: true,
fields: ['slug'],
}),
];
})
.catch((error) => {
console.log(error);
});
},

down: (queryInterface, Sequelize) => {
return queryInterface.sequelize
.query(` UPDATE calendar_events SET slug = '';`)
.then(() => {
queryInterface.changeColumn('calendar_events', 'slug', {
type: Sequelize.STRING,
allowNull: true,
});
});
},
};

0 comments on commit 0d8dc66

Please sign in to comment.