Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: use DBSchema for catalog (#3915)

rebelice84dff97cff2f
diff --git a/plugin/advisor/catalog/catalog.go b/plugin/advisor/catalog/catalog.go
index 31eb3724c3..94314bca44 100644
--- a/plugin/advisor/catalog/catalog.go
+++ b/plugin/advisor/catalog/catalog.go
@@ -1,97 +1,7 @@
// Package catalog provides API definition for catalog service.
package catalog
-import (
- "github.com/bytebase/bytebase/plugin/advisor/db"
-)
-
// Catalog is the service for catalog.
type Catalog interface {
GetFinder() *Finder
}
-
-// Database is the database.
-type Database struct {
- Name string
- CharacterSet string
- Collation string
- DbType db.Type
- SchemaList []*Schema
-}
-
-// Schema is the database schema.
-type Schema struct {
- Name string
- TableList []*Table
- ViewList []*View
- ExtensionList []*Extension
-}
-
-// Table is the database table.
-type Table struct {
- Name string
- Type string
- // Engine isn't supported for Postgres, Snowflake, SQLite.
- Engine string
- // Collation isn't supported for Postgres, ClickHouse, Snowflake, SQLite.
- Collation string
- RowCount int64
- // DataSize isn't supported for SQLite.
- DataSize int64
- // IndexSize isn't supported for ClickHouse, Snowflake, SQLite.
- IndexSize int64
- // DataFree isn't supported for Postgres, ClickHouse, Snowflake, SQLite.
- DataFree int64
- // CreateOptions isn't supported for Postgres, ClickHouse, Snowflake, SQLite.
- CreateOptions string
- // Comment isn't supported for SQLite.
- Comment string
- ColumnList []*Column
- // IndexList isn't supported for ClickHouse, Snowflake.
- IndexList []*Index
-}
-
-// Index is the database index.
-type Index struct {
- Name string
- // This could refer to a column or an expression.
- ExpressionList []string
- // Type isn't supported for SQLite.
- Type string
- Unique bool
- Primary bool
- // Visible isn't supported for Postgres, SQLite.
- Visible bool
- // Comment isn't supported for SQLite.
- Comment string
-}
-
-// Column the database table column.
-type Column struct {
- Name string
- Position int
- Default *string
- // Nullable isn't supported for ClickHouse.
- Nullable bool
- Type string
- // CharacterSet isn't supported for Postgres, ClickHouse, SQLite.
- CharacterSet string
- // Collation isn't supported for ClickHouse, SQLite.
- Collation string
- // Comment isn't supported for SQLite.
- Comment string
-}
-
-// View is the database view.
-type View struct {
- Name string
- Definition string
- Comment string
-}
-
-// Extension is the database extension.
-type Extension struct {
- Name string
- Version string
- Description string
-}
diff --git a/plugin/advisor/catalog/finder.go b/plugin/advisor/catalog/finder.go
index 60f22bfa50..1826d3d121 100644
--- a/plugin/advisor/catalog/finder.go
+++ b/plugin/advisor/catalog/finder.go
@@ -1,6 +1,9 @@
package catalog
-import "github.com/bytebase/bytebase/plugin/advisor/db"
+import (
+ "github.com/bytebase/bytebase/plugin/advisor/db"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
// FinderContext is the context for finder.
type FinderContext struct {
@@ -18,6 +21,9 @@ type FinderContext struct {
// In this case, we need return the error that column a does not exist in table t,
// instead of ignoring this drop-column statement.
CheckIntegrity bool
+
+ // EngineType is the engine type for database engine.
+ EngineType db.Type
}
// Copy returns the deep copy.
@@ -34,13 +40,13 @@ type Finder struct {
}
// NewFinder creates a new finder.
-func NewFinder(database *Database, ctx *FinderContext) *Finder {
+func NewFinder(database *storepb.DatabaseMetadata, ctx *FinderContext) *Finder {
return &Finder{Origin: newDatabaseState(database, ctx), Final: newDatabaseState(database, ctx)}
}
// NewEmptyFinder creates a finder with empty databse.
-func NewEmptyFinder(ctx *FinderContext, dbType db.Type) *Finder {
- return &Finder{Origin: newDatabaseState(&Database{DbType: dbType}, ctx), Final: newDatabaseState(&Database{DbType: dbType}, ctx)}
+func NewEmptyFinder(ctx *FinderContext) *Finder {
+ return &Finder{Origin: newDatabaseState(&storepb.DatabaseMetadata{}, ctx), Final: newDatabaseState(&storepb.DatabaseMetadata{}, ctx)}
}
// WalkThrough does the walk through.
diff --git a/plugin/advisor/catalog/state.go b/plugin/advisor/catalog/state.go
index 669df42493..d450c69fe7 100644
--- a/plugin/advisor/catalog/state.go
+++ b/plugin/advisor/catalog/state.go
@@ -8,50 +8,46 @@ import (
"strings"
"github.com/bytebase/bytebase/plugin/advisor/db"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
-func newDatabaseState(d *Database, context *FinderContext) *DatabaseState {
+func newDatabaseState(d *storepb.DatabaseMetadata, context *FinderContext) *DatabaseState {
database := &DatabaseState{
ctx: context.Copy(),
name: d.Name,
characterSet: d.CharacterSet,
collation: d.Collation,
- dbType: d.DbType,
+ dbType: context.EngineType,
schemaSet: make(schemaStateMap),
}
- for _, schema := range d.SchemaList {
+ for _, schema := range d.Schemas {
database.schemaSet[schema.Name] = newSchemaState(schema, database.ctx)
}
return database
}
-func newSchemaState(s *Schema, context *FinderContext) *SchemaState {
+func newSchemaState(s *storepb.SchemaMetadata, context *FinderContext) *SchemaState {
schema := &SchemaState{
- ctx: context.Copy(),
- name: s.Name,
- tableSet: make(tableStateMap),
- viewSet: make(viewStateMap),
- extensionSet: make(extensionStateMap),
+ ctx: context.Copy(),
+ name: s.Name,
+ tableSet: make(tableStateMap),
+ viewSet: make(viewStateMap),
}
- for _, table := range s.TableList {
+ for _, table := range s.Tables {
schema.tableSet[table.Name] = newTableState(table)
}
- for _, view := range s.ViewList {
+ for _, view := range s.Views {
schema.viewSet[view.Name] = newViewState(view)
}
- for _, extension := range s.ExtensionList {
- schema.extensionSet[extension.Name] = newExtensionState(extension)
- }
-
return schema
}
-func newViewState(v *View) *ViewState {
+func newViewState(v *storepb.ViewMetadata) *ViewState {
return &ViewState{
name: v.Name,
definition: newStringPointer(v.Definition),
@@ -59,18 +55,9 @@ func newViewState(v *View) *ViewState {
}
}
-func newExtensionState(e *Extension) *ExtensionState {
- return &ExtensionState{
- name: e.Name,
- version: newStringPointer(e.Version),
- description: newStringPointer(e.Description),
- }
-}
-
-func newTableState(t *Table) *TableState {
+func newTableState(t *storepb.TableMetadata) *TableState {
table := &TableState{
name: t.Name,
- tableType: newStringPointer(t.Type),
engine: newStringPointer(t.Engine),
collation: newStringPointer(t.Collation),
comment: newStringPointer(t.Comment),
@@ -78,22 +65,26 @@ func newTableState(t *Table) *TableState {
indexSet: make(indexStateMap),
}
- for _, column := range t.ColumnList {
- table.columnSet[column.Name] = newColumnState(column)
+ for i, column := range t.Columns {
+ table.columnSet[column.Name] = newColumnState(column, i+1)
}
- for _, index := range t.IndexList {
+ for _, index := range t.Indexes {
table.indexSet[index.Name] = newIndexState(index)
}
return table
}
-func newColumnState(c *Column) *ColumnState {
+func newColumnState(c *storepb.ColumnMetadata, position int) *ColumnState {
+ defaultValue := (*string)(nil)
+ if c.HasDefault {
+ defaultValue = copyStringPointer(&c.Default)
+ }
return &ColumnState{
name: c.Name,
- position: newIntPointer(c.Position),
- defaultValue: copyStringPointer(c.Default),
+ position: newIntPointer(position),
+ defaultValue: defaultValue,
nullable: newBoolPointer(c.Nullable),
columnType: newStringPointer(c.Type),
characterSet: newStringPointer(c.CharacterSet),
@@ -102,7 +93,7 @@ func newColumnState(c *Column) *ColumnState {
}
}
-func newIndexState(i *Index) *IndexState {
+func newIndexState(i *storepb.IndexMetadata) *IndexState {
index := &IndexState{
name: i.Name,
indextype: newStringPointer(i.Type),
@@ -110,7 +101,7 @@ func newIndexState(i *Index) *IndexState {
primary: newBoolPointer(i.Primary),
visible: newBoolPointer(i.Visible),
comment: newStringPointer(i.Comment),
- expressionList: copyStringSlice(i.ExpressionList),
+ expressionList: copyStringSlice(i.Expressions),
}
return index
}
@@ -286,18 +277,16 @@ func (d *DatabaseState) FindTable(find *TableFind) *TableState {
// SchemaState is the state for walk-through.
type SchemaState struct {
- ctx *FinderContext
- name string
- tableSet tableStateMap
- viewSet viewStateMap
- extensionSet extensionStateMap
+ ctx *FinderContext
+ name string
+ tableSet tableStateMap
+ viewSet viewStateMap
}
type schemaStateMap map[string]*SchemaState
// TableState is the state for walk-through.
type TableState struct {
- name string
- tableType *string
+ name string
// engine isn't supported for Postgres, Snowflake, SQLite.
engine *string
// collation isn't supported for Postgres, ClickHouse, Snowflake, SQLite.
@@ -317,7 +306,6 @@ func (table *TableState) CountIndex() int {
func (table *TableState) copy() *TableState {
return &TableState{
name: table.name,
- tableType: copyStringPointer(table.tableType),
engine: copyStringPointer(table.engine),
collation: copyStringPointer(table.collation),
comment: copyStringPointer(table.comment),
@@ -446,14 +434,6 @@ type ViewState struct {
}
type viewStateMap map[string]*ViewState
-// ExtensionState is the state for walk-through.
-type ExtensionState struct {
- name string
- version *string
- description *string
-}
-type extensionStateMap map[string]*ExtensionState
-
func copyStringPointer(p *string) *string {
if p != nil {
v := *p
diff --git a/plugin/advisor/catalog/walk_through.go b/plugin/advisor/catalog/walk_through.go
index 9668c43229..52b6756ae3 100644
--- a/plugin/advisor/catalog/walk_through.go
+++ b/plugin/advisor/catalog/walk_through.go
@@ -934,7 +934,6 @@ func (d *DatabaseState) createTable(node *tidbast.CreateTableStmt) *WalkThroughE
table := &TableState{
name: node.Table.Name.O,
- tableType: newEmptyStringPointer(),
engine: newEmptyStringPointer(),
collation: newEmptyStringPointer(),
comment: newEmptyStringPointer(),
@@ -1242,11 +1241,10 @@ func (t *TableState) createPrimaryKey(keys []string, tp string) *WalkThroughErro
func (d *DatabaseState) createSchema(name string) *SchemaState {
schema := &SchemaState{
- ctx: d.ctx.Copy(),
- name: name,
- tableSet: make(tableStateMap),
- viewSet: make(viewStateMap),
- extensionSet: make(extensionStateMap),
+ ctx: d.ctx.Copy(),
+ name: name,
+ tableSet: make(tableStateMap),
+ viewSet: make(viewStateMap),
}
d.schemaSet[name] = schema
diff --git a/plugin/advisor/catalog/walk_through_test.go b/plugin/advisor/catalog/walk_through_test.go
index 2116f876c3..0f82ac99fa 100644
--- a/plugin/advisor/catalog/walk_through_test.go
+++ b/plugin/advisor/catalog/walk_through_test.go
@@ -6,14 +6,16 @@ import (
"github.com/stretchr/testify/require"
"github.com/bytebase/bytebase/plugin/advisor/db"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+
// Register pingcap parser driver.
_ "github.com/pingcap/tidb/types/parser_driver"
)
type testData struct {
- origin *Database
+ origin *storepb.DatabaseMetadata
statement string
- want *Database
+ want *storepb.DatabaseMetadata
err error
}
@@ -24,9 +26,8 @@ var (
func TestWalkThrough(t *testing.T) {
tests := []testData{
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(a TEXT);
@@ -39,9 +40,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(a TEXT DEFAULT '1')
@@ -53,9 +53,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(a INT NOT NULL);
@@ -68,9 +67,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(a INT NOT NULL DEFAULT NULL)
@@ -82,9 +80,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(a int ON UPDATE NOW())
@@ -96,9 +93,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(a int auto_increment, b int auto_increment);
@@ -110,9 +106,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t as select * from t1;
@@ -124,9 +119,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(a int, b int);
@@ -139,9 +133,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(a int, b int);
@@ -154,9 +147,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(a int, b int);
@@ -169,9 +161,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(a int, b int);
@@ -184,9 +175,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(a int, b int);
@@ -200,9 +190,8 @@ func TestWalkThrough(t *testing.T) {
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(a int, b int);
@@ -215,9 +204,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(a int, b int);
@@ -230,26 +218,23 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
ALTER DATABASE CHARACTER SET = utf8mb4;
ALTER DATABASE test COLLATE utf8mb4_polish_ci;
`,
- want: &Database{
+ want: &storepb.DatabaseMetadata{
Name: "test",
- DbType: db.MySQL,
CharacterSet: "utf8mb4",
Collation: "utf8mb4_polish_ci",
- SchemaList: []*Schema{{}},
+ Schemas: []*storepb.SchemaMetadata{{}},
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(
@@ -264,113 +249,114 @@ func TestWalkThrough(t *testing.T) {
);
CREATE TABLE t_copy like t;
`,
- want: &Database{
- Name: "test",
- DbType: db.MySQL,
- SchemaList: []*Schema{
+ want: &storepb.DatabaseMetadata{
+ Name: "test",
+
+ Schemas: []*storepb.SchemaMetadata{
{
Name: "",
- TableList: []*Table{
+ Tables: []*storepb.TableMetadata{
{
Name: "t_copy",
- ColumnList: []*Column{
+ Columns: []*storepb.ColumnMetadata{
{
- Name: "a",
- Position: 1,
- Default: &one,
- Nullable: false,
- Type: "int(11)",
+ Name: "a",
+ Position: 1,
+ HasDefault: true,
+ Default: one,
+ Nullable: false,
+ Type: "int(11)",
},
{
Name: "b",
Position: 2,
- Default: nil,
+ HasDefault: false,
Nullable: false,
Type: "varchar(200)",
CharacterSet: "utf8mb4",
},
{
- Name: "c",
- Position: 3,
- Default: nil,
- Nullable: true,
- Type: "int(11)",
- Comment: "This is a comment",
+ Name: "c",
+ Position: 3,
+ HasDefault: false,
+ Nullable: true,
+ Type: "int(11)",
+ Comment: "This is a comment",
},
{
- Name: "d",
- Position: 4,
- Default: nil,
- Nullable: true,
- Type: "varchar(10)",
- Collation: "utf8mb4_polish_ci",
+ Name: "d",
+ Position: 4,
+ HasDefault: false,
+ Nullable: true,
+ Type: "varchar(10)",
+ Collation: "utf8mb4_polish_ci",
},
},
- IndexList: []*Index{
+ Indexes: []*storepb.IndexMetadata{
{
- Name: "PRIMARY",
- ExpressionList: []string{"a"},
- Type: "BTREE",
- Unique: true,
- Primary: true,
- Visible: true,
+ Name: "PRIMARY",
+ Expressions: []string{"a"},
+ Type: "BTREE",
+ Unique: true,
+ Primary: true,
+ Visible: true,
},
{
- Name: "b",
- ExpressionList: []string{"b"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
+ Name: "b",
+ Expressions: []string{"b"},
+ Type: "BTREE",
+ Unique: true,
+ Primary: false,
+ Visible: true,
},
{
- Name: "idx_a",
- ExpressionList: []string{"a"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
+ Name: "idx_a",
+ Expressions: []string{"a"},
+ Type: "BTREE",
+ Unique: false,
+ Primary: false,
+ Visible: true,
},
{
- Name: "b_2",
- ExpressionList: []string{"b", "a"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
+ Name: "b_2",
+ Expressions: []string{"b", "a"},
+ Type: "BTREE",
+ Unique: false,
+ Primary: false,
+ Visible: true,
},
{
- Name: "b_3",
- ExpressionList: []string{"b", "c", "d"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
+ Name: "b_3",
+ Expressions: []string{"b", "c", "d"},
+ Type: "BTREE",
+ Unique: true,
+ Primary: false,
+ Visible: true,
},
{
- Name: "b_4",
- ExpressionList: []string{"b", "d"},
- Type: "FULLTEXT",
- Unique: false,
- Primary: false,
- Visible: false,
+ Name: "b_4",
+ Expressions: []string{"b", "d"},
+ Type: "FULLTEXT",
+ Unique: false,
+ Primary: false,
+ Visible: false,
},
},
},
{
Name: "t",
- ColumnList: []*Column{
+ Columns: []*storepb.ColumnMetadata{
{
- Name: "a",
- Position: 1,
- Default: &one,
- Nullable: false,
- Type: "int(11)",
+ Name: "a",
+ Position: 1,
+ HasDefault: true,
+ Default: one,
+ Nullable: false,
+ Type: "int(11)",
},
{
Name: "b",
Position: 2,
- Default: nil,
Nullable: false,
Type: "varchar(200)",
CharacterSet: "utf8mb4",
@@ -378,7 +364,6 @@ func TestWalkThrough(t *testing.T) {
{
Name: "c",
Position: 3,
- Default: nil,
Nullable: true,
Type: "int(11)",
Comment: "This is a comment",
@@ -386,60 +371,59 @@ func TestWalkThrough(t *testing.T) {
{
Name: "d",
Position: 4,
- Default: nil,
Nullable: true,
Type: "varchar(10)",
Collation: "utf8mb4_polish_ci",
},
},
- IndexList: []*Index{
+ Indexes: []*storepb.IndexMetadata{
{
- Name: "PRIMARY",
- ExpressionList: []string{"a"},
- Type: "BTREE",
- Unique: true,
- Primary: true,
- Visible: true,
+ Name: "PRIMARY",
+ Expressions: []string{"a"},
+ Type: "BTREE",
+ Unique: true,
+ Primary: true,
+ Visible: true,
},
{
- Name: "b",
- ExpressionList: []string{"b"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
+ Name: "b",
+ Expressions: []string{"b"},
+ Type: "BTREE",
+ Unique: true,
+ Primary: false,
+ Visible: true,
},
{
- Name: "idx_a",
- ExpressionList: []string{"a"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
+ Name: "idx_a",
+ Expressions: []string{"a"},
+ Type: "BTREE",
+ Unique: false,
+ Primary: false,
+ Visible: true,
},
{
- Name: "b_2",
- ExpressionList: []string{"b", "a"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
+ Name: "b_2",
+ Expressions: []string{"b", "a"},
+ Type: "BTREE",
+ Unique: false,
+ Primary: false,
+ Visible: true,
},
{
- Name: "b_3",
- ExpressionList: []string{"b", "c", "d"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
+ Name: "b_3",
+ Expressions: []string{"b", "c", "d"},
+ Type: "BTREE",
+ Unique: true,
+ Primary: false,
+ Visible: true,
},
{
- Name: "b_4",
- ExpressionList: []string{"b", "d"},
- Type: "FULLTEXT",
- Unique: false,
- Primary: false,
- Visible: false,
+ Name: "b_4",
+ Expressions: []string{"b", "d"},
+ Type: "FULLTEXT",
+ Unique: false,
+ Primary: false,
+ Visible: false,
},
},
},
@@ -449,9 +433,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(
@@ -465,27 +448,27 @@ func TestWalkThrough(t *testing.T) {
FULLTEXT (b, d) WITH PARSER ngram INVISIBLE
)
`,
- want: &Database{
- Name: "test",
- DbType: db.MySQL,
- SchemaList: []*Schema{
+ want: &storepb.DatabaseMetadata{
+ Name: "test",
+
+ Schemas: []*storepb.SchemaMetadata{
{
Name: "",
- TableList: []*Table{
+ Tables: []*storepb.TableMetadata{
{
Name: "t",
- ColumnList: []*Column{
+ Columns: []*storepb.ColumnMetadata{
{
- Name: "a",
- Position: 1,
- Default: &one,
- Nullable: false,
- Type: "int(11)",
+ Name: "a",
+ Position: 1,
+ HasDefault: true,
+ Default: one,
+ Nullable: false,
+ Type: "int(11)",
},
{
Name: "b",
Position: 2,
- Default: nil,
Nullable: false,
Type: "varchar(200)",
CharacterSet: "utf8mb4",
@@ -493,7 +476,6 @@ func TestWalkThrough(t *testing.T) {
{
Name: "c",
Position: 3,
- Default: nil,
Nullable: true,
Type: "int(11)",
Comment: "This is a comment",
@@ -501,60 +483,59 @@ func TestWalkThrough(t *testing.T) {
{
Name: "d",
Position: 4,
- Default: nil,
Nullable: true,
Type: "varchar(10)",
Collation: "utf8mb4_polish_ci",
},
},
- IndexList: []*Index{
+ Indexes: []*storepb.IndexMetadata{
{
- Name: "PRIMARY",
- ExpressionList: []string{"a"},
- Type: "BTREE",
- Unique: true,
- Primary: true,
- Visible: true,
+ Name: "PRIMARY",
+ Expressions: []string{"a"},
+ Type: "BTREE",
+ Unique: true,
+ Primary: true,
+ Visible: true,
},
{
- Name: "b",
- ExpressionList: []string{"b"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
+ Name: "b",
+ Expressions: []string{"b"},
+ Type: "BTREE",
+ Unique: true,
+ Primary: false,
+ Visible: true,
},
{
- Name: "idx_a",
- ExpressionList: []string{"a"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
+ Name: "idx_a",
+ Expressions: []string{"a"},
+ Type: "BTREE",
+ Unique: false,
+ Primary: false,
+ Visible: true,
},
{
- Name: "b_2",
- ExpressionList: []string{"b", "a"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
+ Name: "b_2",
+ Expressions: []string{"b", "a"},
+ Type: "BTREE",
+ Unique: false,
+ Primary: false,
+ Visible: true,
},
{
- Name: "b_3",
- ExpressionList: []string{"b", "c", "d"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
+ Name: "b_3",
+ Expressions: []string{"b", "c", "d"},
+ Type: "BTREE",
+ Unique: true,
+ Primary: false,
+ Visible: true,
},
{
- Name: "b_4",
- ExpressionList: []string{"b", "d"},
- Type: "FULLTEXT",
- Unique: false,
- Primary: false,
- Visible: false,
+ Name: "b_4",
+ Expressions: []string{"b", "d"},
+ Type: "FULLTEXT",
+ Unique: false,
+ Primary: false,
+ Visible: false,
},
},
},
@@ -564,9 +545,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(
@@ -575,16 +555,16 @@ func TestWalkThrough(t *testing.T) {
PRIMARY KEY (a, b)
)
`,
- want: &Database{
- Name: "test",
- DbType: db.MySQL,
- SchemaList: []*Schema{
+ want: &storepb.DatabaseMetadata{
+ Name: "test",
+
+ Schemas: []*storepb.SchemaMetadata{
{
Name: "",
- TableList: []*Table{
+ Tables: []*storepb.TableMetadata{
{
Name: "t",
- ColumnList: []*Column{
+ Columns: []*storepb.ColumnMetadata{
{
Name: "a",
Position: 1,
@@ -598,14 +578,14 @@ func TestWalkThrough(t *testing.T) {
Nullable: false,
},
},
- IndexList: []*Index{
+ Indexes: []*storepb.IndexMetadata{
{
- Name: "PRIMARY",
- ExpressionList: []string{"a", "b"},
- Type: "BTREE",
- Unique: true,
- Primary: true,
- Visible: true,
+ Name: "PRIMARY",
+ Expressions: []string{"a", "b"},
+ Type: "BTREE",
+ Unique: true,
+ Primary: true,
+ Visible: true,
},
},
},
@@ -615,31 +595,28 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t1(a int, b int, c int);
CREATE TABLE t2(a int);
DROP TABLE t1, t2
`,
- want: &Database{
- Name: "test",
- DbType: db.MySQL,
- SchemaList: []*Schema{
+ want: &storepb.DatabaseMetadata{
+ Name: "test",
+
+ Schemas: []*storepb.SchemaMetadata{
{
- TableList: []*Table{},
- ViewList: []*View{},
- ExtensionList: []*Extension{},
+ Tables: []*storepb.TableMetadata{},
+ Views: []*storepb.ViewMetadata{},
},
},
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
DROP TABLE t1, t2
@@ -651,45 +628,42 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(a int);
RENAME TABLE t to other_db.t1
`,
- want: &Database{
- Name: "test",
- DbType: db.MySQL,
- SchemaList: []*Schema{
+ want: &storepb.DatabaseMetadata{
+ Name: "test",
+
+ Schemas: []*storepb.SchemaMetadata{
{},
},
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(a int);
RENAME TABLE t to test.t1
`,
- want: &Database{
- Name: "test",
- DbType: db.MySQL,
- SchemaList: []*Schema{
+ want: &storepb.DatabaseMetadata{
+ Name: "test",
+
+ Schemas: []*storepb.SchemaMetadata{
{
Name: "",
- TableList: []*Table{
+ Tables: []*storepb.TableMetadata{
{
Name: "t1",
- ColumnList: []*Column{
+ Columns: []*storepb.ColumnMetadata{
{
Name: "a",
Position: 1,
- Default: nil,
Nullable: true,
Type: "int(11)",
},
@@ -701,9 +675,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `-- this is comment
DROP DATABASE test;
@@ -716,9 +689,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(
@@ -746,37 +718,34 @@ func TestWalkThrough(t *testing.T) {
ALTER TABLE t_copy RENAME INDEX b TO idx_b;
ALTER TABLE t_copy ALTER INDEX b_3 INVISIBLE;
`,
- want: &Database{
- Name: "test",
- DbType: db.MySQL,
- SchemaList: []*Schema{
+ want: &storepb.DatabaseMetadata{
+ Name: "test",
+
+ Schemas: []*storepb.SchemaMetadata{
{
Name: "",
- TableList: []*Table{
+ Tables: []*storepb.TableMetadata{
{
Name: "t_copy",
Collation: "utf8mb4_0900_ai_ci",
Engine: "INNODB",
Comment: "This is a table comment",
- ColumnList: []*Column{
+ Columns: []*storepb.ColumnMetadata{
{
Name: "b",
Position: 1,
- Default: nil,
Nullable: true,
Type: "varchar(20)",
},
{
Name: "a_copy",
Position: 2,
- Default: nil,
Nullable: false,
Type: "int(11)",
},
{
Name: "a1",
Position: 3,
- Default: nil,
Nullable: true,
Type: "int(11)",
},
@@ -784,7 +753,6 @@ func TestWalkThrough(t *testing.T) {
{
Name: "d_copy",
Position: 4,
- Default: nil,
Nullable: true,
Type: "varchar(10)",
Collation: "utf8mb4_polish_ci",
@@ -792,51 +760,50 @@ func TestWalkThrough(t *testing.T) {
{
Name: "e",
Position: 5,
- Default: nil,
Nullable: true,
Type: "int(11)",
},
},
- IndexList: []*Index{
+ Indexes: []*storepb.IndexMetadata{
{
- Name: "idx_b",
- ExpressionList: []string{"b"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
+ Name: "idx_b",
+ Expressions: []string{"b"},
+ Type: "BTREE",
+ Unique: true,
+ Primary: false,
+ Visible: true,
},
{
- Name: "idx_a",
- ExpressionList: []string{"a_copy"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
+ Name: "idx_a",
+ Expressions: []string{"a_copy"},
+ Type: "BTREE",
+ Unique: false,
+ Primary: false,
+ Visible: true,
},
{
- Name: "b_3",
- ExpressionList: []string{"b", "d_copy"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: false,
+ Name: "b_3",
+ Expressions: []string{"b", "d_copy"},
+ Type: "BTREE",
+ Unique: true,
+ Primary: false,
+ Visible: false,
},
{
- Name: "b_4",
- ExpressionList: []string{"b", "d_copy"},
- Type: "FULLTEXT",
- Unique: false,
- Primary: false,
- Visible: false,
+ Name: "b_4",
+ Expressions: []string{"b", "d_copy"},
+ Type: "FULLTEXT",
+ Unique: false,
+ Primary: false,
+ Visible: false,
},
{
- Name: "idx_a_b",
- ExpressionList: []string{"a_copy", "b"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
+ Name: "idx_a_b",
+ Expressions: []string{"a_copy", "b"},
+ Type: "BTREE",
+ Unique: false,
+ Primary: false,
+ Visible: true,
},
},
},
@@ -846,9 +813,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(
@@ -862,27 +828,27 @@ func TestWalkThrough(t *testing.T) {
CREATE UNIQUE INDEX b_3 on t(b, c, d);
CREATE FULLTEXT INDEX b_4 on t(b, d) WITH PARSER ngram INVISIBLE;
`,
- want: &Database{
- Name: "test",
- DbType: db.MySQL,
- SchemaList: []*Schema{
+ want: &storepb.DatabaseMetadata{
+ Name: "test",
+
+ Schemas: []*storepb.SchemaMetadata{
{
Name: "",
- TableList: []*Table{
+ Tables: []*storepb.TableMetadata{
{
Name: "t",
- ColumnList: []*Column{
+ Columns: []*storepb.ColumnMetadata{
{
- Name: "a",
- Position: 1,
- Default: &one,
- Nullable: false,
- Type: "int(11)",
+ Name: "a",
+ Position: 1,
+ HasDefault: true,
+ Default: one,
+ Nullable: false,
+ Type: "int(11)",
},
{
Name: "b",
Position: 2,
- Default: nil,
Nullable: false,
Type: "varchar(200)",
CharacterSet: "utf8mb4",
@@ -890,7 +856,6 @@ func TestWalkThrough(t *testing.T) {
{
Name: "c",
Position: 3,
- Default: nil,
Nullable: true,
Type: "int(11)",
Comment: "This is a comment",
@@ -898,60 +863,59 @@ func TestWalkThrough(t *testing.T) {
{
Name: "d",
Position: 4,
- Default: nil,
Nullable: true,
Type: "varchar(10)",
Collation: "utf8mb4_polish_ci",
},
},
- IndexList: []*Index{
+ Indexes: []*storepb.IndexMetadata{
{
- Name: "PRIMARY",
- ExpressionList: []string{"a"},
- Type: "BTREE",
- Unique: true,
- Primary: true,
- Visible: true,
+ Name: "PRIMARY",
+ Expressions: []string{"a"},
+ Type: "BTREE",
+ Unique: true,
+ Primary: true,
+ Visible: true,
},
{
- Name: "b",
- ExpressionList: []string{"b"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
+ Name: "b",
+ Expressions: []string{"b"},
+ Type: "BTREE",
+ Unique: true,
+ Primary: false,
+ Visible: true,
},
{
- Name: "idx_a",
- ExpressionList: []string{"a"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
+ Name: "idx_a",
+ Expressions: []string{"a"},
+ Type: "BTREE",
+ Unique: false,
+ Primary: false,
+ Visible: true,
},
{
- Name: "b_2",
- ExpressionList: []string{"b", "a"},
- Type: "BTREE",
- Unique: false,
- Primary: false,
- Visible: true,
+ Name: "b_2",
+ Expressions: []string{"b", "a"},
+ Type: "BTREE",
+ Unique: false,
+ Primary: false,
+ Visible: true,
},
{
- Name: "b_3",
- ExpressionList: []string{"b", "c", "d"},
- Type: "BTREE",
- Unique: true,
- Primary: false,
- Visible: true,
+ Name: "b_3",
+ Expressions: []string{"b", "c", "d"},
+ Type: "BTREE",
+ Unique: true,
+ Primary: false,
+ Visible: true,
},
{
- Name: "b_4",
- ExpressionList: []string{"b", "d"},
- Type: "FULLTEXT",
- Unique: false,
- Primary: false,
- Visible: false,
+ Name: "b_4",
+ Expressions: []string{"b", "d"},
+ Type: "FULLTEXT",
+ Unique: false,
+ Primary: false,
+ Visible: false,
},
},
},
@@ -961,9 +925,8 @@ func TestWalkThrough(t *testing.T) {
},
},
{
- origin: &Database{
- Name: "test",
- DbType: db.MySQL,
+ origin: &storepb.DatabaseMetadata{
+ Name: "test",
},
statement: `
CREATE TABLE t(
@@ -972,40 +935,40 @@ func TestWalkThrough(t *testing.T) {
);
DROP INDEX b on t;
`,
- want: &Database{
- Name: "test",
- DbType: db.MySQL,
- SchemaList: []*Schema{
+ want: &storepb.DatabaseMetadata{
+ Name: "test",
+
+ Schemas: []*storepb.SchemaMetadata{
{
Name: "",
- TableList: []*Table{
+ Tables: []*storepb.TableMetadata{
{
Name: "t",
- ColumnList: []*Column{
+ Columns: []*storepb.ColumnMetadata{
{
- Name: "a",
- Position: 1,
- Default: &one,
- Nullable: false,
- Type: "int(11)",
+ Name: "a",
+ Position: 1,
+ HasDefault: true,
+ Default: one,
+ Nullable: false,
+ Type: "int(11)",
},
{
Name: "b",
Position: 2,
- Default: nil,
Nullable: false,
Type: "varchar(200)",
CharacterSet: "utf8mb4",
},
},
- IndexList: []*Index{
+ Indexes: []*storepb.IndexMetadata{
{
- Name: "PRIMARY",
- ExpressionList: []string{"a"},
- Type: "BTREE",
- Unique: true,
- Primary: true,
- Visible: true,
+ Name: "PRIMARY",
+ Expressions: []string{"a"},
+ Type: "BTREE",
+ Unique: true,
+ Primary: true,
+ Visible: true,
},
},
},
@@ -1017,14 +980,14 @@ func TestWalkThrough(t *testing.T) {
}
for _, test := range tests {
- state := newDatabaseState(test.origin, &FinderContext{CheckIntegrity: true})
+ state := newDatabaseState(test.origin, &FinderContext{CheckIntegrity: true, EngineType: db.MySQL})
err := state.WalkThrough(test.statement)
if test.err != nil {
require.Equal(t, err, test.err)
continue
}
require.NoError(t, err)
- want := newDatabaseState(test.want, &FinderContext{CheckIntegrity: true})
+ want := newDatabaseState(test.want, &FinderContext{CheckIntegrity: true, EngineType: db.MySQL})
require.Equal(t, want, state, test.statement)
}
}
@@ -1052,7 +1015,6 @@ func TestWalkThroughForIncompleteOriginalCatalog(t *testing.T) {
tableSet: tableStateMap{
"t": {
name: "t",
- tableType: nil,
engine: nil,
collation: nil,
comment: nil,
@@ -1070,8 +1032,7 @@ func TestWalkThroughForIncompleteOriginalCatalog(t *testing.T) {
},
},
},
- viewSet: viewStateMap{},
- extensionSet: extensionStateMap{},
+ viewSet: viewStateMap{},
},
},
},
@@ -1091,7 +1052,6 @@ func TestWalkThroughForIncompleteOriginalCatalog(t *testing.T) {
tableSet: tableStateMap{
"t": {
name: "t",
- tableType: nil,
engine: nil,
collation: nil,
comment: nil,
@@ -1103,8 +1063,7 @@ func TestWalkThroughForIncompleteOriginalCatalog(t *testing.T) {
indexSet: indexStateMap{},
},
},
- viewSet: viewStateMap{},
- extensionSet: extensionStateMap{},
+ viewSet: viewStateMap{},
},
},
},
@@ -1124,7 +1083,6 @@ func TestWalkThroughForIncompleteOriginalCatalog(t *testing.T) {
tableSet: tableStateMap{
"t1": {
name: "t1",
- tableType: nil,
engine: nil,
collation: nil,
comment: nil,
@@ -1132,8 +1090,7 @@ func TestWalkThroughForIncompleteOriginalCatalog(t *testing.T) {
indexSet: indexStateMap{},
},
},
- viewSet: viewStateMap{},
- extensionSet: extensionStateMap{},
+ viewSet: viewStateMap{},
},
},
},
@@ -1160,7 +1117,6 @@ func TestWalkThroughForIncompleteOriginalCatalog(t *testing.T) {
tableSet: tableStateMap{
"t": {
name: "t",
- tableType: nil,
engine: nil,
collation: nil,
comment: nil,
@@ -1223,8 +1179,7 @@ func TestWalkThroughForIncompleteOriginalCatalog(t *testing.T) {
},
},
},
- viewSet: viewStateMap{},
- extensionSet: extensionStateMap{},
+ viewSet: viewStateMap{},
},
},
},
@@ -1255,7 +1210,6 @@ func TestWalkThroughForIncompleteOriginalCatalog(t *testing.T) {
tableSet: tableStateMap{
"t": {
name: "t",
- tableType: newEmptyStringPointer(),
engine: newEmptyStringPointer(),
collation: newEmptyStringPointer(),
comment: newEmptyStringPointer(),
@@ -1359,8 +1313,7 @@ func TestWalkThroughForIncompleteOriginalCatalog(t *testing.T) {
},
},
},
- viewSet: viewStateMap{},
- extensionSet: extensionStateMap{},
+ viewSet: viewStateMap{},
},
},
},
@@ -1375,11 +1328,10 @@ func TestWalkThroughForIncompleteOriginalCatalog(t *testing.T) {
dbType: db.MySQL,
schemaSet: schemaStateMap{
"": {
- ctx: &FinderContext{CheckIntegrity: false},
- name: "",
- tableSet: tableStateMap{},
- viewSet: viewStateMap{},
- extensionSet: extensionStateMap{},
+ ctx: &FinderContext{CheckIntegrity: false},
+ name: "",
+ tableSet: tableStateMap{},
+ viewSet: viewStateMap{},
},
},
},
@@ -1394,18 +1346,17 @@ func TestWalkThroughForIncompleteOriginalCatalog(t *testing.T) {
dbType: db.MySQL,
schemaSet: schemaStateMap{
"": {
- ctx: &FinderContext{CheckIntegrity: false},
- name: "",
- tableSet: tableStateMap{},
- viewSet: viewStateMap{},
- extensionSet: extensionStateMap{},
+ ctx: &FinderContext{CheckIntegrity: false},
+ name: "",
+ tableSet: tableStateMap{},
+ viewSet: viewStateMap{},
},
},
},
},
}
for _, test := range tests {
- finder := NewEmptyFinder(&FinderContext{CheckIntegrity: false}, db.MySQL)
+ finder := NewEmptyFinder(&FinderContext{CheckIntegrity: false, EngineType: db.MySQL})
err := finder.WalkThrough(test.statement)
if test.err != nil {
require.Equal(t, err, test.err)
diff --git a/plugin/advisor/utils_for_tests.go b/plugin/advisor/utils_for_tests.go
index e24db351eb..251ceec3bd 100644
--- a/plugin/advisor/utils_for_tests.go
+++ b/plugin/advisor/utils_for_tests.go
@@ -19,6 +19,7 @@ import (
"github.com/bytebase/bytebase/plugin/advisor/catalog"
"github.com/bytebase/bytebase/plugin/advisor/db"
database "github.com/bytebase/bytebase/plugin/db"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
var (
@@ -42,15 +43,14 @@ var (
// MockIndexColumnList is the mock index column list for test.
MockIndexColumnList = []string{"id", "name"}
// MockMySQLDatabase is the mock MySQL database for test.
- MockMySQLDatabase = &catalog.Database{
- Name: "test",
- DbType: db.MySQL,
- SchemaList: []*catalog.Schema{
+ MockMySQLDatabase = &storepb.DatabaseMetadata{
+ Name: "test",
+ Schemas: []*storepb.SchemaMetadata{
{
- TableList: []*catalog.Table{
+ Tables: []*storepb.TableMetadata{
{
Name: MockTableName,
- ColumnList: []*catalog.Column{
+ Columns: []*storepb.ColumnMetadata{
{
Name: "id",
Type: "int",
@@ -60,21 +60,21 @@ var (
Type: "varchar(255)",
},
},
- IndexList: []*catalog.Index{
+ Indexes: []*storepb.IndexMetadata{
{
- Name: MockOldMySQLPKName,
- ExpressionList: []string{"id", "name"},
- Unique: true,
- Primary: true,
+ Name: MockOldMySQLPKName,
+ Expressions: []string{"id", "name"},
+ Unique: true,
+ Primary: true,
},
{
- Name: MockOldUKName,
- ExpressionList: []string{"id", "name"},
- Unique: true,
+ Name: MockOldUKName,
+ Expressions: []string{"id", "name"},
+ Unique: true,
},
{
- Name: MockOldIndexName,
- ExpressionList: []string{"id", "name"},
+ Name: MockOldIndexName,
+ Expressions: []string{"id", "name"},
},
},
},
@@ -83,34 +83,33 @@ var (
},
}
// MockPostgreSQLDatabase is the mock PostgreSQL database for test.
- MockPostgreSQLDatabase = &catalog.Database{
- Name: "test",
- DbType: db.Postgres,
- SchemaList: []*catalog.Schema{
+ MockPostgreSQLDatabase = &storepb.DatabaseMetadata{
+ Name: "test",
+ Schemas: []*storepb.SchemaMetadata{
{
Name: "public",
- TableList: []*catalog.Table{
+ Tables: []*storepb.TableMetadata{
{
Name: MockTableName,
- ColumnList: []*catalog.Column{
+ Columns: []*storepb.ColumnMetadata{
{Name: "id"},
{Name: "name"},
},
- IndexList: []*catalog.Index{
+ Indexes: []*storepb.IndexMetadata{
{
- Name: MockOldPostgreSQLPKName,
- ExpressionList: []string{"id", "name"},
- Unique: true,
- Primary: true,
+ Name: MockOldPostgreSQLPKName,
+ Expressions: []string{"id", "name"},
+ Unique: true,
+ Primary: true,
},
{
- Name: MockOldUKName,
- ExpressionList: []string{"id", "name"},
- Unique: true,
+ Name: MockOldUKName,
+ Expressions: []string{"id", "name"},
+ Unique: true,
},
{
- Name: MockOldIndexName,
- ExpressionList: []string{"id", "name"},
+ Name: MockOldIndexName,
+ Expressions: []string{"id", "name"},
},
},
},
@@ -161,7 +160,7 @@ func RunSQLReviewRuleTest(t *testing.T, rule SQLReviewRuleType, dbType db.Type,
if dbType == db.Postgres {
database = MockPostgreSQLDatabase
}
- finder := catalog.NewFinder(database, &catalog.FinderContext{CheckIntegrity: true})
+ finder := catalog.NewFinder(database, &catalog.FinderContext{CheckIntegrity: true, EngineType: dbType})
payload, err := SetDefaultSQLReviewRulePayload(rule)
require.NoError(t, err)
diff --git a/server/openapi_sql.go b/server/openapi_sql.go
index 13eee42e2c..c52250e358 100644
--- a/server/openapi_sql.go
+++ b/server/openapi_sql.go
@@ -31,7 +31,7 @@ type catalogService struct {
func newCatalogService(dbType advisorDB.Type) *catalogService {
return &catalogService{
- finder: catalog.NewEmptyFinder(&catalog.FinderContext{CheckIntegrity: false}, dbType),
+ finder: catalog.NewEmptyFinder(&catalog.FinderContext{CheckIntegrity: false, EngineType: dbType}),
}
}
diff --git a/sql-server/advise.go b/sql-server/advise.go
index d6a9948672..90135d5399 100644
--- a/sql-server/advise.go
+++ b/sql-server/advise.go
@@ -29,7 +29,7 @@ type catalogService struct {
func newCatalogService(dbType advisorDB.Type) *catalogService {
return &catalogService{
- finder: catalog.NewEmptyFinder(&catalog.FinderContext{CheckIntegrity: false}, dbType),
+ finder: catalog.NewEmptyFinder(&catalog.FinderContext{CheckIntegrity: false, EngineType: dbType}),
}
}
diff --git a/store/catalog.go b/store/catalog.go
index 3c6f5a81e3..cc15109b9f 100644
--- a/store/catalog.go
+++ b/store/catalog.go
@@ -2,15 +2,13 @@ package store
import (
"context"
- "sort"
- "strings"
- "github.com/pkg/errors"
+ "google.golang.org/protobuf/encoding/protojson"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/plugin/advisor/catalog"
advisorDB "github.com/bytebase/bytebase/plugin/advisor/db"
"github.com/bytebase/bytebase/plugin/db"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
var (
@@ -19,39 +17,31 @@ var (
// Catalog is the database catalog.
type Catalog struct {
- Database *catalog.Database
- Finder *catalog.Finder
+ Finder *catalog.Finder
}
// NewCatalog creates a new database catalog.
func (s *Store) NewCatalog(ctx context.Context, databaseID int, engineType db.Type) (catalog.Catalog, error) {
- database, err := s.GetDatabase(ctx, &api.DatabaseFind{
- ID: &databaseID,
- })
+ databaseMeta, err := s.GetDBSchema(ctx, databaseID)
if err != nil {
return nil, err
}
- if database == nil {
+ if databaseMeta == nil {
return nil, nil
}
- dbType, err := advisorDB.ConvertToAdvisorDBType(string(engineType))
- if err != nil {
+ var databaseSchema storepb.DatabaseMetadata
+ if err := protojson.Unmarshal([]byte(databaseMeta.Metadata), &databaseSchema); err != nil {
return nil, err
}
- databaseData := &catalog.Database{
- Name: database.Name,
- CharacterSet: database.CharacterSet,
- Collation: database.Collation,
- DbType: dbType,
- }
-
- if databaseData.SchemaList, err = s.getSchemaList(ctx, databaseID, engineType); err != nil {
+ dbType, err := advisorDB.ConvertToAdvisorDBType(string(engineType))
+ if err != nil {
return nil, err
}
- c := &Catalog{Database: databaseData}
- c.Finder = catalog.NewFinder(c.Database, &catalog.FinderContext{CheckIntegrity: true})
+
+ c := &Catalog{}
+ c.Finder = catalog.NewFinder(&databaseSchema, &catalog.FinderContext{CheckIntegrity: true, EngineType: dbType})
return c, nil
}
@@ -59,194 +49,3 @@ func (s *Store) NewCatalog(ctx context.Context, databaseID int, engineType db.Ty
func (c *Catalog) GetFinder() *catalog.Finder {
return c.Finder
}
-
-type schemaMap map[string]*catalog.Schema
-
-func (m schemaMap) getOrCreateSchema(name string) *catalog.Schema {
- if schema, found := m[name]; found {
- return schema
- }
-
- schema := &catalog.Schema{
- Name: name,
- }
- m[name] = schema
- return schema
-}
-
-func (s *Store) getSchemaList(ctx context.Context, databaseID int, engineType db.Type) ([]*catalog.Schema, error) {
- schemaSet := make(schemaMap)
-
- // find table list
- tableList, err := s.FindTable(ctx, &api.TableFind{
- DatabaseID: &databaseID,
- })
- if err != nil {
- return nil, err
- }
- for _, table := range tableList {
- tableData := convertTable(table)
- schemaName := ""
- if engineType == db.Postgres {
- if schemaName, tableData.Name, err = splitPGSchema(tableData.Name); err != nil {
- return nil, err
- }
- }
-
- // find index list
- indexList, err := s.FindIndex(ctx, &api.IndexFind{
- DatabaseID: &databaseID,
- TableID: &table.ID,
- })
- if err != nil {
- return nil, err
- }
- tableData.IndexList = convertIndexList(indexList)
-
- // find column list
- columnList, err := s.FindColumn(ctx, &api.ColumnFind{
- DatabaseID: &databaseID,
- TableID: &table.ID,
- })
- if err != nil {
- return nil, err
- }
- tableData.ColumnList = convertColumnList(columnList)
-
- schema := schemaSet.getOrCreateSchema(schemaName)
- schema.TableList = append(schema.TableList, tableData)
- }
-
- // find view list
- viewList, err := s.FindView(ctx, &api.ViewFind{
- DatabaseID: &databaseID,
- })
- if err != nil {
- return nil, err
- }
- for _, view := range viewList {
- viewData := convertView(view)
- schemaName := ""
- if engineType == db.Postgres {
- if schemaName, viewData.Name, err = splitPGSchema(viewData.Name); err != nil {
- return nil, err
- }
- }
- schema := schemaSet.getOrCreateSchema(schemaName)
- schema.ViewList = append(schema.ViewList, viewData)
- }
-
- // find extension list
- extensionList, err := s.FindDBExtension(ctx, &api.DBExtensionFind{
- DatabaseID: &databaseID,
- })
- if err != nil {
- return nil, err
- }
- for _, extension := range extensionList {
- schema := schemaSet.getOrCreateSchema(extension.Schema)
- schema.ExtensionList = append(schema.ExtensionList, &catalog.Extension{
- Name: extension.Name,
- Version: extension.Version,
- Description: extension.Description,
- })
- }
-
- var schemaList []*catalog.Schema
- for _, schema := range schemaSet {
- schemaList = append(schemaList, schema)
- }
- return schemaList, nil
-}
-
-func splitPGSchema(name string) (string, string, error) {
- list := strings.Split(name, ".")
- if len(list) != 2 {
- return "", "", errors.Errorf("split failed: the expected name is schemaName.name, but get %s", name)
- }
- return list[0], list[1], nil
-}
-
-func convertView(view *api.View) *catalog.View {
- return &catalog.View{
- Name: view.Name,
- Definition: view.Definition,
- Comment: view.Comment,
- }
-}
-
-func convertTable(table *api.Table) *catalog.Table {
- return &catalog.Table{
- Name: table.Name,
- Type: table.Type,
- Engine: table.Engine,
- Collation: table.Collation,
- RowCount: table.RowCount,
- DataSize: table.DataSize,
- IndexSize: table.IndexSize,
- DataFree: table.DataFree,
- CreateOptions: table.CreateOptions,
- Comment: table.Comment,
- }
-}
-
-func convertIndexList(list []*api.Index) []*catalog.Index {
- if len(list) == 0 {
- return nil
- }
- var res []*catalog.Index
- indexMap := make(map[string][]*api.Index)
- for _, expression := range list {
- indexMap[expression.Name] = append(indexMap[expression.Name], expression)
- }
-
- for _, expressionList := range indexMap {
- res = append(res, convertIndexExceptExpression(expressionList))
- }
- // Order it cause the random iteration order in Go, see https://go.dev/blog/maps
- sort.Slice(res, func(i, j int) bool {
- return res[i].Name < res[j].Name
- })
- return res
-}
-
-func convertIndexExceptExpression(list []*api.Index) *catalog.Index {
- if len(list) == 0 {
- return nil
- }
- res := &catalog.Index{
- Name: list[0].Name,
- Type: list[0].Type,
- Unique: list[0].Unique,
- Primary: list[0].Primary,
- Visible: list[0].Visible,
- Comment: list[0].Comment,
- }
- sort.Slice(list, func(i, j int) bool {
- return list[i].Position < list[j].Position
- })
- for _, expression := range list {
- res.ExpressionList = append(res.ExpressionList, expression.Expression)
- }
- return res
-}
-
-func convertColumnList(list []*api.Column) []*catalog.Column {
- if len(list) == 0 {
- return nil
- }
- var res []*catalog.Column
- for _, column := range list {
- res = append(res, &catalog.Column{
- Name: column.Name,
- Position: column.Position,
- Default: column.Default,
- Nullable: column.Nullable,
- Type: column.Type,
- CharacterSet: column.CharacterSet,
- Collation: column.Collation,
- Comment: column.Comment,
- })
- }
- return res
-}
diff --git a/store/catalog_test.go b/store/catalog_test.go
deleted file mode 100644
index 7dd0df7b6b..0000000000
--- a/store/catalog_test.go
+++ /dev/null
@@ -1,58 +0,0 @@
-package store
-
-import (
- "testing"
-
- "github.com/stretchr/testify/assert"
-
- "github.com/bytebase/bytebase/api"
- "github.com/bytebase/bytebase/plugin/advisor/catalog"
-)
-
-func TestConvertIndexList(t *testing.T) {
- type testData struct {
- origin []*api.Index
- want []*catalog.Index
- }
-
- tests := []testData{
- {
- origin: []*api.Index{
- {
- Name: "idx_1",
- Position: 1,
- Expression: "id",
- },
- {
- Name: "idx_1",
- Position: 2,
- Expression: "name",
- },
- {
- Name: "idx_1",
- Position: 3,
- Expression: "user",
- },
- {
- Name: "idx_2",
- Position: 1,
- Expression: "name",
- },
- },
- want: []*catalog.Index{
- {
- Name: "idx_1",
- ExpressionList: []string{"id", "name", "user"},
- },
- {
- Name: "idx_2",
- ExpressionList: []string{"name"},
- },
- },
- },
- }
-
- for _, test := range tests {
- assert.Equal(t, test.want, convertIndexList(test.origin))
- }
-}