|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package v1_25 |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "code.gitea.io/gitea/models/migrations/base" |
| 10 | + "code.gitea.io/gitea/modules/setting" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "xorm.io/xorm" |
| 14 | + "xorm.io/xorm/schemas" |
| 15 | +) |
| 16 | + |
| 17 | +func prepareOldProtectedBranch(t *testing.T) (*xorm.Engine, func()) { |
| 18 | + type ProtectedBranch struct { |
| 19 | + ID int64 `xorm:"pk autoincr"` |
| 20 | + RepoID int64 `xorm:"UNIQUE(s)"` |
| 21 | + RuleName string `xorm:"'branch_name' UNIQUE(s)"` |
| 22 | + } |
| 23 | + |
| 24 | + return base.PrepareTestEnv(t, 0, new(ProtectedBranch)) |
| 25 | +} |
| 26 | + |
| 27 | +func Test_AddOwnerIDToProtectedBranch(t *testing.T) { |
| 28 | + x, deferable := prepareOldProtectedBranch(t) |
| 29 | + defer deferable() |
| 30 | + if x == nil { |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + // The test fixtures will have already created the UQE_protected_branch_s index. |
| 35 | + // We can run the migration. |
| 36 | + assert.NoError(t, AddOwnerIDToProtectedBranch(x)) |
| 37 | + |
| 38 | + // Verify that the new column exists. |
| 39 | + type ProtectedBranch struct { |
| 40 | + ID int64 `xorm:"pk autoincr"` |
| 41 | + RepoID int64 `xorm:"INDEX DEFAULT 0"` |
| 42 | + OwnerID int64 `xorm:"INDEX DEFAULT 0"` |
| 43 | + RuleName string |
| 44 | + } |
| 45 | + |
| 46 | + has, err := x.Dialect().IsColumnExist(x.DB(), t.Context(), "protected_branch", "owner_id") |
| 47 | + assert.NoError(t, err) |
| 48 | + assert.True(t, has, "owner_id column should exist") |
| 49 | + |
| 50 | + // Skip index check for unsupported DBs |
| 51 | + if setting.Database.Type.IsMSSQL() || setting.Database.Type.IsSQLite3() { |
| 52 | + t.Log("Skipping index check for unsupported database") |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + table, err := x.TableInfo("protected_branch") |
| 57 | + assert.NoError(t, err) |
| 58 | + |
| 59 | + // Check if the old index is gone |
| 60 | + _, exist := table.Indexes["UQE_protected_branch_s"] |
| 61 | + assert.False(t, exist, "old index UQE_protected_branch_s should not exist") |
| 62 | + |
| 63 | + // Check if new indexes are created |
| 64 | + idx, exist := table.Indexes["UQE_protected_branch_repo_id_branch_name"] |
| 65 | + assert.True(t, exist, "new index UQE_protected_branch_repo_id_branch_name should exist") |
| 66 | + if exist { |
| 67 | + assert.Equal(t, schemas.UniqueType, idx.Type) |
| 68 | + assert.ElementsMatch(t, []string{"repo_id", "branch_name"}, idx.Cols) |
| 69 | + } |
| 70 | + |
| 71 | + idx, exist = table.Indexes["UQE_protected_branch_owner_id_branch_name"] |
| 72 | + assert.True(t, exist, "new index UQE_protected_branch_owner_id_branch_name should exist") |
| 73 | + if exist { |
| 74 | + assert.Equal(t, schemas.UniqueType, idx.Type) |
| 75 | + assert.ElementsMatch(t, []string{"owner_id", "branch_name"}, idx.Cols) |
| 76 | + } |
| 77 | + |
| 78 | + // Test repo-level unique constraint |
| 79 | + _, err = x.Insert(&ProtectedBranch{RepoID: 1, RuleName: "main"}) |
| 80 | + assert.NoError(t, err) |
| 81 | + _, err = x.Insert(&ProtectedBranch{RepoID: 1, RuleName: "main"}) |
| 82 | + assert.Error(t, err, "should fail to insert duplicate repo-level rule") |
| 83 | + |
| 84 | + // Test org-level unique constraint |
| 85 | + _, err = x.Insert(&ProtectedBranch{OwnerID: 1, RuleName: "main"}) |
| 86 | + assert.NoError(t, err) |
| 87 | + _, err = x.Insert(&ProtectedBranch{OwnerID: 1, RuleName: "main"}) |
| 88 | + assert.Error(t, err, "should fail to insert duplicate org-level rule") |
| 89 | + |
| 90 | + // Test that repo-level and org-level rules with the same name don't conflict |
| 91 | + _, err = x.Insert(&ProtectedBranch{RepoID: 2, RuleName: "develop"}) |
| 92 | + assert.NoError(t, err) |
| 93 | + _, err = x.Insert(&ProtectedBranch{OwnerID: 2, RuleName: "develop"}) |
| 94 | + assert.NoError(t, err) |
| 95 | + |
| 96 | + // Test that rules with repo_id=0 or owner_id=0 don't conflict with partial indexes |
| 97 | + _, err = x.Insert(&ProtectedBranch{RepoID: 3, OwnerID: 0, RuleName: "feature-a"}) |
| 98 | + assert.NoError(t, err) |
| 99 | + _, err = x.Insert(&ProtectedBranch{RepoID: 4, OwnerID: 0, RuleName: "feature-a"}) |
| 100 | + assert.NoError(t, err) |
| 101 | + |
| 102 | + _, err = x.Insert(&ProtectedBranch{RepoID: 0, OwnerID: 3, RuleName: "feature-b"}) |
| 103 | + assert.NoError(t, err) |
| 104 | + _, err = x.Insert(&ProtectedBranch{RepoID: 0, OwnerID: 4, RuleName: "feature-b"}) |
| 105 | + assert.NoError(t, err) |
| 106 | +} |
0 commit comments