Skip to content

Commit c93dc82

Browse files
CamilleTeruelCamille Teruel
andauthored
fix: Fix azure devops migration from 0.17 to 0.18 (#5999)
Cherry-pick of #5957 * fix: Fix AzDO connection migration 0.17 -> 0.18 In 0.17, all connection fields are encrypted. In 0.18 only `token` is, so we need a migration of AzDo connection to decrypt all fields but `token`. * fix: Allow setting entities=None to ScopeConfig Allow setting `entities` field to `None` in `ScopeConfig` and fallback to default value. * feat: Add RenameColumn migration op * fix: Add missing AzDO migrations from 0.17 to 0.18 Now that migrations have been made manual, we need to add all the migrations for all that changed from 0.17 to 0.18 that were automatic before... Those are all required migrations collected with `git diff v0.17.0 v0.18.0-beta7 -- backend/python/plugins/azuredevops/azuredevops/models.py`: * Rename GitRepository.transformation_rule_id -> scope_config_id * Add GitRepository.provider * Rename Job.startTime -> start_time * Rename Job.finishTime -> start_time --------- Co-authored-by: Camille Teruel <camille.teruel@meri.co>
1 parent 03ee5cf commit c93dc82

File tree

5 files changed

+135
-5
lines changed

5 files changed

+135
-5
lines changed

backend/python/plugins/azuredevops/azuredevops/models.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,11 @@ def add_raw_data_params_table_to_scope(b: MigrationScriptBuilder):
158158

159159
@migration(20230802000001, name="rename startTime/finishTime to start_time/finish_time")
160160
def rename_starttime_and_finishtime_for_job(b: MigrationScriptBuilder):
161-
b.execute(f'ALTER TABLE _tool_azuredevops_jobs RENAME COLUMN startTime TO start_time', Dialect.MYSQL, ignore_error=True)
162-
b.execute(f'ALTER TABLE _tool_azuredevops_jobs RENAME COLUMN finishTime TO finish_time', Dialect.MYSQL, ignore_error=True)
163-
b.execute(f'ALTER TABLE _tool_azuredevops_jobs RENAME COLUMN `startTime` TO start_time', Dialect.POSTGRESQL, ignore_error=True)
164-
b.execute(f'ALTER TABLE _tool_azuredevops_jobs RENAME COLUMN `finishTime` TO finish_time', Dialect.POSTGRESQL, ignore_error=True)
161+
b.rename_column('_tool_azuredevops_jobs', 'startTime', 'start_time')
162+
b.rename_column('_tool_azuredevops_jobs', 'finishTime', 'finish_time')
163+
164+
165+
@migration(20230825150421, name="add missing migrations from 0.17 to 0.18")
166+
def add_missing_migrations_0_17_to_0_18(b: MigrationScriptBuilder):
167+
b.rename_column('_tool_azuredevops_gitrepositories', 'transformation_rule_id', 'scope_config_id')
168+
b.add_column('_tool_azuredevops_gitrepositories', 'provider', 'varchar(255)')

backend/python/pydevlake/pydevlake/model.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ class ScopeConfig(ToolTable, Model):
8787
name: str = Field(default="default")
8888
domain_types: list[DomainType] = Field(default=list(DomainType), alias="entities")
8989

90+
@validator('domain_types', pre=True, always=True)
91+
def set_default_domain_types(cls, v):
92+
if v is None:
93+
return list(DomainType)
94+
return v
95+
9096

9197
class RawModel(SQLModel):
9298
id: int = Field(primary_key=True)

backend/server/services/remote/models/migration.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,27 @@ func (o DropTableOperation) Execute(dal dal.Dal) errors.Error {
9797

9898
var _ Operation = (*DropTableOperation)(nil)
9999

100+
type RenameColumnOperation struct {
101+
Table string `json:"table"`
102+
OldName string `json:"old_name"`
103+
NewName string `json:"new_name"`
104+
}
105+
106+
func (o RenameColumnOperation) Execute(dal dal.Dal) errors.Error {
107+
if !dal.HasColumn(o.Table, o.OldName) {
108+
return nil
109+
}
110+
if dal.HasColumn(o.Table, o.NewName) {
111+
err := dal.DropColumns(o.Table, o.NewName)
112+
if err != nil {
113+
return err
114+
}
115+
}
116+
return dal.RenameColumn(o.Table, o.OldName, o.NewName)
117+
}
118+
119+
var _ Operation = (*RenameTableOperation)(nil)
120+
100121
type RenameTableOperation struct {
101122
OldName string `json:"old_name"`
102123
NewName string `json:"new_name"`
@@ -155,10 +176,12 @@ func (s *RemoteMigrationScript) UnmarshalJSON(data []byte) error {
155176
operation = &DropColumnOperation{}
156177
case "drop_table":
157178
operation = &DropTableOperation{}
179+
case "rename_column":
180+
operation = &RenameColumnOperation{}
158181
case "rename_table":
159182
operation = &RenameTableOperation{}
160183
default:
161-
return errors.BadInput.New("unsupported operation type")
184+
return errors.BadInput.New("unsupported operation type: " + operationType)
162185
}
163186
err = json.Unmarshal(operationRaw, operation)
164187
if err != nil {
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package azuredevops
19+
20+
import (
21+
"github.com/apache/incubator-devlake/core/context"
22+
"github.com/apache/incubator-devlake/core/errors"
23+
"github.com/apache/incubator-devlake/core/models/migrationscripts/archived"
24+
"github.com/apache/incubator-devlake/core/plugin"
25+
"github.com/apache/incubator-devlake/helpers/migrationhelper"
26+
)
27+
28+
var _ plugin.MigrationScript = (*DecryptConnectionFields)(nil)
29+
30+
type azureDevopsConnection20230825 struct {
31+
archived.Model
32+
Name string
33+
Token string
34+
Proxy *string
35+
Organization *string
36+
}
37+
38+
type DecryptConnectionFields struct{}
39+
40+
func (script *DecryptConnectionFields) Up(basicRes context.BasicRes) errors.Error {
41+
encryptionSecret := basicRes.GetConfig(plugin.EncodeKeyEnvStr)
42+
if encryptionSecret == "" {
43+
return errors.BadInput.New("invalid encryptionSecret")
44+
}
45+
46+
err := migrationhelper.TransformColumns(
47+
basicRes,
48+
script,
49+
"_tool_azuredevops_azuredevopsconnections",
50+
[]string{"name", "proxy", "organization"},
51+
func(src *azureDevopsConnection20230825) (*azureDevopsConnection20230825, errors.Error) {
52+
encName := src.Name
53+
name, err := plugin.Decrypt(encryptionSecret, encName)
54+
if err != nil {
55+
return src, nil
56+
}
57+
src.Name = name
58+
59+
if src.Proxy != nil {
60+
encProxy := *src.Proxy
61+
decProxy, err := plugin.Decrypt(encryptionSecret, encProxy)
62+
if err != nil {
63+
return src, nil
64+
}
65+
if decProxy == "" {
66+
src.Proxy = nil
67+
} else {
68+
src.Proxy = &decProxy
69+
}
70+
}
71+
72+
if src.Organization != nil {
73+
encOrg := *src.Organization
74+
decOrg, err := plugin.Decrypt(encryptionSecret, encOrg)
75+
if err != nil {
76+
return src, nil
77+
}
78+
if decOrg == "" {
79+
src.Organization = nil
80+
} else {
81+
src.Organization = &decOrg
82+
}
83+
}
84+
return src, nil
85+
},
86+
)
87+
return err
88+
}
89+
90+
func (*DecryptConnectionFields) Version() uint64 {
91+
return 20230825090504
92+
}
93+
94+
func (script *DecryptConnectionFields) Name() string {
95+
return "Decrypt Azure DevOps connection fields"
96+
}

backend/server/services/remote/models/migrationscripts/register.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
var allMigrations = map[string][]plugin.MigrationScript{
2626
"azuredevops": {
2727
&azuredevops.AddRawDataForScope{},
28+
&azuredevops.DecryptConnectionFields{},
2829
},
2930
}
3031

0 commit comments

Comments
 (0)