如何利用 Heroku 的 'postdeploy' 脚本在配置的 Heroku PostgreSQL 数据库中创建 table?
How to utilize Heroku's 'postdeploy' script to create table within a provisioned Heroku PostgreSQL database?
我正在开发一个在 Heroku 上设置的网络应用程序。我希望其他人能够自己使用它,所以我正在尝试创建一个 'Deploy to Heroku' 按钮以包含在我的存储库的自述文件中。
根据 Heroku 的文档1、2,我创建了一个 app.json
文件,其中概述了 Heroku 需要提供的所有内容应用程序。这是我的 app.json
文件的样子:
{
"name": "[title]",
"author": "[author]",
"description": "[desc]",
"repository": "[https://github.com/[user]/[repo]",
"logo": "[url]",
"addons": [
"heroku-postgresql:hobby-dev",
"wwwhisper:solo"
],
"scripts": {
"postdeploy": "node server/models/database.js"
},
"env": {
"TZ": "America/Los_Angeles"
}
}
如您所见,postdeploy
脚本应该调用 database.js
脚本,如下所示:
const pg = require('pg');
const connectionString = process.env.DATABASE_URL;
const client = new pg.Client(connectionString);
client.connect();
client.query('CREATE TABLE IF NOT EXISTS table_name (id uuid, url VARCHAR(2000), \
title TEXT, description TEXT, been_visited BOOLEAN DEFAULT false, created_at TIMESTAMP DEFAULT NOW())', (err, res) => {
if (err) {
client.end();
return console.error('error with PostgreSQL database', err);
}
});
client.end();
我知道查询在本地测试时有效,但是当我用上面的 app.json
测试按钮时,我仍然收到错误 error: relation "tanabata_tree" does not exist
- 这意味着 table 是从未创建。
我哪里/哪里做错了?
1: https://devcenter.heroku.com/articles/heroku-button
https://devcenter.heroku.com/articles/heroku-button
您的 client.end();
位于数据库查询回调函数之外。您的数据库连接在您的创建 table 查询完成之前结束,因为 JavaScript 是 Asynchronous。
解决方案是将 client.end();
放入回调函数中,以便在 数据库查询完成后 调用它。
这是工作代码:
const connectionString = process.env.DATABASE_URL;
const client = new pg.Client(connectionString);
client.connect();
client.query('CREATE TABLE IF NOT EXISTS table_name (id uuid, url VARCHAR(2000), \
title TEXT, description TEXT, been_visited BOOLEAN DEFAULT false, created_at TIMESTAMP DEFAULT NOW())', (err, res) => {
if (err) {
return console.error('error with PostgreSQL database', err);
}
client.end();
});
我正在开发一个在 Heroku 上设置的网络应用程序。我希望其他人能够自己使用它,所以我正在尝试创建一个 'Deploy to Heroku' 按钮以包含在我的存储库的自述文件中。
根据 Heroku 的文档1、2,我创建了一个 app.json
文件,其中概述了 Heroku 需要提供的所有内容应用程序。这是我的 app.json
文件的样子:
{
"name": "[title]",
"author": "[author]",
"description": "[desc]",
"repository": "[https://github.com/[user]/[repo]",
"logo": "[url]",
"addons": [
"heroku-postgresql:hobby-dev",
"wwwhisper:solo"
],
"scripts": {
"postdeploy": "node server/models/database.js"
},
"env": {
"TZ": "America/Los_Angeles"
}
}
如您所见,postdeploy
脚本应该调用 database.js
脚本,如下所示:
const pg = require('pg');
const connectionString = process.env.DATABASE_URL;
const client = new pg.Client(connectionString);
client.connect();
client.query('CREATE TABLE IF NOT EXISTS table_name (id uuid, url VARCHAR(2000), \
title TEXT, description TEXT, been_visited BOOLEAN DEFAULT false, created_at TIMESTAMP DEFAULT NOW())', (err, res) => {
if (err) {
client.end();
return console.error('error with PostgreSQL database', err);
}
});
client.end();
我知道查询在本地测试时有效,但是当我用上面的 app.json
测试按钮时,我仍然收到错误 error: relation "tanabata_tree" does not exist
- 这意味着 table 是从未创建。
我哪里/哪里做错了?
1: https://devcenter.heroku.com/articles/heroku-button https://devcenter.heroku.com/articles/heroku-button
您的 client.end();
位于数据库查询回调函数之外。您的数据库连接在您的创建 table 查询完成之前结束,因为 JavaScript 是 Asynchronous。
解决方案是将 client.end();
放入回调函数中,以便在 数据库查询完成后 调用它。
这是工作代码:
const connectionString = process.env.DATABASE_URL;
const client = new pg.Client(connectionString);
client.connect();
client.query('CREATE TABLE IF NOT EXISTS table_name (id uuid, url VARCHAR(2000), \
title TEXT, description TEXT, been_visited BOOLEAN DEFAULT false, created_at TIMESTAMP DEFAULT NOW())', (err, res) => {
if (err) {
return console.error('error with PostgreSQL database', err);
}
client.end();
});