From c5e9ae0a980b4c39b01c4a918a8dc3ffb74c3fb9 Mon Sep 17 00:00:00 2001 From: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com> Date: Sat, 6 Jan 2024 00:20:00 +0000 Subject: [PATCH] Revert "WIP: We need to find a solution because CockroachDB doesn't support UDFs" This reverts commit e4b3fbdf26eab9409a4d3479006751536cefa87f. Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com> --- common/component/postgresql/v1/postgresql.go | 2 - .../postgresql/v1/postgresql_query.go | 1 - state/postgresql/v1/metadata.yaml | 1 - state/postgresql/v1/migrations.go | 45 +++++-------------- state/postgresql/v1/postgresql.go | 33 -------------- tests/config/state/tests.yml | 4 +- 6 files changed, 12 insertions(+), 74 deletions(-) diff --git a/common/component/postgresql/v1/postgresql.go b/common/component/postgresql/v1/postgresql.go index 7c55b43f4..141242c60 100644 --- a/common/component/postgresql/v1/postgresql.go +++ b/common/component/postgresql/v1/postgresql.go @@ -64,7 +64,6 @@ type Options struct { type MigrateOptions struct { Logger logger.Logger StateTableName string - KeyPrefixFuncName string MetadataTableName string } @@ -121,7 +120,6 @@ func (p *PostgreSQL) Init(ctx context.Context, meta state.Metadata) error { err = p.migrateFn(ctx, p.db, MigrateOptions{ Logger: p.logger, StateTableName: p.metadata.TableName, - KeyPrefixFuncName: p.metadata.TableName + "_key_prefix", MetadataTableName: p.metadata.MetadataTableName, }) if err != nil { diff --git a/common/component/postgresql/v1/postgresql_query.go b/common/component/postgresql/v1/postgresql_query.go index 3b551e6fe..bd3c63e35 100644 --- a/common/component/postgresql/v1/postgresql_query.go +++ b/common/component/postgresql/v1/postgresql_query.go @@ -54,7 +54,6 @@ func (p *PostgreSQLQuery) Features() []state.Feature { state.FeatureTransactional, state.FeatureQueryAPI, state.FeatureTTL, - state.FeatureDeleteWithPrefix, } } diff --git a/state/postgresql/v1/metadata.yaml b/state/postgresql/v1/metadata.yaml index b7dc5acda..fa3199e5c 100644 --- a/state/postgresql/v1/metadata.yaml +++ b/state/postgresql/v1/metadata.yaml @@ -16,7 +16,6 @@ capabilities: - etag - query - ttl - - deleteWithPrefix builtinAuthenticationProfiles: - name: "azuread" metadata: diff --git a/state/postgresql/v1/migrations.go b/state/postgresql/v1/migrations.go index 3f0631c85..b7034ab75 100644 --- a/state/postgresql/v1/migrations.go +++ b/state/postgresql/v1/migrations.go @@ -39,14 +39,14 @@ func performMigrations(ctx context.Context, db pginterfaces.PGXPoolConn, opts po opts.Logger.Infof("Creating state table '%s'", opts.StateTableName) _, err := db.Exec( ctx, - fmt.Sprintf(` -CREATE TABLE IF NOT EXISTS %s ( - key text NOT NULL PRIMARY KEY, - value jsonb NOT NULL, - isbinary boolean NOT NULL, - insertdate TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), - updatedate TIMESTAMP WITH TIME ZONE NULL -)`, + fmt.Sprintf( + `CREATE TABLE IF NOT EXISTS %s ( + key text NOT NULL PRIMARY KEY, + value jsonb NOT NULL, + isbinary boolean NOT NULL, + insertdate TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), + updatedate TIMESTAMP WITH TIME ZONE NULL + )`, opts.StateTableName, ), ) @@ -68,31 +68,6 @@ CREATE TABLE IF NOT EXISTS %s ( } return nil }, - // Migration 2: add the "key_prefix" function and "prefix" index to the state table - func(ctx context.Context) error { - // Create the "key_prefix" function - // Then add the "prefix" index to the state table that can be used by DeleteWithPrefix - opts.Logger.Infof("Creating function '%s' and adding 'prefix' index to table '%s'", opts.KeyPrefixFuncName, opts.StateTableName) - _, err := db.Exec( - ctx, - fmt.Sprintf(` -CREATE FUNCTION %[1]s(k text) RETURNS text -LANGUAGE SQL -IMMUTABLE -LEAKPROOF -RETURNS NULL ON NULL INPUT -RETURN - array_to_string(trim_array(string_to_array(k, '||'),1), '||'); - -CREATE INDEX %[2]s_prefix_idx ON %[2]s (%[1]s("key")) WHERE %[1]s("key") <> ''; -`, - opts.KeyPrefixFuncName, opts.StateTableName, - ), - ) - if err != nil { - return fmt.Errorf("failed to create virtual column: %w", err) - } - return nil - }, - }) + }, + ) } diff --git a/state/postgresql/v1/postgresql.go b/state/postgresql/v1/postgresql.go index 2917924d0..d8d2be92d 100644 --- a/state/postgresql/v1/postgresql.go +++ b/state/postgresql/v1/postgresql.go @@ -14,9 +14,6 @@ limitations under the License. package postgresql import ( - "context" - "strings" - postgresql "github.com/dapr/components-contrib/common/component/postgresql/v1" "github.com/dapr/components-contrib/state" "github.com/dapr/kit/logger" @@ -64,33 +61,3 @@ func NewPostgreSQLStateStore(logger logger.Logger) state.Store { }, }) } - -// PostgreSQLStoreWithDeleteWithPrefix is a state store for PostgreSQL that implements the DeleteWithPrefix method -type PostgreSQLStoreWithDeleteWithPrefix struct { - state.Store -} - -// Features returns the features available in this state store. -func (p *PostgreSQLStoreWithDeleteWithPrefix) Features() []state.Feature { - return append(p.Store.Features(), state.FeatureDeleteWithPrefix) -} - -func (p *PostgreSQLStoreWithDeleteWithPrefix) DeleteWithPrefix(ctx context.Context, req state.DeleteWithPrefixRequest) (state.DeleteWithPrefixResponse, error) { - err := req.Validate() - if err != nil { - return state.DeleteWithPrefixResponse{}, err - } - - ctx, cancel := context.WithTimeout(ctx, p.metadata.Timeout) - defer cancel() - - // Trim the trailing "||" from the prefix - result, err := p.db.Exec(ctx, "DELETE FROM "+p.metadata.TableName+" WHERE "+p.metadata.TableName+`_key_prefix("key") = $1`, strings.TrimSuffix(req.Prefix, "||")) - if err != nil { - return state.DeleteWithPrefixResponse{}, err - } - - return state.DeleteWithPrefixResponse{ - Count: result.RowsAffected(), - }, nil -} diff --git a/tests/config/state/tests.yml b/tests/config/state/tests.yml index 75848f545..b04ba6a14 100644 --- a/tests/config/state/tests.yml +++ b/tests/config/state/tests.yml @@ -35,12 +35,12 @@ components: # This component requires etags to be hex-encoded numbers badEtag: "FFFF" - component: postgresql.v1.docker - operations: [ "transaction", "etag", "first-write", "query", "ttl", "delete-with-prefix" ] + operations: [ "transaction", "etag", "first-write", "query", "ttl" ] config: # This component requires etags to be numeric badEtag: "1" - component: postgresql.v1.azure - operations: [ "transaction", "etag", "first-write", "query", "ttl", "delete-with-prefix" ] + operations: [ "transaction", "etag", "first-write", "query", "ttl" ] config: # This component requires etags to be numeric badEtag: "1"