MCPcopy Create free account
hub / github.com/bytebase/bytebase / generateAlterTable

Function generateAlterTable

backend/plugin/schema/mssql/generate_migration.go:590–684  ·  view source on GitHub ↗
(tableDiff *schema.TableDiff)

Source from the content-addressed store, hash-verified

588}
589
590func generateAlterTable(tableDiff *schema.TableDiff) string {
591 var buf strings.Builder
592
593 // Add columns first (other operations might depend on them)
594 for _, colDiff := range tableDiff.ColumnChanges {
595 if colDiff.Action == schema.MetadataDiffActionCreate {
596 columnDef := generateColumnDefinition(colDiff.NewColumn)
597 _, _ = buf.WriteString("ALTER TABLE [")
598 _, _ = buf.WriteString(tableDiff.SchemaName)
599 _, _ = buf.WriteString("].[")
600 _, _ = buf.WriteString(tableDiff.TableName)
601 _, _ = buf.WriteString("] ADD ")
602 _, _ = buf.WriteString(columnDef)
603 _, _ = buf.WriteString(";\n")
604 }
605 }
606
607 // Alter columns
608 for _, colDiff := range tableDiff.ColumnChanges {
609 if colDiff.Action == schema.MetadataDiffActionAlter {
610 alterColSQL := generateAlterColumn(tableDiff.SchemaName, tableDiff.TableName, colDiff)
611 _, _ = buf.WriteString(alterColSQL)
612 }
613 }
614
615 // Add indexes
616 for _, indexDiff := range tableDiff.IndexChanges {
617 if indexDiff.Action == schema.MetadataDiffActionCreate {
618 if indexDiff.NewIndex.IsConstraint {
619 // Add constraint (primary key or unique constraint)
620 _, _ = buf.WriteString("ALTER TABLE [")
621 _, _ = buf.WriteString(tableDiff.SchemaName)
622 _, _ = buf.WriteString("].[")
623 _, _ = buf.WriteString(tableDiff.TableName)
624 _, _ = buf.WriteString("] ADD CONSTRAINT [")
625 _, _ = buf.WriteString(indexDiff.NewIndex.Name)
626 if indexDiff.NewIndex.Primary {
627 _, _ = buf.WriteString("] PRIMARY KEY")
628 } else if indexDiff.NewIndex.Unique {
629 _, _ = buf.WriteString("] UNIQUE")
630 } else {
631 _, _ = buf.WriteString("]")
632 }
633 switch indexDiff.NewIndex.Type {
634 case "CLUSTERED":
635 _, _ = buf.WriteString(" CLUSTERED")
636 case "NONCLUSTERED":
637 _, _ = buf.WriteString(" NONCLUSTERED")
638 default:
639 // Other index types
640 }
641 _, _ = buf.WriteString(" (")
642 for j, expr := range indexDiff.NewIndex.Expressions {
643 if j > 0 {
644 _, _ = buf.WriteString(", ")
645 }
646 _, _ = buf.WriteString("[")
647 _, _ = buf.WriteString(expr)

Callers 1

generateMigrationFunction · 0.70

Calls 5

generateColumnDefinitionFunction · 0.85
generateCreateIndexFunction · 0.85
generateAddForeignKeyFunction · 0.85
generateAlterColumnFunction · 0.70
StringMethod · 0.65

Tested by

no test coverage detected