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

Add ability to handle explicit schema name. #70

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

psipika
Copy link

@psipika psipika commented Dec 31, 2019

When invoking render_er with a schema name, like:

    render_er(f"postgresql+psycopg2://DB_USERNAME@localhost:5001/DB_NAME",
              "DB_NAME.png",
              mode='graph',
              schema='DB_NAME')

The resulting image is missing the proper relationships. As mentioned in issue #67, the problem is that the left_col argument to Relation is missing the name of the schema. This PR aims to address the problem using the [subjectively] least intrusive means.

Closes #67

@psipika
Copy link
Author

psipika commented Dec 31, 2019

This PR also takes a crack at fixing Travis-CI configuration to start postgresql.

Copy link
Owner

@Alexis-benoist Alexis-benoist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR !

eralchemy/sqla.py Show resolved Hide resolved
# as another parameter into this function from metadata_to_intermediary()
left_column = format_name(fk._column_tokens[1])
if fk._column_tokens[0]:
left_column = "{}.{}".format(format_name(fk._column_tokens[0]),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I require a test of this fix preferably in in integration with a real postgresql

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amended existing schema test to assert that column names match full table names.
Hope that is enough.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need automated testing for this feature possibly in integration with a real Postgres.

Copy link
Author

@psipika psipika Jan 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current test uses the local instance of a postgres database named test and created inside .travis.yml. Is that not enough?

@Alexis-benoist Alexis-benoist self-requested a review January 9, 2020 17:15
@Alexis-benoist
Copy link
Owner

Alexis-benoist commented Jan 9, 2020 via email

@psipika
Copy link
Author

psipika commented Jan 9, 2020

Didn't saw it. Where is the assertion showing the issue #67 is fixed?

Here:

    tables, relationships = database_to_intermediary(db_uri, schema='test')
    table_names = [t.name for t in tables]
....
    # Assert column names match table names, including schema
    assert all(r.right_col in table_names for r in relationships)
    assert all(r.left_col in table_names for r in relationships)

Basically, the table names returned from the function call are in the form <schema name>.<table name>. The names of Relation columns should use the same format. Issue #67 pertains to the fact that the original implementation returned names of the form <table name>, completely omitting the <schema name> portion. The additional assert() calls make sure that the column names returned inside relationships match those in the actual tables.

@@ -74,6 +75,9 @@ def test_database_to_intermediary_with_schema():
# Not in because different schema.
assert relation not in relationships
assert exclude_relation not in relationships
# Assert column names match table names, including schema
assert all(r.right_col in table_names for r in relationships)
assert all(r.left_col in table_names for r in relationships)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you extract this part to another test and make the assertion show the "{}.{}" formatting?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, separated out and checking explicitly for presence/lack of schema name in the relationship column names. Thanks for taking the time to review my updates.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we do a set expected_names like and assert it's equal to the result?

@psipika
Copy link
Author

psipika commented Jan 22, 2020

Ping @Alexis-benoist -- is there anything else I can address to move this forward?

@DanielWeitzenfeld
Copy link

@psipika Thank you for this, I hope it gets merged. Just a heads up that for my case, I needed to change eralchemy/sqla.py:74 to

relation_to_intermediary(fk, metadata.schema or table.schema)

My base is a DeclarativeMeta, and I defined schemas as the table level, e.g.:

class Foo(Base):
    __tablename__ = 'foo'
    __table_args__ = {"schema": "bar"}

Copy link
Owner

@Alexis-benoist Alexis-benoist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the long delay.

@@ -74,6 +75,9 @@ def test_database_to_intermediary_with_schema():
# Not in because different schema.
assert relation not in relationships
assert exclude_relation not in relationships
# Assert column names match table names, including schema
assert all(r.right_col in table_names for r in relationships)
assert all(r.left_col in table_names for r in relationships)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we do a set expected_names like and assert it's equal to the result?

assert all(r.right_col in table_names for r in relationships)
assert all(r.left_col in table_names for r in relationships)

# Assert column names match table names
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same remark as below + code is duplicated.

@yabb85
Copy link

yabb85 commented May 14, 2020

The same problem exist also when the schema is not defined.
I create two schemas in postgresql but when i try to generate a diagram the relations are not generated because the left_col is never in include_tables (included_tables contains the fullname).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing relationships when using Base
4 participants