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

Update URL to url in field_type enum #1032

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/database/migrations/0028-alter-field_type-enum.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TYPE updated_field_type AS ENUM ('string', 'number', 'phone_number', 'email', 'url', 'boolean');
ALTER TABLE base_fields
ALTER COLUMN data_type TYPE updated_field_type USING
CASE
WHEN data_type = 'URL' THEN 'url'::updated_field_type
ELSE data_type::text::updated_field_type
END;
DROP TYPE field_type CASCADE;
ALTER TYPE updated_field_type RENAME to field_type;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice. I thought for a minute there may be another more brief way of doing it but this is probably the straightest-forward approach.

Copy link
Member

@slifty slifty Jul 10, 2024

Choose a reason for hiding this comment

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

This is great, and I have no qualms. I was curious though so asked GPT how it would do it:

-- Step 1: Add the new value to the enum type
ALTER TYPE user_type ADD VALUE 'boss';

-- Step 2: Update the table to use the new value
UPDATE users SET user_type = 'boss' WHERE user_type = 'BOSS';

-- Step 3: Remove the old value (PostgreSQL does not support removing enum values directly)
-- So instead, create a new type without 'BOSS' and replace the old type

-- Create a new enum type without 'BOSS'
CREATE TYPE user_type_new AS ENUM('boss', /* other values */);

-- Alter the table to use the new enum type
ALTER TABLE users
  ALTER COLUMN user_type TYPE user_type_new
  USING user_type::text::user_type_new;

-- Drop the old enum type
DROP TYPE user_type;

-- Rename the new enum type to the original name
ALTER TYPE user_type_new RENAME TO user_type;

It's very similar to your approach, but by adding the url value to the original type / updating the fields to use url, and then swapping the enum to one without the (now unused URL) you could avoid the complexity of the CASE statement.

I defer to you @hminsky2002 on which you prefer!

Copy link
Contributor Author

@hminsky2002 hminsky2002 Jul 11, 2024

Choose a reason for hiding this comment

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

@slifty I think that avoiding a case statement is preferable!
I shall edit this commit with gusto

Copy link
Member

Choose a reason for hiding this comment

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

Following up here since Harry and I went offline to do some rapid back and forth -- Harry did implement this change but psql doesn't like using enums that aren't committed in a transaction (idk why, but whatever.)

Having two migrations felt nastier than a case statement, so he landed back where he started with the case statement!

That was fun :D

4 changes: 2 additions & 2 deletions src/database/seeds/0001-insert-base_fields.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ INSERT INTO base_fields (label, description, short_code, data_type, scope) VALUE
('Organization Postal Code' , '', 'organization_postal_code', 'number', 'proposal' ),
('Organization Phone' , '', 'organization_phone', 'phone_number', 'proposal' ),
('Organization Email' , '', 'organization_email', 'email', 'proposal' ),
('Organization Website' , '', 'organization_website', 'URL', 'proposal' ),
('Organization Website' , '', 'organization_website', 'url', 'proposal' ),
('Organization Mission Statement' , '', 'organization_mission_statement', 'string', 'proposal' ),
('Organization Start Date' , '', 'organization_start_date', 'string', 'proposal' ),
('Organization Fiscal Year End Date' , '', 'organization_fiscal_year_end_date', 'string', 'proposal' ),
Expand Down Expand Up @@ -201,7 +201,7 @@ INSERT INTO base_fields (label, description, short_code, data_type, scope) VALUE
('Review Submission ID' , '', 'review_submission_id', 'number', 'proposal' ),
('Review Status' , '', 'review_status', 'string', 'proposal' ),
('Review Date' , '', 'review_date', 'string', 'proposal' ),
('Review Submission URL' , '', 'review_submission_url', 'URL', 'proposal' ),
('Review Submission URL' , '', 'review_submission_url', 'url', 'proposal' ),
bickelj marked this conversation as resolved.
Show resolved Hide resolved
('Review Labels' , '', 'review_labels', 'string', 'proposal' ),
('Review Affiliate ID' , '', 'review_affiliate_id', 'number', 'proposal' ),
('Review Assigned To' , '', 'review_assigned_to', 'string', 'proposal' ),
Expand Down
2 changes: 1 addition & 1 deletion src/types/BaseField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export enum BaseFieldDataType {
NUMBER = 'number',
PHONE_NUMBER = 'phone_number',
EMAIL = 'email',
URL = 'URL',
URL = 'url',
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

BOOLEAN = 'boolean',
}

Expand Down