Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix duplicate output when multiple schemas have the same named table #134

Merged
merged 1 commit into from
Aug 15, 2019

Conversation

oohira
Copy link
Contributor

@oohira oohira commented Aug 15, 2019

I fixed a bug that two same named tables in diffrent schemas are listed multiple times in the output Markdown file.

Version

  • tbls 1.18.0
  • PostgreSQL 10.10
  • macOS 10.14.6

Steps to reproduce

  1. Run PostgreSQL server
    $ cd $GOPATH/src/github.com/k1LoW/tbls
    $ docker-compose up postgres
  2. Create a testdata initialization SQL file.
    $ vi init.sql
    CREATE SCHEMA schema1;
    CREATE SCHEMA schema2;
    -------------------------------------------------
    SET search_path TO schema1;
    CREATE TABLE users (
      id serial PRIMARY KEY,
      username varchar (50) UNIQUE NOT NULL CHECK(char_length(username) > 4),
      password varchar (50) NOT NULL,
      email varchar (355) UNIQUE NOT NULL
    );
    -------------------------------------------------
    SET search_path TO schema2;
    CREATE TABLE users (
      id serial PRIMARY KEY,
      username varchar (50) UNIQUE NOT NULL CHECK(char_length(username) > 4),
      password varchar (50) NOT NULL,
      email varchar (355) UNIQUE NOT NULL
    );
  3. Execute init.sql via psql command
    $ psql -h localhost -p 55432 -U postgres testdb
    localhost postgres@testdb=# \i init.sql
  4. Execute tbls
    $ tbls doc -f pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable result
    $ less result/README.md

Expected behaviour (This PR)

# testdb

## Tables

| Name | Columns | Comment | Type |
| ---- | ------- | ------- | ---- |
| [schema1.users](schema1.users.md) | 4 | Users table | BASE TABLE |
| [schema2.users](schema2.users.md) | 4 | Users table | BASE TABLE |

...

Actual behaviour (Current master branch)

# testdb

## Tables

| Name | Columns | Comment | Type |
| ---- | ------- | ------- | ---- |
| [schema1.users](schema1.users.md) | 4 | Users table | BASE TABLE |
| [schema2.users](schema2.users.md) | 4 | Users table | BASE TABLE |
| [schema1.users](schema1.users.md) | 4 | Users table | BASE TABLE |
| [schema2.users](schema2.users.md) | 4 | Users table | BASE TABLE |

...

Implementation note

Current

localhost postgres@testdb=# SELECT cls.oid, relnamespace, ns.nspname, relname, tbl.table_schema FROM pg_class cls
INNER JOIN pg_namespace ns on cls.relnamespace = ns.oid
INNER JOIN (SELECT table_name, table_type, table_schema FROM information_schema.tables
    WHERE table_schema != 'pg_catalog' AND table_schema != 'information_schema') tbl
    ON cls.relname = tbl.table_name;
  oid  | relnamespace | nspname | relname | table_schema
-------+--------------+---------+---------+--------------
 16404 |        16388 | schema2 | users   | schema1    # <-- schema mismatch
 16391 |        16387 | schema1 | users   | schema1
 16404 |        16388 | schema2 | users   | schema2
 16391 |        16387 | schema1 | users   | schema2    # <-- schema mismatch
(4 rows)

Fixed

localhost postgres@testdb=# SELECT cls.oid, relnamespace, ns.nspname, relname, tbl.table_schema FROM pg_class cls
INNER JOIN pg_namespace ns on cls.relnamespace = ns.oid
INNER JOIN (SELECT table_name, table_type, table_schema FROM information_schema.tables
    WHERE table_schema != 'pg_catalog' AND table_schema != 'information_schema') tbl
    ON cls.relname = tbl.table_name AND ns.nspname = tbl.table_schema;
  oid  | relnamespace | nspname | relname | table_schema                                                     │tax error at or near ")" at character 151
-------+--------------+---------+---------+--------------                                                    │postgres_1  | 2019-08-15 06:53:09.719 UTC [118] STATEMENT: 
 16391 |        16387 | schema1 | users   | schema1                                                          │ SELECT table_name, table_type, table_schema
 16404 |        16388 | schema2 | users   | schema2                                                          │postgres_1  |   FROM information_schema.tables
(2 rows)

* for PostgreSQL driver
* fix join condition to distinguish tables in diffrent schemas
@codecov
Copy link

codecov bot commented Aug 15, 2019

Codecov Report

Merging #134 into master will decrease coverage by 1.19%.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff            @@
##           master    #134     +/-   ##
========================================
- Coverage   70.59%   69.4%   -1.2%     
========================================
  Files          12      12             
  Lines        2585    2585             
========================================
- Hits         1825    1794     -31     
- Misses        587     624     +37     
+ Partials      173     167      -6
Impacted Files Coverage Δ
drivers/postgres/postgres.go 71.35% <100%> (ø) ⬆️
datasource/datasource.go 21.25% <0%> (-19.38%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 09655f5...c6a389b. Read the comment docs.

@k1LoW k1LoW added the bug Something isn't working label Aug 15, 2019
@k1LoW
Copy link
Owner

k1LoW commented Aug 15, 2019

Hi @oohira !! THANK YOU FOR YOUR FIXXX !!

@k1LoW k1LoW merged commit 99f4c8c into k1LoW:master Aug 15, 2019
k1LoW added a commit that referenced this pull request Aug 15, 2019
@k1LoW
Copy link
Owner

k1LoW commented Aug 15, 2019

Released as v1.18.1.

@oohira
Copy link
Contributor Author

oohira commented Aug 15, 2019

Thank you for merging and adding an unit test case. The quick release is really welcomed. Awesome 👍

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants