/
3_horde_history_add_modseq.php
39 lines (33 loc) · 1.49 KB
/
3_horde_history_add_modseq.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
class HordeHistoryAddModSeq extends Horde_Db_Migration_Base
{
public function up()
{
$t = $this->createTable('horde_histories_modseq', array('autoincrementKey' => false));
$t->column('history_modseq', 'integer', array('null' => false, 'default' => 0));
$t->column('history_modseqempty', 'integer', array('null' => false, 'default' => 0));
$t->end();
$this->addColumn('horde_histories', 'history_modseq', 'integer', array('default' => 0, 'null' => false));
$rows = $this->selectAll('SELECT history_id FROM horde_histories ORDER BY history_ts ASC');
$seq = 1;
$this->beginDbTransaction();
foreach ($rows as $row) {
$this->update(
'UPDATE horde_histories SET history_modseq = ? WHERE history_id = ?',
array($seq++, $row['history_id']));
}
if (!empty($rows)) {
$this->insert('INSERT INTO horde_histories_modseq (history_modseq) VALUES(?)', array($seq - 1));
}
$this->commitDbTransaction();
// Add the index after the new values are set for performance reasons.
$this->addIndex('horde_histories', array('history_modseq'));
// ...same with the autoincrement for this field.
$this->changeColumn('horde_histories_modseq', 'history_modseq', 'autoincrementKey');
}
public function down()
{
$this->dropTable('horde_histories_modseq');
$this->removeColumn('horde_histories', 'history_modseq');
}
}