【问题标题】:How do I migrate an existing production database with Liquibase and JHipster?如何使用 Liquibase 和 JHipster 迁移现有的生产数据库?
【发布时间】:2017-10-24 21:08:34
【问题描述】:

我正在尝试更新我的21-points.com 应用以​​使用JHipster Mini-Book 中的latest code。以下是我目前所做的:

  1. 使用 Heroku 的 pg:pull 命令将我的生产数据库复制到本地数据库:

    heroku pg:pull HEROKU_POSTGRESQL_GRAY_URL health --app health-by-points
    
  2. 安装 Liquibase 并将 PostgreSQL 数据库驱动程序复制到 $LIQUIBASE_HOME/lib 中。

    [mraible:/opt/tools/liquibase-3.5.3-bin] % cp ~/.m2/repository/org/postgresql/postgresql/9.4.1212/postgresql-9.4.1212.jar lib/.
    
  3. 运行 liquibase 的“diffChangeLog”以生成更改日志以从旧数据库迁移到新架构。

    ./liquibase \
    --driver=org.postgresql.Driver --url=jdbc:postgresql://localhost:5432/health --username=postgres \
    diffChangeLog \
    --referenceUrl=jdbc:postgresql://localhost:5432/twentyonepoints \
    --referenceUsername=twentyonepoints --referencePassword=21points
    
  4. 将输出复制到src/main/resources/config/liquibase/changelog/20171024115100_migrate_from_v2.xml 并添加到config/liquibase/master.xml

  5. 在我最新最好的 JHipster 应用程序中更改了 application-prod.xml 以指向我从 Heroku 下载的旧数据库。

  6. ./gradlew -Pprod。启动时出错:

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/jhipster/health/config/DatabaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.ValidationFailedException: Validation Failed:
     2 change sets check sum
          config/liquibase/changelog/00000000000000_initial_schema.xml::00000000000000::jhipster was: 7:eda8cd7fd15284e6128be97bd8edea82 but is now: 7:a6235f40597a13436aa36c6d61db2269
          config/liquibase/changelog/00000000000000_initial_schema.xml::00000000000001::jhipster was: 7:e87abbb935c561251405aea5c91bc3a4 but is now: 7:29cf7a7467c2961e7a2366c4347704d7
    
  7. 运行以下命令来更新数据库中的 md5sum。

    update databasechangelog set md5sum = '7:a6235f40597a13436aa36c6d61db2269' where md5sum = '7:eda8cd7fd15284e6128be97bd8edea82';
    update databasechangelog set md5sum = '7:29cf7a7467c2961e7a2366c4347704d7' where md5sum = '7:e87abbb935c561251405aea5c91bc3a4’;
    
  8. 再次尝试运行。

    2017-10-24 12:12:02.670 ERROR 22960 --- [           main] liquibase                                : classpath:config/liquibase/master.xml: config/liquibase/changelog/20160831020048_added_entity_Points.xml::20160831020048-1::jhipster: Change Set config/liquibase/changelog/20160831020048_added_entity_Points.xml::20160831020048-1::jhipster failed.  Error: ERROR: relation "points" already exists [Failed SQL: CREATE TABLE public.points (id BIGINT NOT NULL, jhi_date date NOT NULL, exercise INT, meals INT, alcohol INT, notes VARCHAR(140), user_id BIGINT, CONSTRAINT PK_POINTS PRIMARY KEY (id))]
    2017-10-24 12:12:02.672  WARN 22960 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/jhipster/health/config/DatabaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set config/liquibase/changelog/20160831020048_added_entity_Points.xml::20160831020048-1::jhipster:
     Reason: liquibase.exception.DatabaseException: ERROR: relation "points" already exists [Failed SQL: CREATE TABLE public.points (id BIGINT NOT NULL, jhi_date date NOT NULL, exercise INT, meals INT, alcohol INT, notes VARCHAR(140), user_id BIGINT, CONSTRAINT PK_POINTS PRIMARY KEY (id))]
    

知道它为什么要再次尝试创建我的数据库表吗?是因为我更改了 md5sum 吗?

从旧的 JHipster 生成的架构迁移到全新的架构时,这些步骤看起来是否正确?我的旧架构和新架构之间的唯一区别是旧架构在user 表中有一个preferences_id 列,而新架构在preferences 表中有一个user_id 列。

【问题讨论】:

    标签: postgresql heroku jhipster liquibase


    【解决方案1】:

    我会做几乎相同的事情:在第 7 步中,我将中继用于处理校验和的 liquibase 方法,即我将从命令行使用 clearCheckSums,然后在下一次运行时,所有校验和都将被重新计算,如果存在仍然是关于想要再次运行的脚本的错误,如第 8 步中那样,那么您可以从命令行使用changelogSync,例如:

    ./liquibase --driver=org.postgresql.Driver --url=jdbc:postgresql://localhost:5432/health --username=postgres clearCheckSums ./liquibase --driver=org.postgresql.Driver --url=jdbc:postgresql://localhost:5432/health --username=postgres changelogSync

    【讨论】:

    • 我通过在我的目标数据库中使用update databasechangelog set md5sum=null; 弄清楚了,这与您的答案非常相似,所以我会接受它。我还必须对生成的 XML 进行一些调整,但这并不太难。我很快就会写一篇关于它的博客文章。
    • 10x 用于接受答案。期待你的帖子。在线时请加个链接,或许我们可以得到一些内幕供github.com/jhipster/generator-jhipster/issues/5954使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 2017-06-23
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多