HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
m110810_162301_userTimestampFix.php
Go to the documentation of this file.
1 <?php
2 
3 class m110810_162301_userTimestampFix extends CDbMigration
4 {
5  public function safeUp()
6  {
7  if (!Yii::app()->getModule('user')) {
8  echo "\n\nAdd to console.php :\n"
9  ."'modules'=>array(\n"
10  ."...\n"
11  ." 'user'=>array(\n"
12  ." ... # copy settings from main config\n"
13  ." ),\n"
14  ."...\n"
15  ."),\n"
16  ."\n";
17 
18  return false;
19  }
20 
21  switch ($this->dbType()) {
22  case "mysql":
23  $this->addColumn(Yii::app()->getModule('user')->tableUsers, 'create_at', "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP");
24  $this->addColumn(Yii::app()->getModule('user')->tableUsers, 'lastvisit_at', "TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00'");
25  $this->execute("UPDATE ".Yii::app()->getModule('user')->tableUsers." SET create_at = FROM_UNIXTIME(createtime), lastvisit_at = IF(lastvisit,FROM_UNIXTIME(lastvisit),'0000-00-00 00:00:00')");
26  $this->dropColumn(Yii::app()->getModule('user')->tableUsers, 'createtime');
27  $this->dropColumn(Yii::app()->getModule('user')->tableUsers, 'lastvisit');
28  break;
29  case "sqlite":
30  default:
31  $this->addColumn(Yii::app()->getModule('user')->tableUsers, 'create_at', "TIMESTAMP");
32  $this->addColumn(Yii::app()->getModule('user')->tableUsers, 'lastvisit_at', "TIMESTAMP");
33  $this->execute("UPDATE ".Yii::app()->getModule('user')->tableUsers." SET create_at = datetime(createtime, 'unixepoch'), lastvisit_at = datetime(lastvisit, 'unixepoch')");
34  $this->execute('ALTER TABLE "'.Yii::app()->getModule('user')->tableUsers.'" RENAME TO "'.__CLASS__.'_'.Yii::app()->getModule('user')->tableUsers.'"');
35  $this->createTable(Yii::app()->getModule('user')->tableUsers, array(
36  "id" => "pk",
37  "username" => "varchar(20) NOT NULL",
38  "password" => "varchar(128) NOT NULL",
39  "email" => "varchar(128) NOT NULL",
40  "activkey" => "varchar(128) NOT NULL",
41  "superuser" => "int(1) NOT NULL",
42  "status" => "int(1) NOT NULL",
43  "create_at" => "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP",
44  "lastvisit_at" => "TIMESTAMP",
45  ));
46  $this->execute('INSERT INTO "'.Yii::app()->getModule('user')->tableUsers.'" SELECT "id","username","password","email","activkey","superuser","status","create_at","lastvisit_at" FROM "'.__CLASS__.'_'.Yii::app()->getModule('user')->tableUsers.'"');
47  $this->dropTable(__CLASS__.'_'.Yii::app()->getModule('user')->tableUsers);
48  break;
49  }
50  }
51 
52  public function safeDown()
53  {
54  switch ($this->dbType()) {
55  case "mysql":
56  $this->addColumn(Yii::app()->getModule('user')->tableUsers, 'createtime', "int(10) NOT NULL");
57  $this->addColumn(Yii::app()->getModule('user')->tableUsers, 'lastvisit', "int(10) NOT NULL");
58  $this->execute("UPDATE ".Yii::app()->getModule('user')->tableUsers." SET createtime = UNIX_TIMESTAMP(create_at), lastvisit = UNIX_TIMESTAMP(lastvisit_at)");
59  $this->dropColumn(Yii::app()->getModule('user')->tableUsers, 'create_at');
60  $this->dropColumn(Yii::app()->getModule('user')->tableUsers, 'lastvisit_at');
61  break;
62  case "sqlite":
63  default:
64  $this->addColumn(Yii::app()->getModule('user')->tableUsers, 'createtime', "int(10)");
65  $this->addColumn(Yii::app()->getModule('user')->tableUsers, 'lastvisit', "int(10)");
66  $this->execute("UPDATE ".Yii::app()->getModule('user')->tableUsers." SET createtime = strftime('%s',create_at), lastvisit = strftime('%s',lastvisit_at)");
67  $this->execute('ALTER TABLE "'.Yii::app()->getModule('user')->tableUsers.'" RENAME TO "'.__CLASS__.'_'.Yii::app()->getModule('user')->tableUsers.'"');
68  $this->createTable(Yii::app()->getModule('user')->tableUsers, array(
69  "id" => "pk",
70  "username" => "varchar(20) NOT NULL",
71  "password" => "varchar(128) NOT NULL",
72  "email" => "varchar(128) NOT NULL",
73  "activkey" => "varchar(128) NOT NULL",
74  "createtime" => "int(10) NOT NULL",
75  "lastvisit" => "int(10) NOT NULL",
76  "superuser" => "int(1) NOT NULL",
77  "status" => "int(1) NOT NULL",
78  ));
79  $this->execute('INSERT INTO "'.Yii::app()->getModule('user')->tableUsers.'" SELECT "id","username","password","email","activkey","createtime","lastvisit","superuser","status" FROM "'.__CLASS__.'_'.Yii::app()->getModule('user')->tableUsers.'"');
80  $this->execute('DROP TABLE "'.__CLASS__.'_'.Yii::app()->getModule('user')->tableUsers.'"');
81  break;
82  }
83  }
84 
85  public function dbType()
86  {
87  list($type) = explode(':', Yii::app()->db->connectionString);
88  echo "type db: ".$type."\n";
89 
90  return $type;
91  }
92 }