Is there a correct and safe way to modify enum column type values? Add new or remove old.
E.g.: I have ENUM ("apple", "banana")
I have 2 tasks that need to add value to the ENUM. 1 needs to add orange and second needs to add peach.
If I get migrations scripts, I will have:
ALTER TABLE example MODIFY COLUMN fruit ENUM("apple", "banana", "orange) NOT NULL
ALTER TABLE example MODIFY COLUMN fruit ENUM("apple", "banana", "peach) NOT NULL
I will end up only with values from the last executed SQL. Is there a way to just add value to existing values?
You can use the show or description command.
produces this on my system if I use print_r to show the row fetched from the database.
Once you have this in a variable in your language, you can parse it.
From dev.mysql.com
More examples are at tutorialspoint.com
EDIT
If you want it all sql then you need to write a procedure to do it which you call from your script. This can fetch the enum value from the information_schema.
I added a column
testjust for testing type enum with values 'a','b','c','d' to one of my tables.Here's a function to demo the concept. To check what is returned by the select statement. Replace the TABLE_SCHEMA, TABLE_NAME and COLUMN_NAME values to suit.
This returns
In your procedure you can get this value as a string (more accurately longtext). You can check if the new value exists. If not, you can add it in.
To add the value 'e' to it requires
Please alter to suit.