Skip to content

Commit

Permalink
removing unique index (#2705)
Browse files Browse the repository at this point in the history
* removing unique index

* ut fix

* adding comments
  • Loading branch information
k-anshul committed Jun 28, 2023
1 parent 733d8bd commit f00c2fe
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion runtime/drivers/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func testCatalog(t *testing.T, catalog drivers.CatalogStore) {

err = catalog.CreateEntry(ctx, instanceID, obj1)
require.Error(t, err)
require.True(t, strings.Contains(err.Error(), "duplicate key") ||
require.True(t, strings.Contains(err.Error(), "already exists") ||
strings.Contains(err.Error(), "Duplicate key") ||
strings.Contains(err.Error(), "UNIQUE constraint failed"))

Expand Down
11 changes: 11 additions & 0 deletions runtime/drivers/duckdb/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ func (c *connection) CreateEntry(ctx context.Context, instanceID string, e *driv
}
defer func() { _ = release() }()

var present bool
if err := conn.QueryRowContext(ctx, "SELECT COUNT(*) > 0 AS present FROM rill.catalog WHERE name = ?", e.Name).Scan(&present); err != nil {
return err
}

// adding a application side check instead of unique index bcz of duckdb limitations on indexes
// https://duckdb.org/docs/sql/indexes#over-eager-unique-constraint-checking
if present {
return fmt.Errorf("catalog entry with name %q already exists", e.Name)
}

// Serialize object
obj, err := proto.Marshal(e.Object)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions runtime/drivers/duckdb/migrations/0005.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS rill.lower_name_unique_idx;
7 changes: 7 additions & 0 deletions runtime/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ func TestRuntime_DeleteInstance(t *testing.T) {
Managed: true,
},
}))
require.ErrorContains(t, svc.Catalog.CreateEntry(ctx, "default", &drivers.CatalogEntry{
Name: "data",
Type: drivers.ObjectTypeModel,
Object: &runtimev1.Model{
Name: "data",
},
}), "catalog entry with name \"data\" already exists")

// delete instance
err = rt.DeleteInstance(ctx, tt.instanceID, tt.dropDB)
Expand Down

0 comments on commit f00c2fe

Please sign in to comment.