| 1 | using AgentForge.Data; |
| 2 | using Microsoft.EntityFrameworkCore.Infrastructure; |
| 3 | using Microsoft.EntityFrameworkCore.Migrations; |
| 4 | |
| 5 | #nullable disable |
| 6 | |
| 7 | namespace AgentForge.Data.Migrations |
| 8 | { |
| 9 | [DbContext(typeof(ForgeDbContext))] |
| 10 | [Migration("20260614180000_AddOrgsAndVisibility")] |
| 11 | public partial class AddOrgsAndVisibility : Migration |
| 12 | { |
| 13 | /// <inheritdoc /> |
| 14 | protected override void Up(MigrationBuilder migrationBuilder) |
| 15 | { |
| 16 | migrationBuilder.CreateTable( |
| 17 | name: "orgs", |
| 18 | columns: table => new |
| 19 | { |
| 20 | Id = table.Column<string>(type: "TEXT", nullable: false), |
| 21 | CreatedAt = table.Column<string>(type: "TEXT", nullable: false), |
| 22 | DefaultVisibility = table.Column<string>(type: "TEXT", nullable: false), |
| 23 | Description = table.Column<string>(type: "TEXT", nullable: false), |
| 24 | DisplayName = table.Column<string>(type: "TEXT", nullable: false), |
| 25 | Slug = table.Column<string>(type: "TEXT", nullable: false), |
| 26 | }, |
| 27 | constraints: table => table.PrimaryKey("PK_orgs", x => x.Id)); |
| 28 | |
| 29 | migrationBuilder.CreateIndex( |
| 30 | name: "IX_orgs_Slug", |
| 31 | table: "orgs", |
| 32 | column: "Slug", |
| 33 | unique: true); |
| 34 | |
| 35 | migrationBuilder.CreateTable( |
| 36 | name: "org_members", |
| 37 | columns: table => new |
| 38 | { |
| 39 | Id = table.Column<string>(type: "TEXT", nullable: false), |
| 40 | CreatedAt = table.Column<string>(type: "TEXT", nullable: false), |
| 41 | IdentityId = table.Column<string>(type: "TEXT", nullable: false), |
| 42 | OrgId = table.Column<string>(type: "TEXT", nullable: false), |
| 43 | Role = table.Column<string>(type: "TEXT", nullable: false), |
| 44 | }, |
| 45 | constraints: table => |
| 46 | { |
| 47 | table.PrimaryKey("PK_org_members", x => x.Id); |
| 48 | table.ForeignKey( |
| 49 | name: "FK_org_members_identities_IdentityId", |
| 50 | column: x => x.IdentityId, |
| 51 | principalTable: "identities", |
| 52 | principalColumn: "Id", |
| 53 | onDelete: ReferentialAction.Cascade); |
| 54 | table.ForeignKey( |
| 55 | name: "FK_org_members_orgs_OrgId", |
| 56 | column: x => x.OrgId, |
| 57 | principalTable: "orgs", |
| 58 | principalColumn: "Id", |
| 59 | onDelete: ReferentialAction.Cascade); |
| 60 | }); |
| 61 | |
| 62 | migrationBuilder.CreateIndex( |
| 63 | name: "IX_org_members_IdentityId", |
| 64 | table: "org_members", |
| 65 | column: "IdentityId"); |
| 66 | |
| 67 | migrationBuilder.CreateIndex( |
| 68 | name: "IX_org_members_OrgId_IdentityId", |
| 69 | table: "org_members", |
| 70 | columns: ["OrgId", "IdentityId"], |
| 71 | unique: true); |
| 72 | |
| 73 | migrationBuilder.AddColumn<string>( |
| 74 | name: "OrgId", |
| 75 | table: "repos", |
| 76 | type: "TEXT", |
| 77 | nullable: true); |
| 78 | |
| 79 | migrationBuilder.AddColumn<string>( |
| 80 | name: "Visibility", |
| 81 | table: "repos", |
| 82 | type: "TEXT", |
| 83 | nullable: false, |
| 84 | defaultValue: "public"); |
| 85 | |
| 86 | migrationBuilder.AddColumn<string>( |
| 87 | name: "DefaultVisibility", |
| 88 | table: "repo_groups", |
| 89 | type: "TEXT", |
| 90 | nullable: true); |
| 91 | |
| 92 | migrationBuilder.AddColumn<string>( |
| 93 | name: "OrgId", |
| 94 | table: "repo_groups", |
| 95 | type: "TEXT", |
| 96 | nullable: true); |
| 97 | |
| 98 | migrationBuilder.Sql( |
| 99 | """ |
| 100 | INSERT INTO orgs (Id, Slug, DisplayName, Description, DefaultVisibility, CreatedAt) |
| 101 | SELECT 'legacy-org', '_legacy', 'Legacy', 'Pre-org migration bucket', 'public', datetime('now') |
| 102 | WHERE EXISTS (SELECT 1 FROM repo_groups) |
| 103 | AND NOT EXISTS (SELECT 1 FROM orgs WHERE Id = 'legacy-org'); |
| 104 | """); |
| 105 | |
| 106 | migrationBuilder.Sql( |
| 107 | """ |
| 108 | UPDATE repo_groups SET OrgId = 'legacy-org' |
| 109 | WHERE OrgId IS NULL AND EXISTS (SELECT 1 FROM orgs WHERE Id = 'legacy-org'); |
| 110 | """); |
| 111 | |
| 112 | migrationBuilder.CreateIndex( |
| 113 | name: "IX_repos_OrgId", |
| 114 | table: "repos", |
| 115 | column: "OrgId"); |
| 116 | |
| 117 | migrationBuilder.CreateIndex( |
| 118 | name: "IX_repo_groups_OrgId", |
| 119 | table: "repo_groups", |
| 120 | column: "OrgId"); |
| 121 | |
| 122 | migrationBuilder.DropIndex( |
| 123 | name: "IX_repo_groups_Slug", |
| 124 | table: "repo_groups"); |
| 125 | |
| 126 | migrationBuilder.CreateIndex( |
| 127 | name: "IX_repo_groups_OrgId_Slug", |
| 128 | table: "repo_groups", |
| 129 | columns: ["OrgId", "Slug"], |
| 130 | unique: true); |
| 131 | |
| 132 | migrationBuilder.AddForeignKey( |
| 133 | name: "FK_repos_orgs_OrgId", |
| 134 | table: "repos", |
| 135 | column: "OrgId", |
| 136 | principalTable: "orgs", |
| 137 | principalColumn: "Id"); |
| 138 | |
| 139 | migrationBuilder.AddForeignKey( |
| 140 | name: "FK_repo_groups_orgs_OrgId", |
| 141 | table: "repo_groups", |
| 142 | column: "OrgId", |
| 143 | principalTable: "orgs", |
| 144 | principalColumn: "Id"); |
| 145 | } |
| 146 | |
| 147 | /// <inheritdoc /> |
| 148 | protected override void Down(MigrationBuilder migrationBuilder) |
| 149 | { |
| 150 | migrationBuilder.DropForeignKey( |
| 151 | name: "FK_repos_orgs_OrgId", |
| 152 | table: "repos"); |
| 153 | |
| 154 | migrationBuilder.DropForeignKey( |
| 155 | name: "FK_repo_groups_orgs_OrgId", |
| 156 | table: "repo_groups"); |
| 157 | |
| 158 | migrationBuilder.DropIndex( |
| 159 | name: "IX_repos_OrgId", |
| 160 | table: "repos"); |
| 161 | |
| 162 | migrationBuilder.DropIndex( |
| 163 | name: "IX_repo_groups_OrgId", |
| 164 | table: "repo_groups"); |
| 165 | |
| 166 | migrationBuilder.DropIndex( |
| 167 | name: "IX_repo_groups_OrgId_Slug", |
| 168 | table: "repo_groups"); |
| 169 | |
| 170 | migrationBuilder.CreateIndex( |
| 171 | name: "IX_repo_groups_Slug", |
| 172 | table: "repo_groups", |
| 173 | column: "Slug", |
| 174 | unique: true); |
| 175 | |
| 176 | migrationBuilder.DropColumn( |
| 177 | name: "OrgId", |
| 178 | table: "repos"); |
| 179 | |
| 180 | migrationBuilder.DropColumn( |
| 181 | name: "Visibility", |
| 182 | table: "repos"); |
| 183 | |
| 184 | migrationBuilder.DropColumn( |
| 185 | name: "DefaultVisibility", |
| 186 | table: "repo_groups"); |
| 187 | |
| 188 | migrationBuilder.DropColumn( |
| 189 | name: "OrgId", |
| 190 | table: "repo_groups"); |
| 191 | |
| 192 | migrationBuilder.DropTable( |
| 193 | name: "org_members"); |
| 194 | |
| 195 | migrationBuilder.DropTable( |
| 196 | name: "orgs"); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |