Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fixes 5018 PostgreSql add Primary Key not nullable types #5020

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

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ alter_table_drop_column ::= DROP [ COLUMN ] {column_name} {
pin = 1
}

alter_table_add_constraint ::= ADD table_constraint
alter_table_add_constraint ::= ADD table_constraint {
mixin = "app.cash.sqldelight.dialects.postgresql.grammar.mixins.AlterTableAddConstraintMixin"
}

type_clause ::= 'TYPE'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package app.cash.sqldelight.dialects.postgresql.grammar.mixins

import app.cash.sqldelight.dialects.postgresql.grammar.psi.PostgreSqlAlterTableAddConstraint
import com.alecstrong.sql.psi.core.psi.AlterTableApplier
import com.alecstrong.sql.psi.core.psi.LazyQuery
import com.alecstrong.sql.psi.core.psi.SqlCompositeElementImpl
import com.alecstrong.sql.psi.core.psi.SqlTypes
import com.intellij.lang.ASTNode

abstract class AlterTableAddConstraintMixin(node: ASTNode) :
SqlCompositeElementImpl(node),
PostgreSqlAlterTableAddConstraint,
AlterTableApplier {
override fun applyTo(lazyQuery: LazyQuery): LazyQuery =
if (tableConstraint.node.findChildByType(SqlTypes.PRIMARY) != null &&
tableConstraint.node.findChildByType(SqlTypes.KEY) != null
) {
val columns = lazyQuery.query.columns.map { queryCol ->
tableConstraint.indexedColumnList.find { indexedCol -> queryCol.element.textMatches(indexedCol) }.let {
queryCol.copy(nullable = if (it != null) false else queryCol.nullable)
}
}
LazyQuery(lazyQuery.tableName, query = { lazyQuery.query.copy(columns = columns) })
} else {
lazyQuery
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class MigrationQueryTest {
checkFixtureCompiles("alter-table-alter-column", PostgreSqlDialect())
}

@Test fun `alter table add constraint`() {
checkFixtureCompiles("alter-table-add-constraint", PostgreSqlDialect())
}

@Test fun `varying query migration packages`() {
checkFixtureCompiles("varying-query-migration-packages", PostgreSqlDialect())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE TestSingle(
first INTEGER,
second TEXT
);

CREATE TABLE TestCompound (
first INTEGER,
second TEXT
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE TestSingle ADD PRIMARY KEY (first);
ALTER TABLE TestCompound ADD CONSTRAINT pk_first_second PRIMARY KEY (first, second);

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
selectSingle:
SELECT *
FROM TestSingle;

selectCompound:
SELECT *
FROM TestCompound;

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.example

import app.cash.sqldelight.Query
import app.cash.sqldelight.TransacterImpl
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.JdbcCursor
import kotlin.Any
import kotlin.Int
import kotlin.String

public class DataQueries(
driver: SqlDriver,
) : TransacterImpl(driver) {
public fun <T : Any> selectSingle(mapper: (first: Int, second: String?) -> T): Query<T> =
Query(-79_317_191, arrayOf("TestSingle"), driver, "Data.sq", "selectSingle", """
|SELECT *
|FROM TestSingle
""".trimMargin()) { cursor ->
check(cursor is JdbcCursor)
mapper(
cursor.getInt(0)!!,
cursor.getString(1)
)
}

public fun selectSingle(): Query<TestSingle> = selectSingle { first, second ->
TestSingle(
first,
second
)
}

public fun <T : Any> selectCompound(mapper: (first: Int, second: String) -> T): Query<T> =
Query(-19_725_220, arrayOf("TestCompound"), driver, "Data.sq", "selectCompound", """
|SELECT *
|FROM TestCompound
""".trimMargin()) { cursor ->
check(cursor is JdbcCursor)
mapper(
cursor.getInt(0)!!,
cursor.getString(1)!!
)
}

public fun selectCompound(): Query<TestCompound> = selectCompound { first, second ->
TestCompound(
first,
second
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example

import kotlin.Int
import kotlin.String

public data class TestCompound(
public val first: Int,
public val second: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example

import kotlin.Int
import kotlin.String

public data class TestSingle(
public val first: Int,
public val second: String?,
)