You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix postgres ID sequences broken by recreate-table (#15015)
* Fix postgres ID sequences broken by recreate-table
Unfortunately there is a subtle problem with recreatetable on postgres which
leads to the sequences not being renamed and not being left at 0.
Fix#14725
Signed-off-by: Andrew Thornton <[email protected]>
* let us try information_schema instead
Signed-off-by: Andrew Thornton <[email protected]>
* try again
Signed-off-by: Andrew Thornton <[email protected]>
Co-authored-by: techknowlogick <[email protected]>
Co-authored-by: 6543 <[email protected]>
// CountBadSequences looks for broken sequences from recreate-table mistakes
323
+
funcCountBadSequences() (int64, error) {
324
+
if!setting.Database.UsePostgreSQL {
325
+
return0, nil
326
+
}
327
+
328
+
sess:=x.NewSession()
329
+
defersess.Close()
330
+
331
+
varsequences []string
332
+
schema:=sess.Engine().Dialect().URI().Schema
333
+
334
+
sess.Engine().SetSchema("")
335
+
iferr:=sess.Table("information_schema.sequences").Cols("sequence_name").Where("sequence_name LIKE 'tmp_recreate__%_id_seq%' AND sequence_catalog = ?", setting.Database.Name).Find(&sequences); err!=nil {
336
+
return0, err
337
+
}
338
+
sess.Engine().SetSchema(schema)
339
+
340
+
returnint64(len(sequences)), nil
341
+
}
342
+
343
+
// FixBadSequences fixes for broken sequences from recreate-table mistakes
344
+
funcFixBadSequences() error {
345
+
if!setting.Database.UsePostgreSQL {
346
+
returnnil
347
+
}
348
+
349
+
sess:=x.NewSession()
350
+
defersess.Close()
351
+
iferr:=sess.Begin(); err!=nil {
352
+
returnerr
353
+
}
354
+
355
+
varsequences []string
356
+
schema:=sess.Engine().Dialect().URI().Schema
357
+
358
+
sess.Engine().SetSchema("")
359
+
iferr:=sess.Table("information_schema.sequences").Cols("sequence_name").Where("sequence_name LIKE 'tmp_recreate__%_id_seq%' AND sequence_catalog = ?", setting.Database.Name).Find(&sequences); err!=nil {
// Copyright 2021 The Gitea Authors. All rights reserved.
2
+
// Use of this source code is governed by a MIT-style
3
+
// license that can be found in the LICENSE file.
4
+
5
+
package migrations
6
+
7
+
import (
8
+
"fmt"
9
+
"regexp"
10
+
11
+
"code.gitea.io/gitea/modules/log"
12
+
"code.gitea.io/gitea/modules/setting"
13
+
"xorm.io/xorm"
14
+
)
15
+
16
+
funcfixPostgresIDSequences(x*xorm.Engine) error {
17
+
if!setting.Database.UsePostgreSQL {
18
+
returnnil
19
+
}
20
+
21
+
sess:=x.NewSession()
22
+
defersess.Close()
23
+
iferr:=sess.Begin(); err!=nil {
24
+
returnerr
25
+
}
26
+
27
+
varsequences []string
28
+
schema:=sess.Engine().Dialect().URI().Schema
29
+
30
+
sess.Engine().SetSchema("")
31
+
iferr:=sess.Table("information_schema.sequences").Cols("sequence_name").Where("sequence_name LIKE 'tmp_recreate__%_id_seq%' AND sequence_catalog = ?", setting.Database.Name).Find(&sequences); err!=nil {
0 commit comments