Skip to content

Instantly share code, notes, and snippets.

@jazahn
Created April 4, 2012 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jazahn/2305129 to your computer and use it in GitHub Desktop.
Save jazahn/2305129 to your computer and use it in GitHub Desktop.
Autoincrememnt Yii Migration helper
<?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