AlterTableDef generates alter table sql
(s *schema.Schema, cascade bool)
| 511 | |
| 512 | //AlterTableDef generates alter table sql |
| 513 | func (db *DB) AlterTableDef(s *schema.Schema, cascade bool) (string, []string, error) { |
| 514 | var existing []string |
| 515 | rows, err := db.DB.Query(fmt.Sprintf("select * from `%s` limit 1;", s.GetDbTableName())) |
| 516 | if err == nil { |
| 517 | defer rows.Close() |
| 518 | existing, err = rows.Columns() |
| 519 | } |
| 520 | |
| 521 | if err != nil { |
| 522 | return "", nil, err |
| 523 | } |
| 524 | |
| 525 | cols, relations, indices := db.genTableCols(s, cascade, existing) |
| 526 | cols = append(cols, relations...) |
| 527 | |
| 528 | if len(cols) == 0 { |
| 529 | return "", nil, nil |
| 530 | } |
| 531 | alterTable := fmt.Sprintf("alter table`%s` add (%s);\n", s.GetDbTableName(), strings.Join(cols, ",")) |
| 532 | log.Debug("Altering table: " + alterTable) |
| 533 | log.Debug("Altering indices: " + strings.Join(indices, "")) |
| 534 | return alterTable, indices, nil |
| 535 | } |
| 536 | |
| 537 | //GenTableDef generates create table sql |
| 538 | func (db *DB) GenTableDef(s *schema.Schema, cascade bool) (string, []string) { |
no test coverage detected