0085-user-role.ts 842 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import * as Sequelize from 'sequelize'
  2. async function up (utils: {
  3. transaction: Sequelize.Transaction,
  4. queryInterface: Sequelize.QueryInterface,
  5. sequelize: Sequelize.Sequelize,
  6. db: any
  7. }): Promise<void> {
  8. const q = utils.queryInterface
  9. await q.renameColumn('Users', 'role', 'oldRole')
  10. const data = {
  11. type: Sequelize.INTEGER,
  12. allowNull: true
  13. }
  14. await q.addColumn('Users', 'role', data)
  15. let query = 'UPDATE "Users" SET "role" = 0 WHERE "oldRole" = \'admin\''
  16. await utils.sequelize.query(query)
  17. query = 'UPDATE "Users" SET "role" = 2 WHERE "oldRole" = \'user\''
  18. await utils.sequelize.query(query)
  19. data.allowNull = false
  20. await q.changeColumn('Users', 'role', data)
  21. await q.removeColumn('Users', 'oldRole')
  22. }
  23. function down (options) {
  24. throw new Error('Not implemented.')
  25. }
  26. export {
  27. up,
  28. down
  29. }