Use umzug to seed the database
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?
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 :)
You could wrap the seeding migrations in an if block which checks for the environment.
Great!!!
But I will seed it with permanent data, like city names and ids.
Thanks :)
Yeah in that case it should work nicely :)
:+1:
are there any examples of running seed data along with the migration?
Im trying to seed data but the migrations never says "migrated", only "Migrating", have any idea of why?
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();
})
});
});
});
Any response here? Things are quite blurry regarding seeders.
+1
Never mind;
Just changed from
path: '../migrations'
to
path: '../seeders'
at
new Umzug({...})
What about SequelizeMeta and SequelizeData tables ?
I would also like to see an example if possible! 👍
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
pathkeys inmigrationsConfigandseedersConfigobjects 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");
});
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.
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.
My 2024 code that works https://stackoverflow.com/a/78306843/10099510