typescript - knex 迁移对使用 docker 创建的 postgres 数据库没有任何影响
问题描述
在 docker 上创建了一个 postgres 容器,安装了基本设置,knex migration: lateset
但数据库没有收到迁移。
Knex 版本:最新操作系统:Ubuntu 18.04
根本不认为这是一个问题,但我不知道为什么它没有在我的 docker 容器上更新 postgres 中的数据库。
如何以最简单的方式迁移,使用 docker 容器连接我的 postgres,并使用 knex 迁移到它?
当我做的时候输出sudo yarn knex migrate:latest
juliano@pc:~/Desktop/TDD-typescript-backend-ObjectionJs$ sudo yarn knex migrate: latestyarn run v1.22.0
$ /home/juliano/Desktop/TDD-typescript-backend-ObjectionJs/node_modules/.bin/knex migrate: latest
Requiring external module ts-node/register
Done in 0.67s.
我的 knexfile.ts:
require('dotenv').config({
path: process.env.NODE_ENV === 'test' ? '.env.test' : '.env'
})
module.exports = {
client: 'postgresql',
connection: {
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
options: {
port: process.env.DB_PORT
}
}
}
我的 .env:
DB_HOST=127.0.0.1
DB_USER=postgres
DB_PASS=docker
DB_NAME=database
DB_PORT=5432
我的迁移:
const tableName = 'users'
// TYPE KNEX AND TABLE?
exports.up = function (knex:any) {
return knex.schema
.createTable(tableName, (table:any) => {
table.increments('id').primary()
table.string('name')
table.string('email')
table.string('password_hash')
table.date('created_at')
table.date('updated_at')
})
}
exports.down = function (knex:any) {
return knex.schema
.dropTable('users')
}
我的码头工人ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e552f8788b1 postgres "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:5432->5432/tcp database
解决方案
可以肯定的是,尝试将 typescript 的扩展名添加到您的 knexfile 中。
module.exports = {
client: 'pg',
migrations: {
extension: 'ts'
}
};
您还可以将一些调试打印添加到您的 knexfile 和迁移中,以确保其中的某些代码实际执行。
要验证您的连接参数是否有效,您可以编写 test.ts:
const config = require('knexfile');
const knex = require('knex')(config);
knex.select(1)
.then(() => console.log('Connection works'))
.catch(err => console.log('Connection failure', err));
推荐阅读
- c# - 本地化器资源未在已发布版本中解析
- android - 联系我们了解 FrameLayout、ScrollView 和 BottomNavigationView 的设计
- reactjs - 取消静音后如何使嵌入的 YouTube 视频重新启动?
- opencv - 在 OpenCV 中使用 Windows 剪贴板图像
- python-3.x - Selenium 循环通过 Mailinator 中的电子邮件
- sparql - Wikidata SPARQL:如果可用,如何按日期过滤?
- reactjs - 如何使用 React 从 Firestore 文档中渲染单个字段
- oop - 如何组合使用对象(现实世界的例子)
- mysql - 如何模拟 DB 进行单元测试
- javascript - 如何使链接的背景跟随光标?