diff --git a/ee/sqlstore/postgressqlstore/dialect.go b/ee/sqlstore/postgressqlstore/dialect.go deleted file mode 100644 index 4f6987bda776..000000000000 --- a/ee/sqlstore/postgressqlstore/dialect.go +++ /dev/null @@ -1,456 +0,0 @@ -package postgressqlstore - -import ( - "context" - "fmt" - "reflect" - "slices" - - "github.com/SigNoz/signoz/pkg/errors" - "github.com/uptrace/bun" -) - -var ( - Identity = "id" - Integer = "bigint" - Text = "text" -) - -var ( - Org = "org" - User = "user" - UserNoCascade = "user_no_cascade" - FactorPassword = "factor_password" - CloudIntegration = "cloud_integration" - AgentConfigVersion = "agent_config_version" -) - -var ( - OrgReference = `("org_id") REFERENCES "organizations" ("id")` - UserReference = `("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE` - UserReferenceNoCascade = `("user_id") REFERENCES "users" ("id")` - FactorPasswordReference = `("password_id") REFERENCES "factor_password" ("id")` - CloudIntegrationReference = `("cloud_integration_id") REFERENCES "cloud_integration" ("id") ON DELETE CASCADE` - AgentConfigVersionReference = `("version_id") REFERENCES "agent_config_version" ("id")` -) - -type dialect struct{} - -func (dialect *dialect) IntToTimestamp(ctx context.Context, bun bun.IDB, table string, column string) error { - columnType, err := dialect.GetColumnType(ctx, bun, table, column) - if err != nil { - return err - } - - // bigint for postgres and INTEGER for sqlite - if columnType != "bigint" { - return nil - } - - // if the columns is integer then do this - if _, err := bun. - ExecContext(ctx, `ALTER TABLE `+table+` RENAME COLUMN `+column+` TO `+column+`_old`); err != nil { - return err - } - - // add new timestamp column - if _, err := bun. - NewAddColumn(). - Table(table). - ColumnExpr(column + " TIMESTAMP"). - Exec(ctx); err != nil { - return err - } - - if _, err := bun. - NewUpdate(). - Table(table). - Set(column + " = to_timestamp(cast(" + column + "_old as INTEGER))"). - Where("1=1"). - Exec(ctx); err != nil { - return err - } - - // drop old column - if _, err := bun. - NewDropColumn(). - Table(table). - Column(column + "_old"). - Exec(ctx); err != nil { - return err - } - - return nil -} - -func (dialect *dialect) IntToBoolean(ctx context.Context, bun bun.IDB, table string, column string) error { - columnExists, err := dialect.ColumnExists(ctx, bun, table, column) - if err != nil { - return err - } - if !columnExists { - return nil - } - - columnType, err := dialect.GetColumnType(ctx, bun, table, column) - if err != nil { - return err - } - - if columnType != "bigint" { - return nil - } - - if _, err := bun. - ExecContext(ctx, `ALTER TABLE `+table+` RENAME COLUMN `+column+` TO `+column+`_old`); err != nil { - return err - } - - // add new boolean column - if _, err := bun. - NewAddColumn(). - Table(table). - ColumnExpr(column + " BOOLEAN"). - Exec(ctx); err != nil { - return err - } - - // copy data from old column to new column, converting from int to boolean - if _, err := bun.NewUpdate(). - Table(table). - Set(column + " = CASE WHEN " + column + "_old = 1 THEN true ELSE false END"). - Where("1=1"). - Exec(ctx); err != nil { - return err - } - - // drop old column - if _, err := bun.NewDropColumn().Table(table).Column(column + "_old").Exec(ctx); err != nil { - return err - } - - return nil -} - -func (dialect *dialect) GetColumnType(ctx context.Context, bun bun.IDB, table string, column string) (string, error) { - var columnType string - - err := bun.NewSelect(). - ColumnExpr("data_type"). - TableExpr("information_schema.columns"). - Where("table_name = ?", table). - Where("column_name = ?", column). - Scan(ctx, &columnType) - if err != nil { - return "", err - } - - return columnType, nil -} - -func (dialect *dialect) ColumnExists(ctx context.Context, bun bun.IDB, table string, column string) (bool, error) { - var count int - err := bun.NewSelect(). - ColumnExpr("COUNT(*)"). - TableExpr("information_schema.columns"). - Where("table_name = ?", table). - Where("column_name = ?", column). - Scan(ctx, &count) - - if err != nil { - return false, err - } - - return count > 0, nil -} - -func (dialect *dialect) AddColumn(ctx context.Context, bun bun.IDB, table string, column string, columnExpr string) error { - exists, err := dialect.ColumnExists(ctx, bun, table, column) - if err != nil { - return err - } - if !exists { - _, err = bun. - NewAddColumn(). - Table(table). - ColumnExpr(column + " " + columnExpr). - Exec(ctx) - if err != nil { - return err - } - - } - - return nil -} - -func (dialect *dialect) RenameColumn(ctx context.Context, bun bun.IDB, table string, oldColumnName string, newColumnName string) (bool, error) { - oldColumnExists, err := dialect.ColumnExists(ctx, bun, table, oldColumnName) - if err != nil { - return false, err - } - - newColumnExists, err := dialect.ColumnExists(ctx, bun, table, newColumnName) - if err != nil { - return false, err - } - - if newColumnExists { - return true, nil - } - - if !oldColumnExists { - return false, errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "old column: %s doesn't exist", oldColumnName) - } - - _, err = bun. - ExecContext(ctx, "ALTER TABLE "+table+" RENAME COLUMN "+oldColumnName+" TO "+newColumnName) - if err != nil { - return false, err - } - return true, nil -} - -func (dialect *dialect) DropColumn(ctx context.Context, bun bun.IDB, table string, column string) error { - exists, err := dialect.ColumnExists(ctx, bun, table, column) - if err != nil { - return err - } - if exists { - _, err = bun. - NewDropColumn(). - Table(table). - Column(column). - Exec(ctx) - if err != nil { - return err - } - - } - - return nil -} - -func (dialect *dialect) TableExists(ctx context.Context, bun bun.IDB, table interface{}) (bool, error) { - - count := 0 - err := bun. - NewSelect(). - ColumnExpr("count(*)"). - Table("pg_catalog.pg_tables"). - Where("tablename = ?", bun.Dialect().Tables().Get(reflect.TypeOf(table)).Name). - Scan(ctx, &count) - - if err != nil { - return false, err - } - - if count == 0 { - return false, nil - } - - return true, nil -} - -func (dialect *dialect) RenameTableAndModifyModel(ctx context.Context, bun bun.IDB, oldModel interface{}, newModel interface{}, references []string, cb func(context.Context) error) error { - if len(references) == 0 { - return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "cannot run migration without reference") - } - exists, err := dialect.TableExists(ctx, bun, newModel) - if err != nil { - return err - } - if exists { - return nil - } - - var fkReferences []string - for _, reference := range references { - if reference == Org && !slices.Contains(fkReferences, OrgReference) { - fkReferences = append(fkReferences, OrgReference) - } else if reference == User && !slices.Contains(fkReferences, UserReference) { - fkReferences = append(fkReferences, UserReference) - } else if reference == UserNoCascade && !slices.Contains(fkReferences, UserReferenceNoCascade) { - fkReferences = append(fkReferences, UserReferenceNoCascade) - } else if reference == FactorPassword && !slices.Contains(fkReferences, FactorPasswordReference) { - fkReferences = append(fkReferences, FactorPasswordReference) - } else if reference == CloudIntegration && !slices.Contains(fkReferences, CloudIntegrationReference) { - fkReferences = append(fkReferences, CloudIntegrationReference) - } else if reference == AgentConfigVersion && !slices.Contains(fkReferences, AgentConfigVersionReference) { - fkReferences = append(fkReferences, AgentConfigVersionReference) - } - } - - createTable := bun. - NewCreateTable(). - IfNotExists(). - Model(newModel) - - for _, fk := range fkReferences { - createTable = createTable.ForeignKey(fk) - } - - _, err = createTable.Exec(ctx) - if err != nil { - return err - } - - err = cb(ctx) - if err != nil { - return err - } - - _, err = bun. - NewDropTable(). - IfExists(). - Model(oldModel). - Exec(ctx) - if err != nil { - return err - } - - return nil -} - -func (dialect *dialect) AddNotNullDefaultToColumn(ctx context.Context, bun bun.IDB, table string, column, columnType, defaultValue string) error { - query := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s, ALTER COLUMN %s SET NOT NULL", table, column, defaultValue, column) - if _, err := bun.ExecContext(ctx, query); err != nil { - return err - } - return nil -} - -func (dialect *dialect) UpdatePrimaryKey(ctx context.Context, bun bun.IDB, oldModel interface{}, newModel interface{}, reference string, cb func(context.Context) error) error { - if reference == "" { - return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "cannot run migration without reference") - } - oldTableName := bun.Dialect().Tables().Get(reflect.TypeOf(oldModel)).Name - newTableName := bun.Dialect().Tables().Get(reflect.TypeOf(newModel)).Name - - columnType, err := dialect.GetColumnType(ctx, bun, oldTableName, Identity) - if err != nil { - return err - } - if columnType == Text { - return nil - } - - fkReference := "" - if reference == Org { - fkReference = OrgReference - } else if reference == User { - fkReference = UserReference - } - - _, err = bun. - NewCreateTable(). - IfNotExists(). - Model(newModel). - ForeignKey(fkReference). - Exec(ctx) - - if err != nil { - return err - } - - err = cb(ctx) - if err != nil { - return err - } - - _, err = bun. - NewDropTable(). - IfExists(). - Model(oldModel). - Exec(ctx) - if err != nil { - return err - } - - _, err = bun. - ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s RENAME TO %s", newTableName, oldTableName)) - if err != nil { - return err - } - - return nil -} - -func (dialect *dialect) AddPrimaryKey(ctx context.Context, bun bun.IDB, oldModel interface{}, newModel interface{}, reference string, cb func(context.Context) error) error { - if reference == "" { - return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "cannot run migration without reference") - } - oldTableName := bun.Dialect().Tables().Get(reflect.TypeOf(oldModel)).Name - newTableName := bun.Dialect().Tables().Get(reflect.TypeOf(newModel)).Name - - identityExists, err := dialect.ColumnExists(ctx, bun, oldTableName, Identity) - if err != nil { - return err - } - if identityExists { - return nil - } - - fkReference := "" - if reference == Org { - fkReference = OrgReference - } else if reference == User { - fkReference = UserReference - } - - _, err = bun. - NewCreateTable(). - IfNotExists(). - Model(newModel). - ForeignKey(fkReference). - Exec(ctx) - - if err != nil { - return err - } - - err = cb(ctx) - if err != nil { - return err - } - - _, err = bun. - NewDropTable(). - IfExists(). - Model(oldModel). - Exec(ctx) - if err != nil { - return err - } - - _, err = bun. - ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s RENAME TO %s", newTableName, oldTableName)) - if err != nil { - return err - } - - return nil -} - -func (dialect *dialect) DropColumnWithForeignKeyConstraint(ctx context.Context, bunIDB bun.IDB, model interface{}, column string) error { - existingTable := bunIDB.Dialect().Tables().Get(reflect.TypeOf(model)) - columnExists, err := dialect.ColumnExists(ctx, bunIDB, existingTable.Name, column) - if err != nil { - return err - } - - if !columnExists { - return nil - } - - _, err = bunIDB. - NewDropColumn(). - Model(model). - Column(column). - Exec(ctx) - if err != nil { - return err - } - - return nil -} diff --git a/ee/sqlstore/postgressqlstore/provider.go b/ee/sqlstore/postgressqlstore/provider.go index 7e3bd22f34e6..3160b1a30c92 100644 --- a/ee/sqlstore/postgressqlstore/provider.go +++ b/ee/sqlstore/postgressqlstore/provider.go @@ -18,7 +18,6 @@ type provider struct { settings factory.ScopedProviderSettings sqldb *sql.DB bundb *sqlstore.BunDB - dialect *dialect } func NewFactory(hookFactories ...factory.ProviderFactory[sqlstore.SQLStoreHook, sqlstore.Config]) factory.ProviderFactory[sqlstore.SQLStore, sqlstore.Config] { @@ -59,7 +58,6 @@ func New(ctx context.Context, providerSettings factory.ProviderSettings, config settings: settings, sqldb: sqldb, bundb: sqlstore.NewBunDB(settings, sqldb, pgdialect.New(), hooks), - dialect: new(dialect), }, nil } @@ -71,10 +69,6 @@ func (provider *provider) SQLDB() *sql.DB { return provider.sqldb } -func (provider *provider) Dialect() sqlstore.SQLDialect { - return provider.dialect -} - func (provider *provider) BunDBCtx(ctx context.Context) bun.IDB { return provider.bundb.BunDBCtx(ctx) } @@ -99,7 +93,3 @@ func (provider *provider) WrapAlreadyExistsErrf(err error, code errors.Code, for return err } - -func (dialect *dialect) ToggleForeignKeyConstraint(ctx context.Context, bun *bun.DB, enable bool) error { - return nil -} diff --git a/pkg/sqlmigration/sqlmigration.go b/pkg/sqlmigration/sqlmigration.go index 3365c50ae15f..e49559db3fcf 100644 --- a/pkg/sqlmigration/sqlmigration.go +++ b/pkg/sqlmigration/sqlmigration.go @@ -2,7 +2,6 @@ package sqlmigration import ( "context" - "errors" "github.com/SigNoz/signoz/pkg/factory" "github.com/uptrace/bun" @@ -22,19 +21,6 @@ type SQLMigration interface { Down(context.Context, *bun.DB) error } -var ( - ErrNoExecute = errors.New("no execute") -) - -var ( - OrgReference = "org" - UserReference = "user" - UserReferenceNoCascade = "user_no_cascade" - FactorPasswordReference = "factor_password" - CloudIntegrationReference = "cloud_integration" - AgentConfigVersionReference = "agent_config_version" -) - func New( ctx context.Context, settings factory.ProviderSettings, diff --git a/pkg/sqlmigrator/sqlmigrator.go b/pkg/sqlmigrator/sqlmigrator.go index bc66fedd1cd4..1e70dea78cc4 100644 --- a/pkg/sqlmigrator/sqlmigrator.go +++ b/pkg/sqlmigrator/sqlmigrator.go @@ -8,6 +8,7 @@ import ( type SQLMigrator interface { // Migrate migrates the database. Migrate acquires a lock on the database and runs the migrations. Migrate(context.Context) error + // Rollback rolls back the database. Rollback acquires a lock on the database and rolls back the migrations. Rollback(context.Context) error } diff --git a/pkg/sqlstore/sqlitesqlstore/dialect.go b/pkg/sqlstore/sqlitesqlstore/dialect.go deleted file mode 100644 index 9e267a0eef12..000000000000 --- a/pkg/sqlstore/sqlitesqlstore/dialect.go +++ /dev/null @@ -1,509 +0,0 @@ -package sqlitesqlstore - -import ( - "context" - "fmt" - "reflect" - "slices" - "strings" - - "github.com/SigNoz/signoz/pkg/errors" - "github.com/uptrace/bun" -) - -const ( - Identity string = "id" - Integer string = "INTEGER" - Text string = "TEXT" -) - -const ( - Org string = "org" - User string = "user" - UserNoCascade string = "user_no_cascade" - FactorPassword string = "factor_password" - CloudIntegration string = "cloud_integration" - AgentConfigVersion string = "agent_config_version" -) - -const ( - OrgReference string = `("org_id") REFERENCES "organizations" ("id")` - UserReference string = `("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE` - UserNoCascadeReference string = `("user_id") REFERENCES "users" ("id")` - FactorPasswordReference string = `("password_id") REFERENCES "factor_password" ("id")` - CloudIntegrationReference string = `("cloud_integration_id") REFERENCES "cloud_integration" ("id") ON DELETE CASCADE` - AgentConfigVersionReference string = `("version_id") REFERENCES "agent_config_version" ("id")` -) - -const ( - OrgField string = "org_id" -) - -type dialect struct{} - -func (dialect *dialect) GetColumnType(ctx context.Context, bun bun.IDB, table string, column string) (string, error) { - var columnType string - - err := bun. - NewSelect(). - ColumnExpr("type"). - TableExpr("pragma_table_info(?)", table). - Where("name = ?", column). - Scan(ctx, &columnType) - if err != nil { - return "", err - } - - return columnType, nil -} - -func (dialect *dialect) IntToTimestamp(ctx context.Context, bun bun.IDB, table string, column string) error { - columnType, err := dialect.GetColumnType(ctx, bun, table, column) - if err != nil { - return err - } - - if columnType != "INTEGER" { - return nil - } - - // if the columns is integer then do this - if _, err := bun.ExecContext(ctx, `ALTER TABLE `+table+` RENAME COLUMN `+column+` TO `+column+`_old`); err != nil { - return err - } - - // add new timestamp column - if _, err := bun. - NewAddColumn(). - Table(table). - ColumnExpr(column + " TIMESTAMP"). - Exec(ctx); err != nil { - return err - } - - // copy data from old column to new column, converting from int (unix timestamp) to timestamp - if _, err := bun. - NewUpdate(). - Table(table). - Set(column + " = datetime(" + column + "_old, 'unixepoch')"). - Where("1=1"). - Exec(ctx); err != nil { - return err - } - - // drop old column - if _, err := bun.NewDropColumn().Table(table).Column(column + "_old").Exec(ctx); err != nil { - return err - } - - return nil -} - -func (dialect *dialect) IntToBoolean(ctx context.Context, bun bun.IDB, table string, column string) error { - columnExists, err := dialect.ColumnExists(ctx, bun, table, column) - if err != nil { - return err - } - if !columnExists { - return nil - } - - columnType, err := dialect.GetColumnType(ctx, bun, table, column) - if err != nil { - return err - } - - if columnType != "INTEGER" { - return nil - } - - if _, err := bun.ExecContext(ctx, `ALTER TABLE `+table+` RENAME COLUMN `+column+` TO `+column+`_old`); err != nil { - return err - } - - // add new boolean column - if _, err := bun.NewAddColumn().Table(table).ColumnExpr(column + " BOOLEAN").Exec(ctx); err != nil { - return err - } - - // copy data from old column to new column, converting from int to boolean - if _, err := bun. - NewUpdate(). - Table(table). - Set(column + " = CASE WHEN " + column + "_old = 1 THEN true ELSE false END"). - Where("1=1"). - Exec(ctx); err != nil { - return err - } - - // drop old column - if _, err := bun.NewDropColumn().Table(table).Column(column + "_old").Exec(ctx); err != nil { - return err - } - - return nil -} - -func (dialect *dialect) ColumnExists(ctx context.Context, bun bun.IDB, table string, column string) (bool, error) { - var count int - err := bun.NewSelect(). - ColumnExpr("COUNT(*)"). - TableExpr("pragma_table_info(?)", table). - Where("name = ?", column). - Scan(ctx, &count) - - if err != nil { - return false, err - } - - return count > 0, nil -} - -func (dialect *dialect) AddColumn(ctx context.Context, bun bun.IDB, table string, column string, columnExpr string) error { - exists, err := dialect.ColumnExists(ctx, bun, table, column) - if err != nil { - return err - } - if !exists { - _, err = bun. - NewAddColumn(). - Table(table). - ColumnExpr(column + " " + columnExpr). - Exec(ctx) - if err != nil { - return err - } - - } - - return nil -} - -func (dialect *dialect) RenameColumn(ctx context.Context, bun bun.IDB, table string, oldColumnName string, newColumnName string) (bool, error) { - oldColumnExists, err := dialect.ColumnExists(ctx, bun, table, oldColumnName) - if err != nil { - return false, err - } - - newColumnExists, err := dialect.ColumnExists(ctx, bun, table, newColumnName) - if err != nil { - return false, err - } - - if newColumnExists { - return true, nil - } - - if !oldColumnExists { - return false, errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "old column: %s doesn't exist", oldColumnName) - } - - _, err = bun. - ExecContext(ctx, "ALTER TABLE "+table+" RENAME COLUMN "+oldColumnName+" TO "+newColumnName) - if err != nil { - return false, err - } - return true, nil -} - -func (dialect *dialect) DropColumn(ctx context.Context, bun bun.IDB, table string, column string) error { - exists, err := dialect.ColumnExists(ctx, bun, table, column) - if err != nil { - return err - } - if exists { - _, err = bun. - NewDropColumn(). - Table(table). - Column(column). - Exec(ctx) - if err != nil { - return err - } - - } - - return nil -} - -func (dialect *dialect) TableExists(ctx context.Context, bun bun.IDB, table interface{}) (bool, error) { - count := 0 - err := bun. - NewSelect(). - ColumnExpr("count(*)"). - Table("sqlite_master"). - Where("type = ?", "table"). - Where("name = ?", bun.Dialect().Tables().Get(reflect.TypeOf(table)).Name). - Scan(ctx, &count) - - if err != nil { - return false, err - } - - if count == 0 { - return false, nil - } - - return true, nil -} - -func (dialect *dialect) RenameTableAndModifyModel(ctx context.Context, bun bun.IDB, oldModel interface{}, newModel interface{}, references []string, cb func(context.Context) error) error { - if len(references) == 0 { - return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "cannot run migration without reference") - } - exists, err := dialect.TableExists(ctx, bun, newModel) - if err != nil { - return err - } - if exists { - return nil - } - - var fkReferences []string - for _, reference := range references { - if reference == Org && !slices.Contains(fkReferences, OrgReference) { - fkReferences = append(fkReferences, OrgReference) - } else if reference == User && !slices.Contains(fkReferences, UserReference) { - fkReferences = append(fkReferences, UserReference) - } else if reference == UserNoCascade && !slices.Contains(fkReferences, UserNoCascadeReference) { - fkReferences = append(fkReferences, UserNoCascadeReference) - } else if reference == FactorPassword && !slices.Contains(fkReferences, FactorPasswordReference) { - fkReferences = append(fkReferences, FactorPasswordReference) - } else if reference == CloudIntegration && !slices.Contains(fkReferences, CloudIntegrationReference) { - fkReferences = append(fkReferences, CloudIntegrationReference) - } else if reference == AgentConfigVersion && !slices.Contains(fkReferences, AgentConfigVersionReference) { - fkReferences = append(fkReferences, AgentConfigVersionReference) - } - } - - createTable := bun. - NewCreateTable(). - IfNotExists(). - Model(newModel) - - for _, fk := range fkReferences { - createTable = createTable.ForeignKey(fk) - } - - _, err = createTable.Exec(ctx) - if err != nil { - return err - } - - err = cb(ctx) - if err != nil { - return err - } - - _, err = bun. - NewDropTable(). - IfExists(). - Model(oldModel). - Exec(ctx) - if err != nil { - return err - } - - return nil -} - -func (dialect *dialect) AddNotNullDefaultToColumn(ctx context.Context, bun bun.IDB, table string, column, columnType, defaultValue string) error { - if _, err := bun.NewAddColumn().Table(table).ColumnExpr(fmt.Sprintf("%s_new %s NOT NULL DEFAULT %s ", column, columnType, defaultValue)).Exec(ctx); err != nil { - return err - } - - if _, err := bun.NewUpdate().Table(table).Set(fmt.Sprintf("%s_new = %s", column, column)).Where("1=1").Exec(ctx); err != nil { - return err - } - - if _, err := bun.NewDropColumn().Table(table).ColumnExpr(column).Exec(ctx); err != nil { - return err - } - - if _, err := bun.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s RENAME COLUMN %s_new TO %s", table, column, column)); err != nil { - return err - } - - return nil -} - -func (dialect *dialect) UpdatePrimaryKey(ctx context.Context, bun bun.IDB, oldModel interface{}, newModel interface{}, reference string, cb func(context.Context) error) error { - if reference == "" { - return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "cannot run migration without reference") - } - oldTableName := bun.Dialect().Tables().Get(reflect.TypeOf(oldModel)).Name - newTableName := bun.Dialect().Tables().Get(reflect.TypeOf(newModel)).Name - - columnType, err := dialect.GetColumnType(ctx, bun, oldTableName, Identity) - if err != nil { - return err - } - if columnType == Text { - return nil - } - - fkReference := "" - if reference == Org { - fkReference = OrgReference - } else if reference == User { - fkReference = UserReference - } - - _, err = bun. - NewCreateTable(). - IfNotExists(). - Model(newModel). - ForeignKey(fkReference). - Exec(ctx) - - if err != nil { - return err - } - - err = cb(ctx) - if err != nil { - return err - } - - _, err = bun. - NewDropTable(). - IfExists(). - Model(oldModel). - Exec(ctx) - if err != nil { - return err - } - - _, err = bun. - ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s RENAME TO %s", newTableName, oldTableName)) - if err != nil { - return err - } - - return nil -} - -func (dialect *dialect) AddPrimaryKey(ctx context.Context, bun bun.IDB, oldModel interface{}, newModel interface{}, reference string, cb func(context.Context) error) error { - if reference == "" { - return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "cannot run migration without reference") - } - oldTableName := bun.Dialect().Tables().Get(reflect.TypeOf(oldModel)).Name - newTableName := bun.Dialect().Tables().Get(reflect.TypeOf(newModel)).Name - - identityExists, err := dialect.ColumnExists(ctx, bun, oldTableName, Identity) - if err != nil { - return err - } - if identityExists { - return nil - } - - fkReference := "" - if reference == Org { - fkReference = OrgReference - } else if reference == User { - fkReference = UserReference - } - - _, err = bun. - NewCreateTable(). - IfNotExists(). - Model(newModel). - ForeignKey(fkReference). - Exec(ctx) - - if err != nil { - return err - } - - err = cb(ctx) - if err != nil { - return err - } - - _, err = bun. - NewDropTable(). - IfExists(). - Model(oldModel). - Exec(ctx) - if err != nil { - return err - } - - _, err = bun. - ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s RENAME TO %s", newTableName, oldTableName)) - if err != nil { - return err - } - - return nil -} - -func (dialect *dialect) DropColumnWithForeignKeyConstraint(ctx context.Context, bunIDB bun.IDB, model interface{}, column string) error { - var isForeignKeyEnabled bool - if err := bunIDB.QueryRowContext(ctx, "PRAGMA foreign_keys").Scan(&isForeignKeyEnabled); err != nil { - return err - } - - if isForeignKeyEnabled { - return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "foreign keys are enabled, please disable them before running this migration") - } - - existingTable := bunIDB.Dialect().Tables().Get(reflect.TypeOf(model)) - columnExists, err := dialect.ColumnExists(ctx, bunIDB, existingTable.Name, column) - if err != nil { - return err - } - - if !columnExists { - return nil - } - - newTableName := existingTable.Name + "_tmp" - - // Create the newTmpTable query - createTableQuery := bunIDB.NewCreateTable().Model(model).ModelTableExpr(newTableName) - - var columnNames []string - - for _, field := range existingTable.Fields { - if field.Name != column { - columnNames = append(columnNames, string(field.SQLName)) - } - - if field.Name == OrgField { - createTableQuery = createTableQuery.ForeignKey(OrgReference) - } - } - - if _, err = createTableQuery.Exec(ctx); err != nil { - return err - } - - // Copy data from old table to new table - if _, err := bunIDB.ExecContext(ctx, fmt.Sprintf("INSERT INTO %s SELECT %s FROM %s", newTableName, strings.Join(columnNames, ", "), existingTable.Name)); err != nil { - return err - } - - _, err = bunIDB.NewDropTable().Table(existingTable.Name).Exec(ctx) - if err != nil { - return err - } - - _, err = bunIDB.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s RENAME TO %s", newTableName, existingTable.Name)) - if err != nil { - return err - } - - return nil -} - -func (dialect *dialect) ToggleForeignKeyConstraint(ctx context.Context, bun *bun.DB, enable bool) error { - if enable { - _, err := bun.ExecContext(ctx, "PRAGMA foreign_keys = ON") - return err - } - - _, err := bun.ExecContext(ctx, "PRAGMA foreign_keys = OFF") - return err -} diff --git a/pkg/sqlstore/sqlitesqlstore/provider.go b/pkg/sqlstore/sqlitesqlstore/provider.go index 1dc62d9b174d..f9669438153a 100644 --- a/pkg/sqlstore/sqlitesqlstore/provider.go +++ b/pkg/sqlstore/sqlitesqlstore/provider.go @@ -16,7 +16,6 @@ type provider struct { settings factory.ScopedProviderSettings sqldb *sql.DB bundb *sqlstore.BunDB - dialect *dialect } func NewFactory(hookFactories ...factory.ProviderFactory[sqlstore.SQLStoreHook, sqlstore.Config]) factory.ProviderFactory[sqlstore.SQLStore, sqlstore.Config] { @@ -48,7 +47,6 @@ func New(ctx context.Context, providerSettings factory.ProviderSettings, config settings: settings, sqldb: sqldb, bundb: sqlstore.NewBunDB(settings, sqldb, sqlitedialect.New(), hooks), - dialect: new(dialect), }, nil } @@ -60,10 +58,6 @@ func (provider *provider) SQLDB() *sql.DB { return provider.sqldb } -func (provider *provider) Dialect() sqlstore.SQLDialect { - return provider.dialect -} - func (provider *provider) BunDBCtx(ctx context.Context) bun.IDB { return provider.bundb.BunDBCtx(ctx) } diff --git a/pkg/sqlstore/sqlstore.go b/pkg/sqlstore/sqlstore.go index 185a92467f4b..3a05d8a0ac5e 100644 --- a/pkg/sqlstore/sqlstore.go +++ b/pkg/sqlstore/sqlstore.go @@ -17,9 +17,6 @@ type SQLStore interface { // BunDB returns an instance of bun.DB. This is the recommended way to interact with the database. BunDB() *bun.DB - // Returns the dialect of the database. - Dialect() SQLDialect - // RunInTxCtx runs the given callback in a transaction. It creates and injects a new context with the transaction. // If a transaction is present in the context, it will be used. RunInTxCtx(ctx context.Context, opts *SQLStoreTxOptions, cb func(ctx context.Context) error) error @@ -38,51 +35,3 @@ type SQLStore interface { type SQLStoreHook interface { bun.QueryHook } - -type SQLDialect interface { - // Returns the type of the column for the given table and column. - GetColumnType(context.Context, bun.IDB, string, string) (string, error) - - // Migrates an integer column to a timestamp column for the given table and column. - IntToTimestamp(context.Context, bun.IDB, string, string) error - - // Migrates an integer column to a boolean column for the given table and column. - IntToBoolean(context.Context, bun.IDB, string, string) error - - // Adds a not null default to the given column for the given table, column, columnType and defaultValue. - AddNotNullDefaultToColumn(context.Context, bun.IDB, string, string, string, string) error - - // Checks if a column exists in a table for the given table and column. - ColumnExists(context.Context, bun.IDB, string, string) (bool, error) - - // Adds a column to a table for the given table, column and columnType. - AddColumn(context.Context, bun.IDB, string, string, string) error - - // Drops a column from a table for the given table and column. - DropColumn(context.Context, bun.IDB, string, string) error - - // Renames a column in a table for the given table, old column name and new column name. - RenameColumn(context.Context, bun.IDB, string, string, string) (bool, error) - - // Renames a table and modifies the given model for the given table, old model, new model, references and callback. The old model - // and new model must inherit bun.BaseModel. - RenameTableAndModifyModel(context.Context, bun.IDB, interface{}, interface{}, []string, func(context.Context) error) error - - // Updates the primary key for the given table, old model, new model, reference and callback. The old model and new model - // must inherit bun.BaseModel. - UpdatePrimaryKey(context.Context, bun.IDB, interface{}, interface{}, string, func(context.Context) error) error - - // Adds a primary key to the given table, old model, new model, reference and callback. The old model and new model - // must inherit bun.BaseModel. - AddPrimaryKey(context.Context, bun.IDB, interface{}, interface{}, string, func(context.Context) error) error - - // Drops the column and the associated foreign key constraint for the given table and column. - DropColumnWithForeignKeyConstraint(context.Context, bun.IDB, interface{}, string) error - - // Checks if a table exists. - TableExists(ctx context.Context, bun bun.IDB, table interface{}) (bool, error) - - // Toggles foreign key constraint for the given database. This makes sense only for sqlite. This cannot take a transaction as an argument and needs to take the db - // as an argument. - ToggleForeignKeyConstraint(ctx context.Context, bun *bun.DB, enable bool) error -} diff --git a/pkg/sqlstore/sqlstoretest/dialect.go b/pkg/sqlstore/sqlstoretest/dialect.go deleted file mode 100644 index 4b92d9e530fb..000000000000 --- a/pkg/sqlstore/sqlstoretest/dialect.go +++ /dev/null @@ -1,66 +0,0 @@ -package sqlstoretest - -import ( - "context" - - "github.com/uptrace/bun" -) - -type dialect struct { -} - -func (dialect *dialect) IntToTimestamp(ctx context.Context, bun bun.IDB, table string, column string) error { - return nil -} - -func (dialect *dialect) IntToBoolean(ctx context.Context, bun bun.IDB, table string, column string) error { - return nil -} - -func (dialect *dialect) GetColumnType(ctx context.Context, bun bun.IDB, table string, column string) (string, error) { - return "", nil -} - -func (dialect *dialect) ColumnExists(ctx context.Context, bun bun.IDB, table string, column string) (bool, error) { - return false, nil -} - -func (dialect *dialect) AddColumn(ctx context.Context, bun bun.IDB, table string, column string, columnExpr string) error { - return nil -} - -func (dialect *dialect) RenameColumn(ctx context.Context, bun bun.IDB, table string, oldColumnName string, newColumnName string) (bool, error) { - return true, nil -} - -func (dialect *dialect) DropColumn(ctx context.Context, bun bun.IDB, table string, column string) error { - return nil -} - -func (dialect *dialect) RenameTableAndModifyModel(ctx context.Context, bun bun.IDB, oldModel interface{}, newModel interface{}, references []string, cb func(context.Context) error) error { - return nil -} - -func (dialect *dialect) AddNotNullDefaultToColumn(ctx context.Context, bun bun.IDB, table string, column, columnType, defaultValue string) error { - return nil -} - -func (dialect *dialect) UpdatePrimaryKey(ctx context.Context, bun bun.IDB, oldModel interface{}, newModel interface{}, reference string, cb func(context.Context) error) error { - return nil -} - -func (dialect *dialect) AddPrimaryKey(ctx context.Context, bun bun.IDB, oldModel interface{}, newModel interface{}, reference string, cb func(context.Context) error) error { - return nil -} - -func (dialect *dialect) DropColumnWithForeignKeyConstraint(ctx context.Context, bun bun.IDB, model interface{}, column string) error { - return nil -} - -func (dialect *dialect) TableExists(ctx context.Context, bun bun.IDB, table interface{}) (bool, error) { - return true, nil -} - -func (dialect *dialect) ToggleForeignKeyConstraint(ctx context.Context, bun *bun.DB, enable bool) error { - return nil -} diff --git a/pkg/sqlstore/sqlstoretest/provider.go b/pkg/sqlstore/sqlstoretest/provider.go index a04624a52b00..72d5a776a1f8 100644 --- a/pkg/sqlstore/sqlstoretest/provider.go +++ b/pkg/sqlstore/sqlstoretest/provider.go @@ -16,10 +16,9 @@ import ( var _ sqlstore.SQLStore = (*Provider)(nil) type Provider struct { - db *sql.DB - mock sqlmock.Sqlmock - bunDB *bun.DB - dialect *dialect + db *sql.DB + mock sqlmock.Sqlmock + bunDB *bun.DB } func New(config sqlstore.Config, matcher sqlmock.QueryMatcher) *Provider { @@ -39,10 +38,9 @@ func New(config sqlstore.Config, matcher sqlmock.QueryMatcher) *Provider { } return &Provider{ - db: db, - mock: mock, - bunDB: bunDB, - dialect: new(dialect), + db: db, + mock: mock, + bunDB: bunDB, } } @@ -58,10 +56,6 @@ func (provider *Provider) Mock() sqlmock.Sqlmock { return provider.mock } -func (provider *Provider) Dialect() sqlstore.SQLDialect { - return provider.dialect -} - func (provider *Provider) BunDBCtx(ctx context.Context) bun.IDB { return provider.bunDB }