Skip to content

Commit

Permalink
Replace AllDatabasesWithNames so that revision dbs show up in informa…
Browse files Browse the repository at this point in the history
…tion_schema tables
  • Loading branch information
tbantle22 committed Sep 4, 2024
1 parent 9d73913 commit 5355443
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 5 deletions.
2 changes: 1 addition & 1 deletion server/tables/information_schema/columns_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ var columnsSchema = sql.Schema{
func columnsRowIter(ctx *sql.Context, catalog sql.Catalog, allColsWithDefaultValue sql.Schema) (sql.RowIter, error) {
var rows []sql.Row

databases, err := information_schema.AllDatabases(ctx, catalog, false)
databases, err := information_schema.AllDatabasesWithNames(ctx, catalog, false)

Check failure on line 105 in server/tables/information_schema/columns_table.go

View workflow job for this annotation

GitHub Actions / Run Staticcheck

undefined: information_schema.AllDatabasesWithNames

Check failure on line 105 in server/tables/information_schema/columns_table.go

View workflow job for this annotation

GitHub Actions / Verify format

undefined: information_schema.AllDatabasesWithNames

Check failure on line 105 in server/tables/information_schema/columns_table.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

undefined: information_schema.AllDatabasesWithNames

Check failure on line 105 in server/tables/information_schema/columns_table.go

View workflow job for this annotation

GitHub Actions / Bats tests (ubuntu-22.04)

undefined: information_schema.AllDatabasesWithNames

Check failure on line 105 in server/tables/information_schema/columns_table.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

undefined: information_schema.AllDatabasesWithNames

Check failure on line 105 in server/tables/information_schema/columns_table.go

View workflow job for this annotation

GitHub Actions / Bats tests (macos-latest)

undefined: information_schema.AllDatabasesWithNames

Check failure on line 105 in server/tables/information_schema/columns_table.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

undefined: information_schema.AllDatabasesWithNames
if err != nil {
return nil, err
}
Expand Down
81 changes: 81 additions & 0 deletions server/tables/information_schema/databases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2024 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package information_schema

import (
"strings"

"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/information_schema"
"github.com/dolthub/go-mysql-server/sql/mysql_db"
)

// allDatabasesWithNames returns the current database(s) and their catalog and schema names.
func allDatabasesWithNames(ctx *sql.Context, cat sql.Catalog, privCheck bool) ([]information_schema.DbWithNames, error) {
var dbs []information_schema.DbWithNames

currentDB := ctx.GetCurrentDatabase()

allDbs := cat.AllDatabases(ctx)
for _, db := range allDbs {
if privCheck {
if privDatabase, ok := db.(mysql_db.PrivilegedDatabase); ok {
db = privDatabase.Unwrap()
}
}

sdb, ok := db.(sql.SchemaDatabase)
if ok {
var dbsForSchema []information_schema.DbWithNames
schemas, err := sdb.AllSchemas(ctx)
if err != nil {
return nil, err
}

for _, schema := range schemas {
dbName := db.Name()
revDb, _ := splitRevisionDbName(dbName)
// Add database it is the current database/revision database and if SchemaName exists
if schema.SchemaName() != "" && (dbName == currentDB || revDb == currentDB) {
dbsForSchema = append(dbsForSchema, information_schema.DbWithNames{schema, schema.Name(), schema.SchemaName()})
}
}

if len(dbsForSchema) > 0 {
// TODO: information_schema should be included in the schema list
infoSchemaDB, err := cat.Database(ctx, sql.InformationSchemaDatabaseName)
if err != nil {
return nil, err
}
dbsForSchema = append(dbsForSchema, information_schema.DbWithNames{infoSchemaDB, sdb.Name(), sql.InformationSchemaDatabaseName})

dbs = append(dbs, dbsForSchema...)
}
}
}

return dbs, nil
}

// splitRevisionDbName splits a database name into the base name and the revision.
func splitRevisionDbName(dbName string) (string, string) {
var baseName, rev string
parts := strings.SplitN(dbName, "/", 2)
baseName = parts[0]
if len(parts) > 1 {
rev = parts[1]
}
return baseName, rev
}
1 change: 1 addition & 0 deletions server/tables/information_schema/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ import (
func Init() {
information_schema.NewColumnsTable = newColumnsTable
information_schema.NewSchemataTable = newSchemataTable
information_schema.AllDatabasesWithNames = allDatabasesWithNames

Check failure on line 25 in server/tables/information_schema/init.go

View workflow job for this annotation

GitHub Actions / Run Staticcheck

undefined: information_schema.AllDatabasesWithNames

Check failure on line 25 in server/tables/information_schema/init.go

View workflow job for this annotation

GitHub Actions / Verify format

undefined: information_schema.AllDatabasesWithNames

Check failure on line 25 in server/tables/information_schema/init.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

undefined: information_schema.AllDatabasesWithNames

Check failure on line 25 in server/tables/information_schema/init.go

View workflow job for this annotation

GitHub Actions / Bats tests (ubuntu-22.04)

undefined: information_schema.AllDatabasesWithNames

Check failure on line 25 in server/tables/information_schema/init.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

undefined: information_schema.AllDatabasesWithNames

Check failure on line 25 in server/tables/information_schema/init.go

View workflow job for this annotation

GitHub Actions / Bats tests (macos-latest)

undefined: information_schema.AllDatabasesWithNames

Check failure on line 25 in server/tables/information_schema/init.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

undefined: information_schema.AllDatabasesWithNames
}
2 changes: 1 addition & 1 deletion server/tables/information_schema/schemata_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var schemataSchema = sql.Schema{

// schemataRowIter implements the sql.RowIter for the information_schema.SCHEMATA table.
func schemataRowIter(ctx *sql.Context, c sql.Catalog) (sql.RowIter, error) {
dbs, err := information_schema.AllDatabases(ctx, c, false)
dbs, err := information_schema.AllDatabasesWithNames(ctx, c, false)

Check failure on line 44 in server/tables/information_schema/schemata_table.go

View workflow job for this annotation

GitHub Actions / Run Staticcheck

undefined: information_schema.AllDatabasesWithNames (compile)

Check failure on line 44 in server/tables/information_schema/schemata_table.go

View workflow job for this annotation

GitHub Actions / Verify format

undefined: information_schema.AllDatabasesWithNames

Check failure on line 44 in server/tables/information_schema/schemata_table.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

undefined: information_schema.AllDatabasesWithNames

Check failure on line 44 in server/tables/information_schema/schemata_table.go

View workflow job for this annotation

GitHub Actions / Bats tests (ubuntu-22.04)

undefined: information_schema.AllDatabasesWithNames

Check failure on line 44 in server/tables/information_schema/schemata_table.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

undefined: information_schema.AllDatabasesWithNames

Check failure on line 44 in server/tables/information_schema/schemata_table.go

View workflow job for this annotation

GitHub Actions / Bats tests (macos-latest)

undefined: information_schema.AllDatabasesWithNames

Check failure on line 44 in server/tables/information_schema/schemata_table.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

undefined: information_schema.AllDatabasesWithNames
if err != nil {
return nil, err
}
Expand Down
17 changes: 14 additions & 3 deletions testing/go/schemas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,12 +543,12 @@ var SchemaTests = []ScriptTest{
},
},
{
Name: "with branches",
Name: "with branches", // TODO: Use `use db/branch` instead of dolt_checkout for these tests
SetUpScript: []string{
"CREATE SCHEMA myschema",
"SET search_path = 'myschema'",
"CREATE TABLE mytbl (pk BIGINT PRIMARY KEY, v1 BIGINT);",
"set dolt_show_branch_databases to 1;", // TODO: Use `use db/branch` instead of dolt_checkout for these tests and remove this
"set dolt_show_branch_databases to 1;",
},
Assertions: []ScriptTestAssertion{
{
Expand All @@ -562,7 +562,18 @@ var SchemaTests = []ScriptTest{
},
},
{
Skip: true, // TODO: revision database not supported yet
Query: "SELECT catalog_name, schema_name FROM information_schema.schemata;",
Expected: []sql.Row{
{"postgres", "myschema"},
{"postgres", "pg_catalog"},
{"postgres", "public"},
{"postgres", "information_schema"},
{"postgres/main", "myschema"},
{"postgres/main", "pg_catalog"},
{"postgres/main", "public"},
{"postgres/main", "information_schema"}},
},
{
Query: "SELECT schema_name FROM information_schema.schemata WHERE catalog_name = 'postgres/main';",
Expected: []sql.Row{
{"myschema"},
Expand Down

0 comments on commit 5355443

Please sign in to comment.