Skip to content

Commit

Permalink
Merge pull request #157 from adangel:index-prefix
Browse files Browse the repository at this point in the history
Support createIndex with specifying index prefix length (#148) #157

* index-prefix:
  Verify that index prefix length works also without liquibase-percona
  Support createIndex with specifying index prefix length (#148)
  • Loading branch information
adangel committed Nov 19, 2021
2 parents 5f201db + dd2f1b0 commit b86d970
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ integration test.

### Next

* [#148](https://github.com/liquibase/liquibase-percona/issues/148): Support createIndex with specifying index prefix length

### Version 4.6.1 (2021-11-06)

* Support for Liquibase 4.6.1.
Expand Down
3 changes: 3 additions & 0 deletions src/it/createIndex/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,8 @@ def assertColumn(resultset, unique, keyName, columnName) {
assert 1 == resultset.getInt(2)
}

// sub_part
assert 0 == resultset.getInt(8);

assert columnName == resultset.getString(5);
}
63 changes: 63 additions & 0 deletions src/it/createIndexPrefix/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under
the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may
obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to
in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under
the License. -->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>@[email protected]</groupId>
<artifactId>liquibase-percona-it-createIndexPrefix</artifactId>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>

<build>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>@liquibase.version@</version>
<dependencies>
<dependency>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>@mysql.version@</version>
</dependency>
</dependencies>
<configuration>
<changeLogFile>test-changelog.xml</changeLogFile>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://@config_host@:@config_port@/@config_dbname@?useSSL=false&amp;allowPublicKeyRetrieval=true</url>
<username>@config_user@</username>
<password>@config_password@</password>
</configuration>
<executions>
<execution>
<id>updateSQL</id>
<phase>pre-integration-test</phase>
<goals>
<goal>updateSQL</goal>
</goals>
</execution>
<execution>
<id>update</id>
<phase>pre-integration-test</phase>
<goals>
<goal>update</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
31 changes: 31 additions & 0 deletions src/it/createIndexPrefix/setup.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

def con, s;
try {
def props = new Properties();
props.setProperty("user", config_user)
props.setProperty("password", config_password)
con = new com.mysql.cj.jdbc.Driver().connect("jdbc:mysql://${config_host}:${config_port}?useSSL=false&allowPublicKeyRetrieval=true", props)
s = con.createStatement();
s.execute("DROP DATABASE IF EXISTS `${config_dbname}`")
s.execute("CREATE DATABASE `${config_dbname}`")
} finally {
s?.close();
con?.close();
}

println "Prepared empty database `${config_dbname}`"

return true
22 changes: 22 additions & 0 deletions src/it/createIndexPrefix/test-changelog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>

<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-4.2.xsd">

<changeSet id="1" author="Alice">
<createTable tableName="person">
<column name="name" type="varchar(255)">
<constraints primaryKey="true"/>
</column>
<column name="email" type="varchar(255)"/>
</createTable>
</changeSet>

<changeSet id="2" author="Alice">
<createIndex indexName="emailIdx" tableName="person" unique="true">
<column name="email(10)"/>
</createIndex>
</changeSet>
</databaseChangeLog>
70 changes: 70 additions & 0 deletions src/it/createIndexPrefix/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.sql.ResultSet;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()
def buildLogText = buildLog.text;
assert buildLogText.contains("Executing: pt-online-schema-change --alter-foreign-keys-method=auto --nocheck-unique-key-change --alter=\"ADD UNIQUE INDEX emailIdx (email(10))\" --password=*** --execute h=${config_host},P=${config_port},u=${config_user},D=testdb,t=person")
assert buildLogText.contains("Altering `testdb`.`person`...")
assert buildLogText.contains("Successfully altered `testdb`.`person`.")
assert buildLogText.contains("Index emailIdx created")
assert buildLogText.contains("ChangeSet test-changelog.xml::2::Alice ran successfully")

File sql = new File( basedir, 'target/liquibase/migrate.sql' )
assert sql.exists()
def sqlText = sql.text;
assert sqlText.contains("pt-online-schema-change --alter-foreign-keys-method=auto --nocheck-unique-key-change --alter=\"ADD UNIQUE INDEX emailIdx (email(10))\"")
assert !sqlText.contains("password=${config_password}")

// Verify that index prefix length works also without liquibase-percona
// See also https://github.com/liquibase/liquibase/issues/2191
assert sqlText.contains("CREATE UNIQUE INDEX emailIdx ON person(email(10));")

def con, s;
try {
def props = new Properties();
props.setProperty("user", config_user)
props.setProperty("password", config_password)
con = new com.mysql.cj.jdbc.Driver().connect("jdbc:mysql://${config_host}:${config_port}/${config_dbname}?useSSL=false&allowPublicKeyRetrieval=true", props)
s = con.createStatement();
r = s.executeQuery("SHOW INDEX FROM person")
assert r.next()
assert r.next() // we need the second row
assertColumn(r, true, "emailIdx", "email", 10)
r.close()
} finally {
s?.close();
con?.close();
}

def assertColumn(resultset, unique, keyName, columnName, subPart) {
assert keyName == resultset.getString(3)

if (unique) {
assert 0 == resultset.getInt(2)
} else {
assert 1 == resultset.getInt(2)
}

assert subPart == resultset.getInt(8);

assert columnName == resultset.getString(5);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,20 @@ public String generateAlterStatement( Database database ) {
while (iterator.hasNext()) {
AddColumnConfig column = iterator.next();
if (Boolean.TRUE.equals(column.getComputed())) {
// don't quote functions
alter.append(column.getName());
} else {
alter.append(database.escapeColumnName(this.getCatalogName(), this.getSchemaName(), this.getTableName(), column.getName()));
String justColumnName = column.getName();
String prefixLength = "";
// maybe prefix length
int prefixLengthIndex = justColumnName.indexOf('(');
if (prefixLengthIndex > -1) {
prefixLength = justColumnName.substring(prefixLengthIndex);
justColumnName = justColumnName.substring(0, prefixLengthIndex);
}

alter.append(database.escapeColumnName(this.getCatalogName(), this.getSchemaName(), this.getTableName(), justColumnName));
alter.append(prefixLength);
}
if (iterator.hasNext()) {
alter.append(", ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public void testCreateNewIndexReal() {
assertPerconaChange(alterText);
}

@Test
public void testCreateNewIndexRealWithPrefix() {
getChange().getColumns().get(0).setName( "indexedColumn(10)" );
assertPerconaChange( "ADD UNIQUE INDEX theIndexName (indexedColumn(10))" );
}

@Test
public void testCreateIndexNonUnique() {
getChange().setUnique(false);
Expand Down

0 comments on commit b86d970

Please sign in to comment.