Search code examples
databaseprimary-keyliquibase

Can I skip primary key clause from Liquibase LoadUpdateData XML tag


My Table structure as below

CREATE TABLE domain_stat (
    domain_stat bpchar(2) NOT NULL,
    des_domain_stat varchar(40) NULL,
    cde_domain_stat_catg bpchar(1) NOT NULL,
    date_crea timestamp NOT NULL,
    id_crea_user bpchar(8) NOT NULL,
    date_updt timestamp NOT NULL,
    id_updt_user bpchar(8) NOT NULL
);

As such there is no primary key involved in this case, and even my actual data does contain duplicate rows as well.

The problem is via loading data into table using liquibase syntax I have to add primarykey clause forcefully. Is there any way to skip primary key. or any alternative to add composite key so that complete row can be identify as unique row.

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
    <changeSet author="liquibase-docs"
        id="loadUpdateData-example" context="!prod">
        <loadUpdateData encoding="UTF-8"
            file="config/liquibase/data/domain_stat.csv" onlyUpdate="false" primaryKey="id"
            quotchar="'" separator="," tableName="domain_stat">
        </loadUpdateData>
    </changeSet>
</databaseChangeLog>

Solution

  • I have found work around for this problem. Since I dont have primary keys on my tables. I have created a executable bat file and in that I used Postgres \COPY instruction to load CSV file data into respective table.

    Bat file as

    set PGPASSWORD=root
    set DB_HOST_NAME="localhost"
    set DB_PORT="5434"
    set DB_USER="postgres"
    set DB_NAME="db_dev"
    
    echo "Importing accessory Data"
    psql -h %DB_HOST_NAME% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -c "\COPY "dev".p_table       FROM ../data/p_data.csv delimiter ',' csv HEADER NULL AS 'null';"
    
    

    my liquibase XML file looks like as

    <?xml version="1.1" encoding="UTF-8" standalone="no"?>
    <databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
        <changeSet author="liquibase-docs" context="!prod"
            id="executeCommand-example">
            <executeCommand executable="import-data.bat"  />
        </changeSet>
    </databaseChangeLog>
    

    And then import this file into master changelog, this works for me !!