Skip to content

Commit

Permalink
Merge pull request #7963 from dolthub/daylon/doltgres-sys-tables
Browse files Browse the repository at this point in the history
Create pg_catalog by default for Doltgres
  • Loading branch information
Hydrocharged authored Jun 14, 2024
2 parents 58ed430 + f6e434c commit abb243a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
20 changes: 17 additions & 3 deletions go/libraries/doltcore/sqle/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ func (db Database) Name() string {
return db.RequestedName()
}

// Schema returns the name of the schema that this database represents.
func (db Database) Schema() string {
return db.schemaName
}
Expand Down Expand Up @@ -770,7 +771,6 @@ func (db Database) tableInsensitive(ctx *sql.Context, root doltdb.RootValue, tab
return tname, tbl, true, nil
}

// newDoltTable returns a sql.Table wrapping the given underlying dolt table
func (db Database) newDoltTable(tableName string, sch schema.Schema, tbl *doltdb.Table) (sql.Table, error) {
readonlyTable, err := NewDoltTable(tableName, sch, tbl, db, db.editOpts)
if err != nil {
Expand Down Expand Up @@ -1279,7 +1279,11 @@ func (db Database) GetSchema(ctx *sql.Context, schemaName string) (sql.DatabaseS
for _, schema := range schemas {
if strings.EqualFold(schema.Name, schemaName) {
db.schemaName = schema.Name
return db, true, nil
handledSchema, err := HandleSchema(ctx, schemaName, db)
if err != nil {
return nil, false, err
}
return handledSchema, true, nil
}
}

Expand All @@ -1293,6 +1297,12 @@ func (db Database) GetSchema(ctx *sql.Context, schemaName string) (sql.DatabaseS
return nil, false, nil
}

// HandleSchema is used by Doltgres to intercept a database for the purposes of system tables. In Dolt, this just
// returns the given database.
var HandleSchema = func(ctx *sql.Context, schemaName string, db Database) (sql.DatabaseSchema, error) {
return db, nil
}

// AllSchemas implements sql.SchemaDatabase
func (db Database) AllSchemas(ctx *sql.Context) ([]sql.DatabaseSchema, error) {
if !resolve.UseSearchPath {
Expand All @@ -1314,7 +1324,11 @@ func (db Database) AllSchemas(ctx *sql.Context) ([]sql.DatabaseSchema, error) {
for i, schema := range schemas {
sdb := db
sdb.schemaName = schema.Name
dbSchemas[i] = sdb
handledDb, err := HandleSchema(ctx, schema.Name, sdb)
if err != nil {
return nil, err
}
dbSchemas[i] = handledDb
}

return dbSchemas, nil
Expand Down
9 changes: 8 additions & 1 deletion go/libraries/doltcore/sqle/database_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ func (p *DoltDatabaseProvider) CreateCollatedDatabase(ctx *sql.Context, name str
}
}

// If the search path is enabled, we need to create our initial schema object (public is available by default)
// If the search path is enabled, we need to create our initial schema object (public and pg_catalog are available
// by default)
if resolve.UseSearchPath {
workingRoot, err := newEnv.WorkingRoot(ctx)
if err != nil {
Expand All @@ -508,6 +509,12 @@ func (p *DoltDatabaseProvider) CreateCollatedDatabase(ctx *sql.Context, name str
if err != nil {
return err
}
workingRoot, err = workingRoot.CreateDatabaseSchema(ctx, schema.DatabaseSchema{
Name: "pg_catalog",
})
if err != nil {
return err
}

if err = newEnv.UpdateWorkingRoot(ctx, workingRoot); err != nil {
return err
Expand Down

0 comments on commit abb243a

Please sign in to comment.