<upgrade>
    <version number="2">
        <script>ALTER TABLE `mt_cds_object` CHANGE `location` `location` BLOB NULL DEFAULT NULL</script>
        <script>ALTER TABLE `mt_cds_object` CHANGE `metadata` `metadata` BLOB NULL DEFAULT NULL</script>
        <script>ALTER TABLE `mt_cds_object` CHANGE `auxdata` `auxdata` BLOB NULL DEFAULT NULL</script>
        <script>ALTER TABLE `mt_cds_object` CHANGE `resources` `resources` BLOB NULL DEFAULT NULL</script>
        <script>ALTER TABLE `mt_autoscan` CHANGE `location` `location` BLOB NULL DEFAULT NULL</script>
    </version>
    <version number="3">
        <script>ALTER TABLE "ALTER TABLE `mt_autoscan` CHANGE `scan_mode` `scan_mode` ENUM( 'timed', 'inotify' ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL</script>
        <script>ALTER TABLE `mt_autoscan` DROP INDEX `mt_autoscan_obj_id`, ADD UNIQUE `mt_autoscan_obj_id` ( `obj_id` )</script>
        <script>ALTER TABLE `mt_autoscan` ADD `path_ids` BLOB AFTER `location`</script>
    </version>
    <version number="4">
        <script>ALTER TABLE `mt_cds_object` ADD `service_id` varchar(GRBMAX) default NULL</script>
        <script>ALTER TABLE `mt_cds_object` ADD KEY `cds_object_service_id` (`service_id`)</script>
    </version>
    <version number="5" remark="move to metadata">
        <script>
        CREATE TABLE `mt_metadata` (
            `id` int(11) NOT NULL auto_increment,
            `item_id` int(11) NOT NULL,
            `property_name` varchar(255) NOT NULL,
            `property_value` text NOT NULL,
            PRIMARY KEY `id` (`id`),
            KEY `metadata_item_id` (`item_id`),
            CONSTRAINT `mt_metadata_idfk1` FOREIGN KEY (`item_id`) REFERENCES `mt_cds_object` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
        ) ENGINE=MyISAM CHARSET=utf8
        </script>
    </version>
    <version number="6" remark="add config value table">
        <script>
        CREATE TABLE `grb_config_value` (
            `item` varchar(255) primary key,
            `key` varchar(255) NOT NULL,
            `item_value` varchar(255) NOT NULL,
            `status` varchar(20) NOT NULL)
            ENGINE=MyISAM CHARSET=utf8
        </script>
        <script>CREATE INDEX grb_config_value_item ON grb_config_value(item)</script>
    </version>
    <version number="7">
        <script>DROP TABLE mt_cds_active_item</script>
    </version>
    <version number="8" remark="part_number">
        <script>ALTER TABLE `mt_cds_object` ADD `part_number` int(11) default NULL AFTER `flags`</script>
        <script>ALTER TABLE `mt_cds_object` DROP KEY `cds_object_track_number`</script>
        <script>ALTER TABLE `mt_cds_object` ADD KEY `cds_object_track_number` (`part_number`,`track_number`)</script>
    </version>
    <version number="9" remark="bookmark_pos">
        <script>ALTER TABLE `mt_cds_object` ADD `bookmark_pos` int(11) unsigned NOT NULL default '0' AFTER `service_id`</script>
    </version>
    <version number="10" remark="last_modified">
        <script>ALTER TABLE `mt_cds_object` ADD `last_modified` bigint(20) unsigned default NULL AFTER `bookmark_pos`</script>
    </version>
    <version number="11" remark="last_updated">
        <script>ALTER TABLE `mt_cds_object` ADD `last_updated` bigint(20) unsigned default '0' AFTER `last_modified`</script>
        <script>UPDATE `mt_cds_object` SET `last_updated`=`last_modified`</script>
    </version>
    <version number="12" remark="drop metadata column">
        <script migration="metadata">ALTER TABLE `mt_cds_object` DROP COLUMN `metadata`</script>
    </version>
    <version number="13" remark="add resource table">
        <script>
        CREATE TABLE `grb_cds_resource` (
            `id` int(11) primary key auto_increment,
            `item_id` int(11) NOT NULL,
            `res_id` int(11) NOT NULL,
            `handlerType` int(11) NOT NULL,
            `options` text default NULL,
            `parameters` text default NULL,
            CONSTRAINT `grb_cds_resource_fk` FOREIGN KEY (`item_id`) REFERENCES `mt_cds_object` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
        ) ENGINE=MyISAM CHARSET=utf8
        </script>
        <script>INSERT INTO `mt_internal_setting` VALUES('resource_attribute', '')</script>
        <script>CREATE INDEX `grb_cds_resource_id` ON `grb_cds_resource`(`item_id`,`res_id`)</script>
        <script migration="resources">ALTER TABLE `mt_cds_object` DROP COLUMN `resources`</script>
    </version>
    <version number="14" remark="drop redundant index">
        <script>DROP INDEX `grb_config_value_item` ON `grb_config_value`</script>
    </version>
    <version number="15" remark="Replace primary key on grb_cds_resource">
        <script>DROP INDEX `grb_cds_resource_id` ON `grb_cds_resource`</script>
        <script>ALTER TABLE `grb_cds_resource` DROP COLUMN `id`</script>
        <script>ALTER TABLE `grb_cds_resource` ADD PRIMARY KEY (`item_id`, `res_id`)</script>
    </version>
    <version number="16" remark="add client table">
        <script>
        CREATE TABLE `grb_client` (
            `addr` varchar(32) NOT NULL,
            `port` int(11) NOT NULL,
            `addrFamily` int(11) NOT NULL,
            `userAgent` varchar(255) NOT NULL,
            `last` int(11) NOT NULL,
            `age` int(11) NOT NULL,
            PRIMARY KEY (`addr`, `port`)
        ) ENGINE=MyISAM CHARSET=utf8
        </script>
    </version>
    <version number="17" remark="add playstatus table">
        <script>
        CREATE TABLE `grb_playstatus` (
            `group` varchar(GRBMAX) NOT NULL,
            `item_id` int(11) NOT NULL,
            `playCount` int(11) NOT NULL default '0',
            `lastPlayed` int(11) NOT NULL default '0',
            `lastPlayedPosition` int(11) NOT NULL default '0',
            `bookMarkPos` int(11) NOT NULL default '0',
            PRIMARY KEY (`group`, `item_id`),
            CONSTRAINT `grb_played_item` FOREIGN KEY (`item_id`) REFERENCES `mt_cds_object` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
        ) ENGINE=MyISAM CHARSET=utf8
        </script>
        <script>
        INSERT INTO `grb_playstatus` (
            `group`, `item_id`, `playCount`, `bookMarkPos`
        ) SELECT
            'default', `id`, 1, `bookmark_pos`
        FROM `mt_cds_object` where `bookmark_pos` > 0 OR (`flags` &amp; 0x00000200) != 0
        </script>
        <script>ALTER TABLE `mt_cds_object` DROP COLUMN `bookmark_pos`</script>
    </version>
    <version number="18" remark="add resource purpose">
        <script>ALTER TABLE `grb_cds_resource` ADD `purpose` int(11) NOT NULL default '0'</script>
        <script>UPDATE `grb_cds_resource` SET `purpose`=1 WHERE (`parameters` like '%rct=aa%') OR (`parameters` LIKE '%rct=EX_TH%') OR (`parameters` LIKE '%rct=th%') OR (`options` like '%rct=aa%') OR (`options` LIKE '%rct=EX_TH%') OR (`options` LIKE '%rct=th%')</script>
        <script>UPDATE `grb_cds_resource` SET `purpose`=2 WHERE (`parameters` like '%rct=vs%')</script>
        <script>UPDATE `grb_cds_resource` SET `purpose`=0 WHERE `purpose` is null</script>
    </version>
    <version number="19" remark="add autoscan content">
        <script>ALTER TABLE `mt_autoscan` ADD `media_type` int(11) NOT NULL default '-1'</script>
        <script>ALTER TABLE `mt_autoscan` DROP COLUMN `scan_level`</script>
    </version>
    <version number="20" remark="add autoscan container type">
        <script>ALTER TABLE `mt_autoscan` ADD `ct_audio` varchar(255) NOT NULL default 'object.container.album.musicAlbum'</script>
        <script>ALTER TABLE `mt_autoscan` ADD `ct_image` varchar(255) NOT NULL default 'object.container.album.photoAlbum'</script>
        <script>ALTER TABLE `mt_autoscan` ADD `ct_video` varchar(255) NOT NULL default 'object.container'</script>
    </version>
    <version number="21" remark="set default column colation">
        <script>ALTER TABLE `mt_cds_object` MODIFY `dc_title` varchar(GRBMAX) default NULL COLLATE utf8_general_ci</script>
    </version>
    <version number="22" remark="add autoscan follow_symlinks">
        <script>ALTER TABLE `mt_autoscan` ADD `follow_symlinks` tinyint(4) unsigned NOT NULL default '1'</script>
    </version>
    <version number="23" remark="update autoscan table">
        <script>ALTER TABLE `mt_autoscan` ADD `retry_count` int(11) unsigned NOT NULL default '0'</script>
        <script>ALTER TABLE `mt_autoscan` ADD `dir_types` tinyint(4) unsigned NOT NULL default '0'</script>
        <script>ALTER TABLE `mt_autoscan` ADD `force_rescan` tinyint(4) unsigned NOT NULL default '0'</script>
    </version>
    <version number="24" remark="sort_key">
        <script>ALTER TABLE `mt_cds_object` ADD `sort_key` varchar(GRBMAX) default NULL COLLATE utf8_general_ci</script>
        <script>UPDATE `mt_cds_object` SET `sort_key`=`dc_title`</script>
        <script>ALTER TABLE `mt_cds_object` ADD KEY `mt_cds_object_title` (`dc_title`)</script>
        <script>ALTER TABLE `mt_cds_object` ADD KEY `mt_cds_object_sort_key` (`sort_key`)</script>
    </version>
    <version number="25" remark="resource data type">
        <script>ALTER TABLE `grb_cds_resource` ADD `size2` bigint(20) default NULL</script>
        <script>UPDATE `grb_cds_resource` SET `size2`=`size` WHERE `size` is not null AND `size` != ''</script>
        <script>UPDATE `grb_cds_resource` SET `size2`=NULL WHERE size is null OR size = ''</script>
        <script>ALTER TABLE `grb_cds_resource` DROP COLUMN `size`</script>
        <script>ALTER TABLE `grb_cds_resource` CHANGE `size2` `size` bigint(20) default NULL</script>

        <script>ALTER TABLE `grb_cds_resource` ADD `bitrate2` bigint(20) default NULL</script>
        <script>UPDATE `grb_cds_resource` SET `bitrate2`=`bitrate` WHERE `bitrate` is not null AND `bitrate` != ''</script>
        <script>UPDATE `grb_cds_resource` SET `bitrate2`=NULL WHERE `bitrate` is null OR `bitrate` = ''</script>
        <script>ALTER TABLE `grb_cds_resource` DROP COLUMN `bitrate`</script>
        <script>ALTER TABLE `grb_cds_resource` CHANGE `bitrate2` `bitrate` bigint(20) default NULL</script>

        <script>ALTER TABLE `grb_cds_resource` ADD `sampleFrequency2` bigint(20) default NULL</script>
        <script>UPDATE `grb_cds_resource` SET `sampleFrequency2`=`sampleFrequency` WHERE `sampleFrequency` is not null AND `sampleFrequency` != ''</script>
        <script>UPDATE `grb_cds_resource` SET `sampleFrequency2`=NULL WHERE `sampleFrequency` is null OR `sampleFrequency` = ''</script>
        <script>ALTER TABLE `grb_cds_resource` DROP COLUMN `sampleFrequency`</script>
        <script>ALTER TABLE `grb_cds_resource` CHANGE `sampleFrequency2` `sampleFrequency` bigint(20) default NULL</script>

        <script>ALTER TABLE `grb_cds_resource` ADD `nrAudioChannels2` bigint(20) default NULL</script>
        <script>UPDATE `grb_cds_resource` SET `nrAudioChannels2`=`nrAudioChannels` WHERE `nrAudioChannels` is not null AND `nrAudioChannels` != ''</script>
        <script>UPDATE `grb_cds_resource` SET `nrAudioChannels2`=NULL WHERE `nrAudioChannels` is null OR `nrAudioChannels` = ''</script>
        <script>ALTER TABLE `grb_cds_resource` DROP COLUMN `nrAudioChannels`</script>
        <script>ALTER TABLE `grb_cds_resource` CHANGE `nrAudioChannels2` `nrAudioChannels` bigint(20) default NULL</script>

        <script>ALTER TABLE `grb_cds_resource` ADD `fanArtObject2` bigint(20) default NULL</script>
        <script>UPDATE `grb_cds_resource` SET `fanArtObject2`=`fanArtObject` WHERE `fanArtObject` is not null AND `fanArtObject` != ''</script>
        <script>UPDATE `grb_cds_resource` SET `fanArtObject2`=NULL WHERE `fanArtObject` is null OR `fanArtObject` = ''</script>
        <script>ALTER TABLE `grb_cds_resource` DROP COLUMN `fanArtObject`</script>
        <script>ALTER TABLE `grb_cds_resource` CHANGE `fanArtObject2` `fanArtObject` bigint(20) default NULL</script>

        <script>ALTER TABLE `grb_cds_resource` ADD `fanArtResource2` bigint(20) default NULL</script>
        <script>UPDATE `grb_cds_resource` SET `fanArtResource2`=`fanArtResource` WHERE `fanArtResource` is not null AND `fanArtResource` != ''</script>
        <script>UPDATE `grb_cds_resource` SET `fanArtResource2`=NULL WHERE `fanArtResource` is null OR `fanArtResource` = ''</script>
        <script>ALTER TABLE `grb_cds_resource` DROP COLUMN `fanArtResource`</script>
        <script>ALTER TABLE `grb_cds_resource` CHANGE `fanArtResource2` `fanArtResource` bigint(20) default NULL</script>

        <script>ALTER TABLE `grb_cds_resource` ADD `bitsPerSample2` bigint(20) default NULL</script>
        <script>UPDATE `grb_cds_resource` SET `bitsPerSample2`=`bitsPerSample` WHERE `bitsPerSample` is not null AND `bitsPerSample` != ''</script>
        <script>UPDATE `grb_cds_resource` SET `bitsPerSample2`=NULL WHERE `bitsPerSample` is null OR `bitsPerSample` = ''</script>
        <script>ALTER TABLE `grb_cds_resource` DROP COLUMN `bitsPerSample`</script>
        <script>ALTER TABLE `grb_cds_resource` CHANGE `bitsPerSample2` `bitsPerSample` bigint(20) default NULL</script>

        <script>ALTER TABLE `grb_cds_resource` ADD `orientation2` bigint(20) default NULL</script>
        <script>UPDATE `grb_cds_resource` SET `orientation2`=`orientation` WHERE `orientation` is not null AND `orientation` != ''</script>
        <script>UPDATE `grb_cds_resource` SET `orientation2`=NULL WHERE `orientation` is null OR `orientation` = ''</script>
        <script>ALTER TABLE `grb_cds_resource` DROP COLUMN `orientation`</script>
        <script>ALTER TABLE `grb_cds_resource` CHANGE `orientation2` `orientation` bigint(20) default NULL</script>
    </version>
</upgrade>
