我试图始终使用sequelize和umzug迁移到特定的postgresql数据库架构。让我们称之为custom
模式。默认情况下,所有查询都转到public
模式。
有什么方法可以定义默认模式来以序列化或umzug方式运行所有迁移?
使用以下代码,我可以为迁移中的单个查询定义架构:
// schema defined according to https://sequelize.readthedocs.io/en/latest/docs/migrations/
module.exports = {
up: async (queryInterface, Sequelize) => {
return Sequelize.transaction(async transaction => {
await queryInterface.renameTable({ tableName: 'oldtablename', schema: "custom"}, 'newtablename', { transaction })
})
},
down: async () => {
}
}
它报告说这是成功的,并且使用了正确的模式:
Migration {
path:
'path/to/migrations/migration_test.js',
file: 'migration_test.js',
options:
{ storage: 'sequelize',
storageOptions: [Object],
logging: [Function: bound consoleCall],
upName: 'up',
downName: 'down',
migrations: [Object],
schema: 'custom' } } ]
但是,我需要的是能够定义所有迁移中的所有查询始终运行的默认模式,而不是在我要运行的每个查询中定义我所需的模式。
我尝试搜索,阅读库的文档并在各处复制粘贴schema: 'custom'
,但除上述示例外,到目前为止,其他任何方法都无效。
我正在使用以下代码来运行迁移:
const sequelizeConn = new Sequelize(ENV_DB_URL, {
schema: 'custom',
logging: false
})
const migrator = new Umzug({
storage: 'sequelize',
storageOptions: {
sequelize: sequelizeConn,
tableName: 'migrations',
schema: 'custom'
},
logging: console.log,
migrations: {
params: [
sequelizeConn.getQueryInterface(),
sequelizeConn
],
path: `${process.cwd()}/src/database/migrations`,
pattern: /\.js$/
}
})
migrator.up()
我的umzug是2.2.0
,后继是5.3.0
。
migrations
表已正确创建到custom
模式,但是迁移仍在public
模式中进行。
在运行未在查询本身中指定架构的迁移时,出现以下错误。从错误中我们可以看到模式是未定义的:
{ SequelizeDatabaseError: relation "oldtablename" does not exist
at Query.formatError (/usr/src/app/node_modules/sequelize/lib/dialects/postgres/query.js:354:16)
at query.catch.err (/usr/src/app/node_modules/sequelize/lib/dialects/postgres/query.js:71:18)
at tryCatcher (/usr/src/app/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/usr/src/app/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/usr/src/app/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/usr/src/app/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/usr/src/app/node_modules/bluebird/js/release/promise.js:690:18)
at _drainQueueStep (/usr/src/app/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/usr/src/app/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/usr/src/app/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/usr/src/app/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
at process.topLevelDomainCallback (domain.js:120:23)
name: 'SequelizeDatabaseError',
parent:
{ error: relation "oldtablename" does not exist
at Connection.parseE (/usr/src/app/node_modules/pg/lib/connection.js:554:11)
at Connection.parseMessage (/usr/src/app/node_modules/pg/lib/connection.js:379:19)
at Socket.<anonymous> (/usr/src/app/node_modules/pg/lib/connection.js:119:22)
at Socket.emit (events.js:189:13)
at Socket.EventEmitter.emit (domain.js:441:20)
at addChunk (_stream_readable.js:284:12)
at readableAddChunk (_stream_readable.js:265:11)
at Socket.Readable.push (_stream_readable.js:220:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
name: 'error',
length: 118,
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'namespace.c',
line: '420',
routine: 'RangeVarGetRelidExtended',
sql:
'ALTER TABLE "oldtablename" RENAME TO "newtablename";' },
original:
{ error: relation "oldtablename" does not exist
at Connection.parseE (/usr/src/app/node_modules/pg/lib/connection.js:554:11)
at Connection.parseMessage (/usr/src/app/node_modules/pg/lib/connection.js:379:19)
at Socket.<anonymous> (/usr/src/app/node_modules/pg/lib/connection.js:119:22)
at Socket.emit (events.js:189:13)
at Socket.EventEmitter.emit (domain.js:441:20)
at addChunk (_stream_readable.js:284:12)
at readableAddChunk (_stream_readable.js:265:11)
at Socket.Readable.push (_stream_readable.js:220:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
name: 'error',
length: 118,
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'namespace.c',
line: '420',
routine: 'RangeVarGetRelidExtended',
sql:
'ALTER TABLE "oldtablename" RENAME TO "newtablename";' },
sql:
'ALTER TABLE "oldtablename" RENAME TO "newtablename";' }
答案 0 :(得分:2)
这有点令人困惑,但是您需要为连接定义一个搜索路径和方言选项。
const sequelizeConn = new Sequelize(ENV_DB_URL, {
schema: 'custom',
logging: false,
searchPath: 'custom',
dialectOptions: {
prependSearchPath: true
}
})