Akrabat_Db_Schema_Manager: table prefix support

Rob Allen posts; I’ve updated Akrabat_Db_Schema_Manager so that it now sup­ports table prefixes.

It uses the application.ini key of resources.db.table_prefix as I couldn’t think of a bet­ter one :) and then uses that for the schema_version table’s name and also makes it avail­able in your change objects.

For exam­ple, if application.ini con­tains resources.db.table_prefix = “myapp”, then the man­ager will cre­ate the table myapp_schema_version to store the cur­rent ver­sion of the schema. In your change classes, you can then do this:

001-Users.php:

class Users extends Akrabat_Db_Schema_AbstractChange
{
function up()
{
$tableName = $this->_tablePrefix . 'users';
$sql = "
CREATE TABLE IF NOT EXISTS $tableName (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(50) NOT NULL,
password varchar(75) NOT NULL,
role varchar(200) NOT NULL DEFAULT 'user',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
$this->_db->query($sql);

$data = array();
$data[’username’] = ‘admin’;
$data[’password’] = sha1(‘password’);
$data[’role’] = ‘admin’;
$this->_db->insert($tableName, $data);
}

func­tion down()
{
$table­Name = $this->_tablePrefix . ‘users’;
$sql= “DROP TABLE IF EXISTS $table­Name”;
$this->_db->query($sql);
}

}

which will cre­ate a table called myapp_users. Note that you are respon­si­ble for using the pre­fix prop­erty as the change classes can­not enforce what you do within the up() and down() meth­ods. It also fol­lows that you’ll have to ensure that your mod­els also use the cor­rect prefix.

I have also made a change to the provider (Akrabat_Tool_DatabaseSchemaProvider) so that it loads the cor­rect application.ini file based on the data in the project’s pro­file. This shouldn’t affect any­one using Akrabat_Db_Schema_Manager, except that we no longer define APPLICATION_ENV and APPLICATION_PATH for you.

Enjoy!

via Rob Allen’s DevNotes.

Tags: , , ,

Leave a Comment

*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes