@@ -55,10 +55,15 @@ 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+
5862func (st * SchemaTable ) Schema () sql.Schema {
63+ currentSchema := toSqlSchemaTableSchema (GetSchemasSchema ())
5964 if st .backingTable == nil {
6065 // No backing table; return a current schema.
61- return SchemaTableSqlSchema () .Schema
66+ return currentSchema .Schema
6267 }
6368
6469 if ! st .backingTable .Schema ().Contains (doltdb .SchemasTablesExtraCol , doltdb .GetSchemasTableName ()) {
@@ -71,7 +76,7 @@ func (st *SchemaTable) Schema() sql.Schema {
7176 return SchemaTableV1SqlSchema ()
7277 }
7378
74- return SchemaTableSqlSchema () .Schema
79+ return currentSchema .Schema
7580}
7681
7782func (st * SchemaTable ) Collation () sql.CollationID {
@@ -127,14 +132,22 @@ var _ sql.IndexAddressableTable = (*SchemaTable)(nil)
127132var _ sql.UpdatableTable = (* SchemaTable )(nil )
128133var _ WritableDoltTableWrapper = (* SchemaTable )(nil )
129134
130- func SchemaTableSqlSchema ( ) sql.PrimaryKeySchema {
131- sqlSchema , err := sqlutil .FromDoltSchema ("" , doltdb .GetSchemasTableName (), SchemaTableSchema () )
135+ func toSqlSchemaTableSchema ( sch schema. Schema ) sql.PrimaryKeySchema {
136+ sqlSchema , err := sqlutil .FromDoltSchema ("" , doltdb .GetSchemasTableName (), sch )
132137 if err != nil {
133138 panic (err ) // should never happen
134139 }
135140 return sqlSchema
136141}
137142
143+ // func SchemaTableSqlSchema() sql.PrimaryKeySchema {
144+ // sqlSchema, err := sqlutil.FromDoltSchema("", doltdb.GetSchemasTableName(), SchemaTableSchema())
145+ // if err != nil {
146+ // panic(err) // should never happen
147+ // }
148+ // return sqlSchema
149+ // }
150+
138151func mustNewColWithTypeInfo (name string , tag uint64 , typeInfo typeinfo.TypeInfo , partOfPK bool , defaultVal string , autoIncrement bool , comment string , constraints ... schema.ColConstraint ) schema.Column {
139152 col , err := schema .NewColumnWithTypeInfo (name , tag , typeInfo , partOfPK , defaultVal , autoIncrement , comment , constraints ... )
140153 if err != nil {
@@ -250,7 +263,7 @@ func getOrCreateDoltSchemasTable(ctx *sql.Context, db Database) (retTbl *Writabl
250263 }
251264
252265 // Create new empty table
253- err = db .createDoltTable (ctx , tname , root , SchemaTableSchema ())
266+ err = db .createDoltTable (ctx , tname , root , GetSchemasSchema ())
254267 if err != nil {
255268 return nil , err
256269 }
@@ -367,8 +380,8 @@ func migrateOldSchemasTableToNew(ctx *sql.Context, db Database, schemasTable *Wr
367380}
368381
369382// 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 )
383+ func fragFromSchemasTable (ctx * sql.Context , tbl * WritableDoltTable , fragType , name , schemaName string ) (r sql.Row , found bool , rerr error ) {
384+ fragType , name , schemaName = strings .ToLower (fragType ), strings .ToLower (name ), strings . ToLower ( schemaName )
372385
373386 // This performs a full table scan in the worst case, but it's only used when adding or dropping a trigger or view
374387 iter , err := SqlTableToRowIter (ctx , tbl .DoltTable , nil )
@@ -387,6 +400,7 @@ func fragFromSchemasTable(ctx *sql.Context, tbl *WritableDoltTable, fragType str
387400 // need to get the column indexes from the current schema
388401 nameIdx := tbl .sqlSchema ().IndexOfColName (doltdb .SchemasTablesNameCol )
389402 typeIdx := tbl .sqlSchema ().IndexOfColName (doltdb .SchemasTablesTypeCol )
403+ schemaNameIdx := tbl .sqlSchema ().IndexOfColName (doltdb .SchemasTablesSchemaNameCol )
390404
391405 for {
392406 sqlRow , err := iter .Next (ctx )
@@ -397,8 +411,13 @@ func fragFromSchemasTable(ctx *sql.Context, tbl *WritableDoltTable, fragType str
397411 return nil , false , err
398412 }
399413
414+ sqlRowSchemaName := ""
415+ if schemaNameIdx >= 0 {
416+ sqlRowSchemaName = sqlRow [schemaNameIdx ].(string )
417+ }
418+
400419 // 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 ) {
420+ if strings .EqualFold (sqlRow [typeIdx ].(string ), fragType ) && strings .EqualFold (sqlRow [nameIdx ].(string ), name ) && strings . EqualFold ( sqlRowSchemaName , schemaName ) {
402421 return sqlRow , true , nil
403422 }
404423 }
@@ -407,9 +426,10 @@ func fragFromSchemasTable(ctx *sql.Context, tbl *WritableDoltTable, fragType str
407426}
408427
409428type schemaFragment struct {
410- name string
411- fragment string
412- created time.Time
429+ name string
430+ schemaName string
431+ fragment string
432+ created time.Time
413433 // sqlMode indicates the SQL_MODE that was used when this schema fragment was initially parsed. SQL_MODE settings
414434 // such as ANSI_QUOTES control customized parsing behavior needed for some schema fragments.
415435 sqlMode string
@@ -424,6 +444,7 @@ func getSchemaFragmentsOfType(ctx *sql.Context, tbl *WritableDoltTable, fragType
424444 // The dolt_schemas table has undergone various changes over time and multiple possible schemas for it exist, so we
425445 // need to get the column indexes from the current schema
426446 nameIdx := tbl .sqlSchema ().IndexOfColName (doltdb .SchemasTablesNameCol )
447+ schemaNameIdx := tbl .sqlSchema ().IndexOfColName (doltdb .SchemasTablesSchemaNameCol )
427448 typeIdx := tbl .sqlSchema ().IndexOfColName (doltdb .SchemasTablesTypeCol )
428449 fragmentIdx := tbl .sqlSchema ().IndexOfColName (doltdb .SchemasTablesFragmentCol )
429450 extraIdx := tbl .sqlSchema ().IndexOfColName (doltdb .SchemasTablesExtraCol )
@@ -463,13 +484,21 @@ func getSchemaFragmentsOfType(ctx *sql.Context, tbl *WritableDoltTable, fragType
463484 sqlModeString = defaultSqlMode
464485 }
465486
487+ schemaNameString := ""
488+ if schemaNameIdx >= 0 {
489+ if s , ok := sqlRow [schemaNameIdx ].(string ); ok {
490+ schemaNameString = s
491+ }
492+ }
493+
466494 // For older tables, use 1 as the trigger creation time
467495 if extraIdx < 0 || sqlRow [extraIdx ] == nil {
468496 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 ,
497+ name : sqlRow [nameIdx ].(string ),
498+ schemaName : schemaNameString ,
499+ fragment : sqlRow [fragmentIdx ].(string ),
500+ created : time .Unix (1 , 0 ).UTC (), // TablePlus editor thinks 0 is out of range
501+ sqlMode : sqlModeString ,
473502 })
474503 continue
475504 }
@@ -481,10 +510,11 @@ func getSchemaFragmentsOfType(ctx *sql.Context, tbl *WritableDoltTable, fragType
481510 }
482511
483512 frags = append (frags , schemaFragment {
484- name : sqlRow [nameIdx ].(string ),
485- fragment : sqlRow [fragmentIdx ].(string ),
486- created : time .Unix (createdTime , 0 ).UTC (),
487- sqlMode : sqlModeString ,
513+ name : sqlRow [nameIdx ].(string ),
514+ schemaName : schemaNameString ,
515+ fragment : sqlRow [fragmentIdx ].(string ),
516+ created : time .Unix (createdTime , 0 ).UTC (),
517+ sqlMode : sqlModeString ,
488518 })
489519 }
490520
0 commit comments