diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnDiff.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnDiff.kt index 8b360f1d7a..5b7f01c7f2 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnDiff.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnDiff.kt @@ -4,15 +4,15 @@ package org.jetbrains.exposed.sql * Represents differences between a column definition and database metadata for the existing column. */ data class ColumnDiff( - /** Whether the nullability of the existing column is correct. */ + /** Whether there is a mismatch between nullability of the existing column and the defined column. */ val nullability: Boolean, - /** Whether the existing column has a matching auto-increment sequence. */ + /** Whether there is a mismatch between auto-increment status of the existing column and the defined column. */ val autoInc: Boolean, - /** Whether the default value of the existing column is correct. */ + /** Whether the default value of the existing column matches that of the defined column. */ val defaults: Boolean, - /** Whether the existing column identifier matches and has the correct casing. */ + /** Whether the existing column identifier matches that of the defined column and has the correct casing. */ val caseSensitiveName: Boolean, - /** Whether the size and scale of the existing column, if applicable, is correct. */ + /** Whether the size and scale of the existing column, if applicable, match those of the defined column. */ val sizeAndScale: Boolean, ) { /** Returns `true` if there is a difference between the column definition and the existing column in the database. */ diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/PostgreSQL.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/PostgreSQL.kt index 22b3ad0876..efb6adacf4 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/PostgreSQL.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/PostgreSQL.kt @@ -408,6 +408,11 @@ open class PostgreSQLDialect(override val name: String = dialectName) : VendorDi val fallbackSequenceName = fallbackSequenceName(tableName = column.table.tableName, columnName = column.name) append("ALTER COLUMN $colName SET DEFAULT nextval('$fallbackSequenceName')") } + } else if (columnDiff.autoInc && column.autoIncColumnType == null) { + // based on logic in SchemaUtils.isIncorrectAutoInc this should only be possible if the existing + // column in database is auto-incrementing while defined table is not + append("ALTER COLUMN $colName TYPE ${column.columnType.sqlType()}") + append(", ALTER COLUMN $colName DROP DEFAULT") } else { append("ALTER COLUMN $colName TYPE ${column.columnType.sqlType()}") } diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/DatabaseMigrationTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/DatabaseMigrationTests.kt index 75304b67b4..460b336f1e 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/DatabaseMigrationTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/DatabaseMigrationTests.kt @@ -12,6 +12,7 @@ import org.jetbrains.exposed.sql.tests.inProperCase import org.jetbrains.exposed.sql.tests.shared.assertEqualCollections import org.jetbrains.exposed.sql.tests.shared.assertEqualLists import org.jetbrains.exposed.sql.tests.shared.assertEquals +import org.jetbrains.exposed.sql.tests.shared.assertFalse import org.jetbrains.exposed.sql.tests.shared.assertTrue import org.jetbrains.exposed.sql.tests.shared.expectException import org.jetbrains.exposed.sql.vendors.MysqlDialect @@ -374,7 +375,7 @@ class DatabaseMigrationTests : DatabaseTestsBase() { when (testDb) { TestDB.POSTGRESQL, TestDB.POSTGRESQLNG -> { assertEquals(2, statements.size) - assertEquals("ALTER TABLE test_table ALTER COLUMN id TYPE BIGINT", statements[0]) + assertEquals("ALTER TABLE test_table ALTER COLUMN id TYPE BIGINT, ALTER COLUMN id DROP DEFAULT", statements[0]) assertEquals(expectedDropSequenceStatement("test_table_id_seq"), statements[1]) } TestDB.ORACLE, TestDB.H2_V2_ORACLE -> { @@ -731,15 +732,13 @@ class DatabaseMigrationTests : DatabaseTestsBase() { val statements = MigrationUtils.statementsRequiredForDatabaseMigration(tableWithoutAutoIncrement) assertEquals(2, statements.size) - assertEquals("ALTER TABLE test_table_auto ALTER COLUMN id TYPE INT", statements[0]) + assertEquals("ALTER TABLE test_table_auto ALTER COLUMN id TYPE INT, ALTER COLUMN id DROP DEFAULT", statements[0]) assertEquals(expectedDropSequenceStatement("test_table_auto_id_seq"), statements[1]) - // fails due to EXPOSED-696 - // https://youtrack.jetbrains.com/issue/EXPOSED-696/PostgreSQL-Drop-of-auto-increment-sequence-fails-after-column-modified-without-dropping-default) -// statements.forEach { exec(it) } -// assertFalse(autoSeq.exists()) -// assertTrue(sequence.exists()) -// assertTrue(implicitSeq.exists()) + statements.forEach { exec(it) } + assertFalse(autoSeq.exists()) + assertTrue(sequence.exists()) + assertTrue(implicitSeq.exists()) } finally { SchemaUtils.drop(tableWithAutoIncrement, tableWithExplSequence, tableWithImplSequence) }