mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-06-04 11:25:52 +08:00

* fix: move migrations to bun * fix: use anonymous structs and move modes to types package * fix: minor changes after tests * fix: remove bun relations and add foreign keys * fix: minor changes * Update pkg/types/agent.go Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> * fix: add migration and postgres provider * fix: address minor comments * fix: use bun create index * fix: add migration * fix: support for postgres in migrations * Update pkg/sqlstore/pgstore/provider.go Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> * Update pkg/sqlmigration/001_add_organization.go Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> * fix: address comments * fix: move max connection to base config * fix: update scope --------- Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
91 lines
2.3 KiB
Go
91 lines
2.3 KiB
Go
package sqlmigration
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/uptrace/bun"
|
|
"github.com/uptrace/bun/migrate"
|
|
"go.signoz.io/signoz/pkg/factory"
|
|
)
|
|
|
|
type modifyDatetime struct{}
|
|
|
|
func NewModifyDatetimeFactory() factory.ProviderFactory[SQLMigration, Config] {
|
|
return factory.NewProviderFactory(factory.MustNewName("modify_datetime"), newModifyDatetime)
|
|
}
|
|
|
|
func newModifyDatetime(_ context.Context, _ factory.ProviderSettings, _ Config) (SQLMigration, error) {
|
|
return &modifyDatetime{}, nil
|
|
}
|
|
|
|
func (migration *modifyDatetime) Register(migrations *migrate.Migrations) error {
|
|
if err := migrations.Register(migration.Up, migration.Down); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (migration *modifyDatetime) Up(ctx context.Context, db *bun.DB) error {
|
|
// only run this for old sqlite db
|
|
if db.Dialect().Name().String() != "sqlite" {
|
|
return nil
|
|
}
|
|
|
|
// begin transaction
|
|
tx, err := db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
|
|
tables := []string{"dashboards", "rules", "planned_maintenance", "ttl_status", "saved_views"}
|
|
columns := []string{"created_at", "updated_at"}
|
|
for _, table := range tables {
|
|
for _, column := range columns {
|
|
if err := modifyColumn(ctx, tx, table, column); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, column := range []string{"started_at", "terminated_at"} {
|
|
if err := modifyColumn(ctx, tx, "agents", column); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func modifyColumn(ctx context.Context, tx bun.Tx, table string, column string) error {
|
|
// rename old column
|
|
if _, err := tx.ExecContext(ctx, `ALTER TABLE `+table+` RENAME COLUMN `+column+` TO `+column+`_old`); err != nil {
|
|
return err
|
|
}
|
|
|
|
// cannot add not null constraint to the column
|
|
if _, err := tx.ExecContext(ctx, `ALTER TABLE `+table+` ADD COLUMN `+column+` TIMESTAMP`); err != nil {
|
|
return err
|
|
}
|
|
|
|
// update the new column with the value of the old column
|
|
if _, err := tx.ExecContext(ctx, `UPDATE `+table+` SET `+column+` = `+column+`_old`); err != nil {
|
|
return err
|
|
}
|
|
|
|
// drop the old column
|
|
if _, err := tx.ExecContext(ctx, `ALTER TABLE `+table+` DROP COLUMN `+column+`_old`); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (migration *modifyDatetime) Down(ctx context.Context, db *bun.DB) error {
|
|
return nil
|
|
}
|