1<?php
2/**
3 * Migrate to Horde_Share_Sql hierarchical shares.
4 *
5 * Copyright 2010-2017 Horde LLC (http://www.horde.org/)
6 *
7 * See the enclosed file COPYING for license information (GPL). If you
8 * did not receive this file, see http://www.horde.org/licenses/gpl.
9 *
10 * @author   Michael J. Rubinsky <mrubinsk@horde.org>
11 * @category Horde
12 * @license  http://www.horde.org/licenses/gpl GPL
13 * @package  Ansel
14 */
15class AnselUpgradeSqlHierarchical extends Horde_Db_Migration_Base
16{
17    /**
18     * Upgrade.
19     */
20    public function up()
21    {
22        try {
23            $this->removeIndex('ansel_shares', 'share_parents');
24        } catch (Exception $e) {}
25        try {
26            $this->removeIndex('ansel_shares', array('name' => 'ansel_shares_share_parents_idx'));
27        } catch (Exception $e) {}
28        $this->addColumn('ansel_shares', 'share_name', 'string', array('limit' => 255, 'null' => false, 'default' => ''));
29        $this->changeColumn('ansel_shares', 'share_parents', 'text');
30
31        // Add sharenames
32        $sql = 'SELECT share_id FROM ansel_shares';
33        $ids = $this->_connection->selectValues($sql);
34        $sql = 'UPDATE ansel_shares SET share_name = ? WHERE share_id = ?';
35        foreach ($ids as $id) {
36            $params = array(strval(new Horde_Support_Randomid()), $id);
37            $this->_connection->update($sql, $params);
38        }
39    }
40
41    /**
42     * Downgrade
43     *
44     */
45    public function down()
46    {
47        $this->removeColumn('ansel_shares', 'share_name');
48        $this->changeColumn('ansel_shares', 'share_parents', 'string', array('limit' => 255));
49        $this->addIndex('ansel_shares', array('share_parents'));
50    }
51
52}
53