Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getting ENUM column values from Mysql database having multiple schemas #2616

Merged
merged 3 commits into from
May 25, 2022
Merged

Fix getting ENUM column values from Mysql database having multiple schemas #2616

merged 3 commits into from
May 25, 2022

Conversation

akatiyar
Copy link

@akatiyar akatiyar commented Mar 9, 2022

Fixes #2594 - getting ENUM column values from Mysql database having multiple schemas with same table name and column name.

Environment

Liquibase Version: 4.8.0 (PR is from Master branch)

Liquibase Integration & Version: Maven

Liquibase Extension(s) & Version: NA

Database Vendor & Version: MySQL

Operating System Type & Version: Mac

Pull Request Type

  • Bug fix (non-breaking change which fixes an issue.)
  • Enhancement/New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

In MySQL, if DB has 2 schemas say db1 and db2. If both db1 and db2 have a table with the same name, stay example_table. And if table in both schemas has a column with same name (say example_column) of type ENUM with different ENUM values, generate-changelog on db1 (or db2) returns a union of values from db1.example_table.example_column and db2.example_table.example_column.

This PR fixes it by adding a check on TABLE_SCHEMA in the where clause to retrieve ENUM values.

Steps To Reproduce

  1. In a MySQL instance, create 2 schemas - say db1 and db2.
  2. In both schemas, have a table with the same name (say example_table) with a column with the same name (say example_column). Column should be of type
CREATE TABLE db1.example_table (example_column ENUM('enum1', 'enum2') NOT NULL);
CREATE TABLE db2.example_table (example_column ENUM('enum3', 'enum4') NOT NULL);

Now if you run liquibase to generate the changelog for either of db1 or db2, it shows ENUM values for the column from both db1 and db2.

$ liquibase generate-changelog --username=*** --password=***  --url=jdbc:mysql://localhost:3306/db1 --changeLogFile=db1_changelog.mysql.sql 

# This generates changes as below.
-- changeset akatiyar:1646776634455-11
CREATE TABLE db1.example_table (example_column ENUM('enum1', 'enum2', 'enum3', 'enum4') NOT NULL);

Actual Behavior

See Steps To Reproduce section above.

Expected/Desired Behavior

See Steps To Reproduce section above.

Screenshots (if appropriate)

If applicable, add screenshots to help explain your problem.

Additional Context

Add any other context about the problem here.

Fast Track PR Acceptance Checklist:

Need Help?

Come chat with us on our discord channel

…hemas with same table name and column name
@nvoxland
Copy link
Contributor

Code Review Notes:

Things to be aware of

  • I didn't run the code, just reviewed the test
  • Code changes make sense for the reported bug
  • Only impacts mysql and mariadb

Things to worry about

  • Nothing

@nvoxland nvoxland requested a review from suryaaki2 April 29, 2022 20:37
@nvoxland nvoxland added the SafeToBuild Indicates that a particular PR contains changes which are safe to build using GitHub actions label Apr 29, 2022
@nvoxland nvoxland removed this from To Do in Conditioning++ Apr 29, 2022
@github-actions
Copy link

github-actions bot commented Apr 29, 2022

Unit Test Results

  4 512 files  ±0    4 512 suites  ±0   34m 11s ⏱️ -56s
  4 413 tests ±0    4 199 ✔️ ±0     214 💤 ±0  0 ±0 
52 236 runs  ±0  47 228 ✔️ ±0  5 008 💤 ±0  0 ±0 

Results for commit a9cd137. ± Comparison against base commit be2cce1.

♻️ This comment has been updated with latest results.

@FBurguer FBurguer self-assigned this May 6, 2022
@nvoxland nvoxland changed the base branch from master to 1_9 May 14, 2022 19:28
@nvoxland nvoxland changed the base branch from 1_9 to master May 14, 2022 19:28
@nvoxland nvoxland added SafeToBuild Indicates that a particular PR contains changes which are safe to build using GitHub actions and removed SafeToBuild Indicates that a particular PR contains changes which are safe to build using GitHub actions labels May 16, 2022
@nvoxland nvoxland added SafeToBuild Indicates that a particular PR contains changes which are safe to build using GitHub actions and removed SafeToBuild Indicates that a particular PR contains changes which are safe to build using GitHub actions labels May 16, 2022
@FBurguer FBurguer self-assigned this May 17, 2022
@FBurguer FBurguer removed their assignment May 17, 2022
@ecarneiro01 ecarneiro01 self-assigned this May 23, 2022
@ecarneiro01
Copy link

For this PR I checked that the changelogs were generated with the correct values for the ENUM defined in the table column. I also verified this in the output of the snapshot command.

First, I created the schemas 'test' and 'test2' and a table with a column of type ENUM in each of them:

CREATE TABLE test.example_table (example_column ENUM('enum1', 'enum2') NOT NULL);
CREATE TABLE test2.example_table (example_column ENUM('enum3', 'enum4') NOT NULL);

After that I checked the following items:

MariaDB

  • Verify the generated changelog has the right values in the created column (XML, SQL, JSON, YAML). PASS
  • Verify the snapshot output shows the right values in the created column (YAML, JSON). PASS

MySQL

  • Verify the generated changelog has the right values in the created column (XML, SQL, JSON, YAML). PASS
  • Verify the snapshot output shows the right values in the created column (YAML, JSON). PASS

This is the output of generateChangelog command in SQL format:

-- liquibase formatted sql

-- changeset enzoc:1653413079112-1
CREATE TABLE example_table (example_column ENUM('enum2', 'enum1') NOT NULL);

And this is the output of the snapshot command in JSON format:

{
  "snapshot": {
    "created": "2022-05-24T15:00:17.663",
    "database": {
      "productVersion": "8.0.26",
      "shortName": "mysql",
      "majorVersion": "8",
      "minorVersion": "0",
      "user": "root@172.18.0.1",
      "productName": "MySQL",
      "url": "jdbc:mysql://localhost:33061/test"
    },
    "objects": {
      "liquibase.structure.core.Catalog": [
        {
          "catalog": {
            "default": true,
            "name": "test",
            "snapshotId": "c7f9100"
          }
        }
      ]
      ,
      "liquibase.structure.core.Column": [
        {
          "column": {
            "name": "example_column",
            "nullable": false,
            "order": "1!{java.lang.Integer}",
            "relation": "liquibase.structure.core.Table#c7f9102",
            "snapshotId": "c7f9103",
            "type": {
              "typeName": "ENUM('enum2', 'enum1')"
            }
          }
        }
      ]
      ,
      "liquibase.structure.core.Schema": [
        {
          "schema": {
            "catalog": "liquibase.structure.core.Catalog#c7f9100",
            "default": true,
            "name": "test",
            "snapshotId": "c7f9101"
          }
        }
      ]
      ,
      "liquibase.structure.core.Table": [
        {
          "table": {
            "columns": [
              "liquibase.structure.core.Column#c7f9103"
            ]
            ,
            "default_tablespace": false,
            "name": "example_table",
            "schema": "liquibase.structure.core.Schema#c7f9101",
            "snapshotId": "c7f9102"
          }
        }
      ]
      
    },
    "snapshotControl": {
      "snapshotControl": {
        "includedType": [
          "liquibase.structure.core.Catalog",
          "liquibase.structure.core.Column",
          "liquibase.structure.core.ForeignKey",
          "liquibase.structure.core.Index",
          "liquibase.structure.core.PrimaryKey",
          "liquibase.structure.core.Schema",
          "liquibase.structure.core.Sequence",
          "liquibase.structure.core.Table",
          "liquibase.structure.core.UniqueConstraint",
          "liquibase.structure.core.View"
        ]
        
      }
    }
  }
}

The rest of the outputed files can be found in this file inside the folder 'postFix':
2616.zip

@nvoxland nvoxland merged commit 1767dd3 into liquibase:master May 25, 2022
@nvoxland nvoxland added this to the NEXT milestone May 25, 2022
@akatiyar akatiyar deleted the fix_enum_values branch June 2, 2022 06:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DBMySQL featureSnapshot IntegrationMaven OSMac SafeToBuild Indicates that a particular PR contains changes which are safe to build using GitHub actions Severity4 ver4.8.0
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

ENUMs are combined when pulling a snapshot if two databases have tables with the same name.
6 participants