forked from yunionio/sqlchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
122 lines (111 loc) · 3.54 KB
/
sync.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
// Copyright 2019 Yunion
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sqlite
import (
"fmt"
"strings"
"github.com/nyl1001/sqlchemy"
"yunion.io/x/log"
)
func (sqlite *SSqliteBackend) CommitTableChangeSQL(ts sqlchemy.ITableSpec, changes sqlchemy.STableChanges) []string {
ret := make([]string, 0)
for _, idx := range changes.RemoveIndexes {
sql := fmt.Sprintf("DROP INDEX IF EXISTS `%s`.`%s`", ts.Name(), idx.Name())
ret = append(ret, sql)
log.Infof("%s;", sql)
}
needNewTable := false
// first check if primary key is modifed
changePrimary := false
oldHasPrimary := false
for _, col := range changes.RemoveColumns {
if col.IsPrimary() {
changePrimary = true
oldHasPrimary = true
}
}
for _, cols := range changes.UpdatedColumns {
if cols.OldCol.IsPrimary() != cols.NewCol.IsPrimary() {
changePrimary = true
}
if cols.OldCol.IsPrimary() {
oldHasPrimary = true
}
}
for _, col := range changes.AddColumns {
if col.IsPrimary() {
changePrimary = true
}
}
if changePrimary && oldHasPrimary {
needNewTable = true
}
/* IGNORE DROP STATEMENT */
for _, col := range changes.RemoveColumns {
sql := fmt.Sprintf("DROP COLUMN `%s`", col.Name())
log.Debugf("skip ALTER TABLE %s %s;", ts.Name(), sql)
// alters = append(alters, sql)
// ignore drop statement
// if the column is auto_increment integer column,
// then need to drop auto_increment attribute
if col.IsAutoIncrement() {
needNewTable = true
}
// if the column is not nullable but no default
// then need to drop the not-nullable attribute
if !col.IsNullable() && col.Default() == "" {
needNewTable = true
}
}
if len(changes.UpdatedColumns) > 0 {
needNewTable = true
}
for _, col := range changes.AddColumns {
sql := fmt.Sprintf("ALTER TABLE `%s` ADD COLUMN %s", ts.Name(), col.DefinitionString())
ret = append(ret, sql)
}
if changePrimary {
needNewTable = true
}
if needNewTable {
newTableName := fmt.Sprintf("%s_tmp", ts.Name())
oldTableName := fmt.Sprintf("%s_old", ts.Name())
// create a table with alter name
var newTable *sqlchemy.STableSpec
newTable = ts.(*sqlchemy.STableSpec).Clone(newTableName, 0)
createSqls := newTable.CreateSQLs()
ret = append(ret, createSqls...)
// insert
colNames := make([]string, 0)
for _, col := range ts.Columns() {
colNames = append(colNames, fmt.Sprintf("`%s`", col.Name()))
}
sql := fmt.Sprintf("INSERT INTO `%s` SELECT %s FROM `%s`", newTableName, strings.Join(colNames, ", "), ts.Name())
ret = append(ret, sql)
// change name
sql = fmt.Sprintf("ALTER TABLE `%s` RENAME TO `%s`", ts.Name(), oldTableName)
ret = append(ret, sql)
sql = fmt.Sprintf("ALTER TABLE `%s` RENAME TO `%s`", newTableName, ts.Name())
ret = append(ret, sql)
}
for _, idx := range changes.AddIndexes {
sql := createIndexSQL(ts, idx)
ret = append(ret, sql)
log.Infof("%s;", sql)
}
return ret
}
func createIndexSQL(ts sqlchemy.ITableSpec, idx sqlchemy.STableIndex) string {
return fmt.Sprintf("CREATE INDEX `%s` ON `%s` (%s)", idx.Name(), ts.Name(), strings.Join(idx.QuotedColumns(), ","))
}