Skip to content

How to query exist data before migration #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rkmax opened this issue Mar 20, 2015 · 5 comments
Closed

How to query exist data before migration #248

rkmax opened this issue Mar 20, 2015 · 5 comments
Labels

Comments

@rkmax
Copy link

rkmax commented Mar 20, 2015

Example

I need add a new notNull: true column, but the table has rows. so

  1. I need get current the data from the table
  2. add the new column with the notNull: true property
  3. insert updated records
  4. update the column definition with notNull: true
dbm = dbm || require('db-migrate');
var type = dbm.dataType,
    async = require('async');

exports.up = function(db, callback) {

  async.series([
      db.addColumn.bind(db, 'users', 'email', {
        type: type.STRING
      }),
      // get the current and update
      db.changeColumn.bind(db, 'users', 'email', {
        type: type.STRING,
        notNull: true
      }),            
  ], callback);
};

exports.down = function(db, callback) {
  db.removeColumn('users', 'email', callback);
};

How i can get the current data?

@rkmax rkmax changed the title How i can query exist data before migration How to query exist data before migration Mar 20, 2015
@wzrdtales
Copy link
Member

Data operations are not exactly a part of the migrations. This more or less belongs to seeding data.

Yet you can't select data, but there are features which are going to be introduced within v0.10.x
I reference here to this:
#215

and this:
#230

So currently you can't do this via any method which is intended to do this. But this doesn't mean it is impossible.

You need to fallback to runSql and select your data by native queries, but why you don't just add a defaultValue to your definition. With a defaultValue you can set notNull to true unless it is keyed with unique or primary.

@rkmax
Copy link
Author

rkmax commented Mar 20, 2015

@wzrdtales yes, The new column is for unique values

@wzrdtales
Copy link
Member

Then you need to fallback to runSql right now.

@rkmax
Copy link
Author

rkmax commented Mar 20, 2015

Thanks that was what I did?

For example

exports.up = function (db, callback) {
  async.series([
    db.addColumn.bind(db, 'users', 'email', {
      type: type.STRING
    }),
    function (cb) {

      return db.runSql('SELECT * FROM users', [], function (err, results) {
        var inserts = [];
        results.rows.forEach(function (row) {
          inserts.push(db.runSql.bind(
              db, 'UPDATE users SET email = ? WHERE id = ?', [row.username + '@example.com', row.id]
          ));
        });
        async.series(inserts, cb);
      });
    },
    db.changeColumn.bind(db, 'users', 'email', {
      type: type.STRING,
      notNull: true
    }),
    db.addIndex.bind(db, 'users', 'email_unique', ['email'], true),
    db.addIndex.bind(db, 'users', 'username_unique', ['username'], true)
  ], callback);
};

@wzrdtales
Copy link
Member

That is probably a solution, if this works for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants