Skip to content

Commit d285905

Browse files
zeripath6543
andauthored
Update the webauthn_credential_id_sequence in Postgres (#19048) (#19060)
Backport #19048 There is (yet) another problem with v210 in that Postgres will silently allow preset ID insertions ... but it will not update the sequence value. This PR simply adds a little step to the end of the v210 migration to update the sequence number. Users who have already migrated who find that they cannot insert new webauthn_credentials into the DB can either run: ```bash gitea doctor recreate-table webauthn_credential ``` or ```bash SELECT setval('webauthn_credential_id_seq', COALESCE((SELECT MAX(id)+1 FROM `webauthn_credential`), 1), false) ``` which will fix the bad sequence. Fix #19012 Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent 4df2320 commit d285905

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

docs/content/doc/developers/guidelines-backend.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ To maintain understandable code and avoid circular dependencies it is important
4242
- `modules/setting`: Store all system configurations read from ini files and has been referenced by everywhere. But they should be used as function parameters when possible.
4343
- `modules/git`: Package to interactive with `Git` command line or Gogit package.
4444
- `public`: Compiled frontend files (javascript, images, css, etc.)
45-
- `routers`: Handling of server requests. As it uses other Gitea packages to serve the request, other packages (models, modules or services) shall not depend on routers.
45+
- `routers`: Handling of server requests. As it uses other Gitea packages to serve the request, other packages (models, modules or services) must not depend on routers.
4646
- `routers/api` Contains routers for `/api/v1` aims to handle RESTful API requests.
4747
- `routers/install` Could only respond when system is in INSTALL mode (INSTALL_LOCK=false).
4848
- `routers/private` will only be invoked by internal sub commands, especially `serv` and `hooks`.
@@ -106,10 +106,20 @@ i.e. `servcies/user`, `models/repository`.
106106
Since there are some packages which use the same package name, it is possible that you find packages like `modules/user`, `models/user`, and `services/user`. When these packages are imported in one Go file, it's difficult to know which package we are using and if it's a variable name or an import name. So, we always recommend to use import aliases. To differ from package variables which are commonly in camelCase, just use **snake_case** for import aliases.
107107
i.e. `import user_service "code.gitea.io/gitea/services/user"`
108108

109+
### Important Gotchas
110+
111+
- Never write `x.Update(exemplar)` without an explicit `WHERE` clause:
112+
- This will cause all rows in the table to be updated with the non-zero values of the exemplar - including IDs.
113+
- You should usually write `x.ID(id).Update(exemplar)`.
114+
- If during a migration you are inserting into a table using `x.Insert(exemplar)` where the ID is preset:
115+
- You will need to ``SET IDENTITY_INSERT `table` ON`` for the MSSQL variant (the migration will fail otherwise)
116+
- However, you will also need to update the id sequence for postgres - the migration will silently pass here but later insertions will fail:
117+
``SELECT setval('table_name_id_seq', COALESCE((SELECT MAX(id)+1 FROM `table_name`), 1), false)``
118+
109119
### Future Tasks
110120

111121
Currently, we are creating some refactors to do the following things:
112122

113123
- Correct that codes which doesn't follow the rules.
114124
- There are too many files in `models`, so we are moving some of them into a sub package `models/xxx`.
115-
- Some `modules` sub packages should be moved to `services` because they depends on `models`.
125+
- Some `modules` sub packages should be moved to `services` because they depend on `models`.

models/migrations/v210.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,11 @@ func remigrateU2FCredentials(x *xorm.Engine) error {
174174
regs = regs[:0]
175175
}
176176

177+
if x.Dialect().URI().DBType == schemas.POSTGRES {
178+
if _, err := x.Exec("SELECT setval('webauthn_credential_id_seq', COALESCE((SELECT MAX(id)+1 FROM `webauthn_credential`), 1), false)"); err != nil {
179+
return err
180+
}
181+
}
182+
177183
return nil
178184
}

0 commit comments

Comments
 (0)