From b57b436fe764371fafe2972243d6ca9904a7452a Mon Sep 17 00:00:00 2001 From: Shinichi Urabe Date: Sat, 5 Sep 2015 03:38:43 +0900 Subject: [PATCH 1/2] (refs #3836) Alter session table colmuns. --- config/doctrine/schema.yml | 9 ++--- data/fixtures/000_revision.yml | 2 +- data/migrations/048_alter_table_session.php | 52 +++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 data/migrations/048_alter_table_session.php diff --git a/config/doctrine/schema.yml b/config/doctrine/schema.yml index 4dd5f54..2c83739 100644 --- a/config/doctrine/schema.yml +++ b/config/doctrine/schema.yml @@ -495,12 +495,13 @@ BannerUseImage: Session: actAs: [] columns: - id: { type: string(128), primary: true } - session_data: { type: string, comment: "Session information" } - time: { type: string, comment: "Timestamp of generated time" } + id: { type: varbinary(128), notnull: true, primary: true } + session_data: { type: blob(65532), notnull: true, comment: "Session information" } + time: { type: integer(4), unsigned: true, notnull: true, comment: "Timestamp" } + session_lifetime: { type: integer(3), notnull: true, comment: "Lifetime" } options: type: INNODB - collate: utf8_unicode_ci + collate: utf8_bin charset: utf8 comment: "Saves session data" diff --git a/data/fixtures/000_revision.yml b/data/fixtures/000_revision.yml index 8e23eda..97b733e 100644 --- a/data/fixtures/000_revision.yml +++ b/data/fixtures/000_revision.yml @@ -1,4 +1,4 @@ SnsConfig: current_revision: name: "OpenPNE_revision" - value: 47 + value: 48 diff --git a/data/migrations/048_alter_table_session.php b/data/migrations/048_alter_table_session.php new file mode 100644 index 0000000..7cd05b9 --- /dev/null +++ b/data/migrations/048_alter_table_session.php @@ -0,0 +1,52 @@ +execute('ALTER TABLE session CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin'); + $conn->execute('ALTER TABLE session MODIFY COLUMN `id` varbinary(128) NOT NULL DEFAULT \'\''); + $conn->execute('ALTER TABLE session MODIFY COLUMN `session_data` blob NOT NULL COMMENT \'Session information\''); + $conn->execute('ALTER TABLE session MODIFY COLUMN `time` int(10) unsigned NOT NULL COMMENT \'Timestamp\''); + $columns = $conn->import->listTableColumns('session'); + if (isset($columns['session_lifetime'])) + { + return; + } + + $conn->execute('ALTER TABLE session ADD COLUMN `session_lifetime` mediumint(9) NOT NULL COMMENT \'Lifetime\''); + } + + public function postUp() + { + $time = (int) time(); + $lifetime = (int) ini_get('session.gc_maxlifetime'); + $count = Doctrine::getTable('Session')->createQuery() + ->delete() + ->where('time < ? - ?', array($time, $lifetime)) + ->execute(); + $this->log(sprintf('DELETED %d sessions', $count)); + + $count = Doctrine::getTable('Session')->createQuery() + ->update() + ->set('session_lifetime', 'time + ? - ?', array($lifetime, $time)) + ->set('time', '?', $time) + ->execute(); + $this->log(sprintf('UPDATED %d sessions', $count)); + } + + public function log($str) + { + printf('[%s] %s%s', date('Y-m-d H:i:s'), $str, PHP_EOL); + } +} -- 2.5.1