-
-
Notifications
You must be signed in to change notification settings - Fork 157
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
Comments
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 :) |
👍 |
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
|
Any response here? Things are quite blurry regarding seeders. |
+1 |
Never mind; Just changed from
to
at
|
What about |
I would also like to see an example if possible! 👍 |
An example usage of running migrations & seeders in sequence. Use separate instances of I hope this helps someone. Note: YMMV.
/* <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. |
Worked for me, thanks. |
Uses example implementation from sequelize/umzug#24 (comment)
Uses example implementation from sequelize/umzug#24 (comment)
My 2024 code that works https://stackoverflow.com/a/78306843/10099510 |
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?
The text was updated successfully, but these errors were encountered: