-
Notifications
You must be signed in to change notification settings - Fork 1
Multi Schema Support #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
4301e53
ad95b7f
a23563b
99a2a36
4844e1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,13 +70,17 @@ func (gj *graphjinEngine) getIntroResult() (data json.RawMessage, err error) { | |
|
|
||
| // Initializes the database discovery process on graphjin | ||
| func (gj *graphjinEngine) initDiscover() (err error) { | ||
| switch gj.conf.DBType { | ||
| case "": | ||
| // Set the database type based on the configuration | ||
| gj.dbtype = gj.conf.Database.Type | ||
| if gj.dbtype == "" { | ||
| gj.dbtype = "postgres" | ||
| case "mssql": | ||
| gj.dbtype = "mysql" | ||
| } | ||
|
|
||
| switch gj.dbtype { | ||
| case "mysql", "postgres", "mssql": | ||
| // Valid database types, use as is | ||
| default: | ||
| gj.dbtype = gj.conf.DBType | ||
| gj.dbtype = "postgres" | ||
| } | ||
|
|
||
| if err = gj._initDiscover(); err != nil { | ||
|
|
@@ -117,6 +121,14 @@ func (gj *graphjinEngine) _initDiscover() (err error) { | |
| } | ||
| } | ||
|
|
||
| // Set the database type in the database info | ||
| if gj.dbinfo.Type == "" { | ||
| gj.dbinfo.Type = gj.conf.Database.Type | ||
| if gj.dbinfo.Type == "" { | ||
| gj.dbinfo.Type = "postgres" | ||
| } | ||
| } | ||
|
|
||
| if !gj.prod && gj.conf.EnableSchema { | ||
| var buf bytes.Buffer | ||
| if err := writeSchema(gj.dbinfo, &buf); err != nil { | ||
|
|
@@ -133,48 +145,87 @@ func (gj *graphjinEngine) _initDiscover() (err error) { | |
|
|
||
| // Initializes the database schema on graphjin | ||
| func (gj *graphjinEngine) initSchema() error { | ||
| gj.log.Printf("DEBUG: Initializing schema for database type: %s", gj.dbtype) | ||
| if gj.dbinfo == nil { | ||
| gj.log.Printf("WARNING: dbinfo is nil in initSchema") | ||
| } else { | ||
| gj.log.Printf("DEBUG: dbinfo has %d tables", len(gj.dbinfo.Tables)) | ||
| } | ||
|
|
||
| if err := gj._initSchema(); err != nil { | ||
| return fmt.Errorf("%s: %w", gj.dbtype, err) | ||
| } | ||
|
|
||
| if gj.schema == nil { | ||
| gj.log.Printf("WARNING: schema is still nil after _initSchema") | ||
| } else { | ||
| gj.log.Printf("DEBUG: Schema initialized with default schema: %s", gj.schema.DefaultSchema()) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // _initSchema initializes the database schema with proper error handling and validation | ||
| func (gj *graphjinEngine) _initSchema() (err error) { | ||
| // Validate database tables exist | ||
| if len(gj.dbinfo.Tables) == 0 { | ||
| return fmt.Errorf("no tables found in database") | ||
| } | ||
|
|
||
| schema := gj.dbinfo.Schema | ||
| for i, t := range gj.conf.Tables { | ||
| if t.Schema == "" { | ||
| gj.conf.Tables[i].Schema = schema | ||
| t.Schema = schema | ||
| } | ||
| // skip aliases | ||
| if t.Table != "" && t.Type == "" { | ||
| continue | ||
| } | ||
| if err = gj.addTableInfo(t); err != nil { | ||
| return | ||
| } | ||
| // Create a new DBSchema instance | ||
| schemaConfig := sdata.Config{ | ||
| DefaultSchema: gj.conf.Database.Schemas.Default, | ||
| AllowedSchemas: gj.conf.Database.Schemas.Allowed, | ||
| CrossSchemaSeparator: gj.conf.Database.Schemas.Separator, | ||
| } | ||
|
|
||
| if err = addTables(gj.conf, gj.dbinfo); err != nil { | ||
| return | ||
| // Set default values if not provided | ||
| if schemaConfig.CrossSchemaSeparator == "" { | ||
| schemaConfig.CrossSchemaSeparator = "Of" | ||
| } | ||
|
|
||
| if err = addForeignKeys(gj.conf, gj.dbinfo); err != nil { | ||
| return | ||
| gj.schema, err = sdata.NewDBSchema(gj.dbinfo, nil, schemaConfig) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Table aliases not passed to schema initializationThe |
||
| if err != nil { | ||
| return fmt.Errorf("failed to create database schema: %w", err) | ||
| } | ||
|
|
||
| gj.schema, err = sdata.NewDBSchema( | ||
| gj.dbinfo, | ||
| getDBTableAliases(gj.conf)) | ||
| if err != nil { | ||
| return | ||
| // Process table configurations | ||
| processTableConfigs := func() error { | ||
| schema := gj.dbinfo.Schema | ||
| for i, t := range gj.conf.Tables { | ||
| // Set default schema if not specified | ||
| if t.Schema == "" { | ||
| gj.conf.Tables[i].Schema = schema | ||
| t.Schema = schema | ||
| } | ||
|
|
||
| // Skip alias configurations | ||
| if t.Table != "" && t.Type == "" { | ||
| continue | ||
| } | ||
|
|
||
| // Add table info to the schema | ||
| if err := gj.addTableInfo(t); err != nil { | ||
| return fmt.Errorf("failed to add table info for %s: %w", t.Name, err) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| return | ||
| // Execute schema operations in sequence | ||
| operations := []func() error{ | ||
| processTableConfigs, | ||
| func() error { return addTables(gj.conf, gj.dbinfo) }, | ||
| func() error { return addForeignKeys(gj.conf, gj.dbinfo) }, | ||
| } | ||
|
|
||
| for _, op := range operations { | ||
| if err := op(); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (gj *graphjinEngine) initIntro() (err error) { | ||
|
|
@@ -194,14 +245,21 @@ func (gj *graphjinEngine) initIntro() (err error) { | |
|
|
||
| // Initializes the qcode compilers | ||
| func (gj *graphjinEngine) initCompilers() (err error) { | ||
| // Debug logging for schema initialization | ||
| if gj.schema == nil { | ||
| gj.log.Printf("WARNING: Schema is nil in initCompilers") | ||
| } else { | ||
| gj.log.Printf("DEBUG: Schema initialized with default schema: %s", gj.schema.DefaultSchema()) | ||
| } | ||
|
|
||
| qcc := qcode.Config{ | ||
| TConfig: gj.tmap, | ||
| DefaultBlock: gj.conf.DefaultBlock, | ||
| DefaultLimit: gj.conf.DefaultLimit, | ||
| DisableAgg: gj.conf.DisableAgg, | ||
| DisableFuncs: gj.conf.DisableFuncs, | ||
| EnableCamelcase: gj.conf.EnableCamelcase, | ||
| DBSchema: gj.schema.DBSchema(), | ||
| DBSchema: gj.schema.DefaultSchema(), | ||
| Validators: valid.Validators, | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Debug logging statements accidentally left in production code
Multiple
DEBUG:andWARNING:log statements have been left in the production code. The PR discussion confirms this was unintentional, with the author stating "that code isn't supposed to be in this branch.. i was playing with the library, learning a few new things". These debug statements will clutter production logs with internal implementation details.Additional Locations (2)
core/core.go#L158-L163core/core.go#L247-L253