vitess.io/vitess@v0.16.2/go/vt/schemadiff/column.go (about) 1 /* 2 Copyright 2022 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package schemadiff 18 19 import ( 20 "strings" 21 22 "vitess.io/vitess/go/vt/sqlparser" 23 ) 24 25 // columnDetails decorates a column with more details, used by diffing logic 26 type columnDetails struct { 27 col *sqlparser.ColumnDefinition 28 prevCol *columnDetails // previous in sequence in table definition 29 nextCol *columnDetails // next in sequence in table definition 30 } 31 32 func (c *columnDetails) identicalOtherThanName(other *sqlparser.ColumnDefinition) bool { 33 if other == nil { 34 return false 35 } 36 return sqlparser.Equals.SQLNode(c.col.Type, other.Type) 37 } 38 39 func (c *columnDetails) prevColName() string { 40 if c.prevCol == nil { 41 return "" 42 } 43 return c.prevCol.col.Name.String() 44 } 45 46 func (c *columnDetails) nextColName() string { 47 if c.nextCol == nil { 48 return "" 49 } 50 return c.nextCol.col.Name.String() 51 } 52 53 func getColName(id *sqlparser.IdentifierCI) *sqlparser.ColName { 54 return &sqlparser.ColName{Name: *id} 55 } 56 57 type ModifyColumnDiff struct { 58 modifyColumn *sqlparser.ModifyColumn 59 } 60 61 func NewModifyColumnDiff(modifyColumn *sqlparser.ModifyColumn) *ModifyColumnDiff { 62 return &ModifyColumnDiff{modifyColumn: modifyColumn} 63 } 64 65 func NewModifyColumnDiffByDefinition(definition *sqlparser.ColumnDefinition) *ModifyColumnDiff { 66 modifyColumn := &sqlparser.ModifyColumn{ 67 NewColDefinition: definition, 68 } 69 return NewModifyColumnDiff(modifyColumn) 70 } 71 72 type ColumnDefinitionEntity struct { 73 columnDefinition *sqlparser.ColumnDefinition 74 } 75 76 func NewColumnDefinitionEntity(c *sqlparser.ColumnDefinition) *ColumnDefinitionEntity { 77 return &ColumnDefinitionEntity{columnDefinition: c} 78 } 79 80 // ColumnDiff compares this table statement with another table statement, and sees what it takes to 81 // change this table to look like the other table. 82 // It returns an AlterTable statement if changes are found, or nil if not. 83 // the other table may be of different name; its name is ignored. 84 func (c *ColumnDefinitionEntity) ColumnDiff(other *ColumnDefinitionEntity, _ *DiffHints) *ModifyColumnDiff { 85 if sqlparser.Equals.RefOfColumnDefinition(c.columnDefinition, other.columnDefinition) { 86 return nil 87 } 88 89 return NewModifyColumnDiffByDefinition(other.columnDefinition) 90 } 91 92 // IsTextual returns true when this column is of textual type, and is capable of having a character set property 93 func (c *ColumnDefinitionEntity) IsTextual() bool { 94 return charsetTypes[strings.ToLower(c.columnDefinition.Type.Type)] 95 }