-
Notifications
You must be signed in to change notification settings - Fork 513
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
MySQL: Add support for dropping keys #4489
Open
hfhbd
wants to merge
4
commits into
master
Choose a base branch
from
hfhbd/mysqlDropKeys
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
67 changes: 67 additions & 0 deletions
67
.../src/main/kotlin/app/cash/sqldelight/dialects/mysql/grammar/mixins/DropForeignKeyMixin.kt
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,67 @@ | ||
package app.cash.sqldelight.dialects.mysql.grammar.mixins | ||
|
||
import app.cash.sqldelight.dialects.mysql.grammar.psi.MySqlAlterTableDropForeignKey | ||
import com.alecstrong.sql.psi.core.SqlAnnotationHolder | ||
import com.alecstrong.sql.psi.core.psi.SqlColumnDef | ||
import com.alecstrong.sql.psi.core.psi.SqlColumnName | ||
import com.alecstrong.sql.psi.core.psi.SqlCompositeElementImpl | ||
import com.alecstrong.sql.psi.core.psi.SqlCreateTableStmt | ||
import com.alecstrong.sql.psi.core.psi.SqlTableConstraint | ||
import com.intellij.lang.ASTNode | ||
import com.intellij.psi.util.childrenOfType | ||
import com.intellij.psi.util.parentOfType | ||
|
||
internal abstract class DropForeignKeyMixin( | ||
node: ASTNode, | ||
) : SqlCompositeElementImpl(node), | ||
MySqlAlterTableDropForeignKey { | ||
override fun annotate(annotationHolder: SqlAnnotationHolder) { | ||
super.annotate(annotationHolder) | ||
val foreignKey = childrenOfType<SqlColumnName>().single() | ||
val foundColumnDef = foreignKey.getColumnDefOrNull() | ||
if (foundColumnDef == null || !foundColumnDef.isForeignKey()) { | ||
annotationHolder.createErrorAnnotation( | ||
foreignKey, | ||
"${foreignKey.name} is not a foreign key.", | ||
) | ||
} | ||
} | ||
|
||
private fun SqlColumnName.getColumnDefOrNull(): SqlColumnDef? { | ||
val ref = reference?.resolve() ?: return null | ||
val tables = tablesAvailable(this) | ||
for (table in tables) { | ||
val tableDef = table.tableName.parentOfType<SqlCreateTableStmt>() ?: continue | ||
for (columnDef in tableDef.columnDefList) { | ||
val columnRef = columnDef.columnName.reference | ||
if (columnRef != null && columnRef.resolve() == ref) { | ||
return columnDef | ||
} | ||
} | ||
} | ||
return null | ||
} | ||
|
||
private fun SqlColumnDef.isForeignKey(): Boolean { | ||
for (columnConstraint in columnConstraintList) { | ||
if (columnConstraint.foreignKeyClause != null) { | ||
return true | ||
} | ||
} | ||
val createTableStmt: SqlCreateTableStmt? = parentOfType() | ||
if (createTableStmt != null) { | ||
for (tableConstraints in createTableStmt.tableConstraintList) { | ||
val foreignKeyClause = tableConstraints.foreignKeyClause | ||
if (foreignKeyClause != null) { | ||
val columns = (foreignKeyClause.parent as SqlTableConstraint).columnNameList | ||
for (column in columns) { | ||
if (column.reference?.resolve() == columnName) { | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
dialects/mysql/src/test/fixtures_mysql/alter-table-drop-key/1.s
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,20 @@ | ||
CREATE TABLE parent ( | ||
id INT NOT NULL, | ||
PRIMARY KEY (id) | ||
); | ||
|
||
CREATE TABLE child ( | ||
id INT, | ||
parent_id INT, | ||
FOREIGN KEY (parent_id) REFERENCES parent(id) | ||
); | ||
|
||
ALTER TABLE parent | ||
DROP PRIMARY KEY; | ||
|
||
ALTER TABLE child | ||
DROP FOREIGN KEY parent_id; | ||
|
||
-- Should fail, id isn't a foreign key | ||
ALTER TABLE child | ||
DROP FOREIGN KEY id; |
16 changes: 16 additions & 0 deletions
16
dialects/mysql/src/test/fixtures_mysql/alter-table-drop-key/2.s
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,16 @@ | ||
CREATE TABLE parent2 ( | ||
id INT NOT NULL, | ||
PRIMARY KEY (id) | ||
); | ||
|
||
CREATE TABLE child2 ( | ||
id INT, | ||
parent_id2 INT REFERENCES parent2(id) | ||
); | ||
|
||
ALTER TABLE child2 | ||
DROP FOREIGN KEY parent_id2; | ||
|
||
-- Should fail, id isn't a foreign key | ||
ALTER TABLE child2 | ||
DROP FOREIGN KEY id; |
2 changes: 2 additions & 0 deletions
2
dialects/mysql/src/test/fixtures_mysql/alter-table-drop-key/failure.txt
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,2 @@ | ||
1.s line 20:19 - id is not a foreign key. | ||
2.s line 16:19 - id is not a foreign key. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be moved to sql-psi to allow reusing getting the columnDef and its foreign status in other dialects too, will do it in another PR.