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

Add proper support for autoIncrement fields #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions project/app-changelog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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-latest.xsd">

<!-- Changes to include -->
<includeAll path="project/changes/"/>

</databaseChangeLog>

Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

<changeSet id="1" author="example">
<changeSet id="3" author="example">
<ext:createTable tableName="managed_system" tableFormat="delta">
<column name="id" type="int"/>
<column name="name" type="varchar(20)"/>
</ext:createTable>
</changeSet>

<changeSet id="2" author="example">
<changeSet id="4" author="example">
<createTable tableName="user_table">
<column name="id" type="int"/>
<column name="username" type="varchar(20)"/>
<column name="password" type="varchar(20)"/>
</createTable>
</changeSet>

<changeSet id="3" author="example">
<ext:optimize tableName="secure_user_table" zorderColumns="col1"/>
<changeSet id="5" author="example">
<ext:optimize tableName="user_table" zorderColumns="id"/>
</changeSet>

</databaseChangeLog>
Expand Down
24 changes: 24 additions & 0 deletions project/changes/example.changelog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
databaseChangeLog:

- changeSet:
id: 1
author: codydavis
changes:
- createTable:
catalogName: main
schemaName: liquibase
tableName: test_yml
columns:
- column:
name: id
type: bigint
autoIncrement: true

- column:
name: name_of_col
type: string

- column:
name: test_struct
type: STRUCT<ID string, name string>

3 changes: 3 additions & 0 deletions project/changes/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--liquibase formatted sql
--changeset cody:2
INSERT INTO main.liquibase.test_yml (id, name_of_col, test_struct) VALUES ('1', 'this_col', named_struct("ID", "2", "name","cody"))
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,53 @@ public boolean supportsTablespaces() {
return false;
}


@Override
protected String getAutoIncrementClause(final String generationType, final Boolean defaultOnNull) {
public String getAutoIncrementClause(final BigInteger startWith, final BigInteger incrementBy, final String generationType, final Boolean defaultOnNull) {
if (!this.supportsAutoIncrement()) {
return "";
}

// generate an SQL:2003 STANDARD compliant auto increment clause by default

String autoIncrementClause = getAutoIncrementClause(generationType, defaultOnNull);

boolean generateStartWith = generateAutoIncrementStartWith(startWith);
boolean generateIncrementBy = generateAutoIncrementBy(incrementBy);

if (generateStartWith || generateIncrementBy) {
autoIncrementClause += getAutoIncrementOpening();
if (generateStartWith) {
autoIncrementClause += String.format(getAutoIncrementStartWithClause(), (startWith == null) ? defaultAutoIncrementStartWith : startWith);
}

if (StringUtil.isEmpty(generationType)) {
return super.getAutoIncrementClause();
if (generateIncrementBy) {
if (generateStartWith) { // for databricks there is no comma
autoIncrementClause += " ";

}

autoIncrementClause += String.format(getAutoIncrementByClause(), (incrementBy == null) ? defaultAutoIncrementBy : incrementBy);
}

autoIncrementClause += getAutoIncrementClosing();
}

String autoIncrementClause = "GENERATED %s AS IDENTITY"; // %s -- [ ALWAYS | BY DEFAULT ]
return String.format(autoIncrementClause, generationType);
return autoIncrementClause;
}

protected String getAutoIncrementClause() {
return "GENERATED BY DEFAULT AS IDENTITY";
}

@Override
protected String getAutoIncrementStartWithClause() {
return "%d";
return "START WITH %d";
}

@Override
protected String getAutoIncrementByClause() {
return "%d";
return "INCREMENT BY %d";
}

@Override
Expand Down