github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdcv2/metadata/sql/tables.go (about) 1 // Copyright 2023 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package sql 15 16 import ( 17 "time" 18 19 "github.com/pingcap/tiflow/cdc/model" 20 "github.com/pingcap/tiflow/cdcv2/metadata" 21 "github.com/pingcap/tiflow/pkg/security" 22 "gorm.io/gorm" 23 ) 24 25 const ( 26 tableNameUpstream = "upstream" 27 tableNameChangefeedInfo = "changefeed_info" 28 tableNameChangefeedState = "changefeed_state" 29 tableNameSchedule = "schedule" 30 tableNameProgress = "progress" 31 ) 32 33 // UpstreamDO mapped from table <upstream> 34 type UpstreamDO struct { 35 ID uint64 `gorm:"column:id;type:bigint(20) unsigned;primaryKey" json:"id"` 36 Endpoints string `gorm:"column:endpoints;type:text;not null" json:"endpoints"` 37 Config *security.Credential `gorm:"column:config;type:text" json:"config"` 38 Version uint64 `gorm:"column:version;type:bigint(20) unsigned;not null" json:"version"` 39 UpdateAt time.Time `gorm:"column:update_at;type:datetime(6);not null;autoUpdateTime" json:"update_at"` 40 } 41 42 // equal checks whether two UpstreamDO are equal. 43 func (u *UpstreamDO) equal(other *UpstreamDO) bool { 44 if u != other && (u == nil || other == nil) { 45 return false 46 } 47 48 if u.ID != other.ID { 49 return false 50 } 51 if u.Endpoints != other.Endpoints { 52 return false 53 } 54 55 if u.Config == other.Config { 56 return true 57 } 58 59 if u.Config == nil && other.Config != nil { 60 return false 61 } 62 if u.Config != nil && other.Config == nil { 63 return false 64 } 65 66 if u.Config.CAPath != other.Config.CAPath || 67 u.Config.CertPath != other.Config.CertPath || 68 u.Config.KeyPath != other.Config.KeyPath || 69 len(u.Config.CertAllowedCN) != len(other.Config.CertAllowedCN) { 70 return false 71 } 72 for i := range u.Config.CertAllowedCN { 73 if u.Config.CertAllowedCN[i] != other.Config.CertAllowedCN[i] { 74 return false 75 } 76 } 77 78 return true 79 } 80 81 // GetKey returns the key of the upstream. 82 func (u *UpstreamDO) GetKey() uint64 { 83 return u.ID 84 } 85 86 // GetVersion returns the version of the upstream. 87 func (u *UpstreamDO) GetVersion() uint64 { 88 return u.Version 89 } 90 91 // GetUpdateAt returns the update time of the upstream. 92 func (u *UpstreamDO) GetUpdateAt() time.Time { 93 return u.UpdateAt 94 } 95 96 // TableName Upstream's table name 97 func (*UpstreamDO) TableName() string { 98 return tableNameUpstream 99 } 100 101 // ChangefeedInfoDO mapped from table <changefeed_info> 102 type ChangefeedInfoDO struct { 103 metadata.ChangefeedInfo 104 RemovedAt *time.Time `gorm:"column:removed_at;type:datetime(6)" json:"removed_at"` 105 106 Version uint64 `gorm:"column:version;type:bigint(20) unsigned;not null" json:"version"` 107 UpdateAt time.Time `gorm:"column:update_at;type:datetime(6);not null;autoUpdateTime" json:"update_at"` 108 } 109 110 // GetKey returns the key of the changefeed info. 111 func (c *ChangefeedInfoDO) GetKey() metadata.ChangefeedUUID { 112 return c.UUID 113 } 114 115 // GetVersion returns the version of the changefeed info. 116 func (c *ChangefeedInfoDO) GetVersion() uint64 { 117 return c.Version 118 } 119 120 // GetUpdateAt returns the update time of the changefeed info. 121 func (c *ChangefeedInfoDO) GetUpdateAt() time.Time { 122 return c.UpdateAt 123 } 124 125 // TableName ChangefeedInfo's table name 126 func (*ChangefeedInfoDO) TableName() string { 127 return tableNameChangefeedInfo 128 } 129 130 // ChangefeedStateDO mapped from table <changefeed_state> 131 type ChangefeedStateDO struct { 132 metadata.ChangefeedState 133 134 Version uint64 `gorm:"column:version;type:bigint(20) unsigned;not null" json:"version"` 135 UpdateAt time.Time `gorm:"column:update_at;type:datetime(6);not null;autoUpdateTime" json:"update_at"` 136 } 137 138 // GetKey returns the key of the changefeed state. 139 func (c *ChangefeedStateDO) GetKey() metadata.ChangefeedUUID { 140 return c.ChangefeedUUID 141 } 142 143 // GetVersion returns the version of the changefeed state. 144 func (c *ChangefeedStateDO) GetVersion() uint64 { 145 return c.Version 146 } 147 148 // GetUpdateAt returns the update time of the changefeed state. 149 func (c *ChangefeedStateDO) GetUpdateAt() time.Time { 150 return c.UpdateAt 151 } 152 153 // TableName ChangefeedState's table name 154 func (*ChangefeedStateDO) TableName() string { 155 return tableNameChangefeedState 156 } 157 158 // ScheduleDO mapped from table <schedule> 159 type ScheduleDO struct { 160 metadata.ScheduledChangefeed 161 162 Version uint64 `gorm:"column:version;type:bigint(20) unsigned;not null" json:"version"` 163 UpdateAt time.Time `gorm:"column:update_at;type:datetime(6);not null;autoUpdateTime" json:"update_at"` 164 } 165 166 // GetKey returns the key of the schedule. 167 func (s *ScheduleDO) GetKey() metadata.ChangefeedUUID { 168 return s.ChangefeedUUID 169 } 170 171 // GetVersion returns the version of the schedule. 172 func (s *ScheduleDO) GetVersion() uint64 { 173 return s.Version 174 } 175 176 // GetUpdateAt returns the update time of the schedule. 177 func (s *ScheduleDO) GetUpdateAt() time.Time { 178 return s.UpdateAt 179 } 180 181 // TableName Schedule's table name 182 func (*ScheduleDO) TableName() string { 183 return tableNameSchedule 184 } 185 186 // ProgressDO mapped from table <progress> 187 type ProgressDO struct { 188 CaptureID model.CaptureID `gorm:"column:capture_id;type:varchar(128);primaryKey" json:"capture_id"` 189 Progress *metadata.CaptureProgress `gorm:"column:progress;type:longtext" json:"progress"` 190 191 Version uint64 `gorm:"column:version;type:bigint(20) unsigned;not null" json:"version"` 192 UpdateAt time.Time `gorm:"column:update_at;type:datetime(6);not null;autoUpdateTime" json:"update_at"` 193 } 194 195 // GetKey returns the key of the progress. 196 func (p *ProgressDO) GetKey() model.CaptureID { 197 return p.CaptureID 198 } 199 200 // GetVersion returns the version of the progress. 201 func (p *ProgressDO) GetVersion() uint64 { 202 return p.Version 203 } 204 205 // GetUpdateAt returns the update time of the progress. 206 func (p *ProgressDO) GetUpdateAt() time.Time { 207 return p.UpdateAt 208 } 209 210 // TableName Progress's table name 211 func (*ProgressDO) TableName() string { 212 return tableNameProgress 213 } 214 215 // AutoMigrate checks the metadata-related tables and creates or changes the table structure 216 // as needed based on in-memory struct definition. 217 func AutoMigrate(db *gorm.DB) error { 218 return db.AutoMigrate( 219 &UpstreamDO{}, 220 &ChangefeedInfoDO{}, 221 &ChangefeedStateDO{}, 222 &ScheduleDO{}, 223 &ProgressDO{}, 224 ) 225 }