mirror of
https://github.com/uptrace/go-clickhouse.git
synced 2025-06-08 23:26:11 +02:00
chmigrate: add MissingMigrations
This commit is contained in:
parent
6bfb99e2c1
commit
2c9485da7f
@ -294,6 +294,25 @@ func TestDateTime64(t *testing.T) {
|
|||||||
require.Equal(t, in.Time.UnixNano(), out.Time.UnixNano())
|
require.Equal(t, in.Time.UnixNano(), out.Time.UnixNano())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInvalidType(t *testing.T) {
|
||||||
|
t.Skip()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
db := chDB()
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
var dest struct {
|
||||||
|
Numbers []float32
|
||||||
|
}
|
||||||
|
err := db.NewSelect().
|
||||||
|
ColumnExpr("groupArray(number) AS numbers").
|
||||||
|
TableExpr("numbers(10)").
|
||||||
|
Scan(ctx, &dest)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, []float64{}, dest.Numbers)
|
||||||
|
}
|
||||||
|
|
||||||
type Event struct {
|
type Event struct {
|
||||||
ch.CHModel `ch:"goch_events,partition:toYYYYMM(created_at)"`
|
ch.CHModel `ch:"goch_events,partition:toYYYYMM(created_at)"`
|
||||||
|
|
||||||
|
@ -82,7 +82,7 @@ func (m *Migrator) MigrationsWithStatus(ctx context.Context) (MigrationSlice, er
|
|||||||
func (m *Migrator) migrationsWithStatus(ctx context.Context) (MigrationSlice, int64, error) {
|
func (m *Migrator) migrationsWithStatus(ctx context.Context) (MigrationSlice, int64, error) {
|
||||||
sorted := m.migrations.Sorted()
|
sorted := m.migrations.Sorted()
|
||||||
|
|
||||||
applied, err := m.selectAppliedMigrations(ctx)
|
applied, err := m.AppliedMigrations(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
@ -377,8 +377,31 @@ func (m *Migrator) MarkUnapplied(ctx context.Context, migration *Migration) erro
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// selectAppliedMigrations selects applied (applied) migrations in descending order.
|
func (m *Migrator) TruncateTable(ctx context.Context) error {
|
||||||
func (m *Migrator) selectAppliedMigrations(ctx context.Context) (MigrationSlice, error) {
|
_, err := m.db.Exec("TRUNCATE TABLE ?", ch.Ident(m.table))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// MissingMigrations returns applied migrations that can no longer be found.
|
||||||
|
func (m *Migrator) MissingMigrations(ctx context.Context) (MigrationSlice, error) {
|
||||||
|
applied, err := m.AppliedMigrations(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
existing := migrationMap(m.migrations.ms)
|
||||||
|
for i := len(applied) - 1; i >= 0; i-- {
|
||||||
|
m := &applied[i]
|
||||||
|
if _, ok := existing[m.Name]; ok {
|
||||||
|
applied = append(applied[:i], applied[i+1:]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return applied, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AppliedMigrations selects applied migrations in descending order.
|
||||||
|
func (m *Migrator) AppliedMigrations(ctx context.Context) (MigrationSlice, error) {
|
||||||
var ms MigrationSlice
|
var ms MigrationSlice
|
||||||
if err := m.db.NewSelect().
|
if err := m.db.NewSelect().
|
||||||
ColumnExpr("*").
|
ColumnExpr("*").
|
||||||
|
Loading…
x
Reference in New Issue
Block a user