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 big integer column support #143

Merged
merged 1 commit into from
Feb 15, 2020
Merged
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
24 changes: 0 additions & 24 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/harmonica.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 3 additions & 20 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
#
# Harmonica: Kotlin Database Migration Tool
# Copyright (C) 2019 Kenji Otsuka
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

#Tue Feb 11 10:56:09 CET 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-bin.zip
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package com.improve_future.harmonica.core

import com.improve_future.harmonica.core.adapter.*
import com.improve_future.harmonica.core.adapter.DbAdapter
import com.improve_future.harmonica.core.adapter.MySqlAdapter
import com.improve_future.harmonica.core.adapter.OracleAdapter
Expand Down Expand Up @@ -150,6 +149,62 @@ abstract class AbstractMigration {
addColumn(tableName, integerColumn, first, justBeforeColumnName)
}

/**
* Add new big integer column to existing table.
*
* @param tableName Table name.
* @param columnName Column name.
* @param nullable
* @param default
* @param unsigned Valid only for MySQL.
* @param first You add column at first of the column (valid only for MySQL)
* @param justBeforeColumnName Column name the new column to be added just after.
* (valid only for MySQL)
*/
fun addBigIntegerColumn(
tableName: String, columnName: String,
nullable: Boolean = true, default: Long? = null,
unsigned: Boolean = false,
first: Boolean = false,
justBeforeColumnName: String? = null
) {
val bigIntegerColumn = BigIntegerColumn(columnName)
bigIntegerColumn.also {
it.nullable = nullable
it.default = default
it.unsigned = unsigned
}
addColumn(tableName, bigIntegerColumn, first, justBeforeColumnName)
}

/**
* Add new big integer column to existing table.
*
* @param tableName Table name.
* @param columnName Column name.
* @param nullable
* @param default
* @param unsigned Valid only for MySQL.
* @param first You add column at first of the column (valid only for MySQL)
* @param justBeforeColumnName Column name the new column to be added just after.
* (valid only for MySQL)
*/
fun addBigIntegerColumn(
tableName: String, columnName: String,
nullable: Boolean = true, default: RawSql,
unsigned: Boolean = false,
first: Boolean = false,
justBeforeColumnName: String? = null
) {
val integerColumn = BigIntegerColumn(columnName)
integerColumn.also {
it.nullable = nullable
it.sqlDefault = default.sql
it.unsigned = unsigned
}
addColumn(tableName, integerColumn, first, justBeforeColumnName)
}

/**
* Add new decimal column to existing table.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ internal abstract class DbAdapter(private val connection: ConnectionInterface) {
protected open fun sqlType(column: AbstractColumn): String {
return when (column) {
is IntegerColumn -> "INTEGER"
is BigIntegerColumn -> "BIGINT"
is VarcharColumn -> "VARCHAR"
is DecimalColumn -> "DECIMAL"
is BooleanColumn -> "BOOL"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ internal class MySqlAdapter(connection: ConnectionInterface) :
sql += " UNSIGNED"
}
}
is BigIntegerColumn -> {
if (column.unsigned) {
sql += " UNSIGNED"
}
}
}
if (!column.nullable) sql += " NOT NULL"
if (column.hasDefault) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ internal class OracleAdapter(connection: ConnectionInterface) : DbAdapter(connec
override fun sqlType(column: AbstractColumn): String {
return when (column) {
is IntegerColumn -> "NUMBER"
is BigIntegerColumn -> "NUMBER"
is DecimalColumn -> "NUMBER"
is DateTimeColumn -> "DATE"
is TimeColumn -> "TIMESTAMP"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,52 @@ class TableBuilder {
return builder
}

/**
* Add big integer column.
*
* @param columnName
* @param nullable `false` for `NOT NULL` constraint. The default value is `true`.
* @param default
* @param unsigned Valid only for MySQL.
* @return
*/
fun bigInteger(
columnName: String,
nullable: Boolean = true,
default: Long? = null,
unsigned: Boolean = false
): ColumnBuilder {
val builder = ColumnBuilder(BigIntegerColumn(columnName).also {
it.nullable = nullable
it.default = default
})
addColumn(builder.column)
return builder
}

/**
* Add big integer column.
*
* @param columnName
* @param nullable `false` for `NOT NULL` constraint. The default value is `true`.
* @param default
* @param unsigned Valid only for MySQL.
* @return
*/
fun bigInteger(
columnName: String,
nullable: Boolean = true,
default: RawSql,
unsigned: Boolean = false
): ColumnBuilder {
val builder = ColumnBuilder(BigIntegerColumn(columnName).also {
it.nullable = nullable
it.sqlDefault = default.sql
})
addColumn(builder.column)
return builder
}

/**
* add varchar column
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package com.improve_future.harmonica.core.table.column
import com.improve_future.harmonica.core.Index
import com.improve_future.harmonica.core.RawSql


internal typealias Type = Int

internal abstract class AbstractColumn(
Expand Down Expand Up @@ -71,7 +70,6 @@ internal abstract class AbstractColumn(

var nullable = true


abstract var sqlDefault: String?

val hasDefault: Boolean
Expand All @@ -83,9 +81,9 @@ internal abstract class AbstractColumn(
val hasReference: Boolean
get() {
return !(
referenceTable.isNullOrBlank() ||
referenceColumn.isNullOrBlank()
)
referenceTable.isNullOrBlank() ||
referenceColumn.isNullOrBlank()
)
}

var comment: String? = null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Harmonica: Kotlin Database Migration Tool
* Copyright (C) 2020 Kenji Otsuka
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.improve_future.harmonica.core.table.column

// Exposed take a Long type as BigInteger in database, actually BigInteger can be bigger than Long
// but for now we limit it to the size of a Long
internal class BigIntegerColumn(name: String) : AbstractColumn(name) {
override var sqlDefault: String? = null

var default: Long?
get() = sqlDefault?.toLongOrNull()
set(value) {
sqlDefault = value?.toString()
}

var unsigned = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package com.improve_future.harmonica.core.table.column

import com.improve_future.harmonica.core.RawSql

internal class IntegerColumn(name: String) : AbstractColumn(name) {
override var sqlDefault: String? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ class MySqlAdapterTest {
)
}

@Test
fun testAddColumnForBigInteger() {
val connection = StubConnection()
val adapter = MySqlAdapter(connection)

val bigIntegerColumn = BigIntegerColumn("bigInteger")

adapter.addColumn(
"table_name", bigIntegerColumn,
AddingColumnOption()
)
assertEquals(
"ALTER TABLE table_name ADD COLUMN bigInteger BIGINT;",
connection.executedSqlList.first()
)
}

@Test
fun testAddColumnForBlob() {
val connection = StubConnection()
Expand Down
Loading