Skip to content

Add support for DEFAULT (expression) in column definitions - #306

Merged
JanJakes merged 8 commits into
WordPress:developfrom
wojtekn:fix/function-calls-in-parentheses
Jan 27, 2026
Merged

JanJakes merged 8 commits into
WordPress:developfrom
wojtekn:fix/function-calls-in-parentheses

Conversation

@wojtekn

@wojtekn wojtekn commented Jan 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds full support for DEFAULT (expression) in CREATE TABLE statement column definitions.

Fixes #300 - Support for DEFAULT (now()).

Problem

The MySQL 8.0 syntax DEFAULT (function()) was failing because the translator only consumed a single token after the DEFAULT keyword. For expressions like DEFAULT (now()), it would only capture the opening parenthesis (, causing the query translation to fail.

Solution

  1. Full DEFAULT (expression) support in the AST driver.

  2. Enhanced the DEFAULT clause handler in WP_SQLite_Translator to:

  • Detect when a DEFAULT value starts with an opening parenthesis
  • Track parenthesis depth to consume the complete expression
  • Handle nested parentheses correctly

@JanJakes

Copy link
Copy Markdown
Member

@wojtekn Thank you for looking into this! Good job working with the AST and even fixing the old driver 👍

Generally, it looks good to me, and it is the right approach to work with the new driver. On the AST side, I spotted one issue that is related to only implementing a subset of the expression functionality, and that is that for expressions such as this one:

DEFAULT (DATE_ADD(NOW(), INTERVAL 1 YEAR))

It would detect and incorrectly return the value of NOW(). That's because the implementation checks whether there is that function call, but it can't easily determine that there is nothing else. Fixing that is not straightforward with arbitrary nesting, e.g. for DEFAULT (((now()))), and doing the full expression support is easy, so I think we could even go all the way. Let me see if I can push my idea somehow. I probably can't modify this branch as it's from a fork, but I can push another one.

Additionally, in the MySQL column metadata, there needs to be an indication that it has a DEFAULT_GENERATED value, but I can see that we already seem to be doing that, so I think we're safe on this one. Perhaps it needs a test in case there isn't one yet.

@JanJakes JanJakes changed the title Fix CREATE TABLE with DEFAULT function calls in parentheses Add support for DEFAULT (expression) in column definitions Jan 16, 2026
@JanJakes

JanJakes commented Jan 16, 2026 •

Copy link
Copy Markdown
Member

@wojtekn It turns out I can push to this branch. I just have no idea how to manage PRs from forks 😆

I added one commit with a quick implementation of the full DEFAULT (expression) on the AST side. I will check it again after the weekend and see if it needs some polishing and/or more tests.

TODO:

  • Check if we need to test more expressions.
  • Add a test for metadata (DEFAULT_GENERATED ).
  • Check if we can improve stored expression formatting.

Thanks again for pushing this forward!

@JanJakes
JanJakes requested a review from a team January 21, 2026 13:00
@JanJakes JanJakes mentioned this pull request Jan 21, 2026
@JanJakes

Copy link
Copy Markdown
Member

No more feedback here, so let's get it in.

@JanJakes
JanJakes merged commit fe3db10 into WordPress:develop Jan 27, 2026
15 checks passed
@wojtekn
wojtekn deleted the fix/function-calls-in-parentheses branch February 2, 2026 15:32
@wojtekn

wojtekn commented Feb 2, 2026

Copy link
Copy Markdown
Contributor Author

@JanJakes, thanks for taking it over and finishing it.

@JanJakes JanJakes added this to the Release 3.0 milestone Aug 12, 2026
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.

Create table with DEFAULT (now()) fails in AST driver

2 participants