Skip to content

Commit

Permalink
Add new elements to returned dbinfo struct
Browse files Browse the repository at this point in the history
Signed-off-by: Rohit Nayak <[email protected]>
  • Loading branch information
rohit-nayak-ps committed Dec 1, 2024
1 parent e76c125 commit ee3ed63
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 66 deletions.
21 changes: 21 additions & 0 deletions go/dbinfo/dbinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type Info struct {
FileType string `json:"fileType"`
Tables []*TableInfo `json:"tables"`
GlobalVariables map[string]string `json:"globalVariables"`
PrimaryKeys PrimaryKeys `json:"primaryKeys"`
Indexes Indexes `json:"indexes"`
ForeignKeys ForeignKeys `json:"foreignKeys"`
}

func Get(cfg Config) (*Info, error) {
Expand Down Expand Up @@ -115,10 +118,28 @@ func Get(cfg Config) (*Info, error) {
return nil, err
}

primaryKeys, err := dbh.getPrimaryKeys()
if err != nil {
return nil, err
}

indexes, err := dbh.getIndexes()
if err != nil {
return nil, err
}

foreignKeys, err := dbh.getForeignKeys()
if err != nil {
return nil, err
}

dbInfo := &Info{
FileType: "dbinfo",
Tables: tableInfo,
GlobalVariables: globalVariables,
PrimaryKeys: primaryKeys,
Indexes: indexes,
ForeignKeys: foreignKeys,
}
return dbInfo, nil
}
Expand Down
50 changes: 25 additions & 25 deletions go/dbinfo/dbinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ func TestDBInfoGet(t *testing.T) {
"film_category": {"film_id", "category_id"},
"film_actor": {"actor_id", "film_id"},
}
for tableName, columns := range want {
for tableName, Columns := range want {
pk, ok := pks[tableName]
require.True(t, ok)
require.Equal(t, columns, pk.columns)
require.Equal(t, Columns, pk.Columns)
}
})

Expand All @@ -169,16 +169,16 @@ func TestDBInfoGet(t *testing.T) {
require.Equal(t, 16, idxs.len())
idx, ok := idxs["film_actor"]
require.True(t, ok)
require.Len(t, idx.indexes, 2)
require.Equal(t, "idx_fk_film_id", idx.indexes["idx_fk_film_id"].indexName)
require.Equal(t, []string{"film_id"}, idx.indexes["idx_fk_film_id"].columns)
require.Len(t, idx.Indexes, 2)
require.Equal(t, "idx_fk_film_id", idx.Indexes["idx_fk_film_id"].IndexName)
require.Equal(t, []string{"film_id"}, idx.Indexes["idx_fk_film_id"].Columns)
idx, ok = idxs["rental"]
require.True(t, ok)
require.Len(t, idx.indexes, 5)
require.Equal(t, "rental_date", idx.indexes["rental_date"].indexName)
require.Equal(t, []string{"rental_date", "inventory_id", "customer_id"}, idx.indexes["rental_date"].columns)
require.Equal(t, "PRIMARY", idx.indexes["PRIMARY_KEY"].indexName)
require.Equal(t, []string{"rental_id"}, idx.indexes["PRIMARY_KEY"].columns)
require.Len(t, idx.Indexes, 5)
require.Equal(t, "rental_date", idx.Indexes["rental_date"].IndexName)
require.Equal(t, []string{"rental_date", "inventory_id", "customer_id"}, idx.Indexes["rental_date"].Columns)
require.Equal(t, "PRIMARY", idx.Indexes["PRIMARY_KEY"].IndexName)
require.Equal(t, []string{"rental_id"}, idx.Indexes["PRIMARY_KEY"].Columns)
})

t.Run("foreign keys", func(t *testing.T) {
Expand All @@ -188,24 +188,24 @@ func TestDBInfoGet(t *testing.T) {
fk, ok := fks["city"]
require.True(t, ok)
require.Len(t, fk, 1)
require.Equal(t, "city", fk[0].tableName)
require.Equal(t, "country_id", fk[0].columnName)
require.Equal(t, "fk_city_country", fk[0].constraintName)
require.Equal(t, "country", fk[0].referencedTableName)
require.Equal(t, "country_id", fk[0].referencedColumnName)
require.Equal(t, "city", fk[0].TableName)
require.Equal(t, "country_id", fk[0].ColumnName)
require.Equal(t, "fk_city_country", fk[0].ConstraintName)
require.Equal(t, "country", fk[0].ReferencedTableName)
require.Equal(t, "country_id", fk[0].ReferencedColumnName)

fk, ok = fks["store"]
require.True(t, ok)
require.Len(t, fk, 2)
require.Equal(t, "store", fk[0].tableName)
require.Equal(t, "address_id", fk[0].columnName)
require.Equal(t, "fk_store_address", fk[0].constraintName)
require.Equal(t, "address", fk[0].referencedTableName)
require.Equal(t, "address_id", fk[0].referencedColumnName)
require.Equal(t, "store", fk[1].tableName)
require.Equal(t, "manager_staff_id", fk[1].columnName)
require.Equal(t, "fk_store_staff", fk[1].constraintName)
require.Equal(t, "staff", fk[1].referencedTableName)
require.Equal(t, "staff_id", fk[1].referencedColumnName)
require.Equal(t, "store", fk[0].TableName)
require.Equal(t, "address_id", fk[0].ColumnName)
require.Equal(t, "fk_store_address", fk[0].ConstraintName)
require.Equal(t, "address", fk[0].ReferencedTableName)
require.Equal(t, "address_id", fk[0].ReferencedColumnName)
require.Equal(t, "store", fk[1].TableName)
require.Equal(t, "manager_staff_id", fk[1].ColumnName)
require.Equal(t, "fk_store_staff", fk[1].ConstraintName)
require.Equal(t, "staff", fk[1].ReferencedTableName)
require.Equal(t, "staff_id", fk[1].ReferencedColumnName)
})
}
83 changes: 42 additions & 41 deletions go/dbinfo/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,20 @@ func (dbh *DBHelper) getGlobalVariables() (map[string]string, error) {
return gv, nil
}

type primaryKey struct {
tableName string
columns []string
type PrimaryKey struct {
TableName string `json:"tableName"`
Columns []string `json:"columns"`
}
type primaryKeys map[string]*primaryKey
type PrimaryKeys map[string]*PrimaryKey

func (dbh *DBHelper) getPrimaryKeys() (primaryKeys, error) {
func (dbh *DBHelper) getPrimaryKeys() (PrimaryKeys, error) {
vtConn, cancel, err := dbh.GetConnection()
if err != nil {
return nil, err
}
defer cancel()

pks := make(primaryKeys)
pks := make(PrimaryKeys)
queryPrimaryKeys := "select table_name, column_name from information_schema.key_column_usage where constraint_name = 'PRIMARY' and table_schema = '%s' order by table_name"
query := fmt.Sprintf(queryPrimaryKeys, dbh.vtParams.DbName)
qr, err := vtConn.ExecuteFetch(query, -1, false)
Expand All @@ -156,38 +156,39 @@ func (dbh *DBHelper) getPrimaryKeys() (primaryKeys, error) {
columnName := row[1].ToString()
pk, ok := pks[tableName]
if !ok {
pk = &primaryKey{tableName: tableName}
pk = &PrimaryKey{TableName: tableName}
pks[tableName] = pk
}
pk.columns = append(pk.columns, columnName)
pk.Columns = append(pk.Columns, columnName)
}
return pks, nil
}

type tableIndex struct {
tableName string
indexes map[string]*index
type TableIndex struct {
TableName string `json:"tableName"`
Indexes map[string]*Index `json:"indexes"`
}
type index struct {
indexName string
columns []string
nonUnique bool

type Index struct {
IndexName string `json:"indexName"`
Columns []string `json:"columns"`
NonUnique bool `json:"nonUnique,omitempty"`
}

type indexes map[string]*tableIndex
type Indexes map[string]*TableIndex

func (i *indexes) len() int {
func (i *Indexes) len() int {
return len(*i)
}

func (dbh *DBHelper) getIndexes() (indexes, error) {
func (dbh *DBHelper) getIndexes() (Indexes, error) {
vtConn, cancel, err := dbh.GetConnection()
if err != nil {
return nil, err
}
defer cancel()

idxs := make(indexes)
idxs := make(Indexes)
queryIndexes := "select table_name, index_name, column_name, non_unique from information_schema.statistics where table_schema = '%s' order by table_name, index_name"
query := fmt.Sprintf(queryIndexes, dbh.vtParams.DbName)
qr, err := vtConn.ExecuteFetch(query, -1, false)
Expand All @@ -201,19 +202,19 @@ func (dbh *DBHelper) getIndexes() (indexes, error) {
nonUnique, _ := row[3].ToBool()
tidx, ok := idxs[tableName]
if !ok {
tidx = &tableIndex{tableName: tableName, indexes: make(map[string]*index)}
tidx = &TableIndex{TableName: tableName, Indexes: make(map[string]*Index)}
idxs[tableName] = tidx
}
idxName := indexName
if idxName == "PRIMARY" {
idxName = "PRIMARY_KEY"
}
idx, ok := tidx.indexes[idxName]
idx, ok := tidx.Indexes[idxName]
if !ok {
idx = &index{indexName: indexName, nonUnique: nonUnique}
tidx.indexes[idxName] = idx
idx = &Index{IndexName: indexName, NonUnique: nonUnique}
tidx.Indexes[idxName] = idx
}
idx.columns = append(idx.columns, columnName)
idx.Columns = append(idx.Columns, columnName)
}
return idxs, nil
}
Expand All @@ -223,28 +224,28 @@ func (dbh *DBHelper) getIndexes() (indexes, error) {
// FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
// WHERE TABLE_SCHEMA = 'your_database_name' AND REFERENCED_TABLE_NAME IS NOT NULL;

type foreignKey struct {
tableName string
columnName string
constraintName string
referencedTableName string
referencedColumnName string
type ForeignKey struct {
TableName string `json:"tableName"`
ColumnName string `json:"columnName"`
ConstraintName string `json:"constraintName"`
ReferencedTableName string `json:"referencedTableName"`
ReferencedColumnName string `json:"referencedColumnName"`
}

func (fk *foreignKey) String() string {
return fmt.Sprintf("Table: %s, Column: %s, Constraint: %s, RefTable: %s, RefColumn: %s", fk.tableName, fk.columnName, fk.constraintName, fk.referencedTableName, fk.referencedColumnName)
func (fk *ForeignKey) String() string {
return fmt.Sprintf("Table: %s, Column: %s, Constraint: %s, RefTable: %s, RefColumn: %s", fk.TableName, fk.ColumnName, fk.ConstraintName, fk.ReferencedTableName, fk.ReferencedColumnName)
}

type foreignKeys map[string][]*foreignKey
type ForeignKeys map[string][]*ForeignKey

func (dbh *DBHelper) getForeignKeys() (foreignKeys, error) {
func (dbh *DBHelper) getForeignKeys() (ForeignKeys, error) {
vtConn, cancel, err := dbh.GetConnection()
if err != nil {
return nil, err
}
defer cancel()

fks := make(foreignKeys)
fks := make(ForeignKeys)
queryForeignKeys := "select table_name, column_name, constraint_name, referenced_table_name, referenced_column_name from information_schema.key_column_usage where table_schema = '%s' and referenced_table_name is not null"
query := fmt.Sprintf(queryForeignKeys, dbh.vtParams.DbName)
qr, err := vtConn.ExecuteFetch(query, -1, false)
Expand All @@ -257,12 +258,12 @@ func (dbh *DBHelper) getForeignKeys() (foreignKeys, error) {
constraintName := row[2].ToString()
referencedTableName := row[3].ToString()
referencedColumnName := row[4].ToString()
fk := &foreignKey{
tableName: tableName,
columnName: columnName,
constraintName: constraintName,
referencedTableName: referencedTableName,
referencedColumnName: referencedColumnName,
fk := &ForeignKey{
TableName: tableName,
ColumnName: columnName,
ConstraintName: constraintName,
ReferencedTableName: referencedTableName,
ReferencedColumnName: referencedColumnName,
}
fks[tableName] = append(fks[tableName], fk)
}
Expand Down

0 comments on commit ee3ed63

Please sign in to comment.