プロジェクト

全般

プロフィール

0001-refs-3836-Alter-session-table-colmuns.patch

Shinichi Urabe, 2015-09-05 03:50

ダウンロード (3.63 KB)

差分を表示:

config/doctrine/schema.yml
495 495
Session:
496 496
  actAs: []
497 497
  columns:
498
    id: { type: string(128), primary: true }
499
    session_data: { type: string, comment: "Session information" }
500
    time: { type: string, comment: "Timestamp of generated time" }
498
    id: { type: varbinary(128), notnull: true, primary: true }
499
    session_data: { type: blob(65532), notnull: true, comment: "Session information" }
500
    time: { type: integer(4), unsigned: true, notnull: true, comment: "Timestamp" }
501
    session_lifetime: { type: integer(3), notnull: true, comment: "Lifetime" }
501 502
  options:
502 503
    type: INNODB
503
    collate: utf8_unicode_ci
504
    collate: utf8_bin
504 505
    charset: utf8
505 506
    comment: "Saves session data"
506 507

  
data/fixtures/000_revision.yml
1 1
SnsConfig:
2 2
  current_revision:
3 3
    name:  "OpenPNE_revision"
4
    value: 47
4
    value: 48
data/migrations/048_alter_table_session.php
1
<?php
2

  
3
/**
4
 * This file is part of the OpenPNE package.
5
 * (c) OpenPNE Project (http://www.openpne.jp/)
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file and the NOTICE file that were distributed with this source code.
9
 */
10

  
11
class alterTableSession048 extends opMigration
12
{
13
  public function up()
14
  {
15
    $conn = Doctrine_Manager::connection();
16

  
17
    $conn->execute('ALTER TABLE session CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin');
18
    $conn->execute('ALTER TABLE session MODIFY COLUMN `id` varbinary(128) NOT NULL DEFAULT \'\'');
19
    $conn->execute('ALTER TABLE session MODIFY COLUMN `session_data` blob NOT NULL COMMENT \'Session information\'');
20
    $conn->execute('ALTER TABLE session MODIFY COLUMN `time` int(10) unsigned NOT NULL COMMENT \'Timestamp\'');
21
    $columns = $conn->import->listTableColumns('session');
22
    if (isset($columns['session_lifetime']))
23
    {
24
      return;
25
    }
26

  
27
    $conn->execute('ALTER TABLE session ADD COLUMN `session_lifetime` mediumint(9) NOT NULL COMMENT \'Lifetime\'');
28
  }
29

  
30
  public function postUp()
31
  {
32
    $time = (int) time();
33
    $lifetime = (int) ini_get('session.gc_maxlifetime');
34
    $count = Doctrine::getTable('Session')->createQuery()
35
      ->delete()
36
      ->where('time < ? - ?', array($time, $lifetime))
37
      ->execute();
38
    $this->log(sprintf('DELETED %d sessions', $count));
39

  
40
    $count = Doctrine::getTable('Session')->createQuery()
41
      ->update()
42
      ->set('session_lifetime', 'time + ? - ?', array($lifetime, $time))
43
      ->set('time', '?', $time)
44
      ->execute();
45
    $this->log(sprintf('UPDATED %d sessions', $count));
46
  }
47

  
48
  public function log($str)
49
  {
50
    printf('[%s] %s%s', date('Y-m-d H:i:s'), $str, PHP_EOL);
51
  }
52
}
0
-