Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion backend/helpers/migrationhelper/migrationhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,19 @@ func CopyTableColumns[S any, D any](
if err != nil {
return errors.Default.Wrap(err, fmt.Sprintf("failed to load data from src table [%s]", srcTableName))
}
defer cursor.Close()
if !reflect.ValueOf(cursor).IsNil() {
defer cursor.Close()
}

batch, err := helper.NewBatchSave(basicRes, reflect.TypeOf(new(D)), 200, dstTableName)
if err != nil {
return errors.Default.Wrap(err, fmt.Sprintf("failed to instantiate BatchSave for table [%s]", srcTableName))
}
defer batch.Close()

if reflect.ValueOf(cursor).IsNil() {
return nil
}
for cursor.Next() {
srcTable := new(S)
err1 := db.Fetch(cursor, srcTable)
Expand Down
19 changes: 19 additions & 0 deletions backend/impls/dalgorm/dalgorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,18 @@ func (d *Dalgorm) IsDuplicationError(err error) bool {
return strings.Contains(strings.ToLower(err.Error()), "duplicate")
}

// IsCachedPlanError checks if the error is related to postgres cached query plan
// This error occurs occasionally in Postgres when reusing a cached query
// plan. It can be safely ignored since it does not actually affect results.
func (d *Dalgorm) IsCachedPlanError(err error) bool {
return strings.Contains(strings.ToLower(err.Error()), "cached plan must not change result type")
}

// IsJsonOrderError checks if the error is related to postgres json ordering
func (d *Dalgorm) IsJsonOrderError(err error) bool {
return strings.Contains(err.Error(), "identify an ordering operator for type json")
}

// RawCursor (Deprecated) executes raw sql query and returns a database cursor
func (d *Dalgorm) RawCursor(query string, params ...interface{}) (*sql.Rows, errors.Error) {
rows, err := d.db.Raw(query, params...).Rows()
Expand All @@ -436,5 +448,12 @@ func (d *Dalgorm) convertGormError(err error) errors.Error {
if d.IsDuplicationError(err) {
return errors.BadInput.WrapRaw(err)
}
if d.IsJsonOrderError(err) {
return errors.BadInput.WrapRaw(err)
}
if d.IsCachedPlanError(err) {
return nil
}

panic(err)
}
17 changes: 9 additions & 8 deletions backend/impls/dalgorm/dalgorm_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,20 @@ func (t *DalgormTransaction) LockTables(lockTables dal.LockTables) errors.Error
}
return t.Exec(fmt.Sprintf("LOCK TABLES %s", clause))
case "postgres":
clause := ""
for _, lockTable := range lockTables {
if clause != "" {
clause += ", "
}
clause += lockTable.TableName()
var clause string
if lockTable.Exclusive {
clause += " IN EXCLUSIVE MODE"
clause = "EXCLUSIVE"
} else {
clause += " IN SHARE MODE"
clause = "SHARE"
}
stmt := fmt.Sprintf("LOCK TABLE %s IN %s MODE;", lockTable.TableName(), clause)
err := t.Exec(stmt)
if err != nil {
return err
}
}
return t.Exec(fmt.Sprintf("LOCK TABLE %s", clause))
return nil
default:
panic(fmt.Errorf("unknown dialect %s", t.Dialect()))
}
Expand Down