Skip to content

Commit

Permalink
feat: support for modify query mat col in migrations (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
nityanandagohain authored Oct 26, 2024
1 parent 528ffca commit ea3fe4e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
54 changes: 54 additions & 0 deletions cmd/signozschemamigrator/schema_migrator/table_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,57 @@ func (c CreateMaterializedViewOperation) ToSQL() string {
sql.WriteString(c.Query)
return sql.String()
}

// ModifyQueryMaterializedViewOperation is used to represent the ALTER TABLE ... MODIFY QUERY statement in the SQL.
type ModifyQueryMaterializedViewOperation struct {
cluster string
Database string
ViewName string
Query string
}

// OnCluster is used to specify the cluster on which the operation should be performed.
// This is useful when the operation is to be performed on a cluster setup.
func (c ModifyQueryMaterializedViewOperation) OnCluster(cluster string) Operation {
c.cluster = cluster
return &c
}

func (c ModifyQueryMaterializedViewOperation) WithReplication() Operation {
// no-op
return &c
}

func (c ModifyQueryMaterializedViewOperation) ShouldWaitForDistributionQueue() (bool, string, string) {
return false, c.Database, c.ViewName
}

func (c ModifyQueryMaterializedViewOperation) IsMutation() bool {
// Modify materialized view is not a mutation.
return false
}

func (c ModifyQueryMaterializedViewOperation) IsIdempotent() bool {
// Modify materialized view is idempotent. It will not change the materialized view if the materialized view already exists.
return true
}

func (c ModifyQueryMaterializedViewOperation) IsLightweight() bool {
// Modify materialized view is lightweight.
return true
}

func (c ModifyQueryMaterializedViewOperation) ToSQL() string {
var sql strings.Builder
sql.WriteString("ALTER TABLE ")
sql.WriteString(c.Database)
sql.WriteString(".")
sql.WriteString(c.ViewName)
if c.cluster != "" {
sql.WriteString(" ON CLUSTER ")
sql.WriteString(c.cluster)
}
sql.WriteString(" MODIFY QUERY ")
sql.WriteString(c.Query)
return sql.String()
}
24 changes: 24 additions & 0 deletions cmd/signozschemamigrator/schema_migrator/table_operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,30 @@ func TestCreateMaterializedView(t *testing.T) {
}
}

func TestModifyQueryMaterializedView(t *testing.T) {
testCases := []struct {
name string
op Operation
want string
}{
{
name: "modify-materialized-view",
op: ModifyQueryMaterializedViewOperation{
Database: "db",
ViewName: "view",
Query: "SELECT id, ts, value FROM db.table",
},
want: "ALTER TABLE db.view MODIFY QUERY SELECT id, ts, value FROM db.table",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.want, tc.op.ToSQL())
})
}
}

func TestCreateWithCluster(t *testing.T) {
testCases := []struct {
name string
Expand Down

0 comments on commit ea3fe4e

Please sign in to comment.