Jaxel
01-03-2009, 07:43 PM
Okay, lets say I have a mod... and in version1, I have the following install code... (the code is an example, I know its not functioning, but thats got nothing to do with my problem)
$db->query_write("CREATE TABLE IF NOT EXISTS table (
column1 int auto_increment,
column2 int,
PRIMARY KEY (column1)
)");
Then I create a version2, with an extra column... so I made my install code this...
$db->query_write("CREATE TABLE IF NOT EXISTS table (
column1 int auto_increment,
column2 int,
column3 int,
PRIMARY KEY (column1)
)");
$db->query_write("ALTER TABLE table ADD COLUMN column3 int)");
Now I am making a version 3... and there is yet another column to add... I tried the following code, but I got an error when it hit the ADD COLUMN column3 code... because column3 already exists.
$db->query_write("
CREATE TABLE IF NOT EXISTS table (
column1 int auto_increment,
column2 int,
column3 int,
column4 int,
PRIMARY KEY (column1)
)");
$db->query_write("ALTER TABLE table ADD COLUMN column3 int)");
$db->query_write("ALTER TABLE table ADD COLUMN column4 int)");
I tried looking for a possible IF NOT EXIST condition for altering tables to add columns, but it looks like you cant do it. I know I could just delete the add column3 query, and it would work great for people who are updating from version2 to version3. But that line would still be required if people were upgrading from version1. So is there a proper way for me to handle this problem?
$db->query_write("CREATE TABLE IF NOT EXISTS table (
column1 int auto_increment,
column2 int,
PRIMARY KEY (column1)
)");
Then I create a version2, with an extra column... so I made my install code this...
$db->query_write("CREATE TABLE IF NOT EXISTS table (
column1 int auto_increment,
column2 int,
column3 int,
PRIMARY KEY (column1)
)");
$db->query_write("ALTER TABLE table ADD COLUMN column3 int)");
Now I am making a version 3... and there is yet another column to add... I tried the following code, but I got an error when it hit the ADD COLUMN column3 code... because column3 already exists.
$db->query_write("
CREATE TABLE IF NOT EXISTS table (
column1 int auto_increment,
column2 int,
column3 int,
column4 int,
PRIMARY KEY (column1)
)");
$db->query_write("ALTER TABLE table ADD COLUMN column3 int)");
$db->query_write("ALTER TABLE table ADD COLUMN column4 int)");
I tried looking for a possible IF NOT EXIST condition for altering tables to add columns, but it looks like you cant do it. I know I could just delete the add column3 query, and it would work great for people who are updating from version2 to version3. But that line would still be required if people were upgrading from version1. So is there a proper way for me to handle this problem?