NYCPHP Meetup

NYPHP.org

[nycphp-talk] Creating database tables when deploying products

Ben Sgro (ProjectSkyline) ben at projectskyline.com
Mon Mar 26 10:40:48 EDT 2007


Hello, 

When creating software, I inherited a method from my previous company of creating a file, 'createdb.php' that includes
the instructions to create all the database tables and populate them w/default values.

When deploying, I run the script first and all is setup.

I've included two different segments from files that I've used to create tables: 
The first is an older procedural method, the second is newer OOP code.

...
...
...
/* create admin table if it does not exist */
    if ( @ !$tableSet[constAdminStr] )
    {
        error_log("createdb.php: creating 'hhc.admin' table");
        $sqlStr = "CREATE TABLE admin "
                . " (id int(11) NOT NULL auto_increment,"
                . " username varchar(64) default '',"
                . " password varchar(64) default '',"
                . " last_login int(12)   default 0,"
                . " primary key(id, username)"
                . ')';
        DBAS_MySQLQuery($sqlStr, $db);

        /* password: test */
        $userStr = constUserName;
        $md5Pass = md5(constUserPass);
        error_log("createdb: creating admin account '$userStr'");
        $sqlStr = "INSERT INTO admin "
                . " SET "
                . " username='$userStr',"
                . " password='$md5Pass'";
        DBAS_MySQLQuery($sqlStr, $db);
    }
...
...
...
function PRNK_CreateDatabaseTables($dbObject)
    {
        $debug = 1;
        $defE = constDatabaseDefaultEmpty;
        
        $dbObject->DatabaseCreateTable(constDatabaseTableAdmin);
        $dbObject->DatabaseCreateField("id", "int(11)", "NOT NULL",
                                       "auto_increment");      
        $dbObject->DatabaseCreateField("username", "varchar(64)", $defE);
        $dbObject->DatabaseCreateField("password", "varchar(64)", $defE);
        $dbObject->DatabaseCreateField("primary", "key(id, username)");
        $dbObject->DatabaseCreateCommit($debug);
        
        $dbObject->DatabaseCreateTable(constDatabaseTableIdea);
        $dbObject->DatabaseCreateField("idea_id", "int(11)", "NOT NULL",
                                       "auto_increment");
        $dbObject->DatabaseCreateField("email",   "varchar(64)",   $defE);
        $dbObject->DatabaseCreateField("phone",   "varchar(64)",   $defE);
        $dbObject->DatabaseCreateField("content", "varchar(2048)", $defE);
        $dbObject->DatabaseCreateField("votes",   "int(11)", "default 0");
        $dbObject->DatabaseCreateField("time",    "int(11)", "default 0");
        $dbObject->DatabaseCreateField("primary", "key(idea_id)");
        $dbObject->DatabaseCreateCommit($debug);
        
        $dbObject->DatabaseCreateTable(constDatabaseTableVote);
        $dbObject->DatabaseCreateField("vote_id", "int(11)", "NOT NULL",
                                       "auto_increment");
        $dbObject->DatabaseCreateField("idea_id", "int(11)", "default 0");
        $dbObject->DatabaseCreateField("email",   "varchar(64)", $defE);
        $dbObject->DatabaseCreateField("primary", "key(vote_id)");
        $dbObject->DatabaseCreateCommit($debug);
    }
...
...
...
So my question is ... is what I'm doing ok? .. there must be a better way to do it ... How are you all
doing database table creation for products that need to be deployed on multiple systems.?

Thanks!

- Ben


ProjectSkyline.com
ben at projectskyline.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20070326/57002e76/attachment.html>


More information about the talk mailing list