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

SQLAlchemy/DDL: Handle server_default column schema argument well #556

Merged
merged 1 commit into from
Jun 9, 2023

Conversation

JanLikar
Copy link
Contributor

@JanLikar JanLikar commented May 26, 2023

Summary of the changes / Why this is an improvement

CrateDB's SQLAlchemy dialect now handles the server_default when generating table DDL.

Fix crate/sqlalchemy-cratedb#106.

Checklist

  • CLA is signed

Copy link
Member

@amotl amotl left a comment

Choose a reason for hiding this comment

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

Dear Jan,

thanks a stack for submitting this patch. I like that the test methods enumerate different kinds of values / value types the server_default argument is able to obtain.

Other than a suggestion about reStructuredText syntax, and maybe a slight naming change on the test methods, I think it will be good to go.

With kind regards,
Andreas.

docs/sqlalchemy.rst Outdated Show resolved Hide resolved
src/crate/client/sqlalchemy/tests/create_table_test.py Outdated Show resolved Hide resolved
src/crate/client/sqlalchemy/tests/create_table_test.py Outdated Show resolved Hide resolved
@amotl amotl requested a review from seut May 30, 2023 14:37
@amotl amotl changed the title Implement server_default for SA columns SQLAlchemy/DDL: Handle server_default column schema argument well May 30, 2023
'PRIMARY KEY (pk)\n)\n\n'), ())

def test_column_server_default_string(self):
class DummyTable(self.Base):

Check notice

Code scanning / CodeQL

Unused local variable

Variable DummyTable is not used.
'PRIMARY KEY (pk)\n)\n\n'), ())

def test_column_server_default_func(self):
class DummyTable(self.Base):

Check notice

Code scanning / CodeQL

Unused local variable

Variable DummyTable is not used.
'PRIMARY KEY (pk)\n)\n\n'), ())

def test_column_server_default_constant(self):
class DummyTable(self.Base):

Check notice

Code scanning / CodeQL

Unused local variable

Variable DummyTable is not used.
@JanLikar JanLikar force-pushed the column-server-default-454 branch from 9757321 to 6a32045 Compare May 31, 2023 10:54
@JanLikar
Copy link
Contributor Author

JanLikar commented May 31, 2023

Other than a suggestion about reStructuredText syntax, and maybe a slight naming change on the test methods, I think it will be good to go.

I applied the fixes and squashed the commits. To be honest, I've been using Markdown so much lately I didn't even realize this is rst, thanks for spotting the issue.

Copy link
Member

@seut seut left a comment

Choose a reason for hiding this comment

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

Look good! I've just added a type related suggestion related to a test case.

fake_cursor.execute.assert_called_with(
('\nCREATE TABLE t (\n\t'
'pk STRING NOT NULL, \n\t'
'a TIMESTAMP DEFAULT \'Zaphod\', \n\t'
Copy link
Member

Choose a reason for hiding this comment

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

This is a non-working example as CrateDB will complain here that the Zaphod string literal cannot be converted to timestamp. Can we use a different example here? E.g. a TEXT DEFAULT 'CrateDB rocks' or something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch. Fixed.

Copy link
Member

Choose a reason for hiding this comment

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

Hi again. After invoking CI, it looks like the test case test_column_server_default_string needs corresponding adjustments to compensate this fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, sorry about that, I would have run the tests before pushing, but I was traveling without access to a dev environment.

I'm home now and the tests pass for me, so I think we're good to go.

Copy link
Member

@amotl amotl Jun 8, 2023

Choose a reason for hiding this comment

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

Dear Jan,

no worries about that hiccup, and welcome back. Now that I've just merged GH-555, it is my turn to apologize for the inconvenience that you may need to rebase your patch now, and most probably also resolve some conflicts around the same spots where @fetzerms made his changes. Please take your time.

If you could also add a changelog item like 7b86801, the patch will be perfect and complete. Thank you so much!

With kind regards,
Andreas.

P.S.: Don't worry about failing GHA tests. Right now, they are only failing because coverage stats can't be uploaded to Codecov on behalf of your account's permissions, because it would need access to this repository's tokens. While it is a public repository which usually does not need any authentication tokens, we established them the other day after Codecov tripped us very often. It is a known issue that it sometimes blocks access from GitHub, and it happens more often on larger test matrix scenarios, where many requests are being submitted from GitHub to Codecov, like this one. Now you know the whole story ;].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Andreas, thank you for your kind words! I rebased and pushed my changes, luckily the conflicts were relatively mild.

@JanLikar JanLikar force-pushed the column-server-default-454 branch from 6a32045 to 1d9cf51 Compare June 5, 2023 14:07
@JanLikar JanLikar requested a review from seut June 5, 2023 14:08
# TODO: once supported add default here

default = self.get_column_default_string(column)
if default is not None:

Choose a reason for hiding this comment

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

Do we intentionally want to intentionally ignore empty values like [], "", {}. Why is the reason behind not using?

Suggested change
if default is not None:
if default:

Trying to understand, thanks! 😁

Copy link
Member

Choose a reason for hiding this comment

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

@sanchezfdezjavier
Not sure if I correctly understand your questions, any value beside None will evaluate to True.

'' is not None => True
[] is not None => True
{} is not None => True
False is not None => True

While using if default: it would evaluate to False for all the above examples, see also https://docs.python.org/3/library/stdtypes.html#truth.

Copy link
Member

Choose a reason for hiding this comment

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

Hi Javi,

get_column_default_string() probably returns a string, so in the case of non-empty strings, your suggestion might also work in this case.

But a) @seut is generally right, and b) explicitly testing against is not None also feels safer for me, because it does not suffer from any implicit bool-casting flaws.

With kind regards,
Andreas.

Choose a reason for hiding this comment

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

Awesome, that makes sense. Thanks for clarifying!

@JanLikar JanLikar force-pushed the column-server-default-454 branch 2 times, most recently from 6e098dd to 1b63dfa Compare June 8, 2023 23:21
CrateDB's SQLAlchemy dialect now handles the `server_default` when generating
table DDL.

Fix #454.
@JanLikar JanLikar force-pushed the column-server-default-454 branch from 1b63dfa to e84aa11 Compare June 8, 2023 23:22
'PRIMARY KEY (pk)\n)\n\n'), ())

def test_column_server_default_string(self):
class DummyTable(self.Base):

Check notice

Code scanning / CodeQL

Unused local variable

Variable DummyTable is not used.
'PRIMARY KEY (pk)\n)\n\n'), ())

def test_column_server_default_func(self):
class DummyTable(self.Base):

Check notice

Code scanning / CodeQL

Unused local variable

Variable DummyTable is not used.
@amotl amotl merged commit 4c62ecf into crate:master Jun 9, 2023
@amotl
Copy link
Member

amotl commented Jun 9, 2023

Merged. Thank you again, and have a good weekend.

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.

SQLAlchemy/DDL: Support server_default column definition option
4 participants