Now I am doing this to change the type of a column from a row to an enumeration. Is there an alternative way to do this?
Is it possible to use it as knex.raw to form such a request?
CREATE TYPE type AS ENUM ('disabled', 'include', 'exclude'); ALTER TABLE test_table ALTER COLUMN test_col DROP DEFAULT; ALTER TABLE test_table ALTER COLUMN test_col TYPE logic USING(test_col::type), ALTER COLUMN test_col SET DEFAULT 'disabled'::logic; return schema .table('offers', function (table) { cols.forEach(function (column) { table.renameColumn(column, column + '_old'); }); }).then(function () { var schema = knex.schema; return schema.table('offers', function (table) { cols.forEach(function (column) { table.enum(column, ['disabled', 'include', 'exclude']).defaultTo('disabled'); }); }); }).then(function () { return knex.select('*').from('offers'); }).then(function (rows) { return Promise.map(rows, function (row) { var data = {}; cols.forEach(function (column) { data[column] = row[column+'_old']; }, data); return knex('offers').where('id', '=', row.id).update(data); }) }).then(function () { var schema = knex.schema; return schema.table('offers',function (table) { cols.forEach(function (column) { table.dropColumn(column+'_old'); }); }); });
chmax source share