forked from liquibase/liquibase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add binary, varbinary and longvarbinary support for Snowflake liquiba…
…se#4225 (liquibase#4226) * Add binary and varbinary support for Snowflake liquibase#4225 --------- Co-authored-by: Avanish <[email protected]> Co-authored-by: filipe <[email protected]>
- Loading branch information
1 parent
bdd5fb1
commit 3fc6c78
Showing
3 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
liquibase-snowflake/src/main/java/liquibase/datatype/core/BinaryTypeSnowflake.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package liquibase.datatype.core; | ||
|
||
import liquibase.change.core.LoadDataChange; | ||
import liquibase.database.Database; | ||
import liquibase.database.core.SnowflakeDatabase; | ||
import liquibase.datatype.DataTypeInfo; | ||
import liquibase.datatype.DatabaseDataType; | ||
import liquibase.datatype.LiquibaseDataType; | ||
import liquibase.util.StringUtil; | ||
|
||
import java.util.Locale; | ||
|
||
@DataTypeInfo(name = "binary", aliases = {"longblob", "longvarbinary", "java.sql.Types.BLOB", "java.sql.Types.LONGBLOB", "java.sql.Types.LONGVARBINARY", "java.sql.Types.VARBINARY", "java.sql.Types.BINARY", "varbinary", "binary", "image", "tinyblob", "mediumblob"}, minParameters = 0, maxParameters = 12, priority = LiquibaseDataType.PRIORITY_DATABASE) | ||
public class BinaryTypeSnowflake extends LiquibaseDataType { | ||
@Override | ||
public DatabaseDataType toDatabaseDataType(Database database) { | ||
String originalDefinition = StringUtil.trimToEmpty(getRawDefinition()); | ||
if (database instanceof SnowflakeDatabase) { | ||
if (originalDefinition.toLowerCase(Locale.US).startsWith("varbinary") || originalDefinition.startsWith("java.sql.Types.VARBINARY")) { | ||
return new DatabaseDataType("VARBINARY", getParameters()); | ||
} else { | ||
return new DatabaseDataType("BINARY", getParameters()); | ||
} | ||
} | ||
return super.toDatabaseDataType(database); | ||
} | ||
|
||
@Override | ||
public LoadDataChange.LOAD_DATA_TYPE getLoadTypeName() { | ||
return LoadDataChange.LOAD_DATA_TYPE.OTHER; | ||
} | ||
@Override | ||
public boolean supports(Database database) { | ||
return database instanceof SnowflakeDatabase; | ||
} | ||
|
||
@Override | ||
public int getPriority() { | ||
return PRIORITY_DATABASE; | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
...ibase-snowflake/src/main/resources/META-INF/services/liquibase.datatype.LiquibaseDataType
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
liquibase.datatype.core.TextDataTypeSnowflake | ||
liquibase.datatype.core.TimestampNTZTypeSnowflake | ||
liquibase.datatype.core.DoubleDataTypeSnowflake | ||
liquibase.datatype.core.DoubleDataTypeSnowflake | ||
liquibase.datatype.core.BinaryTypeSnowflake |
82 changes: 82 additions & 0 deletions
82
liquibase-snowflake/src/test/java/liquibase/datatype/BinaryTypeSnowflakeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package liquibase.datatype; | ||
|
||
import liquibase.database.core.OracleDatabase; | ||
import liquibase.database.core.SnowflakeDatabase; | ||
import liquibase.datatype.core.BinaryTypeSnowflake; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static liquibase.servicelocator.PrioritizedService.PRIORITY_DATABASE; | ||
import static org.junit.Assert.*; | ||
|
||
public class BinaryTypeSnowflakeTest { | ||
|
||
|
||
BinaryTypeSnowflake binaryTypeSnowflake; | ||
SnowflakeDatabase snowflakeDatabase; | ||
|
||
@Before | ||
public void setup() { | ||
binaryTypeSnowflake = new BinaryTypeSnowflake(); | ||
snowflakeDatabase = new SnowflakeDatabase(); | ||
} | ||
|
||
@Test | ||
public void toDatabaseDataType() { | ||
DatabaseDataType databaseDataType = binaryTypeSnowflake.toDatabaseDataType(snowflakeDatabase); | ||
assertEquals("BINARY", databaseDataType.getType()); | ||
assertEquals("BINARY", databaseDataType.toSql()); | ||
} | ||
|
||
@Test | ||
public void binaryAliases() { | ||
LiquibaseDataType liquibaseDataType = DataTypeFactory.getInstance().fromDescription("binary", snowflakeDatabase); | ||
String[] aliases = liquibaseDataType.getAliases(); | ||
assertEquals(12, aliases.length); | ||
List<String> snowflakeBinaryAliasList = Arrays.asList("longblob", | ||
"longvarbinary", | ||
"java.sql.Types.BLOB", | ||
"java.sql.Types.LONGBLOB", | ||
"java.sql.Types.LONGVARBINARY", | ||
"java.sql.Types.VARBINARY", | ||
"java.sql.Types.BINARY", | ||
"varbinary", | ||
"binary", | ||
"image", | ||
"tinyblob", | ||
"mediumblob"); | ||
assertTrue(Arrays.asList(aliases).containsAll(snowflakeBinaryAliasList)); | ||
} | ||
|
||
@Test | ||
public void blobConvertsToBinary() { | ||
LiquibaseDataType liquibaseDataType = DataTypeFactory.getInstance().fromDescription("binary", snowflakeDatabase); | ||
assertEquals("liquibase.datatype.core.BinaryTypeSnowflake", liquibaseDataType.getClass().getName()); | ||
} | ||
|
||
@Test | ||
public void supports() { | ||
assertTrue(binaryTypeSnowflake.supports(snowflakeDatabase)); | ||
assertFalse(binaryTypeSnowflake.supports(new OracleDatabase())); | ||
} | ||
|
||
@Test | ||
public void getPriority() { | ||
assertEquals(PRIORITY_DATABASE, binaryTypeSnowflake.getPriority()); | ||
} | ||
|
||
@Test | ||
public void getMinParameters() { | ||
assertEquals(0, binaryTypeSnowflake.getMinParameters(snowflakeDatabase)); | ||
} | ||
|
||
@Test | ||
public void getMaxParameters() { | ||
assertEquals(12, binaryTypeSnowflake.getMaxParameters(snowflakeDatabase)); | ||
} | ||
|
||
|
||
} |