Liquibase: как обновить таблицу соединений из CSV? - PullRequest
0 голосов
/ 23 октября 2018

У меня есть таблица соединения с 2 столбцами:

a_id, b_id

, и я загружаю ее данные из следующих csv

a_id, b_id
1,1
1,2
1,3
2,2
2,3

ЧтоМне нужно сделать, когда некоторые записи добавляются в CSV, чтобы обновить таблицу соединения.

Я попытался с помощью следующего изменения, но он жалуется на дубликаты идентификаторов:

<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>

, ноон жалуется на существующие идентификаторы:

  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>

Это работает, но я хотел бы избежать удаления возможных записей, вставленных пользователем.Любая идея о том, как достичь этой цели?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...