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 support for ON UPDATE CURRENT_TIMESTAMP columns #150

Merged
merged 3 commits into from
Aug 9, 2024

Conversation

JanJakes
Copy link
Collaborator

@JanJakes JanJakes commented Aug 8, 2024

The ON UPDATE CURRENT_TIMESTAMP is a special MySQL-specific construct that can be used in column definitions. There is no such functionality in SQLite. This pull request implements the ON UPDATE CURRENT_TIMESTAMP functionality for both CREATE and ALTER table commands using triggers.

With queries such as:

CREATE TABLE _tmp_table (
	id int(11) NOT NULL,
	created_at timestamp NULL ON UPDATE CURRENT_TIMESTAMP
);

ALTER TABLE _tmp_table ADD COLUMN updated_at timestamp NULL ON UPDATE CURRENT_TIMESTAMP;

The following triggers will be added:

-- created_at
CREATE TRIGGER "___tmp_table_created_at_on_update__"
AFTER UPDATE ON "_tmp_table"
FOR EACH ROW
BEGIN
  UPDATE "_tmp_table" SET "created_at" = CURRENT_TIMESTAMP WHERE id = NEW.id;
END;

-- updated_at
CREATE TRIGGER "___tmp_table_updated_at_on_update__"
AFTER UPDATE ON "_tmp_table"
FOR EACH ROW
BEGIN
  UPDATE "_tmp_table" SET "updated_at" = CURRENT_TIMESTAMP WHERE id = NEW.id;
END;

When the ON UPDATE is dropped using a query such as the following:

ALTER TABLE _tmp_table
CHANGE created_at created_at timestamp NULL,
CHANGE COLUMN updated_at updated_at timestamp NULL

The triggers are dropped as well. To ensure this always works, the trigger is always dropped when a column is being altered. Then, if ON UPDATE is present, it will be recreated; otherwise, it will remain dropped.

Resolves #148.

@JanJakes JanJakes requested a review from bgrgicak August 9, 2024 11:41
@bgrgicak
Copy link
Collaborator

bgrgicak commented Aug 9, 2024

Thanks for the PR @JanJakes!

@bgrgicak bgrgicak merged commit e7b8ce7 into WordPress:develop Aug 9, 2024
8 checks passed
@JanJakes JanJakes deleted the column-on-update branch August 16, 2024 08:45
brandonpayton pushed a commit that referenced this pull request Aug 16, 2024
This just fixes and improves
#150.

With some further testing, I realized there were some issues in my
original implementation:
1. The `ON UPDATE` trigger uses `id` instead of `rowid`. This is a
regression — a query can fail now for anyone who uses `ON UPDATE` and
doesn't have an `id` column. I somehow missed that 🤦‍♂️
2. The trigger is deleted with every column operation, not only for
column `CHANGE`s, and it is actually missed by the `CHANGE` for creation
as well, as that branch uses `continue`/`break` and skips the trigger
creation below.

I fixed both and added test coverage.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Support ON UPDATE CURRENT_TIMESTAMP columns
2 participants