(ctx context.Context, have, want *schema.Table)
| 74 | } |
| 75 | |
| 76 | func (c *Client) autoMigrateTable(ctx context.Context, have, want *schema.Table) error { |
| 77 | changes := want.GetChanges(have) |
| 78 | if len(changes) == 0 { |
| 79 | c.logger.Info().Str("table", want.Name).Msg("Table schema is up-to-date, skip") |
| 80 | return nil |
| 81 | } |
| 82 | |
| 83 | if unsafe := unsafeChanges(changes); len(unsafe) > 0 { |
| 84 | // we can get here only with migrate_mode: forced |
| 85 | c.logger.Info().Str("table", want.Name).Msg("Table exists, force migration required") |
| 86 | return c.recreateTable(ctx, want) |
| 87 | } |
| 88 | |
| 89 | statements := make([]string, 0, len(changes)) |
| 90 | for _, change := range changes { |
| 91 | if change.Type == schema.TableColumnChangeTypeAdd { |
| 92 | statements = append(statements, queries.AddColumn(c.spec.Schema, want, &change.Current)) |
| 93 | } |
| 94 | if change.Type == schema.TableColumnChangeTypeUpdate { |
| 95 | statements = append(statements, queries.UpdateColumnType(c.spec.Schema, want, &change.Current)) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | err := c.execStatements(ctx, want.Name, statements) |
| 100 | if err != nil { |
| 101 | return err |
| 102 | } |
| 103 | |
| 104 | return c.ensureTVP(ctx, want) |
| 105 | } |
| 106 | |
| 107 | func (c *Client) execStatements(ctx context.Context, tableName string, statements []string) error { |
| 108 | if len(statements) == 0 { |
no test coverage detected