Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for modify query mat col in migrations #436

Merged
merged 4 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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