Java Liquibase 为 postgres 创建架构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36419328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:51:57  来源:igfitidea点击:

Liquibase create Schema for postgres

javapostgresqlliquibasedropwizard

提问by Christian

I'm using Dropwizard (1.0.0) and Liquibase to create a database if it's not existing.

如果数据库不存在,我正在使用 Dropwizard (1.0.0) 和 Liquibase 创建一个数据库。

Problem here is that I'm using a different Postgres schema (not public). It seems like Liquibase is not able to create this schema before is it? I was expecting Liquibase to generate this schema, but it always throws a "Schema with name xx not found" if I try to build the database.

这里的问题是我使用的是不同的 Postgres 模式(非公开)。Liquibase 之前似乎无法创建此架构,是吗?我期待 Liquibase 生成此架构,但如果我尝试构建数据库,它总是会抛出“未找到名称为 xx 的架构”。

回答by fspinnenhirn

Even though Liquibase does not have CREATE SCHEMAin its bundled changes/refactorings (and therefore doesn't generate one during a dropwizard db dump), you could still include this as a changeset in your migrations changelog using the sqltag, as follows:

即使 LiquibaseCREATE SCHEMA的捆绑更改/重构中没有(因此不会在 dropwizard 期间生成db dump),您仍然可以使用sql标记将其作为更改集包含在迁移更改日志中,如下所示:

<changeSet author="christian" id="1">
    <sql dbms="postgresql" endDelimiter=";">
        CREATE SCHEMA foo
    </sql>
</changeSet>

Note that Liquibase will create it'sown tables in thePUBLICschema, regardless - before applying any changesets:
If you run db migrate --dry-runin dropwizard, you'll see that Liquibase would first execute

请注意,Liquibase 将PUBLIC架构中创建自己的,无论 - 在应用任何变更集之前:
如果您db migrate --dry-run在 dropwizard 中运行,您将看到 Liquibase 将首先执行

CREATE TABLE PUBLIC.DATABASECHANGELOGLOCK ...
CREATE TABLE PUBLIC.DATABASECHANGELOG ...

before running

跑步前

CREATE SCHEMA foo;

回答by Abe

Not directly in answer to the question, but posting it for anyone who ran into the error I did, with creating tables in multiple schemas. I was getting an error executing this from maven with the defaultSchemaNameconfiguration.

不是直接回答这个问题,而是将它发布给遇到我所做错误的任何人,在多个模式中创建表。我在使用defaultSchemaName配置从 maven 执行此操作时遇到错误。

[ERROR] Failed to execute goal org.liquibase:liquibase-maven-plugin:3.6.2:update (default-cli) on project demo: Error setting up or running Liquibase: ERROR: relation "databasechangelog" already exists [Failed SQL: CREATE TABLE databasechangelog (ID VARCHAR(255) NOT NULL, AUTHOR VARCHAR(255) NOT NULL, FILENAME VARCHAR(255) NOT NULL, DATEEXECUTED TIMESTAMP WITHOUT TIME ZONE NOT NULL, ORDEREXECUTED INTEGER NOT NULL, EXECTYPE VARCHAR(10) NOT NULL, MD5SUM VARCHAR(35), DESCRIPTION VARCHAR(255), COMMENTS VARCHAR(255), TAG VARCHAR(255), LIQUIBASE VARCHAR(20), CONTEXTS VARCHAR(255), LABELS VARCHAR(255), DEPLOYMENT_ID VARCHAR(10))] -> [Help 1]

I tried to fix it by adding the following configurations to pom.xml, but that was only a partial solution:

我试图通过向 pom.xml 添加以下配置来修复它,但这只是部分解决方案:

<defaultSchemaName>foo</defaultSchemaName>
<changelogSchemaName>foo</changelogSchemaName>

Finally, I got this fixed by adding foo to the end of my connection string also, like this jdbc:postgresql://localhost:5432/postgres?currentSchema=foo

最后,我通过在连接字符串的末尾添加 foo 来解决这个问题,就像这样 jdbc:postgresql://localhost:5432/postgres?currentSchema=foo