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

mysql-kit: fix GENERATED ALWAYS AS ... NOT NULL; #2824

Merged
merged 4 commits into from
Aug 26, 2024

Conversation

juliusmarminge
Copy link
Contributor

@juliusmarminge juliusmarminge commented Aug 22, 2024

GENERATED ALWAYS AS must precede NOT NULL:

CleanShot 2024-08-22 at 17 53 23

Note

From MySQL Docs:

CleanShot 2024-08-22 at 17 46 26

Let me know if I should revert Postgres and SQLite

  • PG seems to accept NOT NULL GENERATED AS ...
    CleanShot 2024-08-22 at 17 53 55

  • Didn't test SQLite

Example:

CREATE TABLE `no-work` (
	`id` varchar(36) NOT NULL,
	`download_url` varchar(2048),
	`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
	`app_id` varchar(36) NOT NULL,
	`marked_for_deletion` tinyint (1) NOT NULL DEFAULT '0',
	`file_key` varchar(300) NOT NULL,
	`upload_failed` tinyint (1) DEFAULT '0',
	`deleted_at` timestamp NULL,
	`callback_response` int,
	`status` varchar(32) NOT NULL GENERATED ALWAYS AS (
  --                                  ^ Syntax error ❌
		CASE WHEN `deleted_at` IS NOT NULL THEN
			_utf8mb4 'Deleted'
		WHEN `marked_for_deletion` = 1 THEN
			_utf8mb4 'Deletion Pending'
		WHEN `upload_failed` = 1 THEN
			_utf8mb4 'Failed'
		WHEN `callback_response` IS NOT NULL
			AND `callback_response` != 200 THEN
			_utf8mb4 'Callback Failed'
		WHEN `download_url` IS NOT NULL THEN
			_utf8mb4 'Uploaded'
		ELSE
			_utf8mb4 'Uploading'
		END) STORED,
	PRIMARY KEY (`id`)) ENGINE InnoDB,
CHARSET utf8mb4,
COLLATE utf8mb4_0900_ai_ci;
CREATE TABLE `works` (
	`id` varchar(36) NOT NULL,
	`download_url` varchar(2048),
	`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
	`app_id` varchar(36) NOT NULL,
	`marked_for_deletion` tinyint (1) NOT NULL DEFAULT '0',
	`file_key` varchar(300) NOT NULL,
	`upload_failed` tinyint (1) DEFAULT '0',
	`deleted_at` timestamp NULL,
	`callback_response` int,
	`status` varchar(32) GENERATED ALWAYS AS (
		CASE WHEN `deleted_at` IS NOT NULL THEN
			_utf8mb4 'Deleted'
		WHEN `marked_for_deletion` = 1 THEN
			_utf8mb4 'Deletion Pending'
		WHEN `upload_failed` = 1 THEN
			_utf8mb4 'Failed'
		WHEN `callback_response` IS NOT NULL
			AND `callback_response` != 200 THEN
			_utf8mb4 'Callback Failed'
		WHEN `download_url` IS NOT NULL THEN
			_utf8mb4 'Uploaded'
		ELSE
			_utf8mb4 'Uploading'
		END) STORED NOT NULL,
  --                        ^ All good ✅
	PRIMARY KEY (`id`)) ENGINE InnoDB,
CHARSET utf8mb4,
COLLATE utf8mb4_0900_ai_ci;

@juliusmarminge juliusmarminge marked this pull request as ready for review August 22, 2024 15:54
@juliusmarminge juliusmarminge changed the title fix(kit): change order to support not-null generated columns mysql-kit: fix GENERATED ALWAYS AS ... NOT NULL; Aug 22, 2024
@AndriiSherman AndriiSherman changed the base branch from main to beta August 26, 2024 09:13
@AndriiSherman AndriiSherman merged commit e2f75fb into drizzle-team:beta Aug 26, 2024
0 of 6 checks passed
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.

2 participants