Skip to content

Commit

Permalink
infoschema , util, autoid: Compatible with some tables in pg_catalog (#…
Browse files Browse the repository at this point in the history
…63)

* Compatible with some tables in pg_catalog

Signed-off-by: Orion7r <50295175+Orion7r@users.noreply.github.com>

* add the correct license information and fix unit test

Signed-off-by: Orion7r <50295175+Orion7r@users.noreply.github.com>
  • Loading branch information
Orion7r authored Sep 9, 2021
1 parent 8687857 commit 4597116
Show file tree
Hide file tree
Showing 8 changed files with 570 additions and 35 deletions.
8 changes: 4 additions & 4 deletions infoschema/infoschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ func (*testSuite) TestT(c *C) {
is := handle.Get()

schemaNames := is.AllSchemaNames()
c.Assert(schemaNames, HasLen, 4)
c.Assert(testutil.CompareUnorderedStringSlice(schemaNames, []string{util.InformationSchemaName.O, util.MetricSchemaName.O, util.PerformanceSchemaName.O, "Test"}), IsTrue)
c.Assert(schemaNames, HasLen, 5)
c.Assert(testutil.CompareUnorderedStringSlice(schemaNames, []string{util.InformationSchemaName.O, util.MetricSchemaName.O, util.PerformanceSchemaName.O, util.PgCatalogName.O, "Test"}), IsTrue)

schemas := is.AllSchemas()
c.Assert(schemas, HasLen, 4)
c.Assert(schemas, HasLen, 5)
schemas = is.Clone()
c.Assert(schemas, HasLen, 4)
c.Assert(schemas, HasLen, 5)

c.Assert(is.SchemaExists(dbName), IsTrue)
c.Assert(is.SchemaExists(noexist), IsFalse)
Expand Down
63 changes: 63 additions & 0 deletions infoschema/pg_catalog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2016 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.

// Copyright 2021 Digital China Group Co.,Ltd
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package infoschema

import (
"fmt"
"github.com/DigitalChinaOpenSource/DCParser/model"
"github.com/DigitalChinaOpenSource/DCParser/mysql"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/util"
)

func init() {
// Initialize the pg_catalog shema database and register the driver to `drivers`
pgCatalogDBID := autoid.PgCatalogSchemaDBID
pgCatalogTables := make([]*model.TableInfo, 0, len(catalogTableNameToColumns))
for name, cols := range catalogTableNameToColumns {
tableInfo := buildTableMeta(name, cols)
pgCatalogTables = append(pgCatalogTables, tableInfo)
var ok bool
tableInfo.ID, ok = catalogTableIDMap[tableInfo.Name.O]
if !ok {
panic(fmt.Sprintf("get information_schema table id failed, unknown system table `%v`", tableInfo.Name.O))
}
for i, c := range tableInfo.Columns {
c.ID = int64(i) + 1
}
}

pgCatalogDB := &model.DBInfo{
ID: pgCatalogDBID,
Name: util.PgCatalogName,
Charset: mysql.DefaultCharset,
Collate: mysql.DefaultCollationName,
Tables: pgCatalogTables,
}

RegisterVirtualTable(pgCatalogDB, createPgCatalogTable)
}
431 changes: 431 additions & 0 deletions infoschema/pg_catalog_tables.go

Large diffs are not rendered by default.

65 changes: 65 additions & 0 deletions infoschema/pg_catalog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2016 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.

// Copyright 2021 Digital China Group Co.,Ltd
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package infoschema_test

import (
"github.com/DigitalChinaOpenSource/DCParser/model"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/testleak"
)

var _ = Suite(&testSuite{})

// TestInfoTables makes sure that all tables of pg_catalog could be found in infoschema handle.
func (*testSuite) TestCatalogTables(c *C) {
defer testleak.AfterTest(c)()
store, err := mockstore.NewMockTikvStore()
c.Assert(err, IsNil)
defer store.Close()
handle := infoschema.NewHandle(store)
builder, err := infoschema.NewBuilder(handle).InitWithDBInfos(nil, 0)
c.Assert(err, IsNil)
builder.Build()
is := handle.Get()
c.Assert(is, NotNil)

infoTables := []string{
"pg_aggregate",
"pg_class",
"pg_inherits",
"pg_namespace",
"pg_partitioned_table",
}
for _, t := range infoTables {
tb, err1 := is.TableByName(util.PgCatalogName, model.NewCIStr(t))
c.Assert(err1, IsNil)
c.Assert(tb, NotNil)
}
}
2 changes: 2 additions & 0 deletions meta/autoid/autoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const (
PerformanceSchemaDBID int64 = SystemSchemaIDFlag | 10000
// MetricSchemaDBID is the metrics_schema schema id, it's exported for test.
MetricSchemaDBID int64 = SystemSchemaIDFlag | 20000
// PgCatalogSchemaDBID is the pg_catalog schema id, it's exports for test.
PgCatalogSchemaDBID int64 = SystemSchemaIDFlag | 30000
)

const (
Expand Down
13 changes: 3 additions & 10 deletions session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -1443,9 +1443,9 @@ func doDDLWorks(s Session) {
mustExecute(s, CreateTablePgType)
// Create postgres pg_user_mapping Table
mustExecute(s, CreateTablePgUserMapping)

// Create postgres pg_all_settings Table
mustExecute(s, CreateTablePgAllSettings)

// Create postgres pg_stat_activity Table
mustExecute(s, CreateTablePgStatActivity)

mustExecute(s, "USE postgres;")
Expand All @@ -1457,19 +1457,12 @@ func doDDLWorks(s Session) {
mustExecute(s, CreateViewPgUser)
// Create postgres pg_group View
mustExecute(s, CreateViewPgGroup)

// Create postgres pg_settings View
mustExecute(s, CreateViewPgSettings)

// Create postgres Database
mustExecute(s, "CREATE DATABASE IF NOT EXISTS pg_catalog;")

mustExecute(s, CreatePgCalogViewPgRoles)
}

func doCreatePgViewsWorks(s Session) {
mustExecute(s, "USE postgres")

//mustExecute(s, CreateViewPgRoles)
}

// doDMLWorks executes DML statements in bootstrap stage.
Expand Down
21 changes: 0 additions & 21 deletions session/bootstrap_pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4836,24 +4836,3 @@ SELECT (ss.a).n FROM
(13430, 'plpgsql_inline_handler', 11, 10, 13, 1, 0, 0, '-', 'f', 0, 0, 1, 0, 'v', 'u', 1, 0, 2278, '2281', NULL, NULL, NULL, NULL, NULL, 'plpgsql_inline_handler', '$libdir/plpgsql', NULL, NULL),
(13431, 'plpgsql_validator', 11, 10, 13, 1, 0, 0, '-', 'f', 0, 0, 1, 0, 'v', 'u', 1, 0, 2278, '26', NULL, NULL, NULL, NULL, NULL, 'plpgsql_validator', '$libdir/plpgsql', NULL, NULL);`
)

// PG PG_Catalog
const (
CreatePgCalogViewPgRoles = `CREATE OR REPLACE DEFINER = root VIEW pg_catalog.pg_roles AS
SELECT
rolname,
rolsuper,
rolinherit,
rolcreaterole,
rolcreatedb,
rolcanlogin,
rolreplication,
rolconnlimit,
'********' as rolpassword,
rolvaliduntil,
rolbypassrls,
setconfig as rolconfig,
pg_authid.oid
FROM postgres.pg_authid LEFT JOIN postgres.pg_db_role_setting s
ON (postgres.pg_authid.oid = setrole AND setdatabase = 0);`
)
2 changes: 2 additions & 0 deletions util/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ var (
PerformanceSchemaName = model.NewCIStr("PERFORMANCE_SCHEMA")
// MetricSchemaName is the `METRICS_SCHEMA` database name.
MetricSchemaName = model.NewCIStr("METRICS_SCHEMA")
// PgCatalogName is the `pg_catalog` database name.
PgCatalogName = model.NewCIStr("pg_catalog")
)

// IsMemOrSysDB uses to check whether dbLowerName is memory database or system database.
Expand Down

0 comments on commit 4597116

Please sign in to comment.