Skip to content

Commit b35f89f

Browse files
committed
db: migrate org.go to orgs.go with GORM
1 parent 069f1ed commit b35f89f

File tree

19 files changed

+545
-469
lines changed

19 files changed

+545
-469
lines changed

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ updates:
44
- package-ecosystem: "gomod"
55
directory: "/"
66
schedule:
7-
interval: "weekly"
7+
interval: "monthly"
88
reviewers:
99
- "gogs/core"
1010
commit-message:

.github/workflows/go.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ jobs:
6161
name: Test
6262
strategy:
6363
matrix:
64-
go-version: [ 1.20.x, 1.21.x ]
64+
go-version: [ 1.21.x ]
6565
platform: [ ubuntu-latest, macos-latest ]
6666
runs-on: ${{ matrix.platform }}
6767
steps:
@@ -101,7 +101,7 @@ jobs:
101101
name: Test Windows
102102
strategy:
103103
matrix:
104-
go-version: [ 1.20.x, 1.21.x ]
104+
go-version: [ 1.21.x ]
105105
platform: [ windows-latest ]
106106
runs-on: ${{ matrix.platform }}
107107
steps:
@@ -139,7 +139,7 @@ jobs:
139139
name: Postgres
140140
strategy:
141141
matrix:
142-
go-version: [ 1.20.x, 1.21.x ]
142+
go-version: [ 1.21.x ]
143143
platform: [ ubuntu-latest ]
144144
runs-on: ${{ matrix.platform }}
145145
services:
@@ -175,7 +175,7 @@ jobs:
175175
name: MySQL
176176
strategy:
177177
matrix:
178-
go-version: [ 1.20.x, 1.21.x ]
178+
go-version: [ 1.21.x ]
179179
platform: [ ubuntu-20.04 ]
180180
runs-on: ${{ matrix.platform }}
181181
steps:
@@ -200,7 +200,7 @@ jobs:
200200
name: SQLite - Go
201201
strategy:
202202
matrix:
203-
go-version: [ 1.20.x, 1.21.x ]
203+
go-version: [ 1.21.x ]
204204
platform: [ ubuntu-latest ]
205205
runs-on: ${{ matrix.platform }}
206206
steps:

docs/dev/database_schema.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,22 @@ Primary keys: id
129129
Primary keys: id
130130
```
131131

132+
# Table "org_user"
133+
134+
```
135+
FIELD | COLUMN | POSTGRESQL | MYSQL | SQLITE3
136+
-----------+-----------+--------------------------------+--------------------------------+---------------------------------
137+
ID | id | BIGSERIAL | BIGINT AUTO_INCREMENT | INTEGER
138+
UserID | uid | BIGINT NOT NULL | BIGINT NOT NULL | INTEGER NOT NULL
139+
OrgID | org_id | BIGINT NOT NULL | BIGINT NOT NULL | INTEGER NOT NULL
140+
IsPublic | is_public | BOOLEAN NOT NULL DEFAULT FALSE | BOOLEAN NOT NULL DEFAULT FALSE | NUMERIC NOT NULL DEFAULT FALSE
141+
IsOwner | is_owner | BOOLEAN NOT NULL DEFAULT FALSE | BOOLEAN NOT NULL DEFAULT FALSE | NUMERIC NOT NULL DEFAULT FALSE
142+
NumTeams | num_teams | BIGINT NOT NULL DEFAULT 0 | BIGINT NOT NULL DEFAULT 0 | INTEGER NOT NULL DEFAULT 0
143+
144+
Primary keys: id
145+
Indexes:
146+
"idx_org_user_org_id" (org_id)
147+
"idx_org_user_user_id" (uid)
148+
"org_user_user_org_unique" UNIQUE (uid, org_id)
149+
```
150+

internal/context/org.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func HandleOrgAssignment(c *Context, args ...bool) {
7373
c.Org.IsMember = true
7474
c.Org.IsTeamMember = true
7575
c.Org.IsTeamAdmin = true
76-
} else if org.IsOrgMember(c.User.ID) {
76+
} else if db.Orgs.HasMember(c.Req.Context(), org.ID, c.User.ID) {
7777
c.Org.IsMember = true
7878
}
7979
} else {

internal/db/actions.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,20 @@ func (db *actions) listByOrganization(ctx context.Context, orgID, actorID, after
8686
/*
8787
Equivalent SQL for PostgreSQL:
8888
89-
SELECT * FROM "action"
89+
<SELECT * FROM "action">
9090
WHERE
9191
user_id = @userID
9292
AND (@skipAfter OR id < @afterID)
9393
AND repo_id IN (
9494
SELECT repository.id FROM "repository"
9595
JOIN team_repo ON repository.id = team_repo.repo_id
96-
WHERE team_repo.team_id IN (
96+
WHERE
97+
team_repo.team_id IN (
9798
SELECT team_id FROM "team_user"
98-
WHERE
99-
team_user.org_id = @orgID AND uid = @actorID)
100-
OR (repository.is_private = FALSE AND repository.is_unlisted = FALSE)
101-
)
99+
WHERE team_user.org_id = @orgID AND uid = @actorID)
100+
)
101+
OR (repository.is_private = FALSE AND repository.is_unlisted = FALSE)
102+
)
102103
ORDER BY id DESC
103104
LIMIT @limit
104105
*/
@@ -120,8 +121,8 @@ func (db *actions) listByOrganization(ctx context.Context, orgID, actorID, after
120121
).
121122
Or("repository.is_private = ? AND repository.is_unlisted = ?", false, false),
122123
).
123-
Limit(conf.UI.User.NewsFeedPagingNum).
124-
Order("id DESC")
124+
Order("id DESC").
125+
Limit(conf.UI.User.NewsFeedPagingNum)
125126
}
126127

127128
func (db *actions) ListByOrganization(ctx context.Context, orgID, actorID, afterID int64) ([]*Action, error) {
@@ -133,7 +134,7 @@ func (db *actions) listByUser(ctx context.Context, userID, actorID, afterID int6
133134
/*
134135
Equivalent SQL for PostgreSQL:
135136
136-
SELECT * FROM "action"
137+
<SELECT * FROM "action">
137138
WHERE
138139
user_id = @userID
139140
AND (@skipAfter OR id < @afterID)
@@ -153,8 +154,8 @@ func (db *actions) listByUser(ctx context.Context, userID, actorID, afterID int6
153154
Where("?", !isProfile || actorID == userID).
154155
Or("is_private = ? AND act_user_id = ?", false, userID),
155156
).
156-
Limit(conf.UI.User.NewsFeedPagingNum).
157-
Order("id DESC")
157+
Order("id DESC").
158+
Limit(conf.UI.User.NewsFeedPagingNum)
158159
}
159160

160161
func (db *actions) ListByUser(ctx context.Context, userID, actorID, afterID int64, isProfile bool) ([]*Action, error) {

internal/db/backup_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestDumpAndImport(t *testing.T) {
3131
}
3232
t.Parallel()
3333

34-
const wantTables = 8
34+
const wantTables = 9
3535
if len(Tables) != wantTables {
3636
t.Fatalf("New table has added (want %d got %d), please add new tests for the table and update this check", wantTables, len(Tables))
3737
}
@@ -197,6 +197,23 @@ func setupDBToDump(t *testing.T, db *gorm.DB) {
197197
Description: "This is a notice",
198198
CreatedUnix: 1588568886,
199199
},
200+
201+
&OrgUser{
202+
ID: 1,
203+
UserID: 1,
204+
OrgID: 11,
205+
IsPublic: true,
206+
IsOwner: true,
207+
NumTeams: 3,
208+
},
209+
&OrgUser{
210+
ID: 2,
211+
UserID: 2,
212+
OrgID: 11,
213+
IsPublic: false,
214+
IsOwner: false,
215+
NumTeams: 0,
216+
},
200217
}
201218
for _, val := range vals {
202219
err := db.Create(val).Error

internal/db/db.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var Tables = []any{
4646
new(Follow),
4747
new(LFSObject), new(LoginSource),
4848
new(Notice),
49+
new(OrgUser),
4950
}
5051

5152
// Init initializes the database with given logger.

internal/db/error.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -133,26 +133,6 @@ func (err ErrDeployKeyNameAlreadyUsed) Error() string {
133133
return fmt.Sprintf("public key already exists [repo_id: %d, name: %s]", err.RepoID, err.Name)
134134
}
135135

136-
// ________ .__ __ .__
137-
// \_____ \_______ _________ ____ |__|____________ _/ |_|__| ____ ____
138-
// / | \_ __ \/ ___\__ \ / \| \___ /\__ \\ __\ |/ _ \ / \
139-
// / | \ | \/ /_/ > __ \| | \ |/ / / __ \| | | ( <_> ) | \
140-
// \_______ /__| \___ (____ /___| /__/_____ \(____ /__| |__|\____/|___| /
141-
// \/ /_____/ \/ \/ \/ \/ \/
142-
143-
type ErrLastOrgOwner struct {
144-
UID int64
145-
}
146-
147-
func IsErrLastOrgOwner(err error) bool {
148-
_, ok := err.(ErrLastOrgOwner)
149-
return ok
150-
}
151-
152-
func (err ErrLastOrgOwner) Error() string {
153-
return fmt.Sprintf("user is the last member of owner team [uid: %d]", err.UID)
154-
}
155-
156136
// __________ .__ __
157137
// \______ \ ____ ______ ____ _____|__|/ |_ ___________ ___.__.
158138
// | _// __ \\____ \ / _ \/ ___/ \ __\/ _ \_ __ < | |

internal/db/models.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func init() {
5757
new(Label), new(IssueLabel), new(Milestone),
5858
new(Mirror), new(Release), new(Webhook), new(HookTask),
5959
new(ProtectBranch), new(ProtectBranchWhitelist),
60-
new(Team), new(OrgUser), new(TeamUser), new(TeamRepo),
60+
new(Team), new(TeamUser), new(TeamRepo),
6161
)
6262

6363
gonicNames := []string{"SSL"}

0 commit comments

Comments
 (0)