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) (#15029)
Backport #15015
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]>
// CountBadSequences looks for broken sequences from recreate-table mistakes
303
+
funcCountBadSequences() (int64, error) {
304
+
if!setting.Database.UsePostgreSQL {
305
+
return0, nil
306
+
}
307
+
308
+
sess:=x.NewSession()
309
+
defersess.Close()
310
+
311
+
varsequences []string
312
+
schema:=sess.Engine().Dialect().URI().Schema
313
+
314
+
sess.Engine().SetSchema("")
315
+
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 {
316
+
return0, err
317
+
}
318
+
sess.Engine().SetSchema(schema)
319
+
320
+
returnint64(len(sequences)), nil
321
+
}
322
+
323
+
// FixBadSequences fixes for broken sequences from recreate-table mistakes
324
+
funcFixBadSequences() error {
325
+
if!setting.Database.UsePostgreSQL {
326
+
returnnil
327
+
}
328
+
329
+
sess:=x.NewSession()
330
+
defersess.Close()
331
+
iferr:=sess.Begin(); err!=nil {
332
+
returnerr
333
+
}
334
+
335
+
varsequences []string
336
+
schema:=sess.Engine().Dialect().URI().Schema
337
+
338
+
sess.Engine().SetSchema("")
339
+
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