This is the responsibility of the CLI of the client to lock before running migration
(ctx context.Context, msgs message.WriteMigrateTables)
| 130 | |
| 131 | // This is the responsibility of the CLI of the client to lock before running migration |
| 132 | func (c *Client) MigrateTables(ctx context.Context, msgs message.WriteMigrateTables) error { |
| 133 | allTables := make(schema.Tables, 0, len(msgs)) |
| 134 | safeTables := make(map[string]bool) |
| 135 | for _, msg := range msgs { |
| 136 | allTables = append(allTables, msg.Table) |
| 137 | safeTables[msg.Table.Name] = !msg.MigrateForce |
| 138 | } |
| 139 | |
| 140 | normalizedTables := c.normalizeTables(allTables) |
| 141 | sqliteTables, err := c.sqliteTables(normalizedTables) |
| 142 | if err != nil { |
| 143 | return err |
| 144 | } |
| 145 | |
| 146 | nonAutoMigratableTables := c.nonAutoMigratableTables(normalizedTables, sqliteTables, safeTables) |
| 147 | if len(nonAutoMigratableTables) > 0 { |
| 148 | return fmt.Errorf("\nCan't migrate tables automatically, migrate manually or consider using 'migrate_mode: forced'. Non auto migratable tables changes:\n\n%s", schema.GetChangesSummary(nonAutoMigratableTables)) |
| 149 | } |
| 150 | |
| 151 | for _, table := range normalizedTables { |
| 152 | c.logger.Info().Str("table", table.Name).Msg("Migrating table") |
| 153 | if len(table.Columns) == 0 { |
| 154 | c.logger.Info().Str("table", table.Name).Msg("Table with no columns, skipping") |
| 155 | continue |
| 156 | } |
| 157 | |
| 158 | sqlite := sqliteTables.Get(table.Name) |
| 159 | if sqlite == nil { |
| 160 | c.logger.Debug().Str("table", table.Name).Msg("Table doesn't exist, creating") |
| 161 | if err := c.createTableIfNotExist(table); err != nil { |
| 162 | return err |
| 163 | } |
| 164 | } else { |
| 165 | changes := table.GetChanges(sqlite) |
| 166 | if c.canAutoMigrate(changes) { |
| 167 | c.logger.Info().Str("table", table.Name).Msg("Table exists, auto-migrating") |
| 168 | if err := c.autoMigrateTable(ctx, table, changes); err != nil { |
| 169 | return err |
| 170 | } |
| 171 | } else { |
| 172 | c.logger.Info().Str("table", table.Name).Msg("Table exists, force migration required") |
| 173 | if err := c.recreateTable(ctx, table); err != nil { |
| 174 | return err |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return nil |
| 181 | } |
| 182 | |
| 183 | func (c *Client) recreateTable(ctx context.Context, table *schema.Table) error { |
| 184 | sql := "drop table if exists " + identifier(table.Name) |
nothing calls this directly
no test coverage detected