umzug icon indicating copy to clipboard operation
umzug copied to clipboard

Use umzug to seed the database

Open filipedeschamps opened this issue 11 years ago • 17 comments

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?

filipedeschamps avatar Mar 02 '15 10:03 filipedeschamps

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 avatar Mar 02 '15 17:03 sdepold

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

sdepold avatar Mar 02 '15 17:03 sdepold

Great!!!

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

Thanks :)

filipedeschamps avatar Mar 02 '15 22:03 filipedeschamps

Yeah in that case it should work nicely :)

sdepold avatar Mar 03 '15 10:03 sdepold

:+1:

filipedeschamps avatar Mar 03 '15 23:03 filipedeschamps

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

mypark avatar Jul 08 '15 18:07 mypark

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

emerak avatar Nov 27 '15 20:11 emerak

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();
            })
        });
    });
});

verjik avatar Mar 26 '17 08:03 verjik

Any response here? Things are quite blurry regarding seeders.

fakiolinho avatar Apr 19 '17 13:04 fakiolinho

+1

rochapablo avatar Jul 04 '17 11:07 rochapablo

Never mind;

Just changed from

path: '../migrations'

to

path: '../seeders'

at

new Umzug({...})

rochapablo avatar Jul 04 '17 11:07 rochapablo

What about SequelizeMeta and SequelizeData tables ?

cyrilchapon avatar Mar 15 '19 10:03 cyrilchapon

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

LeoDoldan7 avatar Aug 09 '19 20:08 LeoDoldan7

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");
});

jagged3dge avatar Feb 26 '20 08:02 jagged3dge

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.

nishant-dani avatar Apr 12 '20 23:04 nishant-dani

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.

heroneto avatar Jul 09 '20 23:07 heroneto

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

dranitski avatar Apr 10 '24 20:04 dranitski