SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
feat: rename org_domains table to auth_domain (#9910)
Karan Balani666bfa7a0f19
diff --git a/pkg/signoz/provider.go b/pkg/signoz/provider.goindex 6aa39b8555..8b62e5977b 100644--- a/pkg/signoz/provider.go+++ b/pkg/signoz/provider.go@@ -159,6 +159,7 @@ func NewSQLMigrationProviderFactories(sqlmigration.NewUpdateAuthzFactory(sqlstore, sqlschema),sqlmigration.NewUpdateUserPreferenceFactory(sqlstore, sqlschema),sqlmigration.NewUpdateOrgPreferenceFactory(sqlstore, sqlschema),+ sqlmigration.NewRenameOrgDomainsFactory(sqlstore, sqlschema),)}diff --git a/pkg/sqlmigration/057_rename_org_domains.go b/pkg/sqlmigration/057_rename_org_domains.gonew file mode 100644index 0000000000..29df3126f8--- /dev/null+++ b/pkg/sqlmigration/057_rename_org_domains.go@@ -0,0 +1,158 @@+package sqlmigration++import (+ "context"++ "github.com/SigNoz/signoz/pkg/factory"+ "github.com/SigNoz/signoz/pkg/sqlschema"+ "github.com/SigNoz/signoz/pkg/sqlstore"+ "github.com/SigNoz/signoz/pkg/types"+ "github.com/SigNoz/signoz/pkg/valuer"+ "github.com/uptrace/bun"+ "github.com/uptrace/bun/migrate"+)++type storableOrgDomain struct {+ bun.BaseModel `bun:"table:org_domains"`++ types.Identifiable+ Name string `bun:"name,type:text,notnull" json:"name"`+ Data string `bun:"data,type:text,notnull" json:"-"`+ OrgID valuer.UUID `bun:"org_id,type:text,notnull" json:"orgId"`+ types.TimeAuditable+}++type storableAuthDomain struct {+ bun.BaseModel `bun:"table:auth_domain"`++ types.Identifiable+ Name string `bun:"name,type:text,notnull" json:"name"`+ Data string `bun:"data,type:text,notnull" json:"-"`+ OrgID valuer.UUID `bun:"org_id,type:text,notnull" json:"orgId"`+ types.TimeAuditable+}++type renameOrgDomains struct {+ sqlStore sqlstore.SQLStore+ sqlSchema sqlschema.SQLSchema+}++func NewRenameOrgDomainsFactory(sqlStore sqlstore.SQLStore, sqlSchema sqlschema.SQLSchema) factory.ProviderFactory[SQLMigration, Config] {+ return factory.NewProviderFactory(factory.MustNewName("rename_org_domains"), func(ctx context.Context, ps factory.ProviderSettings, c Config) (SQLMigration, error) {+ return newRenameOrgDomains(ctx, ps, c, sqlStore, sqlSchema)+ })+}++func newRenameOrgDomains(_ context.Context, _ factory.ProviderSettings, _ Config, sqlStore sqlstore.SQLStore, sqlSchema sqlschema.SQLSchema) (SQLMigration, error) {+ return &renameOrgDomains{+ sqlStore: sqlStore,+ sqlSchema: sqlSchema,+ }, nil+}++func (migration *renameOrgDomains) Register(migrations *migrate.Migrations) error {+ if err := migrations.Register(migration.Up, migration.Down); err != nil {+ return err+ }+ return nil+}++func (migration *renameOrgDomains) Up(ctx context.Context, db *bun.DB) error {+ // check if the `auth_domain` table already exists+ _, _, err := migration.sqlSchema.GetTable(ctx, sqlschema.TableName("auth_domain"))+ if err == nil {+ return nil+ }++ orgDomainTable, _, err := migration.sqlSchema.GetTable(ctx, sqlschema.TableName("org_domains"))+ if err != nil {+ return err+ }++ tx, err := db.BeginTx(ctx, nil)+ if err != nil {+ return err+ }++ defer func() {+ _ = tx.Rollback()+ }()++ oldOrgDomains := []*storableOrgDomain{}+ err = tx.NewSelect().Model(&oldOrgDomains).Scan(ctx)+ if err != nil {+ return err+ }++ // drop table `org_domains`+ orgDomainDropSQLs := migration.sqlSchema.Operator().DropTable(orgDomainTable)+ for _, sql := range orgDomainDropSQLs {+ if _, err := tx.ExecContext(ctx, string(sql)); err != nil {+ return err+ }+ }++ // create table `auth_domain`+ authDomainTableCreateSQLs := migration.sqlSchema.Operator().CreateTable(&sqlschema.Table{+ Name: "auth_domain",+ Columns: []*sqlschema.Column{+ {Name: "id", DataType: sqlschema.DataTypeText, Nullable: false},+ {Name: "name", DataType: sqlschema.DataTypeText, Nullable: false},+ {Name: "data", DataType: sqlschema.DataTypeText, Nullable: false},+ {Name: "org_id", DataType: sqlschema.DataTypeText, Nullable: false},+ {Name: "created_at", DataType: sqlschema.DataTypeTimestamp, Nullable: false},+ {Name: "updated_at", DataType: sqlschema.DataTypeTimestamp, Nullable: false},+ },+ PrimaryKeyConstraint: &sqlschema.PrimaryKeyConstraint{+ ColumnNames: []sqlschema.ColumnName{"id"},+ },+ ForeignKeyConstraints: []*sqlschema.ForeignKeyConstraint{+ {+ ReferencingColumnName: sqlschema.ColumnName("org_id"),+ ReferencedTableName: sqlschema.TableName("organizations"),+ ReferencedColumnName: sqlschema.ColumnName("id"),+ },+ },+ })+ for _, sql := range authDomainTableCreateSQLs {+ if _, err := tx.ExecContext(ctx, string(sql)); err != nil {+ return err+ }+ }++ // create index on `auth_domain`+ authDomainIndexSQLs := migration.sqlSchema.Operator().CreateIndex(&sqlschema.UniqueIndex{TableName: "auth_domain", ColumnNames: []sqlschema.ColumnName{"name", "org_id"}})+ for _, sql := range authDomainIndexSQLs {+ if _, err := tx.ExecContext(ctx, string(sql)); err != nil {+ return err+ }+ }++ // convert old org domains to new auth domains+ authDomains := []*storableAuthDomain{}+ for _, orgDomain := range oldOrgDomains {+ authDomains = append(authDomains, &storableAuthDomain{+ Identifiable: orgDomain.Identifiable,+ TimeAuditable: orgDomain.TimeAuditable,+ Name: orgDomain.Name,+ Data: orgDomain.Data,+ OrgID: orgDomain.OrgID,+ })+ }++ if len(authDomains) > 0 {+ if _, err := tx.NewInsert().Model(&authDomains).Exec(ctx); err != nil {+ return err+ }+ }++ if err := tx.Commit(); err != nil {+ return err+ }++ return nil+}++func (migration *renameOrgDomains) Down(_ context.Context, _ *bun.DB) error {+ return nil+}diff --git a/pkg/sqlschema/operator.go b/pkg/sqlschema/operator.goindex fe8fddcc6e..8cdc994a81 100644--- a/pkg/sqlschema/operator.go+++ b/pkg/sqlschema/operator.go@@ -25,8 +25,9 @@ func (operator *Operator) CreateTable(table *Table) [][]byte {}func (operator *Operator) RenameTable(table *Table, newName TableName) [][]byte {+ sqls := [][]byte{table.ToRenameSQL(operator.fmter, newName)}table.Name = newName- return [][]byte{table.ToRenameSQL(operator.fmter, newName)}+ return sqls}func (operator *Operator) RecreateTable(table *Table, uniqueConstraints []*UniqueConstraint) [][]byte {diff --git a/pkg/types/authtypes/domain.go b/pkg/types/authtypes/domain.goindex a33ad4c6fb..20b824fe29 100644--- a/pkg/types/authtypes/domain.go+++ b/pkg/types/authtypes/domain.go@@ -48,7 +48,7 @@ type UpdateableAuthDomain struct {}type StorableAuthDomain struct {- bun.BaseModel `bun:"table:org_domains"`+ bun.BaseModel `bun:"table:auth_domain"`types.IdentifiableName string `bun:"name" json:"name"`