Yii Framework Forum: Yii Migration Example With Safe Up - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii Migration Example With Safe Up Trasection is use with safe up Rate Topic: -----

#1 User is offline   anil.dhiman 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 02-February 12
  • Location:india

Posted 08 November 2013 - 12:37 AM

public function safeUp()
    {
        $this->execute("SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;");
        $this->execute("SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;");
        $this->execute("SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';");
        $this->execute('CREATE SCHEMA IF NOT EXISTS `sms-log` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;');

        $this->createTable('tbl_user_login', array(
                'uuid' => "CHAR(36) NOT NULL",
                'username' => "VARCHAR(225) NOT NULL COMMENT 'username or email address for login'",
                'password' => "TEXT NOT NULL COMMENT 'encrypted key password'",
                'created_date' => "DATETIME NOT NULL COMMENT 'user account created on date'",
                'mobile' => "INT(12) NOT NULL COMMENT 'mobile number for mobile confirmation and to send notification'",
                'is_active' => "BIT(1) NOT NULL COMMENT 'to check whether confirm their details or not'",
                'blocked' => "BIT(1) NULL COMMENT 'if user try more then 5 time to login with wrong details'",
                'bocked_time' => "DATETIME NULL COMMENT 'store user blocked time'",
                'failed_login_attempts' => "INT(1) NULL COMMENT 'failed login attempt counting'",
                'PRIMARY KEY (`uuid`)',
            ), 'ENGINE=InnoDB');

        $this->execute("SET SQL_MODE=@OLD_SQL_MODE;");
        $this->execute("SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;");
        $this->execute("SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;");
    }

0

#2 User is offline   Mubashar Iqbal 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 18-September 13
  • Location:Lahore

Posted 08 November 2013 - 12:44 AM

read this article:
http://www.yiiframew...onal-migrations

Transaction support is to implement the safeUp() method instead of up(), and safeDown() instead of down(). For example,

class m101129_185401_create_news_table extends CDbMigration
{
public function safeUp()
{
$this->createTable('tbl_news', array(
'id' => 'pk',
'title' => 'string NOT NULL',
'content' => 'text',
));
}

public function safeDown()
{
$this->dropTable('tbl_news');
}
}

When Yii performs the migration, it will start a DB transaction and then call safeUp() or safeDown(). If any DB error occurs in safeUp() or safeDown(), the transaction will be rolled back, thus ensuring the database remain in a good shape.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users