Skip to content

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.

fix: handle foreign key constraint on rule and planned maintenance deletion (#10632)

Pandey4554a09a4239
diff --git a/ee/sqlstore/postgressqlstore/provider.go b/ee/sqlstore/postgressqlstore/provider.go
index a969ac7fd1..a635e851c9 100644
--- a/ee/sqlstore/postgressqlstore/provider.go
+++ b/ee/sqlstore/postgressqlstore/provider.go
@@ -101,7 +101,7 @@ func (provider *provider) WrapNotFoundErrf(err error, code errors.Code, format s
func (provider *provider) WrapAlreadyExistsErrf(err error, code errors.Code, format string, args ...any) error {
var pgErr *pgconn.PgError
- if errors.As(err, &pgErr) && pgErr.Code == "23505" {
+ if errors.As(err, &pgErr) && (pgErr.Code == "23505" || pgErr.Code == "23503") {
return errors.Wrapf(err, errors.TypeAlreadyExists, code, format, args...)
}
diff --git a/pkg/ruler/rulestore/sqlrulestore/maintenance.go b/pkg/ruler/rulestore/sqlrulestore/maintenance.go
index 3282af5e6a..d20c92b85a 100644
--- a/pkg/ruler/rulestore/sqlrulestore/maintenance.go
+++ b/pkg/ruler/rulestore/sqlrulestore/maintenance.go
@@ -4,6 +4,7 @@ import (
"context"
"time"
+ "github.com/SigNoz/signoz/pkg/errors"
"github.com/SigNoz/signoz/pkg/sqlstore"
"github.com/SigNoz/signoz/pkg/types"
"github.com/SigNoz/signoz/pkg/types/authtypes"
@@ -135,7 +136,7 @@ func (r *maintenance) DeletePlannedMaintenance(ctx context.Context, id valuer.UU
Where("id = ?", id.StringValue()).
Exec(ctx)
if err != nil {
- return err
+ return r.sqlstore.WrapAlreadyExistsErrf(err, errors.CodeAlreadyExists, "cannot delete planned maintenance because it is referenced by associated rules, remove the rules from the planned maintenance first")
}
return nil
diff --git a/pkg/ruler/rulestore/sqlrulestore/rule.go b/pkg/ruler/rulestore/sqlrulestore/rule.go
index 4715641059..a40215cb28 100644
--- a/pkg/ruler/rulestore/sqlrulestore/rule.go
+++ b/pkg/ruler/rulestore/sqlrulestore/rule.go
@@ -6,6 +6,7 @@ import (
"log/slog"
"slices"
+ "github.com/SigNoz/signoz/pkg/errors"
"github.com/SigNoz/signoz/pkg/factory"
"github.com/SigNoz/signoz/pkg/queryparser"
"github.com/SigNoz/signoz/pkg/sqlstore"
@@ -75,7 +76,7 @@ func (r *rule) DeleteRule(ctx context.Context, id valuer.UUID, cb func(context.C
Where("id = ?", id.StringValue()).
Exec(ctx)
if err != nil {
- return err
+ return r.sqlstore.WrapAlreadyExistsErrf(err, errors.CodeAlreadyExists, "cannot delete rule because it is referenced by a planned maintenance, remove the rule from the planned maintenance first")
}
return cb(ctx)
diff --git a/pkg/signoz/provider.go b/pkg/signoz/provider.go
index 7a80fa310a..f7b7b84c2b 100644
--- a/pkg/signoz/provider.go
+++ b/pkg/signoz/provider.go
@@ -176,6 +176,7 @@ func NewSQLMigrationProviderFactories(
sqlmigration.NewAddStatusUserFactory(sqlstore, sqlschema),
sqlmigration.NewDeprecateUserInviteFactory(sqlstore, sqlschema),
sqlmigration.NewUpdateCloudIntegrationUniqueIndexFactory(sqlstore, sqlschema),
+ sqlmigration.NewUpdatePlannedMaintenanceRuleFactory(sqlstore, sqlschema),
)
}
diff --git a/pkg/sqlmigration/070_update_planned_maintenance_rule.go b/pkg/sqlmigration/070_update_planned_maintenance_rule.go
new file mode 100644
index 0000000000..e922b1620b
--- /dev/null
+++ b/pkg/sqlmigration/070_update_planned_maintenance_rule.go
@@ -0,0 +1,132 @@
+package sqlmigration
+
+import (
+ "context"
+ "github.com/SigNoz/signoz/pkg/factory"
+ "github.com/SigNoz/signoz/pkg/sqlschema"
+ "github.com/SigNoz/signoz/pkg/sqlstore"
+ "github.com/uptrace/bun"
+ "github.com/uptrace/bun/migrate"
+)
+
+type updatePlannedMaintenanceRule struct {
+ sqlstore sqlstore.SQLStore
+ sqlschema sqlschema.SQLSchema
+}
+
+type plannedMaintenanceRuleRow struct {
+ bun.BaseModel `bun:"table:planned_maintenance_rule"`
+
+ ID string `bun:"id"`
+ PlannedMaintenanceID string `bun:"planned_maintenance_id"`
+ RuleID string `bun:"rule_id"`
+}
+
+func NewUpdatePlannedMaintenanceRuleFactory(sqlstore sqlstore.SQLStore, sqlschema sqlschema.SQLSchema) factory.ProviderFactory[SQLMigration, Config] {
+ return factory.NewProviderFactory(
+ factory.MustNewName("update_planned_maintenance_rule"),
+ func(ctx context.Context, ps factory.ProviderSettings, c Config) (SQLMigration, error) {
+ return &updatePlannedMaintenanceRule{
+ sqlstore: sqlstore,
+ sqlschema: sqlschema,
+ }, nil
+ },
+ )
+}
+
+func (migration *updatePlannedMaintenanceRule) Register(migrations *migrate.Migrations) error {
+ if err := migrations.Register(migration.Up, migration.Down); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (migration *updatePlannedMaintenanceRule) Up(ctx context.Context, db *bun.DB) error {
+ table, _, err := migration.sqlschema.GetTable(ctx, sqlschema.TableName("planned_maintenance_rule"))
+ if err != nil {
+ return err
+ }
+
+ if err := migration.sqlschema.ToggleFKEnforcement(ctx, db, false); err != nil {
+ return err
+ }
+
+ tx, err := db.BeginTx(ctx, nil)
+ if err != nil {
+ return err
+ }
+
+ defer func() {
+ _ = tx.Rollback()
+ }()
+
+ // Read all existing rows
+ var rows []*plannedMaintenanceRuleRow
+ err = tx.NewSelect().Model(&rows).Scan(ctx)
+ if err != nil {
+ return err
+ }
+
+ // Drop the existing table
+ dropTableSQLs := migration.sqlschema.Operator().DropTable(table)
+ for _, sql := range dropTableSQLs {
+ if _, err := tx.ExecContext(ctx, string(sql)); err != nil {
+ return err
+ }
+ }
+
+ // Create the table fresh without CASCADE constraints
+ newTable := &sqlschema.Table{
+ Name: sqlschema.TableName("planned_maintenance_rule"),
+ Columns: []*sqlschema.Column{
+ {Name: "id", DataType: sqlschema.DataTypeText, Nullable: false},
+ {Name: "planned_maintenance_id", DataType: sqlschema.DataTypeText, Nullable: false},
+ {Name: "rule_id", DataType: sqlschema.DataTypeText, Nullable: false},
+ },
+ PrimaryKeyConstraint: &sqlschema.PrimaryKeyConstraint{
+ ColumnNames: []sqlschema.ColumnName{"id"},
+ },
+ ForeignKeyConstraints: []*sqlschema.ForeignKeyConstraint{
+ {
+ ReferencingColumnName: "planned_maintenance_id",
+ ReferencedTableName: "planned_maintenance",
+ ReferencedColumnName: "id",
+ },
+ {
+ ReferencingColumnName: "rule_id",
+ ReferencedTableName: "rule",
+ ReferencedColumnName: "id",
+ },
+ },
+ }
+
+ createTableSQLs := migration.sqlschema.Operator().CreateTable(newTable)
+ for _, sql := range createTableSQLs {
+ if _, err := tx.ExecContext(ctx, string(sql)); err != nil {
+ return err
+ }
+ }
+
+ // Re-insert the data
+ if len(rows) > 0 {
+ _, err = tx.NewInsert().Model(&rows).Exec(ctx)
+ if err != nil {
+ return err
+ }
+ }
+
+ if err := tx.Commit(); err != nil {
+ return err
+ }
+
+ if err := migration.sqlschema.ToggleFKEnforcement(ctx, db, true); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (migration *updatePlannedMaintenanceRule) Down(ctx context.Context, db *bun.DB) error {
+ return nil
+}
diff --git a/pkg/sqlstore/sqlitesqlstore/provider.go b/pkg/sqlstore/sqlitesqlstore/provider.go
index ced82d2e83..066f27abd2 100644
--- a/pkg/sqlstore/sqlitesqlstore/provider.go
+++ b/pkg/sqlstore/sqlitesqlstore/provider.go
@@ -100,7 +100,7 @@ func (provider *provider) WrapNotFoundErrf(err error, code errors.Code, format s
func (provider *provider) WrapAlreadyExistsErrf(err error, code errors.Code, format string, args ...any) error {
if sqlite3Err, ok := err.(*sqlite.Error); ok {
- if sqlite3Err.Code() == sqlite3.SQLITE_CONSTRAINT_UNIQUE || sqlite3Err.Code() == sqlite3.SQLITE_CONSTRAINT_PRIMARYKEY {
+ if sqlite3Err.Code() == sqlite3.SQLITE_CONSTRAINT_UNIQUE || sqlite3Err.Code() == sqlite3.SQLITE_CONSTRAINT_PRIMARYKEY || sqlite3Err.Code() == sqlite3.SQLITE_CONSTRAINT_FOREIGNKEY {
return errors.Wrapf(err, errors.TypeAlreadyExists, code, format, args...)
}
}