Created
April 4, 2012 19:56
-
-
Save jazahn/2305129 to your computer and use it in GitHub Desktop.
Autoincrememnt Yii Migration helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Autoincrement { | |
function up($table_name, $driver){ | |
if($driver == 'mysql') { | |
$createAutoincrement = <<< SQL | |
ALTER TABLE $table_name MODIFY COLUMN id INT NOT NULL AUTO_INCREMENT | |
SQL; | |
$this->execute($createAutoincrement); | |
} | |
if($driver == 'oci') { | |
$createSequenceSql = <<< SQL | |
create sequence {$table_name}_SEQ | |
start with 1 | |
increment by 1 | |
nomaxvalue | |
nocache | |
SQL; | |
$createTriggerSql = <<< SQL | |
create or replace trigger {$table_name}_SEQ_TRIGGER | |
before insert on {$table_name} | |
for each row | |
begin | |
select {$table_name}_SEQ.nextval into :new.id from dual; | |
end; | |
SQL; | |
$this->execute($createSequenceSql); | |
$this->execute($createTriggerSql); | |
} | |
} | |
function down($table_name, $driver){ | |
if($driver == 'oci') { | |
// trigger needs to be dropped before the table or it gets "kindof" dropped with the table drop | |
$this->execute("DROP SEQUENCE {$table_name}_SEQ"); | |
$this->execute("DROP TRIGGER {$table_name}_SEQ_TRIGGER"); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment