Liquibase:如何从CSV更新联接表?

时间:2018-10-22 22:09:19

标签: liquibase loaddata

我有一个带有2个列的联接表:

a_id, b_id

并且我正在从以下csv

加载其数据
a_id, b_id
1,1
1,2
1,3
2,2
2,3

我需要做的是,当将某些条目添加到csv中时,以更新联接表。

我尝试了以下changelod,但它抱怨ID重复:

<changeSet id="20180530172823-1" author="developer" runOnChange="true">
    <loadUpdateData
        primaryKey="a_id,b_id"
        catalogName="cat"
        encoding="UTF-8"
        file="config/liquibase/a_b_join.csv"
        schemaName="public"
        separator=","
        quotchar="'"
        tableName="a_b">
    </loadUpdateData>
</changeSet>

但它抱怨现有的ID:

  detail: Key ( a_id,  b_id)=( 1, 1) already exists.
  where: SQL statement "INSERT INTO public. a_b ( a_id,  b_id) VALUES (' 1', '1')"
PL/pgSQL function inline_code_block line 5 at SQL statement [Failed SQL: DO
$$
BEGIN
UPDATE public. a_b SET  b_id = '1' WHERE  a_id = ' 1' AND  b_id is NULL;
IF not found THEN
INSERT INTO public. a_b ( a_id,  b_id) VALUES (' 1', '1');
END IF;
END;
$$
LANGUAGE plpgsql;]
2018-10-23 00: 19:41.864 ERROR 28574 --- [neut-Executor-1] i.g.j.c.liquibase.AsyncSpringLiquibase   : Liquibase could not start correctly, your database is NOT ready: Migration failed for change set config/liquibase/changelog/20180531103611_added_a_b_join_data.xml::20180531103611-1::developer:
     Reason: liquibase.exception.DatabaseException: ERROR: duplicate key value violates unique constraint " a_b_pkey"
  detail: Key ( a_id,  b_id)=( 1, 1) already exists.
  where: SQL statement "INSERT INTO public. a_b ( a_id,  b_id) VALUES (' 1', '1')"
PL/pgSQL function inline_code_block line 5 at SQL statement [Failed SQL: DO
$$
BEGIN
UPDATE public. a_b SET  b_id = '1' WHERE  a_id = ' 1' AND  b_id is NULL;
IF not found THEN
INSERT INTO public. a_b ( a_id,  b_id) VALUES (' 1', '1');
END IF;
END;
$$
LANGUAGE plpgsql;]

liquibase.exception.MigrationFailedException: Migration failed for change set config/liquibase/changelog/20180531103611_added_a_b_join_data.xml::20180531103611-1::developer:
     Reason: liquibase.exception.DatabaseException: ERROR: duplicate key value violates unique constraint " a_b_pkey"
  detail: Key ( a_id,  b_id)=( 1, 1) already exists.
  where: SQL statement "INSERT INTO public. a_b ( a_id,  b_id) VALUES (' 1', '1')"
PL/pgSQL function inline_code_block line 5 at SQL statement [Failed SQL: DO
$$
BEGIN
UPDATE public. a_b SET  b_id = '1' WHERE  a_id = ' 1' AND  b_id is NULL;
IF not found THEN
INSERT INTO public. a_b ( a_id,  b_id) VALUES (' 1', '1');
END IF;
END;
$$
LANGUAGE plpgsql;]
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:6 1)
    at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:51)
    at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:79)
    at liquibase.Liquibase.update(Liquibase.java:214)
    at liquibase.Liquibase.update(Liquibase.java: 12)
    at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:431)
    at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:388)
    at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.initDb(AsyncSpringLiquibase.java:94)
    at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.lambda$afterPropertiesSet$0(AsyncSpringLiquibase.java:77)
    at io.github.jhipster.async.ExceptionHandlingAsyncTaskExecutor.lambda$createWrappedRunnable$1(ExceptionHandlingAsyncTaskExecutor.java:68)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: liquibase.exception.DatabaseException: ERROR: duplicate key value violates unique constraint " a_b_pkey"
  detail: Key ( a_id,  b_id)=( 1, 1) already exists.
  where: SQL statement "INSERT INTO public. a_b ( a_id,  b_id) VALUES (' 1', '1')"
PL/pgSQL function inline_code_block line 5 at SQL statement [Failed SQL: DO
$$
BEGIN
UPDATE public. a_b SET  b_id = '1' WHERE  a_id = ' 1' AND  b_id is NULL;
IF not found THEN
INSERT INTO public. a_b ( a_id,  b_id) VALUES (' 1', '1');
END IF;
END;
$$
LANGUAGE plpgsql;]
    at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:309)
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:55)
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:113)
    at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1277)
    at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1259)
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:582)
    ... 12 common frames omitted
Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint " a_b_pkey"
  detail: Key ( a_id,  b_id)=( 1, 1) already exists.
  where: SQL statement "INSERT INTO public. a_b ( a_id,  b_id) VALUES (' 1', '1')"
PL/pgSQL function inline_code_block line 5 at SQL statement
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2155)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:288)
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:430)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:356)
    at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:303)
    at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:289)
    at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:266)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:262)
    at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95)
    at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java)
    at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:307)
    ... 17 common frames omitted

因此,现在,我首先删除现有值,然后使用以下更改日志从csv加载数据(甚至更改):

<changeSet id="20180530172823-1" author="developer" runOnChange="true">
    <delete catalogName="cat"
            schemaName="public"
            tableName="a_b">
        <where>true</where>
    </delete>

    <loadData
        catalogName="cat"
        encoding="UTF-8"
        file="config/liquibase/a_b_join.csv"
        schemaName="public"
        separator=","
        quotchar="'"
        tableName="a_b">
    </loadUpdateData>
</changeSet>

它有效,但我想避免删除用户插入的可能条目。关于如何实现此目标的任何想法?

0 个答案:

没有答案