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

the autogenerate renderer should escape the quotes in the SQL comments #529

Closed
garaud opened this issue Jan 23, 2019 · 4 comments
Closed

Comments

@garaud
Copy link

garaud commented Jan 23, 2019

Hi,

I use recent versions of SQLAlchemy and alembic in order to insert some comments on columns and tables (cf. https://gerrit.sqlalchemy.org/#/c/sqlalchemy/alembic/+/932/).

When I add a column with a comment to a table in my SQLAlchemy schema, the autogenerate feature fails to load the Python migration script due to a single quote (in my comment). I've got a syntax error since the result of the renderer is malformed.

artist = Table(
    "artist",
    metadata,
    Column("id", Integer, primary_key=True),
    Column("name", String, nullable=False, comment="artist name"),
    # added this column with a single quote in the comment
    Column("band", String, comment="Artist's band")
)

The _render_column in the autogenerate.render module returns something like comment='Artist's band'. There are three single quotes.

I think the renderer should escape the single quotes, e.g. here https://github.com/sqlalchemy/alembic/blob/master/alembic/autogenerate/render.py#L617

I can replace the "'%s'" by a '"%s"' or find a proper way to escape the single quote. There might be other lines in the code where the single quote in comment should be escaped.

In my case, I write the comments in French. So I've a lot of apostrophes! 🙂

I'll submit a PR. I would like to share with you the best way to fix that.

Thanks,
Damien

@zzzeek
Copy link
Member

zzzeek commented Jan 23, 2019

Ohhhh OK right I made sure SQL quoting applied in the generated DDL but we forgot to do it in the autogen python code, OK.

@sqla-tester
Copy link
Collaborator

Damien Garaud has proposed a fix for this issue in the master branch:

Fix single quote escaping for the SQL comments https://gerrit.sqlalchemy.org/1111

@zzzeek
Copy link
Member

zzzeek commented Jan 23, 2019

thanks!

@garaud
Copy link
Author

garaud commented Jan 23, 2019

you're welcome !! 😸

Thanks for this quick review!

sqlalchemy-bot pushed a commit that referenced this issue Jan 24, 2019
Change-Id: I0c0d6701daa1c84b0905b5c4a8cab2383059389e
References: #529
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants