<?xml version="1.0" encoding="UTF-8"?>
<schema>
    <!-- Metadata -->
    <meta>
        <!-- Supported driver types -->
        <drivers>
            <driver>mysql</driver>
            <driver>mysqli</driver>
        </drivers>
    </meta>

    <!-- SQL commands to run on installation and update -->
    <sql>
        <action table="#__akeeba_common" canfail="0">
          <condition type="missing" value=""/>
          <query><![CDATA[
CREATE TABLE `#__akeeba_common` (
	`key` varchar(255) NOT NULL,
	`value` longtext NOT NULL,
	PRIMARY KEY (`key`)
) DEFAULT COLLATE utf8_general_ci CHARSET=utf8;
            ]]></query>
        </action>

        <action table="#__ak_params" canfail="0">
            <condition type="missing" value="" />
            <query><![CDATA[
CREATE TABLE `#__ak_params` (
  `tag` varchar(255) NOT NULL,
  `data` longtext,
  PRIMARY KEY (`tag`)
) DEFAULT CHARACTER SET utf8;
            ]]></query>
        </action>

        <action table="#__ak_profiles" canfail="0">
            <condition type="missing" value="" />
            <query><![CDATA[
CREATE TABLE `#__ak_profiles` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`description` varchar(255) NOT NULL,
`configuration` longtext,
`filters` longtext,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8;
            ]]></query>
        </action>

        <action table="#__ak_profiles" canfail="1">
            <condition type="equals" operator="not" value="1"><![CDATA[
SELECT COUNT(*) FROM `#__ak_profiles` WHERE `id` = 1;
            ]]></condition>

            <query><![CDATA[
INSERT IGNORE INTO `#__ak_profiles`
(`id`,`description`, `configuration`, `filters`) VALUES
(1,'Default Backup Profile','','');
            ]]></query>
        </action>

        <action table="#__ak_stats" canfail="0">
            <condition type="missing" value="" />
            <query><![CDATA[
CREATE TABLE `#__ak_stats` (
	`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
	`description` varchar(255) NOT NULL,
	`comment` longtext,
	`backupstart` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
	`backupend` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
	`status` enum('run','fail','complete') NOT NULL DEFAULT 'run',
	`origin` varchar(30) NOT NULL DEFAULT 'backend',
	`type` varchar(30) NOT NULL DEFAULT 'full',
	`profile_id` bigint(20) NOT NULL DEFAULT '1',
	`archivename` longtext,
	`absolute_path` longtext,
	`multipart` int(11) NOT NULL DEFAULT '0',
	`tag` varchar(255) DEFAULT NULL,
	`backupid` varchar(255) DEFAULT NULL,
	`filesexist` tinyint(3) NOT NULL DEFAULT '1',
	`remote_filename` varchar(1000) DEFAULT NULL,
	`total_size` bigint(20) NOT NULL DEFAULT '0',
	PRIMARY KEY (`id`),
	KEY `idx_fullstatus` (`filesexist`,`status`),
	KEY `idx_stale` (`status`,`origin`)
) DEFAULT CHARACTER SET utf8;
            ]]></query>
        </action>

        <action table="#__ak_storage" canfail="0">
            <condition type="missing" value="" />
            <query><![CDATA[
CREATE TABLE `#__ak_storage` (
	`tag` varchar(255) NOT NULL,
	`lastupdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
	`data` longtext,
	PRIMARY KEY (`tag`)
) DEFAULT CHARACTER SET utf8;
            ]]></query>
        </action>

        <action table="#__ak_users" canfail="0">
            <condition type="missing" value="" />
            <query><![CDATA[
CREATE TABLE `#__ak_users` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `parameters` longtext,
  PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8;
            ]]></query>
        </action>

        <!-- Add the backupid column to #__ak_stats if it's missing -->
        <action table="#__ak_stats" canfail="1">
            <condition type="missing" value="backupid" />
            <query><![CDATA[
ALTER TABLE `#__ak_stats`
ADD COLUMN `backupid` varchar(255) DEFAULT NULL
AFTER `tag`
            ]]></query>
        </action>
    </sql>
</schema>