Skip to content

Commit 7848c12

Browse files
authored
feat: allow specifying the graphdb database name (#235)
1 parent 16a2c5c commit 7848c12

File tree

5 files changed

+140
-98
lines changed

5 files changed

+140
-98
lines changed

pkg/externalfunctions/dataextraction.go

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -881,39 +881,51 @@ func StoreElementsInVectorDatabase(elements []sharedtypes.CodeGenerationElement,
881881
}
882882
}
883883

884+
func graphDbNameOrDefault(dbname string) string {
885+
if dbname == "" {
886+
logging.Log.Debugf(&logging.ContextMap{}, "No database name provided for aali-graphdb, falling back to default 'aali'")
887+
return "aali"
888+
} else {
889+
return dbname
890+
}
891+
}
892+
884893
// StoreElementsInGraphDatabase stores elements in the graph database.
885894
//
886895
// Tags:
887896
// - @displayName: Store Elements in Graph Database
888897
//
889898
// Parameters:
899+
// - dbname: the name of the graphdb to target. If not provided, defaults to "aali".
890900
// - elements: code generation elements.
891-
func StoreElementsInGraphDatabase(elements []sharedtypes.CodeGenerationElement) {
901+
func StoreElementsInGraphDatabase(dbname string, elements []sharedtypes.CodeGenerationElement) {
892902
ctx := &logging.ContextMap{}
893903

904+
dbname = graphDbNameOrDefault(dbname)
905+
894906
// Initialize the graph database.
895-
err := graphdb.Initialize(config.GlobalConfig.GRAPHDB_ADDRESS)
907+
err := graphdb.Initialize(config.GlobalConfig.GRAPHDB_ADDRESS, dbname)
896908
if err != nil {
897-
errMsg := fmt.Sprintf("error initializing graphdb: %v", err)
909+
errMsg := fmt.Sprintf("error initializing graphdb %q: %v", dbname, err)
898910
logging.Log.Error(ctx, errMsg)
899911
panic(errMsg)
900912
}
901913

902-
err = graphdb.GraphDbDriver.CreateSchema()
914+
err = graphdb.GraphDbDriver.CreateSchema(dbname)
903915
if err != nil {
904916
logPanic(ctx, "error error creating aali schema: %v", err)
905917
}
906918

907919
// Add the elements to the graph database.
908-
err = graphdb.GraphDbDriver.AddCodeGenerationElementNodes(elements)
920+
err = graphdb.GraphDbDriver.AddCodeGenerationElementNodes(dbname, elements)
909921
if err != nil {
910922
errMsg := fmt.Sprintf("error adding code gen element nodes to graphdb: %v", err)
911923
logging.Log.Error(ctx, errMsg)
912924
panic(errMsg)
913925
}
914926

915927
// Add the dependencies to the graph database.
916-
err = graphdb.GraphDbDriver.CreateCodeGenerationRelationships(elements)
928+
err = graphdb.GraphDbDriver.CreateCodeGenerationRelationships(dbname, elements)
917929
if err != nil {
918930
errMsg := fmt.Sprintf("error adding code gen relationships to graphdb: %v", err)
919931
logging.Log.Error(ctx, errMsg)
@@ -1317,33 +1329,36 @@ func StoreExamplesInVectorDatabase(examples []sharedtypes.CodeGenerationExample,
13171329
// - @displayName: Store Examples in Graph Database
13181330
//
13191331
// Parameters:
1332+
// - dbname: the name of the graphdb to target. If not provided, defaults to "aali".
13201333
// - examples: code generation examples.
1321-
func StoreExamplesInGraphDatabase(examples []sharedtypes.CodeGenerationExample) {
1334+
func StoreExamplesInGraphDatabase(dbname string, examples []sharedtypes.CodeGenerationExample) {
13221335
ctx := &logging.ContextMap{}
13231336

1337+
dbname = graphDbNameOrDefault(dbname)
1338+
13241339
// Initialize the graph database.
1325-
err := graphdb.Initialize(config.GlobalConfig.GRAPHDB_ADDRESS)
1340+
err := graphdb.Initialize(config.GlobalConfig.GRAPHDB_ADDRESS, dbname)
13261341
if err != nil {
1327-
errMsg := fmt.Sprintf("error initializing graphdb: %v", err)
1342+
errMsg := fmt.Sprintf("error initializing graphdb %q: %v", dbname, err)
13281343
logging.Log.Error(ctx, errMsg)
13291344
panic(errMsg)
13301345
}
13311346

1332-
err = graphdb.GraphDbDriver.CreateSchema()
1347+
err = graphdb.GraphDbDriver.CreateSchema(dbname)
13331348
if err != nil {
13341349
logPanic(ctx, "error error creating aali schema: %v", err)
13351350
}
13361351

13371352
// Add the elements to the graph database.
1338-
err = graphdb.GraphDbDriver.AddCodeGenerationExampleNodes(examples)
1353+
err = graphdb.GraphDbDriver.AddCodeGenerationExampleNodes(dbname, examples)
13391354
if err != nil {
13401355
errMsg := fmt.Sprintf("error adding code gen example nodes to graphdb: %v", err)
13411356
logging.Log.Error(ctx, errMsg)
13421357
panic(errMsg)
13431358
}
13441359

13451360
// Add the dependencies to the graph database.
1346-
err = graphdb.GraphDbDriver.CreateCodeGenerationExampleRelationships(examples)
1361+
err = graphdb.GraphDbDriver.CreateCodeGenerationExampleRelationships(dbname, examples)
13471362
if err != nil {
13481363
errMsg := fmt.Sprintf("error adding code gen example relationships to graphdb: %v", err)
13491364
logging.Log.Error(ctx, errMsg)
@@ -1593,34 +1608,36 @@ func StoreUserGuideSectionsInVectorDatabase(sections []sharedtypes.CodeGeneratio
15931608
// - @displayName: Store User Guide Sections in Graph Database
15941609
//
15951610
// Parameters:
1596-
// - elements: user guide sections.
1597-
// - label: label for the sections (UserGuide by default).
1598-
func StoreUserGuideSectionsInGraphDatabase(sections []sharedtypes.CodeGenerationUserGuideSection) {
1611+
// - dbname: the name of the graphdb to target. If not provided, defaults to "aali".
1612+
// - sections: user guide sections.
1613+
func StoreUserGuideSectionsInGraphDatabase(dbname string, sections []sharedtypes.CodeGenerationUserGuideSection) {
15991614
ctx := &logging.ContextMap{}
16001615

1616+
dbname = graphDbNameOrDefault(dbname)
1617+
16011618
// Initialize the graph database.
1602-
err := graphdb.Initialize(config.GlobalConfig.GRAPHDB_ADDRESS)
1619+
err := graphdb.Initialize(config.GlobalConfig.GRAPHDB_ADDRESS, dbname)
16031620
if err != nil {
16041621
errMsg := fmt.Sprintf("error initializing graphdb: %v", err)
16051622
logging.Log.Error(ctx, errMsg)
16061623
panic(errMsg)
16071624
}
16081625

1609-
err = graphdb.GraphDbDriver.CreateSchema()
1626+
err = graphdb.GraphDbDriver.CreateSchema(dbname)
16101627
if err != nil {
16111628
logPanic(ctx, "error error creating aali schema: %v", err)
16121629
}
16131630

16141631
// Add the elements to the graph database.
1615-
err = graphdb.GraphDbDriver.AddUserGuideSectionNodes(sections)
1632+
err = graphdb.GraphDbDriver.AddUserGuideSectionNodes(dbname, sections)
16161633
if err != nil {
16171634
errMsg := fmt.Sprintf("error adding user guide section nodes to graphdb: %v", err)
16181635
logging.Log.Error(ctx, errMsg)
16191636
panic(errMsg)
16201637
}
16211638

16221639
// Add the dependencies to the graph database.
1623-
err = graphdb.GraphDbDriver.CreateUserGuideSectionRelationships(sections)
1640+
err = graphdb.GraphDbDriver.CreateUserGuideSectionRelationships(dbname, sections)
16241641
if err != nil {
16251642
errMsg := fmt.Sprintf("error adding user guide section relationships to graphdb: %v", err)
16261643
logging.Log.Error(ctx, errMsg)

pkg/externalfunctions/dataextraction_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ func TestStoreElementsInGraphDatabase(t *testing.T) {
761761
logging.InitLogger(&setup.config)
762762

763763
// do some initial checks
764-
const DBNAME = "aali" // this is hardcoded in the graphdb driver for now
764+
const DBNAME = "aali"
765765
client, err := aali_graphdb.DefaultClient(setup.config.GRAPHDB_ADDRESS)
766766
require.NoError(err)
767767

@@ -797,7 +797,7 @@ func TestStoreElementsInGraphDatabase(t *testing.T) {
797797
EnumValues: nil,
798798
}
799799

800-
StoreElementsInGraphDatabase([]sharedtypes.CodeGenerationElement{element})
800+
StoreElementsInGraphDatabase(DBNAME, []sharedtypes.CodeGenerationElement{element})
801801

802802
// query graphdb to make sure things are as they should be
803803

@@ -862,7 +862,7 @@ func TestStoreExamplesInGraphDatabase(t *testing.T) {
862862
logging.InitLogger(&setup.config)
863863

864864
// do some initial checks
865-
const DBNAME = "aali" // this is hardcoded in the graphdb driver for now
865+
const DBNAME = "aali"
866866
client, err := aali_graphdb.DefaultClient(setup.config.GRAPHDB_ADDRESS)
867867
require.NoError(err)
868868

@@ -881,7 +881,7 @@ func TestStoreExamplesInGraphDatabase(t *testing.T) {
881881
},
882882
}
883883

884-
StoreExamplesInGraphDatabase(examples)
884+
StoreExamplesInGraphDatabase(DBNAME, examples)
885885

886886
// query graphdb to make sure things are as they should be
887887

@@ -933,7 +933,7 @@ func TestStoreUserGuideSectionsInGraphDatabase(t *testing.T) {
933933
logging.InitLogger(&setup.config)
934934

935935
// do some initial checks
936-
const DBNAME = "aali" // this is hardcoded in the graphdb driver for now
936+
const DBNAME = "aali"
937937
client, err := aali_graphdb.DefaultClient(setup.config.GRAPHDB_ADDRESS)
938938
require.NoError(err)
939939

@@ -993,7 +993,7 @@ func TestStoreUserGuideSectionsInGraphDatabase(t *testing.T) {
993993
},
994994
}
995995

996-
StoreUserGuideSectionsInGraphDatabase(sections)
996+
StoreUserGuideSectionsInGraphDatabase(DBNAME, sections)
997997

998998
// query graphdb to make sure things are as they should be
999999
type dbUserGuideSection struct {

pkg/externalfunctions/knowledgedb.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ func GetListCollections() (collectionsList []string) {
193193
// - @displayName: Retrieve Dependencies
194194
//
195195
// Parameters:
196+
// - dbname: the name of the graphdb to target. If not provided, defaults to "aali".
196197
// - relationshipName: the name of the relationship to retrieve dependencies for.
197198
// - relationshipDirection: the direction of the relationship to retrieve dependencies for.
198199
// - sourceDocumentId: the document ID of the source node.
@@ -202,14 +203,17 @@ func GetListCollections() (collectionsList []string) {
202203
// Returns:
203204
// - dependenciesIds: the list of dependencies
204205
func RetrieveDependencies(
206+
dbname string,
205207
relationshipName string,
206208
relationshipDirection string,
207209
sourceDocumentId string,
208210
nodeTypesFilter sharedtypes.DbArrayFilter,
209211
maxHopsNumber int) (dependenciesIds []string) {
210212
ctx := &logging.ContextMap{}
213+
dbname = graphDbNameOrDefault(dbname)
211214
dependenciesIds, err := graphdb.GraphDbDriver.RetrieveDependencies(
212215
ctx,
216+
dbname,
213217
relationshipName,
214218
relationshipDirection,
215219
sourceDocumentId,
@@ -254,18 +258,20 @@ func AddGraphDbParameter(parameters aali_graphdb.ParameterMap, name string, valu
254258
// - @displayName: General Graph DB Query
255259
//
256260
// Parameters:
261+
// - dbname: the name of the graphdb to target. If not provided, defaults to "aali".
257262
// - query: the Cypher query to be executed.
258263
// - parameters: parameters to pass to the query during execution
259264
//
260265
// Returns:
261266
// - databaseResponse: the graph db response
262-
func GeneralGraphDbQuery(query string, parameters aali_graphdb.ParameterMap) []map[string]any {
267+
func GeneralGraphDbQuery(dbname string, query string, parameters aali_graphdb.ParameterMap) []map[string]any {
263268
// Initialize the graph database.
264-
err := graphdb.Initialize(config.GlobalConfig.GRAPHDB_ADDRESS)
269+
dbname = graphDbNameOrDefault(dbname)
270+
err := graphdb.Initialize(config.GlobalConfig.GRAPHDB_ADDRESS, dbname)
265271
if err != nil {
266272
logPanic(nil, "error initializing graphdb: %v", err)
267273
}
268-
res, err := graphdb.GraphDbDriver.WriteCypherQuery(query, parameters)
274+
res, err := graphdb.GraphDbDriver.WriteCypherQuery(dbname, query, parameters)
269275
if err != nil {
270276
logPanic(nil, "error executing cypher query: %q", err)
271277
}

pkg/externalfunctions/knowledgedb_test.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ func TestRetrieveDependencies(t *testing.T) {
540540

541541
require := require.New(t)
542542
assert := assert.New(t)
543+
const DBNAME = "aali-test"
543544

544545
setup := setupFlowkitTestContainers(
545546
t,
@@ -554,8 +555,8 @@ func TestRetrieveDependencies(t *testing.T) {
554555
config.GlobalConfig = &setup.config
555556
logging.InitLogger(&setup.config)
556557

557-
require.NoError(graphdb.Initialize(setup.config.GRAPHDB_ADDRESS))
558-
require.NoError(graphdb.GraphDbDriver.CreateSchema())
558+
require.NoError(graphdb.Initialize(setup.config.GRAPHDB_ADDRESS, DBNAME))
559+
require.NoError(graphdb.GraphDbDriver.CreateSchema(DBNAME))
559560
driver := graphdb.GraphDbDriver
560561

561562
// first add data
@@ -575,19 +576,19 @@ func TestRetrieveDependencies(t *testing.T) {
575576
{Name: "4", Level: 0},
576577
{Name: "4a", IsFirstChild: true, Parent: "4", Level: 1},
577578
}
578-
require.NoError(driver.AddUserGuideSectionNodes(data))
579-
require.NoError(driver.CreateUserGuideSectionRelationships(data))
579+
require.NoError(driver.AddUserGuideSectionNodes(DBNAME, data))
580+
require.NoError(driver.CreateUserGuideSectionRelationships(DBNAME, data))
580581

581582
// now test deps
582583
// relationshipName := []string{"NextSibling", "NextParent", "HasFirstChild", "HasChild"}
583584
// relationshipDirection := []string{"in", "out", "both"}
584-
depIds := RetrieveDependencies("NextParent", "out", "3b", sharedtypes.DbArrayFilter{}, 2)
585+
depIds := RetrieveDependencies(DBNAME, "NextParent", "out", "3b", sharedtypes.DbArrayFilter{}, 2)
585586
assert.EqualValues([]string{"4"}, depIds)
586587

587-
depIds = RetrieveDependencies("NextSibling", "in", "3b", sharedtypes.DbArrayFilter{}, 2)
588+
depIds = RetrieveDependencies(DBNAME, "NextSibling", "in", "3b", sharedtypes.DbArrayFilter{}, 2)
588589
assert.EqualValues([]string{"3a"}, depIds)
589590

590-
depIds = RetrieveDependencies("HasChild", "both", "3b", sharedtypes.DbArrayFilter{}, 2)
591+
depIds = RetrieveDependencies(DBNAME, "HasChild", "both", "3b", sharedtypes.DbArrayFilter{}, 2)
591592
expected := []string{"3", "3b1", "3b2"}
592593
assert.Len(depIds, len(expected))
593594
for _, exp := range expected {
@@ -601,6 +602,7 @@ func TestGeneralGraphDbQuery(t *testing.T) {
601602

602603
require := require.New(t)
603604
assert := assert.New(t)
605+
const DBNAME = "test-db"
604606

605607
setup := setupFlowkitTestContainers(
606608
t,
@@ -615,8 +617,8 @@ func TestGeneralGraphDbQuery(t *testing.T) {
615617
config.GlobalConfig = &setup.config
616618
logging.InitLogger(&setup.config)
617619

618-
require.NoError(graphdb.Initialize(setup.config.GRAPHDB_ADDRESS))
619-
require.NoError(graphdb.GraphDbDriver.CreateSchema())
620+
require.NoError(graphdb.Initialize(setup.config.GRAPHDB_ADDRESS, DBNAME))
621+
require.NoError(graphdb.GraphDbDriver.CreateSchema(DBNAME))
620622
driver := graphdb.GraphDbDriver
621623

622624
// first add data
@@ -636,11 +638,11 @@ func TestGeneralGraphDbQuery(t *testing.T) {
636638
{Name: "4", Level: 0},
637639
{Name: "4a", IsFirstChild: true, Parent: "4", Level: 1},
638640
}
639-
require.NoError(driver.AddUserGuideSectionNodes(data))
640-
require.NoError(driver.CreateUserGuideSectionRelationships(data))
641+
require.NoError(driver.AddUserGuideSectionNodes(DBNAME, data))
642+
require.NoError(driver.CreateUserGuideSectionRelationships(DBNAME, data))
641643

642644
// now make a query
643-
res := GeneralGraphDbQuery("MATCH (n {parent:'1'}) RETURN n.name AS name", nil)
645+
res := GeneralGraphDbQuery(DBNAME, "MATCH (n {parent:'1'}) RETURN n.name AS name", nil)
644646
expected := []string{"1a", "1b"}
645647
require.Len(res, len(expected))
646648
for _, r := range res {
@@ -655,6 +657,7 @@ func TestGeneralGraphDbQueryWithParameters(t *testing.T) {
655657

656658
require := require.New(t)
657659
assert := assert.New(t)
660+
const DBNAME = "test-db-params"
658661

659662
setup := setupFlowkitTestContainers(
660663
t,
@@ -669,8 +672,8 @@ func TestGeneralGraphDbQueryWithParameters(t *testing.T) {
669672
config.GlobalConfig = &setup.config
670673
logging.InitLogger(&setup.config)
671674

672-
require.NoError(graphdb.Initialize(setup.config.GRAPHDB_ADDRESS))
673-
require.NoError(graphdb.GraphDbDriver.CreateSchema())
675+
require.NoError(graphdb.Initialize(setup.config.GRAPHDB_ADDRESS, DBNAME))
676+
require.NoError(graphdb.GraphDbDriver.CreateSchema(DBNAME))
674677
driver := graphdb.GraphDbDriver
675678

676679
// first add data
@@ -690,15 +693,15 @@ func TestGeneralGraphDbQueryWithParameters(t *testing.T) {
690693
{Name: "4", Level: 0},
691694
{Name: "4a", IsFirstChild: true, Parent: "4", Level: 1},
692695
}
693-
require.NoError(driver.AddUserGuideSectionNodes(data))
694-
require.NoError(driver.CreateUserGuideSectionRelationships(data))
696+
require.NoError(driver.AddUserGuideSectionNodes(DBNAME, data))
697+
require.NoError(driver.CreateUserGuideSectionRelationships(DBNAME, data))
695698

696699
// make a parameter map
697700
paramMap := aali_graphdb.ParameterMap{}
698701
AddGraphDbParameter(paramMap, "parent", "1", "string")
699702

700703
// now make a query
701-
res := GeneralGraphDbQuery("MATCH (n {parent:$parent}) RETURN n.name AS name", paramMap)
704+
res := GeneralGraphDbQuery(DBNAME, "MATCH (n {parent:$parent}) RETURN n.name AS name", paramMap)
702705
expected := []string{"1a", "1b"}
703706
require.Len(res, len(expected))
704707
for _, r := range res {

0 commit comments

Comments
 (0)