Jump to content
MakeWebGames

Best way for Schema.sql


URBANZ

Recommended Posts

So i'm currently in the process of packaging a module up for the market place and currently figuring out the best way for the SQL's to run when installed. 

At the moment i'm using this to check if the column exists.

ALTER TABLE `userStats` ADD COLUMN IF NOT EXISTS US_vip INT(11) NOT NULL DEFAULT '0';

I am aware this only works for MariaDB which not everyone will be running. What is a good alternative that will work with both as i haven't run a basic MySQL server in years so not sure the simplest way for this. 

Link to comment
Share on other sites

2 minutes ago, Sim said:

I think that works for mySQL as well.

I did try it as a test and get invalid syntax when used on MySQL server so i believe that function is only part of MariaDB and cant find a workaround without selecting the information schema table and selecting the table from the database but again some users might not have permissions to do this. i think for now just sticking with the MariaDB way.

Link to comment
Share on other sites

33 minutes ago, KyleMassacre said:

IF COL_LENGTH('table_name', 'column_name') IS NULL BEGIN ALTER TABLE table_name ADD column_name INT END


IF COL_LENGTH('table_name', 'column_name') IS NULL
BEGIN
    ALTER TABLE table_name
    ADD column_name INT
END

You can try something like this:

 

Thank you Kyle i did see this earlier and gave it a try but couldn't get it working correctly, keep getting invalid syntax. how it stands think i'm just going to stick to the MariaDB, most people should be running that anyway.

Link to comment
Share on other sites

On 4/16/2020 at 11:31 PM, urbanmafia said:

So i'm currently in the process of packaging a module up for the market place and currently figuring out the best way for the SQL's to run when installed. 

At the moment i'm using this to check if the column exists.

ALTER TABLE `userStats` ADD COLUMN IF NOT EXISTS US_vip INT(11) NOT NULL DEFAULT '0';


ALTER TABLE `userStats` ADD COLUMN IF NOT EXISTS US_vip INT(11) NOT NULL DEFAULT '0';

I am aware this only works for MariaDB which not everyone will be running. What is a good alternative that will work with both as i haven't run a basic MySQL server in years so not sure the simplest way for this. 

ALTER TABLE tablename ADD IF NOT EXISTS columname int(11) NOT NULL DEFAULT 0;

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...