|
| 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 migrationscripts |
| 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/plugin" |
| 24 | +) |
| 25 | + |
| 26 | +var _ plugin.MigrationScript = (*addScopeIdFields)(nil) |
| 27 | + |
| 28 | +type addScopeIdFields struct{} |
| 29 | + |
| 30 | +func (*addScopeIdFields) Up(basicRes context.BasicRes) errors.Error { |
| 31 | + db := basicRes.GetDal() |
| 32 | + |
| 33 | + // Add scope_id column to _tool_q_dev_user_data table |
| 34 | + // This field links user data to QDevS3Slice scope, which can then be mapped to projects via project_mapping |
| 35 | + err := db.Exec(` |
| 36 | + ALTER TABLE _tool_q_dev_user_data |
| 37 | + ADD COLUMN IF NOT EXISTS scope_id VARCHAR(255) DEFAULT NULL |
| 38 | + `) |
| 39 | + if err != nil { |
| 40 | + // Try alternative syntax for databases that don't support IF NOT EXISTS |
| 41 | + _ = db.Exec(`ALTER TABLE _tool_q_dev_user_data ADD COLUMN scope_id VARCHAR(255) DEFAULT NULL`) |
| 42 | + } |
| 43 | + |
| 44 | + // Add index on scope_id for better query performance |
| 45 | + _ = db.Exec(`CREATE INDEX IF NOT EXISTS idx_q_dev_user_data_scope_id ON _tool_q_dev_user_data(scope_id)`) |
| 46 | + |
| 47 | + // Add scope_id column to _tool_q_dev_s3_file_meta table |
| 48 | + err = db.Exec(` |
| 49 | + ALTER TABLE _tool_q_dev_s3_file_meta |
| 50 | + ADD COLUMN IF NOT EXISTS scope_id VARCHAR(255) DEFAULT NULL |
| 51 | + `) |
| 52 | + if err != nil { |
| 53 | + _ = db.Exec(`ALTER TABLE _tool_q_dev_s3_file_meta ADD COLUMN scope_id VARCHAR(255) DEFAULT NULL`) |
| 54 | + } |
| 55 | + |
| 56 | + // Add index on scope_id |
| 57 | + _ = db.Exec(`CREATE INDEX IF NOT EXISTS idx_q_dev_s3_file_meta_scope_id ON _tool_q_dev_s3_file_meta(scope_id)`) |
| 58 | + |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func (*addScopeIdFields) Version() uint64 { |
| 63 | + return 20251209000001 |
| 64 | +} |
| 65 | + |
| 66 | +func (*addScopeIdFields) Name() string { |
| 67 | + return "add scope_id field to QDevUserData and QDevS3FileMeta for project association" |
| 68 | +} |
0 commit comments