Skip to content

Commit

Permalink
surface index uniqueness (#88)
Browse files Browse the repository at this point in the history
resolves #87

Uniqueness is a basic, but important, bit of information for an index.
This information has been available from the DB but it was never
surfaced.
  • Loading branch information
Etomyutikos authored and natefinch committed Dec 21, 2017
1 parent ca6baf0 commit 25eb64a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion database/drivers/mysql/parse.go
Expand Up @@ -148,7 +148,7 @@ func parse(log *log.Logger, conn string, schemaNames []string, filterTables func
}
}
if index == nil {
index = &database.Index{Name: s.IndexName}
index = &database.Index{Name: s.IndexName, IsUnique: s.NonUnique == 0}
schemaIndex[s.TableName] = append(schemaIndex[s.TableName], index)
}

Expand Down
8 changes: 5 additions & 3 deletions database/drivers/postgres/parse.go
Expand Up @@ -227,7 +227,7 @@ outer:
}
}
if index == nil {
index = &database.Index{Name: r.IndexName}
index = &database.Index{Name: r.IndexName, IsUnique: r.IsUnique}
schemaIndex[r.TableName] = append(schemaIndex[r.TableName], index)
}

Expand Down Expand Up @@ -434,6 +434,7 @@ type indexResult struct {
SchemaName string
TableName string
IndexName string
IsUnique bool
Columns []string
}

Expand All @@ -443,6 +444,7 @@ func queryIndexes(log *log.Logger, db *sql.DB, schemaNames []string) ([]indexRes
n.nspname as schema,
i.indrelid::regclass as table,
c.relname as name,
i.indisunique as is_unique,
array_to_string(ARRAY(
SELECT pg_get_indexdef(i.indexrelid, k + 1, true)
FROM generate_subscripts(i.indkey, 1) as k
Expand All @@ -464,16 +466,16 @@ func queryIndexes(log *log.Logger, db *sql.DB, schemaNames []string) ([]indexRes

query := fmt.Sprintf(q, strings.Join(spots, ", "))
rows, err := db.Query(query, vals...)
defer rows.Close()
if err != nil {
return nil, errors.WithMessage(err, "error querying indexes")
}
defer rows.Close()

var results []indexResult
for rows.Next() {
var r indexResult
var cs string
if err := rows.Scan(&r.SchemaName, &r.TableName, &r.IndexName, &cs); err != nil {
if err := rows.Scan(&r.SchemaName, &r.TableName, &r.IndexName, &r.IsUnique, &cs); err != nil {
return nil, errors.WithMessage(err, "error scanning index")
}
r.Columns = strings.Split(cs, ",") // array converted to string in query
Expand Down
7 changes: 4 additions & 3 deletions database/info.go
Expand Up @@ -38,8 +38,9 @@ type Table struct {

// Index contains the definition of a database index.
type Index struct {
Name string // name of the index in the database
Columns []*Column // list of columns in this index
Name string // name of the index in the database
IsUnique bool // true if the index is unique
Columns []*Column // list of columns in this index
}

// PrimaryKey contains the definition of a database primary key.
Expand All @@ -50,7 +51,7 @@ type PrimaryKey struct {
Name string // the original name of the key constraint in the db
}

// Foreign Key contains the definition of a database foreign key
// ForeignKey contains the definition of a database foreign key
type ForeignKey struct {
SchemaName string // the original name of the schema in the db
TableName string // the original name of the table in the db
Expand Down
3 changes: 2 additions & 1 deletion run/convert.go
Expand Up @@ -114,7 +114,8 @@ func makeData(log *log.Logger, info *database.Info, cfg *Config) (*data.DBData,

for _, i := range t.Indexes {
index := &data.Index{
DBName: i.Name,
DBName: i.Name,
IsUnique: i.IsUnique,
}
for _, c := range i.Columns {
index.Columns = append(index.Columns, table.ColumnsByName[c.Name])
Expand Down
11 changes: 6 additions & 5 deletions run/data/data.go
Expand Up @@ -100,7 +100,7 @@ type Column struct {
Orig interface{} `yaml:"-" json:"-"` // the raw database column data
}

// Foreign Key contains the
// ForeignKey contains the
type ForeignKey struct {
DBName string // the original name of the foreign key constraint in the db
Name string // the converted name of the foreign key constraint
Expand All @@ -111,7 +111,7 @@ type ForeignKey struct {
FKColumns ForeignKeyColumns // all foreign key columns belonging to the foreign key
}

// Foreign Column contains the definition of a database foreign key at the kcolumn level
// ForeignKeyColumn contains the definition of a database foreign key at the kcolumn level
type ForeignKeyColumn struct {
DBName string // the original name of the foreign key constraint in the db
ColumnDBName string // the original name of the column in the db
Expand All @@ -122,9 +122,10 @@ type ForeignKeyColumn struct {

// Index is the data about a table index.
type Index struct {
Name string // the converted name of the index
DBName string // dbname of the index
Columns Columns // columns used in the index
Name string // the converted name of the index
DBName string // dbname of the index
IsUnique bool // true if index is unique
Columns Columns // columns used in the index
}

// Enum represents a type that has a set of allowed values.
Expand Down
2 changes: 1 addition & 1 deletion run/preview.go
Expand Up @@ -30,7 +30,7 @@ Enum: {{.Name}}({{$schema}}.{{.DBName}})
Table: {{.Name}}({{$schema}}.{{.DBName}}){{if ne .Comment ""}}; {{.Comment}}{{end}}
{{makeTable .Columns "{{.Name}}|{{.DBName}}|{{.Type}}|{{.DBType}}|{{.IsArray}}|{{.IsPrimaryKey}}|{{.IsFK}}|{{.HasFKRef}}|{{.Length}}|{{.UserDefined}}|{{.Nullable}}|{{.HasDefault}}|{{.Comment}}" "Name" "DBName" "Type" "DBType" "IsArray" "IsPrimaryKey" "IsFK" "HasFKRef" "Length" "UserDefined" "Nullable" "HasDefault" "Comment" -}}
Indexes:
{{makeTable .Indexes "{{.Name}}|{{.DBName}}|{{join .Columns.Names \", \"}}" "Name" "DBName" "Columns"}}
{{makeTable .Indexes "{{.Name}}|{{.DBName}}|{{.IsUnique}}|{{join .Columns.Names \", \"}}" "Name" "DBName" "IsUnique" "Columns"}}
{{end -}}
{{end -}}
`))
Expand Down
23 changes: 13 additions & 10 deletions run/preview_test.go
Expand Up @@ -71,7 +71,8 @@ func (dummyDriver) Parse(log *log.Logger, conn string, schemaNames []string, fil
Nullable: true,
}},
Indexes: []*database.Index{{
Name: "col1_pkey",
Name: "col1_pkey",
IsUnique: true,
Columns: []*database.Column{{
Name: "col1",
Type: "int",
Expand Down Expand Up @@ -202,6 +203,7 @@ const expectYaml = `schemas:
indexes:
- name: abc col1_pkey
dbname: col1_pkey
isunique: true
columns:
- name: abc col1
dbname: col1
Expand Down Expand Up @@ -324,11 +326,11 @@ Table: abc table(schema.table); a table
| abc col4 | col4 | | *string | false | false | false | false | 0 | false | true | false | |
+----------+--------+----------+---------+---------+--------------+-------+----------+--------+-------------+----------+------------+--------------+
Indexes:
+---------------+-----------+----------+
| Name | DBName | Columns |
+---------------+-----------+----------+
| abc col1_pkey | col1_pkey | abc col1 |
+---------------+-----------+----------+
+---------------+-----------+----------+----------+
| Name | DBName | IsUnique | Columns |
+---------------+-----------+----------+----------+
| abc col1_pkey | col1_pkey | true | abc col1 |
+---------------+-----------+----------+----------+
Table: abc tb2(schema.tb2)
Expand All @@ -339,10 +341,10 @@ Table: abc tb2(schema.tb2)
| abc col2 | col2 | INTEGER | int | false | false | true | false | 0 | false | false | false | |
+----------+--------+---------+--------+---------+--------------+-------+----------+--------+-------------+----------+------------+---------+
Indexes:
+------+--------+---------+
| Name | DBName | Columns |
+------+--------+---------+
+------+--------+---------+
+------+--------+----------+---------+
| Name | DBName | IsUnique | Columns |
+------+--------+----------+---------+
+------+--------+----------+---------+
`

Expand Down Expand Up @@ -492,6 +494,7 @@ var expectJSON = `
{
"Name": "abc col1_pkey",
"DBName": "col1_pkey",
"IsUnique": true,
"Columns": [
{
"Name": "abc col1",
Expand Down
1 change: 1 addition & 0 deletions site/content/templates/data.md
Expand Up @@ -230,6 +230,7 @@ have the following properties:
| --- | --- | --- |
| Name | string | the converted name of the index
| DBName | string | the name of the index from the database
| IsUnique | bool | true if the index is unique
| Columns | [Columns](#columns) | the list of the columns used in the index

### Indexes
Expand Down

0 comments on commit 25eb64a

Please sign in to comment.