-
-
Notifications
You must be signed in to change notification settings - Fork 47
fix: The HasConstraint is fixed by adding a Unique Check #173
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -396,31 +396,55 @@ WHERE TABLE_CATALOG = ? AND TABLE_NAME = ?`) | |
|
|
||
| { | ||
| _, schemaName, tableName := splitFullQualifiedName(stmt.Table) | ||
| query := "SELECT c.COLUMN_NAME, t.CONSTRAINT_TYPE FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS t JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE c ON c.CONSTRAINT_NAME=t.CONSTRAINT_NAME WHERE t.CONSTRAINT_TYPE IN ('PRIMARY KEY', 'UNIQUE') AND c.TABLE_CATALOG = ? AND c.TABLE_NAME = ?" | ||
|
|
||
| query := "SELECT t.CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS t JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE c ON c.CONSTRAINT_NAME=t.CONSTRAINT_NAME WHERE t.CONSTRAINT_TYPE IN ('PRIMARY KEY', 'UNIQUE') AND c.TABLE_CATALOG = ? AND c.TABLE_NAME = ? AND CONSTRAINT_TYPE = ?" | ||
|
|
||
| queryParameters := []interface{}{m.CurrentDatabase(), tableName} | ||
|
|
||
| if schemaName != "" { | ||
| query += " AND c.TABLE_SCHEMA = ?" | ||
| queryParameters = append(queryParameters, schemaName) | ||
| } | ||
|
|
||
| queryParameters = append(queryParameters, "UNIQUE") | ||
| columnTypeRows, err := m.DB.Raw(query, queryParameters...).Rows() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| uniqueContraints := map[string]int{} | ||
| for columnTypeRows.Next() { | ||
| var constraintName string | ||
| columnTypeRows.Scan(&constraintName) | ||
| uniqueContraints[constraintName]++ | ||
|
Comment on lines
+413
to
+417
|
||
| } | ||
|
Comment on lines
+414
to
+418
|
||
| _ = columnTypeRows.Close() | ||
|
|
||
| query = "SELECT c.COLUMN_NAME, t.CONSTRAINT_NAME, t.CONSTRAINT_TYPE FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS t JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE c ON c.CONSTRAINT_NAME=t.CONSTRAINT_NAME WHERE t.CONSTRAINT_TYPE IN ('PRIMARY KEY', 'UNIQUE') AND c.TABLE_CATALOG = ? AND c.TABLE_NAME = ?" | ||
|
|
||
| queryParameters = []interface{}{m.CurrentDatabase(), tableName} | ||
|
|
||
| if schemaName != "" { | ||
| query += " AND c.TABLE_SCHEMA = ?" | ||
| queryParameters = append(queryParameters, schemaName) | ||
| } | ||
|
|
||
| columnTypeRows, err = m.DB.Raw(query, queryParameters...).Rows() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| for columnTypeRows.Next() { | ||
| var name, columnType string | ||
| _ = columnTypeRows.Scan(&name, &columnType) | ||
| var name, constraintName, columnType string | ||
| _ = columnTypeRows.Scan(&name, &constraintName, &columnType) | ||
| for idx, c := range columnTypes { | ||
| mc := c.(migrator.ColumnType) | ||
| if mc.NameValue.String == name { | ||
| switch columnType { | ||
| case "PRIMARY KEY": | ||
| mc.PrimaryKeyValue = sql.NullBool{Bool: true, Valid: true} | ||
| case "UNIQUE": | ||
| mc.UniqueValue = sql.NullBool{Bool: true, Valid: true} | ||
| if uniqueContraints[constraintName] == 1 { | ||
| mc.UniqueValue = sql.NullBool{Bool: true, Valid: true} | ||
| } | ||
|
Comment on lines
435
to
+447
|
||
| } | ||
| columnTypes[idx] = mc | ||
| break | ||
|
|
@@ -573,17 +597,29 @@ func (m Migrator) HasConstraint(value interface{}, name string) bool { | |
|
|
||
| return m.DB.Raw( | ||
| `SELECT count(*) FROM ( | ||
| -- Check CHECK constraint | ||
| SELECT C.name, T.name as table_name FROM sys.check_constraints as C | ||
| INNER JOIN sys.tables as T on C.parent_object_id=T.object_id | ||
| INNER JOIN INFORMATION_SCHEMA.TABLES as I on I.TABLE_NAME = T.name | ||
| WHERE C.name = ? AND I.TABLE_NAME = ? AND I.TABLE_SCHEMA like ? AND I.TABLE_CATALOG = ? | ||
| UNION | ||
| -- Check foreign key constraints | ||
| SELECT FK.name, T.name as table_name FROM sys.foreign_keys as FK | ||
| INNER JOIN sys.tables as T on FK.parent_object_id=T.object_id | ||
| INNER JOIN INFORMATION_SCHEMA.TABLES as I on I.TABLE_NAME = T.name | ||
| WHERE FK.name = ? AND I.TABLE_NAME = ? AND I.TABLE_SCHEMA like ? AND I.TABLE_CATALOG = ? | ||
| UNION | ||
| -- Check Unique Constraint | ||
| SELECT UK.name, T.name as table_name FROM sys.key_constraints as UK | ||
| INNER JOIN sys.tables as T on UK.parent_object_id = T.object_id | ||
| INNER JOIN INFORMATION_SCHEMA.TABLES as I on I.TABLE_NAME = T.name | ||
| WHERE UK.type = 'UQ' AND UK.name = ? AND I.TABLE_NAME = ? AND I.TABLE_SCHEMA like ? AND I.TABLE_CATALOG = ? | ||
| ) as constraints;`, | ||
| // CHECK constraint parameter | ||
| name, tableName, tableSchema, tableCatalog, | ||
| // Foreign key constraint parameters | ||
| name, tableName, tableSchema, tableCatalog, | ||
| // Unique Constraint Parameter | ||
| name, tableName, tableSchema, tableCatalog, | ||
| ).Row().Scan(&count) | ||
| }) | ||
|
|
||
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.
The UNIQUE-constraint prequery is redundant and slightly unclear: it filters
t.CONSTRAINT_TYPE IN ('PRIMARY KEY','UNIQUE')and also addsAND CONSTRAINT_TYPE = ?(unqualified). Consider simplifying to a single, qualified predicate (e.g.,t.CONSTRAINT_TYPE = 'UNIQUE') to avoid confusion and keep the query planner work minimal.