Heroku 上的种子 MySQL 数据库

Seed MySQL database on Heroku

我有一个位于 ./seeds/index.js 的种子文件,在本地调用时会创建表并用数据为 MySQL 数据库播种。

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

一旦在 heroku 上部署应用程序,我也希望 运行。

我试过运行宁heroku run ./seeds/index.js但得到Permission denied

我还创建了一个 Procfile 并添加了以下行:worker: node ./seeds/index.js 部署时看起来像 运行s:

但是数据库仍然是空的并且没有种子。

如何在部署到 Heroku 期间或之后使用 node.js 文件为 MySQL 数据库做种?

编辑:我应该补充一点,我知道数据库已连接到应用程序,因为如果表在应用程序启动时不存在,就会创建这些表。然而,播种仍然没有发生。

编辑 2:我的 Procfile 的内容格式不正确。我相信它应该是 worker: node ./seeds/index.js 但是,数据仍然没有被填充到数据库中。

heroku 命令格式错误。应该是:heroku run node ./seeds/index.js.