MCPcopy Index your code
hub / github.com/dosco/graphjin / SchemaDiffMultiDB

Function SchemaDiffMultiDB

core/schema_diff.go:444–539  ·  view source on GitHub ↗

SchemaDiffMultiDB computes schema diffs across multiple databases. Tables are assigned to databases based on the @database directive in the schema. Every table must have a @database directive when multiple databases are configured.

(
	connections map[string]*sql.DB,
	dbTypes map[string]string,
	schemaBytes []byte,
	blocklist []string,
	opts DiffOptions,
)

Source from the content-addressed store, hash-verified

442// Tables are assigned to databases based on the @database directive in the schema.
443// Every table must have a @database directive when multiple databases are configured.
444func SchemaDiffMultiDB(
445 connections map[string]*sql.DB,
446 dbTypes map[string]string,
447 schemaBytes []byte,
448 blocklist []string,
449 opts DiffOptions,
450) (map[string][]SchemaOperation, error) {
451 // Parse the schema file
452 ds, err := qcode.ParseSchema(schemaBytes)
453 if err != nil {
454 return nil, fmt.Errorf("failed to parse schema: %w", err)
455 }
456
457 // Validate that all tables have a @database directive
458 missingTables := make(map[string]bool)
459 for _, col := range ds.Columns {
460 if col.Database == "" {
461 missingTables[col.Table] = true
462 }
463 }
464 if len(missingTables) > 0 {
465 // Collect missing table names sorted for deterministic output
466 names := make([]string, 0, len(missingTables))
467 for name := range missingTables {
468 names = append(names, name)
469 }
470 sort.Strings(names)
471
472 // Collect available database names sorted
473 dbNames := make([]string, 0, len(connections))
474 for name := range connections {
475 dbNames = append(dbNames, name)
476 }
477 sort.Strings(dbNames)
478
479 return nil, fmt.Errorf(
480 "tables missing @database directive: %s (available databases: %s). "+
481 "In multi-database mode, every table must have a @database(name: \"...\") directive",
482 strings.Join(names, ", "),
483 strings.Join(dbNames, ", "),
484 )
485 }
486
487 // Group columns by database (from @database directive)
488 columnsByDB := make(map[string][]sdata.DBColumn)
489
490 for _, col := range ds.Columns {
491 columnsByDB[col.Database] = append(columnsByDB[col.Database], col)
492 }
493
494 results := make(map[string][]SchemaOperation)
495
496 // Run diff for each database
497 for dbName, dbConn := range connections {
498 dbType, ok := dbTypes[dbName]
499 if !ok {
500 continue
501 }

Calls 4

SupportsSchemaDDLFunction · 0.85
defaultSchemaForDBTypeFunction · 0.85
attachClusteringKeysFunction · 0.85
computeDiffFunction · 0.85