From ee1884a7844f366ab2f00ca6d84bc6bbfb30364e Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 12 Aug 2026 09:53:23 -0400 Subject: [PATCH] remove neo4j connection function. move schema creation function. --- neo4j.go | 75 ------------------------------------------------------- schema.go | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 75 deletions(-) delete mode 100644 neo4j.go diff --git a/neo4j.go b/neo4j.go deleted file mode 100644 index ccc8571..0000000 --- a/neo4j.go +++ /dev/null @@ -1,75 +0,0 @@ -package heartwood - -import ( - "context" - "github.com/neo4j/neo4j-go-driver/v6/neo4j" -) - -func ConnectNeo4j(ctx context.Context, uri, user, password string) (neo4j.Driver, error) { - driver, err := neo4j.NewDriver( - uri, - neo4j.BasicAuth(user, password, "")) - if err != nil { - return nil, err - } - - err = driver.VerifyConnectivity(ctx) - if err != nil { - return nil, err - } - - return driver, nil -} - -// SetNeo4jSchema ensures that the necessary indexes and constraints exist in -// the database -func SetNeo4jSchema(ctx context.Context, driver neo4j.Driver) error { - schemaQueries := []string{ - // Constraints - - // User.pubkey - `CREATE CONSTRAINT user IF NOT EXISTS - FOR (n:User) REQUIRE n.pubkey IS UNIQUE`, - - // Relay.url - `CREATE CONSTRAINT url IF NOT EXISTS - FOR (n:Relay) REQUIRE n.url IS UNIQUE`, - - // Event.id - `CREATE CONSTRAINT event IF NOT EXISTS - FOR (n:Event) REQUIRE n.id IS UNIQUE`, - - // Tag.(name, value) - `CREATE CONSTRAINT tag IF NOT EXISTS - FOR (n:Tag) REQUIRE (n.name, n.value) IS UNIQUE`, - - // ReplacementKey.(pubkey, kind) - `CREATE CONSTRAINT replacement_key IF NOT EXISTS - FOR (n:ReplacementKey) REQUIRE (n.pubkey, n.kind) IS UNIQUE`, - - // Indexes - - // Event.kind - `CREATE INDEX event_kind IF NOT EXISTS - FOR (n:Event) ON (n.kind)`, - - // Event.created_at - `CREATE INDEX event_created_at IF NOT EXISTS - FOR (n:Event) ON (n.created_at)`, - } - - // Create indexes and constraints - for _, query := range schemaQueries { - _, err := neo4j.ExecuteQuery(ctx, driver, - query, - nil, - neo4j.EagerResultTransformer, - neo4j.ExecuteQueryWithDatabase("neo4j")) - - if err != nil { - return err - } - } - - return nil -} diff --git a/schema.go b/schema.go index 2eb6b30..18d958a 100644 --- a/schema.go +++ b/schema.go @@ -129,3 +129,60 @@ func NewRelationshipWithValidation( return NewRelationship(rtype, start, end, props) } + +// ---------------------------------------------------------------------------- +// Indexes and Constraints +// ---------------------------------------------------------------------------- + +// SetNeo4jSchema ensures that the necessary indexes and constraints exist in +// the database +func SetNeo4jSchema(ctx context.Context, driver neo4j.Driver) error { + schemaQueries := []string{ + // Constraints + + // User.pubkey + `CREATE CONSTRAINT user IF NOT EXISTS + FOR (n:User) REQUIRE n.pubkey IS UNIQUE`, + + // Event.id + `CREATE CONSTRAINT event IF NOT EXISTS + FOR (n:Event) REQUIRE n.id IS UNIQUE`, + + // Tag.(name, value) + `CREATE CONSTRAINT tag IF NOT EXISTS + FOR (n:Tag) REQUIRE (n.name, n.value) IS UNIQUE`, + + // ReplacementKey.(pubkey, kind) + `CREATE CONSTRAINT replacement_key IF NOT EXISTS + FOR (n:ReplacementKey) REQUIRE (n.pubkey, n.kind) IS UNIQUE`, + + // Indexes + + // Event.kind + `CREATE INDEX event_kind IF NOT EXISTS + FOR (n:Event) ON (n.kind)`, + + // Event.created_at + `CREATE INDEX event_created_at IF NOT EXISTS + FOR (n:Event) ON (n.created_at)`, + + // ReplacementKey.kind + `CREATE INDEX replacement_key_kind IF NOT EXISTS + FOR (n:ReplacementKey) ON (n.kind)`, + } + + // Create indexes and constraints + for _, query := range schemaQueries { + _, err := neo4j.ExecuteQuery(ctx, driver, + query, + nil, + neo4j.EagerResultTransformer, + neo4j.ExecuteQueryWithDatabase("neo4j")) + + if err != nil { + return err + } + } + + return nil +}