Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PostgreSQL]Detect full table name using search_path and information_schema #184

Merged
merged 5 commits into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
- name: Run tests
run: env PATH=`go env GOPATH`/bin:$PATH make ci
env:
DEBUG: "true"
GOPROXY: "https://proxy.golang.org"

- name: Run BigQuery integration
Expand Down
2 changes: 1 addition & 1 deletion datasource/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var tests = []struct {
relationCount int
}{
{"my://root:mypass@localhost:33306/testdb", "testdb", 9, 6},
{"pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable", "testdb", 11, 8},
{"pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable", "testdb", 12, 9},
{"json://../testdata/testdb.json", "testdb", 11, 12},
{"ms://SA:MSSQLServer-Passw0rd@localhost:11433/testdb", "testdb", 9, 6},
}
Expand Down
47 changes: 45 additions & 2 deletions drivers/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ func (p *Postgres) Analyze(s *schema.Schema) error {
}
s.Driver.Meta.CurrentSchema = currentSchema

// search_path
var searchPaths string
pathRows, err := p.db.Query(`SHOW search_path`)
defer pathRows.Close()
if err != nil {
return errors.WithStack(err)
}
for pathRows.Next() {
err := pathRows.Scan(&searchPaths)
if err != nil {
return errors.WithStack(err)
}
}
s.Driver.Meta.SearchPaths = strings.Split(searchPaths, ", ")

fullTableNames := []string{}

// tables
tableRows, err := p.db.Query(`
SELECT DISTINCT cls.oid AS oid, cls.relname AS table_name, tbl.table_type AS table_type, tbl.table_schema AS table_schema
Expand Down Expand Up @@ -82,6 +99,8 @@ ORDER BY oid`, s.Name)

name := fmt.Sprintf("%s.%s", tableSchema, tableName)

fullTableNames = append(fullTableNames, name)

table := &schema.Table{
Name: name,
Type: tableType,
Expand Down Expand Up @@ -331,9 +350,12 @@ ORDER BY ordinal_position
r.Columns = append(r.Columns, column)
column.ParentRelations = append(column.ParentRelations, r)
}
if !strings.Contains(strParentTable, ".") {
strParentTable = fmt.Sprintf("%s.%s", currentSchema, strParentTable)

dn, err := detectFullTableName(strParentTable, s.Driver.Meta.SearchPaths, fullTableNames)
if err != nil {
return err
}
strParentTable = dn
parentTable, err := s.FindTableByName(strParentTable)
if err != nil {
return err
Expand Down Expand Up @@ -453,6 +475,27 @@ GROUP BY c.relname, n.nspname, i.relname, i.oid, x.indexrelid
ORDER BY x.indexrelid`
}

func detectFullTableName(name string, searchPaths, fullTableNames []string) (string, error) {
if strings.Contains(name, ".") {
return name, nil
}
fns := []string{}
for _, n := range fullTableNames {
if strings.HasSuffix(n, name) {
for _, p := range searchPaths {
// TODO: Support $user
if n == fmt.Sprintf("%s.%s", p, name) {
fns = append(fns, n)
}
}
}
}
if len(fns) != 1 {
return "", errors.Errorf("can not detect table name: %s", name)
}
return fns[0], nil
}

func colkeyToInts(colkey string) []int {
ints := []int{}
if colkey == "" {
Expand Down
1 change: 1 addition & 0 deletions sample/adjust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
| [public.hyphen-table](public.hyphen-table.md) | 4 | | BASE TABLE |
| [administrator.blogs](administrator.blogs.md) | 6 | | BASE TABLE |
| [backup.blogs](backup.blogs.md) | 5 | | BASE TABLE |
| [backup.blog_options](backup.blog_options.md) | 4 | | BASE TABLE |

## Relations

Expand Down
33 changes: 33 additions & 0 deletions sample/adjust/backup.blog_options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# backup.blog_options

## Description

## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ------- | --------------------------- | ---------------------------------------- | -------- | -------- | ------------------------------- | ------- |
| id | integer | nextval('blog_options_id_seq'::regclass) | false | | | |
| blog_id | integer | | false | | [backup.blogs](backup.blogs.md) | |
| label | text | | true | | | |
| updated | timestamp without time zone | | true | | | |

## Constraints

| Name | Type | Definition |
| ----------------------- | ----------- | ------------------------------------------------------------ |
| blog_options_blog_id_fk | FOREIGN KEY | FOREIGN KEY (blog_id) REFERENCES blogs(id) ON DELETE CASCADE |
| blog_options_pkey | PRIMARY KEY | PRIMARY KEY (id) |

## Indexes

| Name | Definition |
| ----------------- | ----------------------------------------------------------------------------- |
| blog_options_pkey | CREATE UNIQUE INDEX blog_options_pkey ON backup.blog_options USING btree (id) |

## Relations

![er](backup.blog_options.png)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
Binary file added sample/adjust/backup.blog_options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 7 additions & 7 deletions sample/adjust/backup.blogs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ------- | --------------------------- | ---------------------------------------- | -------- | -------- | ------- | ------- |
| id | integer | nextval('backup.blogs_id_seq'::regclass) | false | | | |
| user_id | integer | | false | | | |
| dump | text | | false | | | |
| created | timestamp without time zone | | false | | | |
| updated | timestamp without time zone | | true | | | |
| Name | Type | Default | Nullable | Children | Parents | Comment |
| ------- | --------------------------- | --------------------------------- | -------- | --------------------------------------------- | ------- | ------- |
| id | integer | nextval('blogs_id_seq'::regclass) | false | [backup.blog_options](backup.blog_options.md) | | |
| user_id | integer | | false | | | |
| dump | text | | false | | | |
| created | timestamp without time zone | | false | | | |
| updated | timestamp without time zone | | true | | | |

## Constraints

Expand Down
Binary file modified sample/adjust/backup.blogs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sample/adjust/schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions sample/postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
| [public.hyphen-table](public.hyphen-table.md) | 4 | | BASE TABLE |
| [administrator.blogs](administrator.blogs.md) | 6 | | BASE TABLE |
| [backup.blogs](backup.blogs.md) | 5 | | BASE TABLE |
| [backup.blog_options](backup.blog_options.md) | 4 | | BASE TABLE |

## Relations

Expand Down
33 changes: 33 additions & 0 deletions sample/postgres/backup.blog_options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# backup.blog_options

## Description

## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | integer | nextval('blog_options_id_seq'::regclass) | false | | | |
| blog_id | integer | | false | | [backup.blogs](backup.blogs.md) | |
| label | text | | true | | | |
| updated | timestamp without time zone | | true | | | |

## Constraints

| Name | Type | Definition |
| ---- | ---- | ---------- |
| blog_options_blog_id_fk | FOREIGN KEY | FOREIGN KEY (blog_id) REFERENCES blogs(id) ON DELETE CASCADE |
| blog_options_pkey | PRIMARY KEY | PRIMARY KEY (id) |

## Indexes

| Name | Definition |
| ---- | ---------- |
| blog_options_pkey | CREATE UNIQUE INDEX blog_options_pkey ON backup.blog_options USING btree (id) |

## Relations

![er](backup.blog_options.png)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
Binary file added sample/postgres/backup.blog_options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion sample/postgres/backup.blogs.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | integer | nextval('backup.blogs_id_seq'::regclass) | false | | | |
| id | integer | nextval('blogs_id_seq'::regclass) | false | [backup.blog_options](backup.blog_options.md) | | |
| user_id | integer | | false | | | |
| dump | text | | false | | | |
| created | timestamp without time zone | | false | | | |
Expand Down
Binary file modified sample/postgres/backup.blogs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sample/postgres/schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type Relation struct {

type DriverMeta struct {
CurrentSchema string `json:"current_schema,omitempty" yaml:"currentSchema,omitempty"`
SearchPaths []string `json:"search_paths,omitempty" yaml:"searchPaths,omitempty"`
Dict *dict.Dict `json:"dict,omitempty"`
}

Expand Down
12 changes: 12 additions & 0 deletions testdata/pg.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
DROP TRIGGER IF EXISTS update_users_updated ON users;
DROP TRIGGER IF EXISTS update_posts_updated ON posts;
DROP TABLE IF EXISTS backup.blog_options;
DROP TABLE IF EXISTS backup.blogs;
DROP TABLE IF EXISTS administrator.blogs;
DROP VIEW IF EXISTS post_comments;
Expand Down Expand Up @@ -154,10 +155,21 @@ CREATE TRIGGER update_users_updated

CREATE SCHEMA backup;

ALTER ROLE postgres in DATABASE testdb SET search_path TO "$user",public,backup;

CREATE TABLE backup.blogs (
id serial PRIMARY KEY,
user_id int NOT NULL,
dump text NOT NULL,
created timestamp NOT NULL,
updated timestamp
);

CREATE TABLE backup.blog_options (
id serial PRIMARY KEY,
blog_id int NOT NULL,
label text,
updated timestamp,
CONSTRAINT blog_options_blog_id_fk FOREIGN KEY(blog_id) REFERENCES backup.blogs(id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE
);