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

Use umzug to seed the database #24

Closed
filipedeschamps opened this issue Mar 2, 2015 · 17 comments
Closed

Use umzug to seed the database #24

filipedeschamps opened this issue Mar 2, 2015 · 17 comments

Comments

@filipedeschamps
Copy link

I'm in love with how umzug is simple and easy to use.

And since it's a general task/migration runner, do you think it's a good idea to also use it to seed the database in the right time?

@sdepold
Copy link
Member

sdepold commented Mar 2, 2015

Hi Filipe,

Thanks :) using umzug for seeding should work nicely, you only need to make sure that you don't run the seeding when you are in an environment where you don't want the seeding :)

@sdepold
Copy link
Member

sdepold commented Mar 2, 2015

You could wrap the seeding migrations in an if block which checks for the environment.

@filipedeschamps
Copy link
Author

Great!!!

But I will seed it with permanent data, like city names and ids.

Thanks :)

@sdepold
Copy link
Member

sdepold commented Mar 3, 2015

Yeah in that case it should work nicely :)

@filipedeschamps
Copy link
Author

👍

@mypark
Copy link

mypark commented Jul 8, 2015

are there any examples of running seed data along with the migration?

@emerak
Copy link

emerak commented Nov 27, 2015

Im trying to seed data but the migrations never says "migrated", only "Migrating", have any idea of why?

@verjik
Copy link

verjik commented Mar 26, 2017

Could you please elaborate how to seed
Migration and the rest of test code works fine

var chai = require('chai');
var should = chai.should();
var chaiHttp = require('chai-http');
chai.use(chaiHttp);

var models = require('../../models');
var server = require('../../app');

var Umzug = require('umzug');
var umzug = new Umzug({
    storage: 'sequelize',

    storageOptions: {
        sequelize: models.sequelize,
    },

    migrations: {
        params: [models.sequelize.getQueryInterface(), models.sequelize.constructor, function () {
            throw new Error('Migration tried to use old style "done" callback. Please upgrade to "umzug" and return a promise instead.');
        }],
        pattern: /\.js$/
    }
});

describe('routes : developer', () => {

    beforeEach((done) => {
        umzug.down({ to: 0 })
            .then(() => umzug.up()
                .then(() => // TODO: how do I seed here ?
                    done()));
    });

    afterEach((done) => {
        umzug.down({ to: 0 })
            .then(() => {
                done();
            })
        });
    });
});

@fakiolinho
Copy link

Any response here? Things are quite blurry regarding seeders.

@rochapablo
Copy link

+1

@rochapablo
Copy link

Never mind;

Just changed from

path: '../migrations'

to

path: '../seeders'

at

new Umzug({...})

@cyrilchapon
Copy link

What about SequelizeMeta and SequelizeData tables ?

@LeoDoldan7
Copy link

I would also like to see an example if possible! 👍

@jagged3dge
Copy link

jagged3dge commented Feb 26, 2020

An example usage of running migrations & seeders in sequence.

Use separate instances of Umzug to run migrations & seeders. Both need to be initialized with slightly different config: one instance pointing to the migrations, the other to seeders.

I hope this helps someone.

Note: YMMV.

  • If you are using this, make sure to replace the path keys in migrationsConfig and seedersConfig objects to point to the folder with the appropriate files.
  • There's no error handling code here, you'll need to add that yourselves.
  • It might be possible to make this shorter using Lodash or other deep-assign operations for deriving the config objects.
/* <PROJECT_ROOT>/migrations.js */
var Umzug = require("umzug");
var models = require("./models");

var migrationsConfig = {
  storage: "sequelize",
  storageOptions: {
    sequelize: models.sequelize
    // modelName: 'SequelizeMeta' // No need to specify, because this is default behaviour
  },
  migrations: {
    params: [
      models.sequelize.getQueryInterface(),
      models.sequelize.constructor
    ],
    path: "./migrations", // path to folder containing migrations
    pattern: /\.js$/
  }
};

var seedsConfig = {
  storage: "sequelize",
  storageOptions: {
    sequelize: models.sequelize,
    modelName: 'SequelizeData' // Or whatever you want to name the seeder storage table
  },
  migrations: {
    params: [
      models.sequelize.getQueryInterface(),
      models.sequelize.constructor
    ],
    path: "./seeds", // path to folder containing seeds
    pattern: /\.js$/
  }
};

var migrator = new Umzug(migrationsConfig);
var seeder = new Umzug(seedsConfig);

module.exports = () => migrator.up().then(() => seeder.up());

/* <PROJECT_ROOT>/index.js */
var migrations = require("./migrations");

// Run migrations & seeds
migrations().then(function() {
  console.log("Migrations completed");
});

@nishant-dani
Copy link

It appears for seeders to work - migrations and seeders have to be interleaved to match up with the db schema at the time the seeder was written. Otherwise it could cause issues at runtime if some columns the seeder expects to be there are missing because of migrations.

@heroneto
Copy link

heroneto commented Jul 9, 2020

Could you please elaborate how to seed
Migration and the rest of test code works fine

var chai = require('chai');
var should = chai.should();
var chaiHttp = require('chai-http');
chai.use(chaiHttp);

var models = require('../../models');
var server = require('../../app');

var Umzug = require('umzug');
var umzug = new Umzug({
    storage: 'sequelize',

    storageOptions: {
        sequelize: models.sequelize,
    },

    migrations: {
        params: [models.sequelize.getQueryInterface(), models.sequelize.constructor, function () {
            throw new Error('Migration tried to use old style "done" callback. Please upgrade to "umzug" and return a promise instead.');
        }],
        pattern: /\.js$/
    }
});

describe('routes : developer', () => {

    beforeEach((done) => {
        umzug.down({ to: 0 })
            .then(() => umzug.up()
                .then(() => // TODO: how do I seed here ?
                    done()));
    });

    afterEach((done) => {
        umzug.down({ to: 0 })
            .then(() => {
                done();
            })
        });
    });
});

Worked for me, thanks.

klondikemarlen added a commit to icefoganalytics/elcc-data-management that referenced this issue Sep 7, 2023
klondikemarlen added a commit to icefoganalytics/elcc-data-management that referenced this issue Sep 7, 2023
@dranitski
Copy link

My 2024 code that works https://stackoverflow.com/a/78306843/10099510

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

No branches or pull requests