| 304 | } |
| 305 | |
| 306 | func getConstraints(db *sql.DB, schema, tableName string) ([]Constraint, error) { |
| 307 | query := "SELECT tc.CONSTRAINT_NAME, " + |
| 308 | "kcu.COLUMN_NAME, " + |
| 309 | "kcu.REFERENCED_TABLE_SCHEMA, " + |
| 310 | "kcu.REFERENCED_TABLE_NAME, " + |
| 311 | "kcu.REFERENCED_COLUMN_NAME " + |
| 312 | "FROM information_schema.TABLE_CONSTRAINTS tc " + |
| 313 | "LEFT JOIN information_schema.KEY_COLUMN_USAGE kcu " + |
| 314 | "ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME " + |
| 315 | "WHERE tc.CONSTRAINT_TYPE = 'FOREIGN KEY' " + |
| 316 | fmt.Sprintf("AND tc.TABLE_SCHEMA = '%s' ", schema) + |
| 317 | fmt.Sprintf("AND tc.TABLE_NAME = '%s'", tableName) |
| 318 | rows, err := db.Query(query) |
| 319 | if err != nil { |
| 320 | return nil, err |
| 321 | } |
| 322 | defer rows.Close() |
| 323 | |
| 324 | constraints := []Constraint{} |
| 325 | |
| 326 | for rows.Next() { |
| 327 | var c Constraint |
| 328 | err := rows.Scan(&c.ConstraintName, &c.ColumnName, &c.ReferencedTableSchema, |
| 329 | &c.ReferencedTableName, &c.ReferencedColumnName) |
| 330 | if err != nil { |
| 331 | return nil, fmt.Errorf("cannot read constraints: %s", err) |
| 332 | } |
| 333 | constraints = append(constraints, c) |
| 334 | } |
| 335 | |
| 336 | return constraints, nil |
| 337 | } |
| 338 | |
| 339 | func getTriggers(db *sql.DB, schema, tableName string) ([]Trigger, error) { |
| 340 | query := fmt.Sprintf("SHOW TRIGGERS FROM `%s` LIKE '%s'", schema, tableName) |