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]Fix parsing double-quoted table/column name in definition of Foreign Key #154

Merged
merged 2 commits into from
Jan 16, 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
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, 7},
{"pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable", "testdb", 11, 8},
{"json://../testdata/testdb.json", "testdb", 7, 9},
{"ms://SA:MSSQLServer-Passw0rd@localhost:11433/testdb", "testdb", 9, 6},
}
Expand Down
12 changes: 9 additions & 3 deletions drivers/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,15 @@ ORDER BY ordinal_position
// Relations
for _, r := range relations {
result := reFK.FindAllStringSubmatch(r.Def, -1)
strColumns := strings.Split(result[0][1], ", ")
strParentTable := result[0][2]
strParentColumns := strings.Split(result[0][3], ", ")
strColumns := []string{}
for _, c := range strings.Split(result[0][1], ", ") {
strColumns = append(strColumns, strings.Trim(c, `"`))
}
strParentTable := strings.Trim(result[0][2], `"`)
strParentColumns := []string{}
for _, c := range strings.Split(result[0][3], ", ") {
strParentColumns = append(strParentColumns, strings.Trim(c, `"`))
}
for _, c := range strColumns {
column, err := r.Table.FindColumnByName(c)
if err != nil {
Expand Down
20 changes: 16 additions & 4 deletions sample/adjust/CamelizeTable.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@

## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ------- | --------------------------- | ------------------ | -------- | -------- | ------- | ------- |
| id | uuid | uuid_generate_v4() | false | | | |
| created | timestamp without time zone | | false | | | |
| Name | Type | Default | Nullable | Children | Parents | Comment |
| ------- | --------------------------- | ------------------ | -------- | ------------------------------- | ------- | ------- |
| id | uuid | uuid_generate_v4() | false | [hyphen-table](hyphen-table.md) | | |
| created | timestamp without time zone | | false | | | |

## Constraints

| Name | Type | Definition |
| -------------------- | ------ | ----------- |
| CamelizeTable_id_key | UNIQUE | UNIQUE (id) |

## Indexes

| Name | Definition |
| -------------------- | ------------------------------------------------------------------------------------- |
| CamelizeTable_id_key | CREATE UNIQUE INDEX "CamelizeTable_id_key" ON public."CamelizeTable" USING btree (id) |

## Relations

Expand Down
Binary file modified sample/adjust/CamelizeTable.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/adjust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
| [logs](logs.md) | 7 | audit log table | BASE TABLE |
| [post_comments](post_comments.md) | 7 | post and comments View table | VIEW |
| [CamelizeTable](CamelizeTable.md) | 2 | | BASE TABLE |
| [hyphen-table](hyphen-table.md) | 3 | | BASE TABLE |
| [hyphen-table](hyphen-table.md) | 4 | | BASE TABLE |
| [administrator.blogs](administrator.blogs.md) | 6 | | BASE TABLE |
| [backup.blogs](backup.blogs.md) | 5 | | BASE TABLE |

Expand Down
24 changes: 19 additions & 5 deletions sample/adjust/hyphen-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@

## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ------------- | --------------------------- | ------------------ | -------- | -------- | ------- | ------- |
| id | uuid | uuid_generate_v4() | false | | | |
| hyphen-column | text | | false | | | |
| created | timestamp without time zone | | false | | | |
| Name | Type | Default | Nullable | Children | Parents | Comment |
| --------------- | --------------------------- | ------------------ | -------- | -------- | --------------------------------- | ------- |
| id | uuid | uuid_generate_v4() | false | | | |
| hyphen-column | text | | false | | | |
| CamelizeTableId | uuid | | false | | [CamelizeTable](CamelizeTable.md) | |
| created | timestamp without time zone | | false | | | |

## Constraints

| Name | Type | Definition |
| ------------------------------- | ----------- | -------------------------------------------------------------------------------- |
| hyphen-table_CamelizeTableId_fk | FOREIGN KEY | FOREIGN KEY ("CamelizeTableId") REFERENCES "CamelizeTable"(id) ON DELETE CASCADE |
| hyphen-table_hyphen-column_key | UNIQUE | UNIQUE ("hyphen-column") |

## Indexes

| Name | Definition |
| ------------------------------ | ----------------------------------------------------------------------------------------------------------- |
| hyphen-table_hyphen-column_key | CREATE UNIQUE INDEX "hyphen-table_hyphen-column_key" ON public."hyphen-table" USING btree ("hyphen-column") |

## Relations

Expand Down
Binary file modified sample/adjust/hyphen-table.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.
14 changes: 13 additions & 1 deletion sample/postgres/CamelizeTable.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | uuid | uuid_generate_v4() | false | | | |
| id | uuid | uuid_generate_v4() | false | [hyphen-table](hyphen-table.md) | | |
| created | timestamp without time zone | | false | | | |

## Constraints

| Name | Type | Definition |
| ---- | ---- | ---------- |
| CamelizeTable_id_key | UNIQUE | UNIQUE (id) |

## Indexes

| Name | Definition |
| ---- | ---------- |
| CamelizeTable_id_key | CREATE UNIQUE INDEX "CamelizeTable_id_key" ON public."CamelizeTable" USING btree (id) |

## Relations

![er](CamelizeTable.png)
Expand Down
Binary file modified sample/postgres/CamelizeTable.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/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
| [logs](logs.md) | 7 | audit log table | BASE TABLE |
| [post_comments](post_comments.md) | 7 | post and comments View table | VIEW |
| [CamelizeTable](CamelizeTable.md) | 2 | | BASE TABLE |
| [hyphen-table](hyphen-table.md) | 3 | | BASE TABLE |
| [hyphen-table](hyphen-table.md) | 4 | | BASE TABLE |
| [administrator.blogs](administrator.blogs.md) | 6 | | BASE TABLE |
| [backup.blogs](backup.blogs.md) | 5 | | BASE TABLE |

Expand Down
14 changes: 14 additions & 0 deletions sample/postgres/hyphen-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,22 @@
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | uuid | uuid_generate_v4() | false | | | |
| hyphen-column | text | | false | | | |
| CamelizeTableId | uuid | | false | | [CamelizeTable](CamelizeTable.md) | |
| created | timestamp without time zone | | false | | | |

## Constraints

| Name | Type | Definition |
| ---- | ---- | ---------- |
| hyphen-table_CamelizeTableId_fk | FOREIGN KEY | FOREIGN KEY ("CamelizeTableId") REFERENCES "CamelizeTable"(id) ON DELETE CASCADE |
| hyphen-table_hyphen-column_key | UNIQUE | UNIQUE ("hyphen-column") |

## Indexes

| Name | Definition |
| ---- | ---------- |
| hyphen-table_hyphen-column_key | CREATE UNIQUE INDEX "hyphen-table_hyphen-column_key" ON public."hyphen-table" USING btree ("hyphen-column") |

## Relations

![er](hyphen-table.png)
Expand Down
Binary file modified sample/postgres/hyphen-table.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.
8 changes: 6 additions & 2 deletions testdata/pg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,17 @@ CREATE VIEW post_comments AS (

CREATE TABLE "CamelizeTable" (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
created timestamp NOT NULL
created timestamp NOT NULL,
UNIQUE(id)
);

CREATE TABLE "hyphen-table" (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
"hyphen-column" text NOT NULL,
created timestamp NOT NULL
"CamelizeTableId" uuid NOT NULL,
created timestamp NOT NULL,
CONSTRAINT "hyphen-table_CamelizeTableId_fk" FOREIGN KEY("CamelizeTableId") REFERENCES "CamelizeTable" (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE,
UNIQUE("hyphen-column")
);

CREATE SCHEMA administrator;
Expand Down