This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
/
pt_osc.go
173 lines (148 loc) · 4.52 KB
/
pt_osc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package syncer
import (
"strings"
"github.com/pingcap/errors"
"github.com/pingcap/parser/ast"
"github.com/pingcap/tidb-tools/pkg/filter"
"github.com/pingcap/dm/dm/config"
)
// PT handles pt online schema changes
// (_*).*_new ghost table
// (_*).*_old ghost transh table
// we don't support `--new-table-name` flag
type PT struct {
storge *OnlineDDLStorage
}
// NewPT returns pt online schema changes plugin
func NewPT(cfg *config.SubTaskConfig) (OnlinePlugin, error) {
g := &PT{
storge: NewOnlineDDLStorage(cfg),
}
return g, errors.Trace(g.storge.Init())
}
// Apply implements interface.
// returns ddls, real schema, real table, error
func (p *PT) Apply(tables []*filter.Table, statement string, stmt ast.StmtNode) ([]string, string, string, error) {
if len(tables) < 1 {
return nil, "", "", errors.NotValidf("tables should not be empty!")
}
schema, table := tables[0].Schema, tables[0].Name
targetSchema, targetTable := p.RealName(schema, table)
tp := p.TableType(table)
switch tp {
case realTable:
switch stmt.(type) {
case *ast.RenameTableStmt:
if len(tables) != 2 {
return nil, "", "", errors.NotValidf("tables should contain old and new table name")
}
tp1 := p.TableType(tables[1].Name)
if tp1 == trashTable {
return nil, "", "", nil
} else if tp1 == ghostTable {
return nil, "", "", errors.NotSupportedf("rename table to ghost table %s", statement)
}
}
return []string{statement}, schema, table, nil
case trashTable:
// ignore trashTable
switch stmt.(type) {
case *ast.RenameTableStmt:
if len(tables) != 2 {
return nil, "", "", errors.NotValidf("tables should contain old and new table name")
}
tp1 := p.TableType(tables[1].Name)
if tp1 == ghostTable {
return nil, "", "", errors.NotSupportedf("rename ghost table to other ghost table %s", statement)
}
}
case ghostTable:
// record ghost table ddl changes
switch stmt.(type) {
case *ast.CreateTableStmt:
err := p.storge.Delete(schema, table)
if err != nil {
return nil, "", "", errors.Trace(err)
}
case *ast.DropTableStmt:
err := p.storge.Delete(schema, table)
if err != nil {
return nil, "", "", errors.Trace(err)
}
case *ast.RenameTableStmt:
if len(tables) != 2 {
return nil, "", "", errors.NotValidf("tables should contain old and new table name")
}
tp1 := p.TableType(tables[1].Name)
if tp1 == realTable {
ghostInfo := p.storge.Get(schema, table)
if ghostInfo != nil {
return ghostInfo.DDLs, tables[1].Schema, tables[1].Name, nil
}
return nil, "", "", errors.NotFoundf("online ddls on ghost table `%s`.`%s`", schema, table)
} else if tp1 == ghostTable {
return nil, "", "", errors.NotSupportedf("rename ghost table to other ghost table %s", statement)
}
// rename ghost table to trash table
err := p.storge.Delete(schema, table)
if err != nil {
return nil, "", "", errors.Trace(err)
}
default:
err := p.storge.Save(schema, table, targetSchema, targetTable, statement)
if err != nil {
return nil, "", "", errors.Trace(err)
}
}
}
return nil, schema, table, nil
}
// Finish implements interface
func (p *PT) Finish(schema, table string) error {
if p == nil {
return nil
}
return errors.Trace(p.storge.Delete(schema, table))
}
// TableType implements interface
func (p *PT) TableType(table string) TableType {
// 5 is _ _gho/ghc/del
if len(table) > 5 {
if strings.HasPrefix(table, "_") && strings.HasSuffix(table, "_new") {
return ghostTable
}
if strings.HasPrefix(table, "_") && strings.HasSuffix(table, "_old") {
return trashTable
}
}
return realTable
}
// RealName implements interface
func (p *PT) RealName(schema, table string) (string, string) {
tp := p.TableType(table)
if tp == trashTable || tp == ghostTable {
table = strings.TrimLeft(table, "_")
table = table[:len(table)-4]
}
return schema, table
}
// Clear clears online ddl information
func (p *PT) Clear() error {
return errors.Trace(p.storge.Clear())
}
// Close implements interface
func (p *PT) Close() {
p.storge.Close()
}