Skip to content
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

Feature request: migrate:reset #783

Closed
factoidforrest opened this issue Apr 19, 2015 · 12 comments
Closed

Feature request: migrate:reset #783

factoidforrest opened this issue Apr 19, 2015 · 12 comments

Comments

@factoidforrest
Copy link
Contributor

I frequently change my migrations in my application since it has no production data to maintain yet. I'd like to be able to quickly reset it(rollback all and then migrate latest) instead of running rollback until all of the migrations have rolled back to the base migration, then running migrate latest.

@bendrucker
Copy link
Member

Hey there,

I'm working on the rewrite of migrations (https://github.com/knex/arctic)

I'm willing to consider this but I'm not sure about it quite yet. Are you primarily using this locally? If so wouldn't a script to drop the DB, create it, and then migrate to the latest be a better solution?

@factoidforrest
Copy link
Contributor Author

It would certainly work. I'll throw one together. I was just thinking that it's probably a common enough use case to get put in the lib, but I defer to your judgement. Maybe a migrate:rollback --all would help as well. This library has been absolutely outstanding. Thanks for all the hard work!

@bendrucker
Copy link
Member

Totally with you on an --all flag for rollback. The current API basically just supports latest and rollback but I'm looking to offer much finer control. Will keep you posted.

@robertd
Copy link

robertd commented Nov 13, 2015

@bendrucker What's the status of arctic?

@marbiano
Copy link

@bendrucker Is Arctic going to happen soon?

@justincy
Copy link

justincy commented May 4, 2016

wouldn't a script to drop the DB, create it, and then migrate to the latest be a better solution?

I ran into multiple issues doing this locally with Postgres such as the difficulty of running commands via the CLI, system permissions, db permissions, etc. I did manage to get it to work, but then realized I wanted to run the script both from the CLI and before tests, including re-running migrations and eventually seeds, so I examined the source of Migrate and camp up with this simple routine:

var migrate = knex.migrate;

// Force unlock in case of bad state
migrate.forceFreeMigrationsLock()

// Get completed migrations
.then(function(){
  return migrate._listCompleted();
})

// Rollback migrations
.then(function(completedMigrations){
  return migrate._waterfallBatch(0, completedMigrations.reverse(), 'down');
})

// Migrate to latest
.then(function(){
  return migrate.latest();
});

It should be fairly simple to turn that into a reset() method in Migrate.

@factoidforrest
Copy link
Contributor Author

That's an improvement over what I do to clean before my tests, which is just

    knex.migrate.rollback(dbConf).then ->
        console.log('rolled back database')
        knex.migrate.latest(dbConf).then ->
            console.log('migrated database')
            done()

@bendrucker would justincy's code be accepted in a pull request?

@justincy
Copy link

I updated the code example to add completedMigrations.reverse() when bringing down migrations because they're returned in order of creation.

@sheerun
Copy link

sheerun commented Sep 6, 2016

I wrote knex-migrate that has this feature (named knex-migrate redo).

@dmorgantini
Copy link

dmorgantini commented Apr 26, 2018

I used this code to solve this problem:

const rollbackAllMigrations = () => migrate.forceFreeMigrationsLock()
    .then(() => migrate.currentVersion(knexConfig.migrations))
    .then((migration) => 
        (migration !== 'none' 
          ? migrate.rollback(knexConfig.migrations).then(rollbackAllMigrations) 
          : Promise.resolve()));
return Promise.try(rollbackAllMigrations);

It doesn't use internal methods and allows you to pass in config.

@mmarvick
Copy link

mmarvick commented Jun 7, 2019

It looks like this got added and the issue can be closed: #3187

Pinging @kibertoad, since you merged that PR and probably have the permissions to do this

@kibertoad
Copy link
Collaborator

@mmarvick Thanks for the heads-up!

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

No branches or pull requests

9 participants