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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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);
     }

     function down()
     {
         $tableName = $this->_tablePrefix . 'users';
         $sql= "DROP TABLE IF EXISTS $tableName";
         $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 player