mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-12 22:10:25 +00:00
Prevent access to "current" application models and services from migrations via `golangci` config: eg: ``` models/forgejo_migrations/v14a_ap-change-fedi-handle-structure.go:18:2: import 'forgejo.org/models/user' is not allowed from list 'migration-isolation': Migrations must not import application models. Application models will be the most recent schema for Forgejo, while migrations will be operating against the database schema that existed when they were authored. (depguard) user_model "forgejo.org/models/user" ^ models/forgejo_migrations/v14a_ap-change-fedi-handle-structure.go:21:2: import 'forgejo.org/services/user' is not allowed from list 'migration-isolation': Migrations must not import application services. Application services will reference application models which will use the most recent schema for Forgejo, while migrations will be operating against the database schema that existed when they were authored. (depguard) user_service "forgejo.org/services/user" ``` Fixes an existing migration issue where it isn't possible to add a new column to the `User` table ([test errors that occur](https://codeberg.org/forgejo/forgejo/actions/runs/148633/jobs/10/attempt/1#jobstep-5-323)), but also guarantees that future migrations don't stumble into the same issue by inadvertently referencing live application code from historical migrations. Originally identified and draft fix by @codecat w/ proposed fix in #11870. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. All work and communication must conform to Forgejo's [AI Agreement](https://codeberg.org/forgejo/governance/src/branch/main/AIAgreement.md). There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests for Go changes - I added test coverage for Go changes... - [ ] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I ran... - [x] `make pr-go` before pushing ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [x] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. Co-authored-by: Melissa Geels <melissa@nimble.tools> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11872 Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org> Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package forgejo_migrations
|
|
|
|
import (
|
|
"forgejo.org/modules/setting"
|
|
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
func init() {
|
|
registerMigration(&Migration{
|
|
Description: "remove columns and rework indexes for notification table",
|
|
Upgrade: reworkNotification,
|
|
})
|
|
}
|
|
|
|
func reworkNotification(x *xorm.Engine) error {
|
|
type NotificationStatus uint8
|
|
type Notification struct {
|
|
UserID int64 `xorm:"NOT NULL INDEX(s)"`
|
|
Status NotificationStatus `xorm:"SMALLINT NOT NULL INDEX(s)"`
|
|
}
|
|
|
|
if err := dropIndexIfExists(x, "notification", "IDX_notification_user_id"); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := dropIndexIfExists(x, "notification", "IDX_notification_created_unix"); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := dropIndexIfExists(x, "notification", "IDX_notification_updated_by"); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := dropIndexIfExists(x, "notification", "IDX_notification_commit_id"); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := dropIndexIfExists(x, "notification", "IDX_notification_status"); err != nil {
|
|
return err
|
|
}
|
|
|
|
switch {
|
|
case setting.Database.Type.IsSQLite3():
|
|
|
|
if _, err := x.Exec("ALTER TABLE `notification` DROP COLUMN `updated_by`"); err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err := x.Exec("ALTER TABLE `notification` DROP COLUMN `commit_id`"); err != nil {
|
|
return err
|
|
}
|
|
|
|
case setting.Database.Type.IsMySQL(), setting.Database.Type.IsPostgreSQL():
|
|
if _, err := x.Exec("ALTER TABLE `notification` DROP COLUMN `updated_by`, DROP COLUMN `commit_id`"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
_, err := x.SyncWithOptions(xorm.SyncOptions{IgnoreDropIndices: true}, new(Notification))
|
|
return err
|
|
}
|