MuZZy wrote:
> I need to create a sql script which will run when user installs/upgrades
> my app.
> User may already have the database and tables tructure setup on the
> server, or may not.
> The script needs to conditionally create database, conditionally create
> tables, and conditionally add columns to the tables if needed.
You can get partway there using existing MySQL syntax.
See the respect web pages for CREATE DATABASE, DROP TABLE, CREATE TABLE,
etc.
<a style='text-decoration: underline;' href="http://dev.mysql.com/doc/mysql/en/create-database.html" target="_blank">http://dev.mysql.com/doc/mysql/en/create-database.html</a>
CREATE DATABASE IF NOT EXISTS databaseName ...
<a style='text-decoration: underline;' href="http://dev.mysql.com/doc/mysql/en/create-table.html" target="_blank">http://dev.mysql.com/doc/mysql/en/create-table.html</a>
CREATE TABLE IF NOT EXISTS tableName ...
<a style='text-decoration: underline;' href="http://dev.mysql.com/doc/mysql/en/drop-table.html" target="_blank">http://dev.mysql.com/doc/mysql/en/drop-table.html</a>
DROP TABLE IF EXISTS tableName ...
But there is no equivalent mechanism for conditional creation of columns.
At least one widely-used project comes to mind that does what you're
describing; runs a script to bring a database schema up to date with
current table/field definitions: Bugzilla.
The setup procedure for Bugzilla involves running a script
checksetup.pl, which can upgrade previous Bugzilla installations to the
current version's required schema. Basically, it does this with
ordinary ALTER TABLE ADD COLUMN statements, but ignores any error
returned (as would happen if the column already exists).
If you want to know more, download Bugzilla at
<a style='text-decoration: underline;' href="http://www.bugzilla.org/download/#stable," target="_blank">http://www.bugzilla.org/download/#stable,</a> open the distribution, and
read their checksetup.pl script.
Regards,
Bill K.<!-- ~MESSAGE_AFTER~ -->
>> Stay informed about: PLS HELP! - show databases and tables and columns