Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run update inside knex migration

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:

  • Users [ user_id, first_name, last_name ], and
  • Orders [ 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.

like image 514
sheamus Avatar asked Sep 29 '16 02:09

sheamus


People also ask

What does KNEX migrate latest do?

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.

How do I edit a table in KNEX?

knex.schema.alterTable(tableName, callback) Chooses a database table, and then modifies the table, using the Schema Building functions inside of the callback.

What is the proper syntax for generating a new KNEX migration?

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.


1 Answers

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'));
}
like image 61
Sinux Avatar answered Oct 25 '22 00:10

Sinux