From 42ddbcc3a2d08272b62cef636b62bf439dd5c627 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 20 Aug 2020 07:52:46 -0700 Subject: [PATCH] SQL insert falls back to update fix https://github.com/chrislusf/seaweedfs/issues/1429 --- weed/filer2/abstract_sql/abstract_sql_store.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/weed/filer2/abstract_sql/abstract_sql_store.go b/weed/filer2/abstract_sql/abstract_sql_store.go index 5ade18960..957e40629 100644 --- a/weed/filer2/abstract_sql/abstract_sql_store.go +++ b/weed/filer2/abstract_sql/abstract_sql_store.go @@ -72,11 +72,25 @@ func (store *AbstractSqlStore) InsertEntry(ctx context.Context, entry *filer2.En return fmt.Errorf("insert %s: %s", entry.FullPath, err) } + affectedRows, err := res.RowsAffected() + if err == nil && affectedRows > 0{ + return nil + } + + // now the insert failed possibly due to duplication constraints + glog.V(1).Infof("insert %s falls back to update: %s", entry.FullPath, err) + + res, err = store.getTxOrDB(ctx).ExecContext(ctx, store.SqlUpdate, meta, util.HashStringToLong(dir), name, dir) + if err != nil { + return fmt.Errorf("upsert %s: %s", entry.FullPath, err) + } + _, err = res.RowsAffected() if err != nil { - return fmt.Errorf("insert %s but no rows affected: %s", entry.FullPath, err) + return fmt.Errorf("upsert %s but no rows affected: %s", entry.FullPath, err) } return nil + } func (store *AbstractSqlStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {