Skip to content

Commit

Permalink
feat: MigrateTenants for Database interface
Browse files Browse the repository at this point in the history
  • Loading branch information
pieceowater committed Jan 8, 2025
1 parent 3190b3b commit 81b9872
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/db/ch/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (p *Clickhouse) GetDB() *gorm.DB {
}

// WithTransaction executes a function within a transaction
func (p *Clickhouse) WithTransaction(fn func(tx *gorm.DB) error) error {
func (p *Clickhouse) WithTransaction(_ func(tx *gorm.DB) error) error {
return fmt.Errorf("transactions are not supported in ClickHouse")
}

Expand Down Expand Up @@ -97,3 +97,7 @@ func (p *Clickhouse) SwitchSchema(schema string) *gorm.DB {
}
return p.db
}

func (p *Clickhouse) MigrateTenants(_ []string, _ []any) error {
panic(fmt.Errorf("not implemented yet"))
}
1 change: 1 addition & 0 deletions internal/db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Database interface {
WithTransaction(func(tx *gorm.DB) error) error
SeedData(data []any) error
SwitchSchema(schema string) *gorm.DB
MigrateTenants(schemas []string, autoMigrateEntities []any) error
}

// DatabaseType defines the type of databases supported
Expand Down
16 changes: 16 additions & 0 deletions internal/db/pg/pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,19 @@ func (p *Postgres) SwitchSchema(schema string) *gorm.DB {
}
return p.db
}

func (p *Postgres) MigrateTenants(schemas []string, autoMigrateEntities []any) error {
for _, schema := range schemas {
if err := p.SwitchSchema(schema).Error; err != nil {
return fmt.Errorf("failed to switch to schema %s: %w", schema, err)
}

for _, entity := range autoMigrateEntities {
if err := p.db.AutoMigrate(entity); err != nil {
return fmt.Errorf("failed to auto-migrate entity for schema %s: %w", schema, err)
}
}
}

return nil
}

0 comments on commit 81b9872

Please sign in to comment.