Skip to content

Changing multiple columns example #565

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
2 tasks
harishajdarevic opened this issue Apr 27, 2018 · 2 comments
Closed
2 tasks

Changing multiple columns example #565

harishajdarevic opened this issue Apr 27, 2018 · 2 comments
Labels

Comments

@harishajdarevic
Copy link

I'm submitting a...

  • Bug report
  • Feature request
  • [X ] Question

Current behavior

I want to change multiple columns in one migration. Could you provide example.

This is what I tried so far:

exports.up = function(db) {
    return [
    db.changeColumn(models.PROJECT_BIM_MODEL_SYNC, 'created_at', {
        type: 'timestamp',
        notNull: true,
        defaultValue: new String('CURRENT_TIMESTAMP')
    }),

    db.changeColumn(models.PROJECT_TABLE, 'updated_at', {
        type: 'timestamp',
        notNull: true,
        defaultValue: new String('CURRENT_TIMESTAMP')
    }),
]
};

and

exports.up = function(db) {

    db.changeColumn(models.PROJECT_BIM_MODEL_SYNC, 'created_at', {
        type: 'timestamp',
        notNull: true,
        defaultValue: new String('CURRENT_TIMESTAMP')
    }),

    db.changeColumn(models.PROJECT_TABLE, 'updated_at', {
        type: 'timestamp',
        notNull: true,
        defaultValue: new String('CURRENT_TIMESTAMP')
    }),

   return null;
};

Expected behavior

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


db-migrate version: X.Y.Z
plugins with versions: X.Y.Z
db-migrate driver with versions: 

Additional information:
- Node version: XX  
- Platform:  

Others:

@wzrdtales
Copy link
Member

wzrdtales commented Apr 27, 2018

You need to chain them, use promise migrations in their correct way.

exports.up = function(db) {
    return 
    db.changeColumn(models.PROJECT_BIM_MODEL_SYNC, 'created_at', {
        type: 'timestamp',
        notNull: true,
        defaultValue: new String('CURRENT_TIMESTAMP')
    }).
    then(() => db.changeColumn(models.PROJECT_TABLE, 'updated_at', {
        type: 'timestamp',
        notNull: true,
        defaultValue: new String('CURRENT_TIMESTAMP')
    }));
};

Either that way, or if you're on node 8 you can also do this:

exports.up = async function(db) {
    await db.changeColumn(models.PROJECT_BIM_MODEL_SYNC, 'created_at', {
        type: 'timestamp',
        notNull: true,
        defaultValue: new String('CURRENT_TIMESTAMP')
    }),

    return db.changeColumn(models.PROJECT_TABLE, 'updated_at', {
        type: 'timestamp',
        notNull: true,
        defaultValue: new String('CURRENT_TIMESTAMP')
    })
};

@wzrdtales
Copy link
Member

and never return null unless your migration step does simply nothing.

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