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 <art27@cantab.net>
// CountBadSequences looks for broken sequences from recreate-table mistakes
funcCountBadSequences()(int64,error){
if!setting.Database.UsePostgreSQL{
return0,nil
}
sess:=x.NewSession()
defersess.Close()
varsequences[]string
schema:=sess.Engine().Dialect().URI().Schema
sess.Engine().SetSchema("")
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{
return0,err
}
sess.Engine().SetSchema(schema)
returnint64(len(sequences)),nil
}
// FixBadSequences fixes for broken sequences from recreate-table mistakes
funcFixBadSequences()error{
if!setting.Database.UsePostgreSQL{
returnnil
}
sess:=x.NewSession()
defersess.Close()
iferr:=sess.Begin();err!=nil{
returnerr
}
varsequences[]string
schema:=sess.Engine().Dialect().URI().Schema
sess.Engine().SetSchema("")
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{
iferr:=sess.Table("information_schema.sequences").Cols("sequence_name").Where("sequence_name LIKE ? || '_%' AND sequence_catalog = ?",tableName,setting.Database.Name).Find(&originalSequences);err!=nil{
log.Error("Unable to rename %s to %s. Error: %v",tempTableName,tableName,err)