/
migrate.go
166 lines (136 loc) · 4.4 KB
/
migrate.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
package client
import (
"context"
"errors"
"fmt"
"slices"
"github.com/cloudquery/cloudquery/plugins/destination/clickhouse/queries"
"github.com/cloudquery/cloudquery/plugins/destination/clickhouse/typeconv"
"github.com/cloudquery/cloudquery/plugins/destination/clickhouse/util"
"github.com/cloudquery/plugin-sdk/v4/message"
"github.com/cloudquery/plugin-sdk/v4/schema"
"golang.org/x/sync/errgroup"
)
// MigrateTables relies on the CLI/client to lock before running migration.
func (c *Client) MigrateTables(ctx context.Context, messages message.WriteMigrateTables) error {
have, err := c.getTableDefinitions(ctx, messages)
if err != nil {
return err
}
want, err := typeconv.CanonizedTables(messages)
if err != nil {
return err
}
if err := c.checkForced(have, want, messages); err != nil {
return err
}
const maxConcurrentMigrate = 10
eg, ctx := errgroup.WithContext(ctx)
eg.SetLimit(maxConcurrentMigrate)
for _, want := range want {
want := want
eg.Go(func() (err error) {
c.logger.Info().Str("table", want.Name).Msg("Migrating table started")
defer func() {
c.logger.Err(err).Str("table", want.Name).Msg("Migrating table done")
}()
if len(want.Columns) == 0 {
c.logger.Warn().Str("table", want.Name).Msg("Table with no columns, skip")
return nil
}
have := have.Get(want.Name)
if have == nil {
return c.createTable(ctx, want)
}
return c.autoMigrate(ctx, have, want)
})
}
return eg.Wait()
}
func (c *Client) checkForced(have, want schema.Tables, messages message.WriteMigrateTables) error {
forcedErr := false
for _, m := range messages {
if m.MigrateForce {
continue
}
// check that this migration can go through
have := have.Get(m.Table.Name)
if have == nil {
continue // create new is always OK
}
want := want.Get(m.Table.Name) // and it should never be nil
if unsafe := unsafeChanges(want.GetChanges(have)); len(unsafe) > 0 {
c.logger.Error().
Str("table", m.Table.Name).
Str("changes", util.ChangesPrettified(m.Table.Name, unsafe)).
Msg("migrate manually or consider using 'migrate_mode: forced'")
forcedErr = true
}
}
if forcedErr {
return errors.New("migrate manually or consider using 'migrate_mode: forced'")
}
return nil
}
func unsafeChanges(changes []schema.TableColumnChange) []schema.TableColumnChange {
unsafe := make([]schema.TableColumnChange, 0, len(changes))
for _, c := range changes {
if needsTableDrop(c) {
unsafe = append(unsafe, c)
}
}
return slices.Clip(unsafe)
}
func (c *Client) createTable(ctx context.Context, table *schema.Table) (err error) {
c.logger.Debug().Str("table", table.Name).Msg("Table doesn't exist, creating")
query, err := queries.CreateTable(table, c.spec.Cluster, c.spec.Engine)
if err != nil {
return err
}
if err := c.conn.Exec(ctx, query); err != nil {
return fmt.Errorf("failed to create table, query:\n%s\nerror: %w", query, err)
}
return nil
}
func (c *Client) dropTable(ctx context.Context, table *schema.Table) error {
c.logger.Debug().Str("table", table.Name).Msg("Dropping table")
return c.conn.Exec(ctx, queries.DropTable(table, c.spec.Cluster))
}
func needsTableDrop(change schema.TableColumnChange) bool {
// We can safely add a nullable column without dropping the table
if change.Type == schema.TableColumnChangeTypeAdd && !change.Current.NotNull {
return false
}
// We can safely ignore removal of nullable columns without dropping the table
if change.Type == schema.TableColumnChangeTypeRemove && !change.Previous.NotNull {
return false
}
// TODO: add check for update + new type is extending the current type (uint8 -> uint16, float32 -> float64, new struct field, etc).
return true
}
func (c *Client) autoMigrate(ctx context.Context, have, want *schema.Table) error {
changes := want.GetChanges(have)
if unsafe := unsafeChanges(changes); len(unsafe) > 0 {
// we can get here only with migrate_mode: forced
if err := c.dropTable(ctx, have); err != nil {
return err
}
return c.createTable(ctx, want)
}
for _, change := range changes {
// we only handle new columns
if change.Type != schema.TableColumnChangeTypeAdd {
continue
}
c.logger.Debug().Str("table", want.Name).Str("column", change.Current.Name).Msg("Adding new column")
query, err := queries.AddColumn(want.Name, c.spec.Cluster, change.Current)
if err != nil {
return err
}
err = c.conn.Exec(ctx, query)
if err != nil {
return err
}
}
return nil
}