I want to add a column to a table, then perform some work to populate the column as part of the migration. Populating the column requires some manipulation in code.
Consider two tables:
[ user_id, first_name, last_name ]
, and[ order_id, user_id, other_field_1, other_field_2 ]
.And the function
var getNickName = function(user_row) {
//do a bunch of javascripty stuff here
//based on user_row.first_name and user_row.last_name.
//i.e., stuff not possible in SQL
return 'nickname';
}
I want a knex migration that adds a user_nick_name
field to the Orders table. Then updates the new column with the output from getNickName()
.
I need this to be in a transaction.
I know I need to add the column to orders, then select all the orders, iterate over the orders performing:
pass the user row into getNickName
,
then use that to call update on the users table setting the value.
I just can't seem to get the knex syntax for all this, when a transaction is involved.
Migrations are a way to make database changes or updates, like creating or dropping tables, as well as updating a table with new columns with constraints via generated scripts. We can build these scripts via the command line using knex command line tool.
knex.schema.alterTable(tableName, callback) Chooses a database table, and then modifies the table, using the Schema Building functions inside of the callback.
Knex has a command for this purpose, with this syntax: migrate:make followed by the file name we want to give to the migration. If you look at the project directory after running this command you will see that a new migrations directory has been added and a new migration file has been created in it.
export function up(knex, Promise) {
return knex.select()
.from('Users')
.then((users) => {
const nickNames = users.map((user) => {
return { userId: user.user_id, nickName: getNickName(row) };
});
return knex.transaction((trx) => {
return knex.schema.table('Orders', (table) => table.string('user_nick_name').transacting(trx))
.then(() => {
return Promise.all(
nickNames.map((row) => {
return knex('Orders')
.update({ user_nick_name: row.nickName })
.where('user_id', row.userId)
.transacting(trx);
});
);
})
.then(trx.commit)
.catch(trx.rollback);
});
});
}
export function down(knex) {
return knex.schema.table('Orders', (table) => table.dropColumn('user_nick_name'));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With