@@ -55,10 +55,34 @@ func (st *SchemaTable) String() string {
5555 return doltdb .GetSchemasTableName ()
5656}
5757
58+ // GetSchemasSchema returns the schema of the dolt_schemas system table. This is used
59+ // by Doltgres to update the dolt_schemas schema with an additional schema_name column.
60+ var GetSchemasSchema = SchemaTableSchema
61+
62+ // func getDoltSchemasSchema(backingTable *WritableDoltTable) sql.Schema {
63+ // if backingTable == nil {
64+ // // No backing table; return a current schema.
65+ // return GetSchemasSchema().Schema
66+ // }
67+
68+ // if !backingTable.Schema().Contains(doltdb.SchemasTablesExtraCol, doltdb.GetSchemasTableName()) {
69+ // // No Extra column; return an ancient schema.
70+ // return SchemaTableAncientSqlSchema()
71+ // }
72+
73+ // if !backingTable.Schema().Contains(doltdb.SchemasTablesSqlModeCol, doltdb.GetSchemasTableName()) {
74+ // // No SQL_MODE column; return an old schema.
75+ // return SchemaTableV1SqlSchema()
76+ // }
77+
78+ // return GetSchemasSchema().Schema
79+ // }
80+
5881func (st * SchemaTable ) Schema () sql.Schema {
82+ currentSchema := toSqlSchemaTableSchema (GetSchemasSchema ())
5983 if st .backingTable == nil {
6084 // No backing table; return a current schema.
61- return SchemaTableSqlSchema () .Schema
85+ return currentSchema .Schema
6286 }
6387
6488 if ! st .backingTable .Schema ().Contains (doltdb .SchemasTablesExtraCol , doltdb .GetSchemasTableName ()) {
@@ -71,7 +95,7 @@ func (st *SchemaTable) Schema() sql.Schema {
7195 return SchemaTableV1SqlSchema ()
7296 }
7397
74- return SchemaTableSqlSchema () .Schema
98+ return currentSchema .Schema
7599}
76100
77101func (st * SchemaTable ) Collation () sql.CollationID {
@@ -127,14 +151,22 @@ var _ sql.IndexAddressableTable = (*SchemaTable)(nil)
127151var _ sql.UpdatableTable = (* SchemaTable )(nil )
128152var _ WritableDoltTableWrapper = (* SchemaTable )(nil )
129153
130- func SchemaTableSqlSchema ( ) sql.PrimaryKeySchema {
131- sqlSchema , err := sqlutil .FromDoltSchema ("" , doltdb .GetSchemasTableName (), SchemaTableSchema () )
154+ func toSqlSchemaTableSchema ( sch schema. Schema ) sql.PrimaryKeySchema {
155+ sqlSchema , err := sqlutil .FromDoltSchema ("" , doltdb .GetSchemasTableName (), sch )
132156 if err != nil {
133157 panic (err ) // should never happen
134158 }
135159 return sqlSchema
136160}
137161
162+ // func SchemaTableSqlSchema() sql.PrimaryKeySchema {
163+ // sqlSchema, err := sqlutil.FromDoltSchema("", doltdb.GetSchemasTableName(), SchemaTableSchema())
164+ // if err != nil {
165+ // panic(err) // should never happen
166+ // }
167+ // return sqlSchema
168+ // }
169+
138170func mustNewColWithTypeInfo (name string , tag uint64 , typeInfo typeinfo.TypeInfo , partOfPK bool , defaultVal string , autoIncrement bool , comment string , constraints ... schema.ColConstraint ) schema.Column {
139171 col , err := schema .NewColumnWithTypeInfo (name , tag , typeInfo , partOfPK , defaultVal , autoIncrement , comment , constraints ... )
140172 if err != nil {
@@ -250,7 +282,7 @@ func getOrCreateDoltSchemasTable(ctx *sql.Context, db Database) (retTbl *Writabl
250282 }
251283
252284 // Create new empty table
253- err = db .createDoltTable (ctx , tname , root , SchemaTableSchema ())
285+ err = db .createDoltTable (ctx , tname , root , GetSchemasSchema ())
254286 if err != nil {
255287 return nil , err
256288 }
@@ -367,8 +399,8 @@ func migrateOldSchemasTableToNew(ctx *sql.Context, db Database, schemasTable *Wr
367399}
368400
369401// fragFromSchemasTable returns the row with the given schema fragment if it exists.
370- func fragFromSchemasTable (ctx * sql.Context , tbl * WritableDoltTable , fragType string , name string ) (r sql.Row , found bool , rerr error ) {
371- fragType , name = strings .ToLower (fragType ), strings .ToLower (name )
402+ func fragFromSchemasTable (ctx * sql.Context , tbl * WritableDoltTable , fragType , name , schemaName string ) (r sql.Row , found bool , rerr error ) {
403+ fragType , name , schemaName = strings .ToLower (fragType ), strings .ToLower (name ), strings . ToLower ( schemaName )
372404
373405 // This performs a full table scan in the worst case, but it's only used when adding or dropping a trigger or view
374406 iter , err := SqlTableToRowIter (ctx , tbl .DoltTable , nil )
@@ -387,6 +419,7 @@ func fragFromSchemasTable(ctx *sql.Context, tbl *WritableDoltTable, fragType str
387419 // need to get the column indexes from the current schema
388420 nameIdx := tbl .sqlSchema ().IndexOfColName (doltdb .SchemasTablesNameCol )
389421 typeIdx := tbl .sqlSchema ().IndexOfColName (doltdb .SchemasTablesTypeCol )
422+ schemaNameIdx := tbl .sqlSchema ().IndexOfColName (doltdb .SchemasTablesSchemaNameCol )
390423
391424 for {
392425 sqlRow , err := iter .Next (ctx )
@@ -397,8 +430,13 @@ func fragFromSchemasTable(ctx *sql.Context, tbl *WritableDoltTable, fragType str
397430 return nil , false , err
398431 }
399432
433+ sqlRowSchemaName := ""
434+ if schemaNameIdx >= 0 {
435+ sqlRowSchemaName = sqlRow [schemaNameIdx ].(string )
436+ }
437+
400438 // These columns are case insensitive, make sure to do a case-insensitive comparison
401- if strings .EqualFold (sqlRow [typeIdx ].(string ), fragType ) && strings .EqualFold (sqlRow [nameIdx ].(string ), name ) {
439+ if strings .EqualFold (sqlRow [typeIdx ].(string ), fragType ) && strings .EqualFold (sqlRow [nameIdx ].(string ), name ) && strings . EqualFold ( sqlRowSchemaName , schemaName ) {
402440 return sqlRow , true , nil
403441 }
404442 }
@@ -407,9 +445,10 @@ func fragFromSchemasTable(ctx *sql.Context, tbl *WritableDoltTable, fragType str
407445}
408446
409447type schemaFragment struct {
410- name string
411- fragment string
412- created time.Time
448+ name string
449+ schemaName string
450+ fragment string
451+ created time.Time
413452 // sqlMode indicates the SQL_MODE that was used when this schema fragment was initially parsed. SQL_MODE settings
414453 // such as ANSI_QUOTES control customized parsing behavior needed for some schema fragments.
415454 sqlMode string
@@ -424,6 +463,7 @@ func getSchemaFragmentsOfType(ctx *sql.Context, tbl *WritableDoltTable, fragType
424463 // The dolt_schemas table has undergone various changes over time and multiple possible schemas for it exist, so we
425464 // need to get the column indexes from the current schema
426465 nameIdx := tbl .sqlSchema ().IndexOfColName (doltdb .SchemasTablesNameCol )
466+ schemaNameIdx := tbl .sqlSchema ().IndexOfColName (doltdb .SchemasTablesSchemaNameCol )
427467 typeIdx := tbl .sqlSchema ().IndexOfColName (doltdb .SchemasTablesTypeCol )
428468 fragmentIdx := tbl .sqlSchema ().IndexOfColName (doltdb .SchemasTablesFragmentCol )
429469 extraIdx := tbl .sqlSchema ().IndexOfColName (doltdb .SchemasTablesExtraCol )
@@ -463,13 +503,21 @@ func getSchemaFragmentsOfType(ctx *sql.Context, tbl *WritableDoltTable, fragType
463503 sqlModeString = defaultSqlMode
464504 }
465505
506+ schemaNameString := ""
507+ if schemaNameIdx >= 0 {
508+ if s , ok := sqlRow [schemaNameIdx ].(string ); ok {
509+ schemaNameString = s
510+ }
511+ }
512+
466513 // For older tables, use 1 as the trigger creation time
467514 if extraIdx < 0 || sqlRow [extraIdx ] == nil {
468515 frags = append (frags , schemaFragment {
469- name : sqlRow [nameIdx ].(string ),
470- fragment : sqlRow [fragmentIdx ].(string ),
471- created : time .Unix (1 , 0 ).UTC (), // TablePlus editor thinks 0 is out of range
472- sqlMode : sqlModeString ,
516+ name : sqlRow [nameIdx ].(string ),
517+ schemaName : schemaNameString ,
518+ fragment : sqlRow [fragmentIdx ].(string ),
519+ created : time .Unix (1 , 0 ).UTC (), // TablePlus editor thinks 0 is out of range
520+ sqlMode : sqlModeString ,
473521 })
474522 continue
475523 }
@@ -481,10 +529,11 @@ func getSchemaFragmentsOfType(ctx *sql.Context, tbl *WritableDoltTable, fragType
481529 }
482530
483531 frags = append (frags , schemaFragment {
484- name : sqlRow [nameIdx ].(string ),
485- fragment : sqlRow [fragmentIdx ].(string ),
486- created : time .Unix (createdTime , 0 ).UTC (),
487- sqlMode : sqlModeString ,
532+ name : sqlRow [nameIdx ].(string ),
533+ schemaName : schemaNameString ,
534+ fragment : sqlRow [fragmentIdx ].(string ),
535+ created : time .Unix (createdTime , 0 ).UTC (),
536+ sqlMode : sqlModeString ,
488537 })
489538 }
490539
0 commit comments