Home > Software design >  Seed MySQL database on Heroku
Seed MySQL database on Heroku

Time:09-22

I have a seed file located at ./seeds/index.js that when called locally will create tables and seed a MySQL database with data.

const sequelize = require("../config/connection");
const User = require("../models/User");
const Post = require("../models/Post");
const Comment = require("../models/Comment");
const userData = require("./user-seeds.json");
const postData = require("./post-seeds.json");
const commentData = require("./comment-seeds.json");

//create tables and seed with test data
const seedDatabase = async () => {
    await sequelize.sync({ force: true });

    await User.bulkCreate(userData, {
        individualHooks: true,
        returning: true,
    });

    await Post.bulkCreate(postData, {
        individualHooks: true,
        returning: true,
    });

    await Comment.bulkCreate(commentData, {
        individualHooks: true,
        returning: true,
    });

    process.exit(0);
};

seedDatabase();

I want this to run also once the app is deployed on heroku.

I have tried running heroku run ./seeds/index.js but get Permission denied: enter image description here

I also created a Procfile and added the following line: worker: node ./seeds/index.js and it looks like it runs on deployment: enter image description here

but the database is still empty and unseeded.

How can I seed MySQL database using the node.js file during or after deployment to Heroku?

Edit: I should add I know the database is connected to the app because the tables are getting created if they don't exist on app start. The seeding still isn't happening however.

Edit 2: The content of my Procfile was malformed. I believe it should be worker: node ./seeds/index.js however, data is still not being populated in the database.

CodePudding user response:

The heroku command was malformed. It should be: heroku run node ./seeds/index.js.

  • Related