Skip to content

Commit 04795c2

Browse files
authored
fix(q_dev): replace MariaDB-specific IF NOT EXISTS syntax with DAL methods for MySQL 8.x compatibility (#8745)
1 parent b7c190d commit 04795c2

2 files changed

Lines changed: 16 additions & 22 deletions

File tree

backend/plugins/q_dev/models/migrationscripts/20251209_add_scope_id_fields.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package migrationscripts
1919

2020
import (
2121
"github.com/apache/incubator-devlake/core/context"
22+
"github.com/apache/incubator-devlake/core/dal"
2223
"github.com/apache/incubator-devlake/core/errors"
2324
"github.com/apache/incubator-devlake/core/plugin"
2425
)
@@ -32,29 +33,24 @@ func (*addScopeIdFields) Up(basicRes context.BasicRes) errors.Error {
3233

3334
// Add scope_id column to _tool_q_dev_user_data table
3435
// 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`)
36+
if !db.HasColumn("_tool_q_dev_user_data", "scope_id") {
37+
if err := db.AddColumn("_tool_q_dev_user_data", "scope_id", dal.Varchar); err != nil {
38+
return errors.Default.Wrap(err, "failed to add scope_id to _tool_q_dev_user_data")
39+
}
4240
}
4341

4442
// 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)`)
43+
_ = db.Exec(`CREATE INDEX idx_q_dev_user_data_scope_id ON _tool_q_dev_user_data(scope_id)`)
4644

4745
// 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`)
46+
if !db.HasColumn("_tool_q_dev_s3_file_meta", "scope_id") {
47+
if err := db.AddColumn("_tool_q_dev_s3_file_meta", "scope_id", dal.Varchar); err != nil {
48+
return errors.Default.Wrap(err, "failed to add scope_id to _tool_q_dev_s3_file_meta")
49+
}
5450
}
5551

5652
// 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)`)
53+
_ = db.Exec(`CREATE INDEX idx_q_dev_s3_file_meta_scope_id ON _tool_q_dev_s3_file_meta(scope_id)`)
5854

5955
return nil
6056
}

backend/plugins/q_dev/models/migrationscripts/20260220_add_account_id_to_s3_slice.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package migrationscripts
1919

2020
import (
2121
"github.com/apache/incubator-devlake/core/context"
22+
"github.com/apache/incubator-devlake/core/dal"
2223
"github.com/apache/incubator-devlake/core/errors"
2324
"github.com/apache/incubator-devlake/core/plugin"
2425
)
@@ -30,13 +31,10 @@ type addAccountIdToS3Slice struct{}
3031
func (*addAccountIdToS3Slice) Up(basicRes context.BasicRes) errors.Error {
3132
db := basicRes.GetDal()
3233

33-
err := db.Exec(`
34-
ALTER TABLE _tool_q_dev_s3_slices
35-
ADD COLUMN IF NOT EXISTS account_id VARCHAR(255) DEFAULT NULL
36-
`)
37-
if err != nil {
38-
// Try alternative syntax for databases that don't support IF NOT EXISTS
39-
_ = db.Exec(`ALTER TABLE _tool_q_dev_s3_slices ADD COLUMN account_id VARCHAR(255) DEFAULT NULL`)
34+
if !db.HasColumn("_tool_q_dev_s3_slices", "account_id") {
35+
if err := db.AddColumn("_tool_q_dev_s3_slices", "account_id", dal.Varchar); err != nil {
36+
return errors.Default.Wrap(err, "failed to add account_id to _tool_q_dev_s3_slices")
37+
}
4038
}
4139

4240
return nil

0 commit comments

Comments
 (0)