liquibase / liquibase

Main Liquibase Source
https://www.liquibase.org
Apache License 2.0
4.65k stars 1.84k forks source link

Generated changelog is incorrect from CockroachDB if script has USING HASH #4586

Open Tylorjg opened 1 year ago

Tylorjg commented 1 year ago

Search first

Description

If there is a object such as a table that was created that has one of the columns set to USING HASH, the generated changelog, either by generate-changelog or diff-changelog is incorrect.

In the case of a SQL changelog getting generated, it is not deployable as it creates two changesets, one creating the table with a primary key, and then another changeset creating the same primary key. After removing the second changeset, the object deployed doesn't match the same as a manual deployment.

For an XML generated changelog, the changelog is deployable and doesn't run into the primary key issue, but the object doesn't match a manual deployment.

Steps To Reproduce

  1. Deploy the following SQL code to CockroachDB
CREATE TABLE product (
    product_id int,
    department_id int,
    CONSTRAINT pk_my_table PRIMARY KEY (product_id, department_id) USING HASH
)
  1. Have Liquibase v4.23.0 installed
  2. Create a Liquibase project that can connect to a CockroachDB
  3. Run either generate-changelog or diff-changelog for an SQL or XML output
  4. Attempt to deploy the SQL changelog to a database that doesn't have that table.
  5. In both XML and SQL changelog output, the object doesn't match was is manually created in the database

Expected/Desired Behavior

The generated changelog either by generate-changelog or diff-changelog should be deployable SQL and match the object structure when manually doing a deployment.

Liquibase Version

Liquibase v4.23.0

Database Vendor & Version

CockroachDB v23.1

Liquibase Integration

No response

Liquibase Extensions

No response

OS and/or Infrastructure Type/Provider

No response

Additional Context

No response

Are you willing to submit a PR?

tati-qalified commented 1 year ago

Hello @Tylorjg, I haven't been able to replicate this exact bug.

Here's what I did:

  1. Executed CREATE TABLE product ( product_id int, department_id int, CONSTRAINT pk_my_table PRIMARY KEY (product_id, department_id) USING HASH ) in database system
  2. Ran liquibase generate-changelog (or diff-changelog)
  3. Switched to new database secondSystem
  4. Ran liquibase update using the generated changelog

With Liquibase version 4.23.0 and CockroachDB version 23.1, this is working just fine. One thing to note is that the generated SQL doesn't use "USING HASH"; it creates a hash-type column with a Unique constraint. If this isn't what's expected, then it's a problem, but the changelog file itself isn't generating any errors.

Here is the generated changelog for reference:

-- liquibase formatted sql

-- changeset tfernandez:1692719564225-1
CREATE TABLE "product" ("product_id" BIGINT NOT NULL, "department_id" BIGINT NOT NULL, "crdb_internal_department_id_product_id_shard_16" BIGINT NOT NULL, CONSTRAINT "pk_my_table" PRIMARY KEY ("product_id", "department_id"));

Would you be able to provide the generated code you're getting? Or re-test this and let me know if it's still an issue?

Thank you Tatiana

tati-qalified commented 9 months ago

@Tylorjg just following up on this, were you able to re-test it?