2019-03-24 14:57:29 +01:00
|
|
|
package psql
|
|
|
|
|
|
|
|
import (
|
2019-09-26 06:35:31 +02:00
|
|
|
"context"
|
2019-03-24 14:57:29 +01:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
"github.com/gobuffalo/flect"
|
2019-09-30 07:48:29 +02:00
|
|
|
"github.com/jackc/pgtype"
|
2019-09-26 06:35:31 +02:00
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
2019-03-24 14:57:29 +01:00
|
|
|
)
|
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
type DBTable struct {
|
2019-09-26 06:35:31 +02:00
|
|
|
Name string
|
|
|
|
Type string
|
2019-06-14 06:32:15 +02:00
|
|
|
}
|
|
|
|
|
2019-09-26 06:35:31 +02:00
|
|
|
func GetTables(dbc *pgxpool.Conn) ([]*DBTable, error) {
|
2019-06-14 06:32:15 +02:00
|
|
|
sqlStmt := `
|
2019-09-29 21:20:59 +02:00
|
|
|
SELECT
|
|
|
|
c.relname as "name",
|
|
|
|
CASE c.relkind WHEN 'r' THEN 'table'
|
2019-09-26 06:35:31 +02:00
|
|
|
WHEN 'v' THEN 'view'
|
|
|
|
WHEN 'm' THEN 'materialized view'
|
|
|
|
WHEN 'f' THEN 'foreign table'
|
|
|
|
END as "type"
|
2019-06-14 06:32:15 +02:00
|
|
|
FROM pg_catalog.pg_class c
|
2019-09-29 21:20:59 +02:00
|
|
|
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
2019-06-14 06:32:15 +02:00
|
|
|
WHERE c.relkind IN ('r','v','m','f','')
|
2019-10-01 06:00:15 +02:00
|
|
|
AND n.nspname <> ('pg_catalog')
|
|
|
|
AND n.nspname <> ('information_schema')
|
|
|
|
AND n.nspname !~ ('^pg_toast')
|
2019-09-29 21:20:59 +02:00
|
|
|
AND pg_catalog.pg_table_is_visible(c.oid);`
|
2019-06-14 06:32:15 +02:00
|
|
|
|
2019-09-26 06:35:31 +02:00
|
|
|
var tables []*DBTable
|
2019-06-14 06:32:15 +02:00
|
|
|
|
2019-09-26 06:35:31 +02:00
|
|
|
rows, err := dbc.Query(context.Background(), sqlStmt)
|
2019-06-14 06:32:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error fetching tables: %s", err)
|
|
|
|
}
|
2019-09-26 06:35:31 +02:00
|
|
|
defer rows.Close()
|
2019-06-14 06:32:15 +02:00
|
|
|
|
2019-09-26 06:35:31 +02:00
|
|
|
for rows.Next() {
|
|
|
|
t := DBTable{}
|
|
|
|
err = rows.Scan(&t.Name, &t.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tables = append(tables, &t)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tables, nil
|
2019-06-14 06:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type DBColumn struct {
|
2019-10-01 06:00:15 +02:00
|
|
|
ID int16
|
2019-09-26 06:35:31 +02:00
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
NotNull bool
|
|
|
|
PrimaryKey bool
|
|
|
|
UniqueKey bool
|
|
|
|
FKeyTable string
|
2019-10-01 06:00:15 +02:00
|
|
|
FKeyColID []int16
|
2019-09-26 06:35:31 +02:00
|
|
|
fKeyColID pgtype.Int2Array
|
2019-06-14 06:32:15 +02:00
|
|
|
}
|
|
|
|
|
2019-09-26 06:35:31 +02:00
|
|
|
func GetColumns(dbc *pgxpool.Conn, schema, table string) ([]*DBColumn, error) {
|
2019-06-14 06:32:15 +02:00
|
|
|
sqlStmt := `
|
2019-09-29 21:20:59 +02:00
|
|
|
SELECT
|
|
|
|
f.attnum AS id,
|
|
|
|
f.attname AS name,
|
|
|
|
f.attnotnull AS notnull,
|
|
|
|
pg_catalog.format_type(f.atttypid,f.atttypmod) AS type,
|
|
|
|
CASE
|
|
|
|
WHEN p.contype = ('p'::char) THEN true
|
|
|
|
ELSE false
|
|
|
|
END AS primarykey,
|
|
|
|
CASE
|
|
|
|
WHEN p.contype = ('u'::char) THEN true
|
|
|
|
ELSE false
|
|
|
|
END AS uniquekey,
|
|
|
|
CASE
|
|
|
|
WHEN p.contype = ('f'::char) THEN g.relname
|
|
|
|
ELSE ''::text
|
|
|
|
END AS foreignkey,
|
|
|
|
CASE
|
|
|
|
WHEN p.contype = ('f'::char) THEN p.confkey
|
|
|
|
ELSE ARRAY[]::int2[]
|
|
|
|
END AS foreignkey_fieldnum
|
|
|
|
FROM pg_attribute f
|
|
|
|
JOIN pg_class c ON c.oid = f.attrelid
|
|
|
|
LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnum
|
|
|
|
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
|
|
LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey)
|
|
|
|
LEFT JOIN pg_class AS g ON p.confrelid = g.oid
|
|
|
|
WHERE c.relkind = ('r'::char)
|
|
|
|
AND n.nspname = $1 -- Replace with Schema name
|
|
|
|
AND c.relname = $2 -- Replace with table name
|
|
|
|
AND f.attnum > 0
|
|
|
|
AND f.attisdropped = false
|
|
|
|
ORDER BY id;`
|
2019-09-26 06:35:31 +02:00
|
|
|
|
|
|
|
rows, err := dbc.Query(context.Background(), sqlStmt, schema, table)
|
2019-06-14 06:32:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error fetching columns: %s", err)
|
|
|
|
}
|
2019-09-26 06:35:31 +02:00
|
|
|
defer rows.Close()
|
2019-06-14 06:32:15 +02:00
|
|
|
|
2019-10-01 06:00:15 +02:00
|
|
|
cmap := make(map[int16]*DBColumn)
|
2019-09-29 21:20:59 +02:00
|
|
|
|
2019-09-26 06:35:31 +02:00
|
|
|
for rows.Next() {
|
|
|
|
c := DBColumn{}
|
|
|
|
err = rows.Scan(&c.ID, &c.Name, &c.NotNull, &c.Type, &c.PrimaryKey, &c.UniqueKey,
|
|
|
|
&c.FKeyTable, &c.fKeyColID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-29 21:20:59 +02:00
|
|
|
|
|
|
|
if v, ok := cmap[c.ID]; ok {
|
|
|
|
if c.PrimaryKey {
|
|
|
|
v.PrimaryKey = true
|
|
|
|
}
|
|
|
|
if c.NotNull {
|
|
|
|
v.NotNull = true
|
|
|
|
}
|
|
|
|
if c.UniqueKey {
|
|
|
|
v.UniqueKey = true
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-01 06:00:15 +02:00
|
|
|
err := c.fKeyColID.AssignTo(&c.FKeyColID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-29 21:20:59 +02:00
|
|
|
cmap[c.ID] = &c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cols := make([]*DBColumn, 0, len(cmap))
|
|
|
|
for _, v := range cmap {
|
|
|
|
cols = append(cols, v)
|
2019-06-14 06:32:15 +02:00
|
|
|
}
|
|
|
|
|
2019-09-26 06:35:31 +02:00
|
|
|
return cols, nil
|
2019-06-14 06:32:15 +02:00
|
|
|
}
|
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
type DBSchema struct {
|
2019-06-14 06:32:15 +02:00
|
|
|
t map[string]*DBTableInfo
|
|
|
|
rm map[string]map[string]*DBRel
|
|
|
|
al map[string]struct{}
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-04-05 07:44:30 +02:00
|
|
|
type DBTableInfo struct {
|
2019-09-05 06:09:56 +02:00
|
|
|
Name string
|
|
|
|
Singular bool
|
|
|
|
PrimaryCol string
|
|
|
|
TSVCol string
|
|
|
|
Columns map[string]*DBColumn
|
|
|
|
ColumnNames []string
|
2019-04-05 07:44:30 +02:00
|
|
|
}
|
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
type RelType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
RelBelongTo RelType = iota + 1
|
|
|
|
RelOneToMany
|
|
|
|
RelOneToManyThrough
|
2019-05-13 01:27:26 +02:00
|
|
|
RelRemote
|
2019-03-24 14:57:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type DBRel struct {
|
|
|
|
Type RelType
|
|
|
|
Through string
|
|
|
|
ColT string
|
|
|
|
Col1 string
|
|
|
|
Col2 string
|
|
|
|
}
|
|
|
|
|
2019-09-26 06:35:31 +02:00
|
|
|
func NewDBSchema(db *pgxpool.Pool, aliases map[string][]string) (*DBSchema, error) {
|
2019-04-09 03:24:29 +02:00
|
|
|
schema := &DBSchema{
|
2019-06-14 06:32:15 +02:00
|
|
|
t: make(map[string]*DBTableInfo),
|
|
|
|
rm: make(map[string]map[string]*DBRel),
|
|
|
|
al: make(map[string]struct{}),
|
2019-04-09 03:24:29 +02:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-09-26 06:35:31 +02:00
|
|
|
dbc, err := db.Acquire(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error acquiring connection from pool")
|
|
|
|
}
|
|
|
|
defer dbc.Release()
|
|
|
|
|
|
|
|
tables, err := GetTables(dbc)
|
2019-03-24 14:57:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range tables {
|
2019-09-26 06:35:31 +02:00
|
|
|
cols, err := GetColumns(dbc, "public", t.Name)
|
2019-03-24 14:57:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
schema.updateSchema(t, cols, aliases)
|
2019-03-24 23:16:03 +01:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-03-24 23:16:03 +01:00
|
|
|
return schema, nil
|
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
func (s *DBSchema) updateSchema(
|
|
|
|
t *DBTable,
|
|
|
|
cols []*DBColumn,
|
|
|
|
aliases map[string][]string) {
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-03-24 23:16:03 +01:00
|
|
|
// Foreign key columns in current table
|
2019-10-01 06:00:15 +02:00
|
|
|
colByID := make(map[int16]*DBColumn)
|
2019-06-14 06:32:15 +02:00
|
|
|
columns := make(map[string]*DBColumn, len(cols))
|
2019-09-29 21:20:59 +02:00
|
|
|
colNames := make([]string, 0, len(cols))
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-04-09 03:24:29 +02:00
|
|
|
for i := range cols {
|
|
|
|
c := cols[i]
|
2019-09-05 06:09:56 +02:00
|
|
|
name := strings.ToLower(c.Name)
|
2019-09-29 21:20:59 +02:00
|
|
|
columns[name] = c
|
2019-09-05 06:09:56 +02:00
|
|
|
colNames = append(colNames, name)
|
2019-09-29 21:20:59 +02:00
|
|
|
colByID[c.ID] = c
|
2019-03-24 23:16:03 +01:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
singular := strings.ToLower(flect.Singularize(t.Name))
|
|
|
|
s.t[singular] = &DBTableInfo{
|
2019-09-05 06:09:56 +02:00
|
|
|
Name: t.Name,
|
|
|
|
Singular: true,
|
|
|
|
Columns: columns,
|
|
|
|
ColumnNames: colNames,
|
2019-06-14 06:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
plural := strings.ToLower(flect.Pluralize(t.Name))
|
|
|
|
s.t[plural] = &DBTableInfo{
|
2019-09-05 06:09:56 +02:00
|
|
|
Name: t.Name,
|
|
|
|
Singular: false,
|
|
|
|
Columns: columns,
|
|
|
|
ColumnNames: colNames,
|
2019-06-14 06:32:15 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 03:24:29 +02:00
|
|
|
ct := strings.ToLower(t.Name)
|
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
if al, ok := aliases[ct]; ok {
|
|
|
|
for i := range al {
|
|
|
|
k1 := flect.Singularize(al[i])
|
|
|
|
s.t[k1] = s.t[singular]
|
|
|
|
|
|
|
|
k2 := flect.Pluralize(al[i])
|
|
|
|
s.t[k2] = s.t[plural]
|
|
|
|
|
|
|
|
s.al[k1] = struct{}{}
|
|
|
|
s.al[k2] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jcols := make([]*DBColumn, 0, len(cols))
|
2019-05-15 04:32:12 +02:00
|
|
|
|
2019-03-24 23:16:03 +01:00
|
|
|
for _, c := range cols {
|
|
|
|
switch {
|
2019-04-05 07:44:30 +02:00
|
|
|
case c.Type == "tsvector":
|
2019-06-14 06:32:15 +02:00
|
|
|
s.t[singular].TSVCol = c.Name
|
|
|
|
s.t[plural].TSVCol = c.Name
|
2019-04-05 07:44:30 +02:00
|
|
|
|
2019-03-24 23:16:03 +01:00
|
|
|
case c.PrimaryKey:
|
2019-06-14 06:32:15 +02:00
|
|
|
s.t[singular].PrimaryCol = c.Name
|
|
|
|
s.t[plural].PrimaryCol = c.Name
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-03-24 23:16:03 +01:00
|
|
|
case len(c.FKeyTable) != 0:
|
|
|
|
if len(c.FKeyColID) == 0 {
|
2019-03-24 14:57:29 +01:00
|
|
|
continue
|
|
|
|
}
|
2019-03-24 23:16:03 +01:00
|
|
|
|
|
|
|
// Foreign key column name
|
|
|
|
ft := strings.ToLower(c.FKeyTable)
|
2019-04-09 03:24:29 +02:00
|
|
|
fc, ok := colByID[c.FKeyColID[0]]
|
2019-03-24 14:57:29 +01:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-03-24 23:16:03 +01:00
|
|
|
// Belongs-to relation between current table and the
|
|
|
|
// table in the foreign key
|
|
|
|
rel1 := &DBRel{RelBelongTo, "", "", c.Name, fc.Name}
|
2019-06-14 06:32:15 +02:00
|
|
|
s.SetRel(ct, ft, rel1)
|
2019-03-24 23:16:03 +01:00
|
|
|
|
|
|
|
// One-to-many relation between the foreign key table and the
|
|
|
|
// the current table
|
|
|
|
rel2 := &DBRel{RelOneToMany, "", "", fc.Name, c.Name}
|
2019-06-14 06:32:15 +02:00
|
|
|
s.SetRel(ft, ct, rel2)
|
2019-03-24 23:16:03 +01:00
|
|
|
|
|
|
|
jcols = append(jcols, c)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-24 23:16:03 +01:00
|
|
|
// If table contains multiple foreign key columns it's a possible
|
|
|
|
// join table for many-to-many relationships or multiple one-to-many
|
|
|
|
// relations
|
|
|
|
|
|
|
|
// Below one-to-many relations use the current table as the
|
|
|
|
// join table aka through table.
|
|
|
|
if len(jcols) > 1 {
|
2019-04-04 06:53:24 +02:00
|
|
|
for i := range jcols {
|
|
|
|
for n := range jcols {
|
|
|
|
if n != i {
|
2019-06-14 06:32:15 +02:00
|
|
|
s.updateSchemaOTMT(ct, jcols[i], jcols[n], colByID)
|
2019-04-04 06:53:24 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-24 23:16:03 +01:00
|
|
|
}
|
2019-04-04 06:53:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 03:24:29 +02:00
|
|
|
func (s *DBSchema) updateSchemaOTMT(
|
2019-05-15 04:32:12 +02:00
|
|
|
ct string,
|
|
|
|
col1, col2 *DBColumn,
|
2019-10-01 06:00:15 +02:00
|
|
|
colByID map[int16]*DBColumn) {
|
2019-04-09 03:24:29 +02:00
|
|
|
|
2019-04-04 06:53:24 +02:00
|
|
|
t1 := strings.ToLower(col1.FKeyTable)
|
|
|
|
t2 := strings.ToLower(col2.FKeyTable)
|
2019-03-24 23:16:03 +01:00
|
|
|
|
2019-04-09 03:24:29 +02:00
|
|
|
fc1, ok := colByID[col1.FKeyColID[0]]
|
2019-04-04 06:53:24 +02:00
|
|
|
if !ok {
|
|
|
|
return
|
2019-03-24 23:16:03 +01:00
|
|
|
}
|
2019-04-09 03:24:29 +02:00
|
|
|
fc2, ok := colByID[col2.FKeyColID[0]]
|
2019-04-04 06:53:24 +02:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// One-to-many-through relation between 1nd foreign key table and the
|
|
|
|
// 2nd foreign key table
|
|
|
|
//rel1 := &DBRel{RelOneToManyThrough, ct, fc1.Name, col1.Name}
|
|
|
|
rel1 := &DBRel{RelOneToManyThrough, ct, col2.Name, fc2.Name, col1.Name}
|
2019-06-14 06:32:15 +02:00
|
|
|
s.SetRel(t1, t2, rel1)
|
2019-04-04 06:53:24 +02:00
|
|
|
|
|
|
|
// One-to-many-through relation between 2nd foreign key table and the
|
|
|
|
// 1nd foreign key table
|
|
|
|
//rel2 := &DBRel{RelOneToManyThrough, ct, col2.Name, fc2.Name}
|
|
|
|
rel2 := &DBRel{RelOneToManyThrough, ct, col1.Name, fc1.Name, col2.Name}
|
2019-06-14 06:32:15 +02:00
|
|
|
s.SetRel(t2, t1, rel2)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
func (s *DBSchema) GetTable(table string) (*DBTableInfo, error) {
|
|
|
|
t, ok := s.t[table]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown table '%s'", table)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
func (s *DBSchema) SetRel(child, parent string, rel *DBRel) error {
|
|
|
|
sc := strings.ToLower(flect.Singularize(child))
|
|
|
|
pc := strings.ToLower(flect.Pluralize(child))
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
if _, ok := s.rm[sc]; !ok {
|
|
|
|
s.rm[sc] = make(map[string]*DBRel)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
if _, ok := s.rm[pc]; !ok {
|
|
|
|
s.rm[pc] = make(map[string]*DBRel)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
sp := strings.ToLower(flect.Singularize(parent))
|
|
|
|
pp := strings.ToLower(flect.Pluralize(parent))
|
|
|
|
|
|
|
|
s.rm[sc][sp] = rel
|
|
|
|
s.rm[sc][pp] = rel
|
|
|
|
s.rm[pc][sp] = rel
|
|
|
|
s.rm[pc][pp] = rel
|
|
|
|
|
|
|
|
return nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2019-04-05 07:44:30 +02:00
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
func (s *DBSchema) GetRel(child, parent string) (*DBRel, error) {
|
|
|
|
rel, ok := s.rm[child][parent]
|
2019-04-05 07:44:30 +02:00
|
|
|
if !ok {
|
2019-06-14 06:32:15 +02:00
|
|
|
return nil, fmt.Errorf("unknown relationship '%s' -> '%s'",
|
|
|
|
child, parent)
|
2019-04-05 07:44:30 +02:00
|
|
|
}
|
2019-06-14 06:32:15 +02:00
|
|
|
return rel, nil
|
2019-04-05 07:44:30 +02:00
|
|
|
}
|
2019-05-15 04:32:12 +02:00
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
func (s *DBSchema) IsAlias(name string) bool {
|
|
|
|
_, ok := s.al[name]
|
|
|
|
return ok
|
2019-05-15 04:32:12 +02:00
|
|
|
}
|