DROP TABLE IF EXISTS `plan`;

CREATE TABLE `plan` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `description` varchar(255) default NULL,
  `user_id` int(11) default NULL,
  `creation_date` varchar(25) NOT NULL default '',
  `max_sites` int(11) NOT NULL default '0',
  `max_pages` int(11) NOT NULL default '0',
  `max_root_pages` int(11) NOT NULL default '0',
  `max_hosts` int(11) NOT NULL default '0',
  `max_users` int(11) NOT NULL default '0',
  `max_page_level` int(11) NOT NULL default '0',
  `max_private_templates` int(11) NOT NULL default '-1',
  `is_user_management_allowed` int(1) NOT NULL default '1',
  `is_site_management_allowed` int(1) NOT NULL default '1',
  `is_active` int(1) NOT NULL default '1',
  `is_trial` int(1) NOT NULL default '1',
  `is_publication_settings_management_allowed` int(1) NOT NULL default '0',
  `trial_life_time` int(11) NOT NULL default '0',
  `trial_life_type_id` int(11) NOT NULL default '0',
  `uuid` varchar(100) default '',
  `default_site_family_id` int(11) NOT NULL default '1',
  `is_additional_site_content_allowed` int(1) NOT NULL default '0',
  `additional_site_content` text NOT NULL default '',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `plan_template`;

CREATE TABLE `plan_template` (
  `id` int(11) NOT NULL auto_increment,
  `plan_id` int(11) NOT NULL default '0',
  `template_id` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `plan_id` (`plan_id`),
  UNIQUE KEY `template_id_plan_id` (`template_id`, `plan_id`),
  KEY `template_id` (`template_id`)
) ;


DROP TABLE IF EXISTS `plan_host`;

CREATE TABLE `plan_host` (
  `id` int(11) NOT NULL auto_increment,
  `plan_id` int(11) NOT NULL default '0',
  `host_id` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `plan_id` (`plan_id`),
  UNIQUE KEY `host_id_plan_id` (`host_id`, `plan_id`),
  KEY `host_id` (`host_id`)
) ;


DROP TABLE IF EXISTS `plan_pageset`;

CREATE TABLE `plan_pageset` (
  `id` int(11) NOT NULL auto_increment,
  `plan_id` int(11) NOT NULL default '0',
  `pageset_id` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `plan_id` (`plan_id`),
  UNIQUE KEY `pageset_id_plan_id` (`pageset_id`, `plan_id`),
  KEY `pageset_id` (`pageset_id`)
) ;


DROP TABLE IF EXISTS `plan_module`;

CREATE TABLE `plan_module` (
  `id` int(11) NOT NULL auto_increment,
  `plan_id` int(11) NOT NULL default '0',
  `module_id` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `plan_id` (`plan_id`),
  UNIQUE KEY `module_id_plan_id` (`module_id`, `plan_id`),
  KEY `module_id` (`module_id`)
) ;


DROP TABLE IF EXISTS `plan_publish_mode`;

CREATE TABLE `plan_publish_mode` (
  `id` int(11) NOT NULL auto_increment,
  `plan_id` int(11) NOT NULL default '0',
  `publish_mode_id` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `plan_id` (`plan_id`),
  UNIQUE KEY `module_id_plan_id` (`publish_mode_id`, `plan_id`),
  KEY `module_id` (`publish_mode_id`)
) ;


DROP TABLE IF EXISTS `plan_site_family`;

CREATE TABLE `plan_site_family` (
  `id` int(11) NOT NULL auto_increment,
  `plan_id` int(11) NOT NULL default '0',
  `site_family_id` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `plan_id` (`plan_id`),
  UNIQUE KEY `site_family_id_plan_id` (`site_family_id`, `plan_id`),
  KEY `site_family_id` (`site_family_id`)
) ;


DROP TABLE IF EXISTS `trial_life_type`;

CREATE TABLE `trial_life_type` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `site`;

CREATE TABLE `site` (
  `id` int(11) NOT NULL auto_increment,
  `dir_name` varchar(32) NOT NULL,
  `site_id` varchar(32) NOT NULL,
  `alias` varchar(255) NOT NULL default '',
  `password` varchar(32) NOT NULL default '',
  `creation_date` varchar(25) NOT NULL default '',
  `user_id` int(11) NOT NULL default '0',
  `template_id` int(11) NOT NULL default '0',
  `site_publish_settings_id` int(11) default NULL,
  `is_active` int(1) NOT NULL default '1',
  `site_familia_id` int(1) NOT NULL default '1',
  `logo` varchar(255) NOT NULL,
  `custom_logo` varchar(255),
  `color_scheme` varchar(255) NOT NULL,
  `header` varchar(255) NOT NULL,
  `custom_header` varchar(255),
  `button` varchar(255) NOT NULL,
  `copyright` varchar(255) NOT NULL default '',
  `company_name` varchar(255) NOT NULL default '',
  `company_slogan` varchar(255) NOT NULL default '',
  `locale_id` int(11) NOT NULL default '0',
  `count_pages` int(11) default '0',
  `count_root_pages` int(11) default '0',
  `is_changed_since_last_publication` int(1) NOT NULL default '1',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `site_id` (`site_id`),
  UNIQUE KEY `site_publish_setting_id` (`site_publish_settings_id`),
  KEY `user_id` (`user_id`),
  KEY `template_id` (`template_id`)
) ;


DROP TABLE IF EXISTS `site_publication`;

CREATE TABLE `site_publication` (
  `id` int(11) NOT NULL auto_increment,
  `host_id` int(11) default 0,
  `fallback_ip` varchar(255) default NULL,
  `dsn` varchar(255) default '',
  `url` varchar(255) default '',
  `site_id` int(11) NOT NULL,
  `version` varchar(15) NOT NULL,
  `modules_api_version` varchar(15) NOT NULL,
  `publication_time` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY (`site_id`),
  KEY (`host_id`),
  KEY (`publication_time`)
) ;


DROP TABLE IF EXISTS `site_publish_settings`;

CREATE TABLE `site_publish_settings` (
  `id` int(11) NOT NULL auto_increment,
  `host_id` int(11) default 0,
  `uuid` varchar(100) default '',
  `fallback_ip` varchar(255) default NULL,
  `dsn` varchar(255) default '',
  `url` varchar(255) default '',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `site_familia`;

CREATE TABLE `site_familia` (
  `id` int(11) NOT NULL auto_increment,
  `pageset_id` int(11) NOT NULL,
  `template_category_id` int(11) NOT NULL,
  `code` varchar(255) NOT NULL,
  `uuid` varchar(100) default '',
  `is_built_in` int(1) NOT NULL default '0',
  `user_id` int(11) NOT NULL default '0',
  PRIMARY KEY (`id`),
  KEY (`pageset_id`),
  UNIQUE KEY (`code`)
) ;

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (
  `id` int(11) NOT NULL auto_increment,
  `parent_id` int(11) NOT NULL default '0',
  `plan_id` int(11) NOT NULL default '0',
  `role_id` int(11) NOT NULL default '0',
  `user_settings_id` int(11) default NULL,
  `user_name` varchar(255) default NULL,
  `user_password` varchar(128) default NULL,
  `email` varchar(255) default NULL,
  `first_name` varchar(255) default NULL,
  `last_name` varchar(255) default NULL,
  `creation_date` varchar(25) NOT NULL default '',
  `auth_cookie` varchar(255) default NULL,
  `must_migrate` int(11) default '0',
  `uuid` varchar(100) default '',
  `sso_uid` varchar(100) NOT NULL default '',
  `is_local` int(1) NOT NULL default '1',
  PRIMARY KEY  (`id`),
  KEY (`parent_id`)
) ;


DROP TABLE IF EXISTS `user_reference`;

CREATE TABLE `user_reference` (
  `id` int(11) NOT NULL auto_increment,
  `parent_id` int(11) NOT NULL default '0',
  `child_id` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `user_settings`;

CREATE TABLE `user_settings` (
  `id` int(11) NOT NULL auto_increment,
  `locale_id` int(11) NOT NULL default '0',
  `skin_id` int(11) NOT NULL default '0',
  `logo_path` varchar(255) default NULL,
  `tmp_logo_path` varchar(255) default NULL,
  `custom_link` varchar(255) default NULL,
  `custom_title` varchar(255) default NULL,
  `list_size` int(4) default 0,
  `is_show_overview_page` int(1) NOT NULL default '1',
  `is_show_getting_started` int(1) NOT NULL default '1',
  `is_first_time_login` int(1) NOT NULL default '1',
  `is_locked` int(1) NOT NULL default '1',
  `is_built_in` int(1) NOT NULL default '1',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `template_category`;

CREATE TABLE `template_category` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `template`;

CREATE TABLE `template` (
  `id` int(11) NOT NULL auto_increment,
  `code` varchar(32) NOT NULL default '',
  `name` varchar(32) NOT NULL default '',
  `is_active` int(1) NOT NULL default '1',
  `template_category_id` int(11) NOT NULL default '0',
  `version` varchar(16) NOT NULL default '',
  `defaults` varchar(128) NOT NULL default '',
  `schemes` varchar(128) NOT NULL default '',
  `header_upload_support` tinyint(1),
  `keywords` text,
  PRIMARY KEY  (`id`),
  UNIQUE KEY (`code`)
) ;


DROP TABLE IF EXISTS `skin`;

CREATE TABLE `skin` (
  `id` int(11) NOT NULL auto_increment,
  `code` varchar(255) NOT NULL default '',
  `name` varchar(255) NOT NULL default '',
  `is_active` int(1) NOT NULL default '1',
  `uuid` varchar(100) default '',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `locale`;

CREATE TABLE `locale` (
  `id` int(11) NOT NULL auto_increment,
  `code` varchar(255) NOT NULL default '',
  `name` varchar(255) NOT NULL default '',
  `english_name` varchar(255) NOT NULL default '',
  `decimal_point` char(3) NOT NULL default '.',
  `monetary_unit_code` char(3) NOT NULL default 'USD',
  `currency_symbol_left` varchar(20) NOT NULL default '$',
  `currency_symbol_right` varchar(20) NOT NULL default '',
  `date_format` varchar(10) NOT NULL default 'Y/m/d',
  `time_format` varchar(10) NOT NULL default 'H:i:s',
  `week_begin` tinyint(1) unsigned NOT NULL default '1',
  `is_active` int(1) NOT NULL default '1',
  `uuid` varchar(100) default '',
  PRIMARY KEY  (`id`)
) ;

DROP TABLE IF EXISTS `module`;

CREATE TABLE `module` (
  `id` int(11) NOT NULL auto_increment,
  `code` varchar(255) NOT NULL default '',
  `edit_view` varchar(255) default NULL,
  `name_locale_key` varchar(255) NOT NULL default '',
  `name_locale_section` varchar(255) NOT NULL default '',
  `description_locale_key` varchar(255) NOT NULL default '',
  `description_locale_section` varchar(255) NOT NULL default '',
  `is_active` int(1) NOT NULL default '1',
  `module_type_id` int(11) NOT NULL default '0',
  `version` varchar(255) NOT NULL default '',
  `required_api_version` varchar(255) NOT NULL default '',
  `is_uninstalled` int(1) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY (`code`)
) ;


DROP TABLE IF EXISTS `module_type`;

CREATE TABLE `module_type` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ;

DROP TABLE IF EXISTS `module_instance`;

CREATE TABLE `module_instance` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `instance_id` varchar(50) NOT NULL,
  `site_id` int(11) NOT NULL,
  `module_id` int(11) NOT NULL,
  `time_create` int(11) NOT NULL,
  `time_remove` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY (`module_id`),
  UNIQUE KEY (`instance_id`),
  KEY (`site_id`)
) ;


DROP TABLE IF EXISTS `module_instance_publication`;

CREATE TABLE `module_instance_publication` (
  `id` int(11) NOT NULL auto_increment,
  `module_instance_id` int(11) NOT NULL,
  `site_publication_id` int(11) NOT NULL,
  `published_instance_id` varchar(32) NOT NULL,
  `version` varchar(15) NOT NULL,
  `required_api_version` varchar(15) NOT NULL,
  `publication_time` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY (`module_instance_id`),
  KEY (`publication_time`)
) ;


DROP TABLE IF EXISTS `role`;

CREATE TABLE `role` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `publish_mode`;

CREATE TABLE `publish_mode` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `pageset`;

CREATE TABLE `pageset` (
  `id` int(11) NOT NULL auto_increment,
  `code` varchar(255) NOT NULL default '',
  `root_node_id` int(11) NOT NULL default '0',
  `is_built_in` int(1) NOT NULL default '0',
  `uuid` varchar(100) default '',
  `user_id` int(11) NOT NULL default '0',
  PRIMARY KEY (`id`)
) ;


DROP TABLE IF EXISTS `pageset_node`;

CREATE TABLE `pageset_node` (
  `id`                   int(11) NOT NULL auto_increment,
  `order_id`             int(11) NOT NULL default '0',
  `parent_id`            int(11) NOT NULL default '0',
  `pageset_page_type_id` int(11) NOT NULL default 0,
  `module_code`          varchar(255) default NULL,
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `pageset_page`;

CREATE TABLE `pageset_page` (
  `id`                   int(11)      NOT NULL auto_increment,
  `pageset_node_id`      int(11)      NOT NULL,
  `locale_id`            int(11)      NOT NULL default '0',
  `name`                 varchar(255) NOT NULL default '',
  `content`              text         NOT NULL default '',
  PRIMARY KEY (`id`)
) ;


DROP TABLE IF EXISTS `host`;

CREATE TABLE `host` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL default '',
  `is_active` int(1) NOT NULL default '1',
  `user_id` int(11) NOT NULL default '0',
  `uuid` varchar(100) default '',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `custom_setting`;

CREATE TABLE `custom_setting` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL default '',
  `value` text NOT NULL default '',
  `locale_id` int(11) NOT NULL default '0',
  `user_id` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `log`;

CREATE TABLE `log` (
  `id` int(11) NOT NULL auto_increment,
  `message` text NOT NULL default '',
  `level` int(11) NOT NULL default '0',
  `creation_date` varchar(25) NOT NULL default '',
  `user_name` varchar(255) NOT NULL default '',
  `ip` varchar(16) NOT NULL default '',
  `request_uri` varchar(255) NOT NULL default '',
  `referer` varchar(255) NOT NULL default '',
  `user_agent` varchar(255) NOT NULL default '',
  `log_source_id` int(11) NOT NULL default '0',
  `log_type_id` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `log_source`;

CREATE TABLE `log_source` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `log_type`;

CREATE TABLE `log_type` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `config_section`;

CREATE TABLE `config_section` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `name` (`name`)
) ;


DROP TABLE IF EXISTS `config_param`;

CREATE TABLE `config_param` (
  `id` int(11) NOT NULL auto_increment,
  `config_section_id` int(11) NOT NULL default '0',
  `name` varchar(255) NOT NULL default '',
  `value` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `view`;

CREATE TABLE `view` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `action`;

CREATE TABLE `action` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `view_access`;

CREATE TABLE `view_access` (
  `id` int(11) NOT NULL auto_increment,
  `role_id` int(11) NOT NULL default '0',
  `view_id` int(11) NOT NULL default '0',
  `is_allowed` int(1) NOT NULL default '1',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `action_access`;

CREATE TABLE `action_access` (
  `id` int(11) NOT NULL auto_increment,
  `role_id` int(11) NOT NULL default '0',
  `action_id` int(11) NOT NULL default '0',
  `is_allowed` int(1) NOT NULL default '1',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `license_key`;

CREATE TABLE `license_key` (
  `id` int(11) NOT NULL auto_increment,
  `data` text NOT NULL default '',
  `name` varchar(255) NOT NULL default '',
  `update_date` varchar(25) default '',
  `update_password` varchar(255) default '',
  `expiration_date` varchar(25) default '',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `session`;

CREATE TABLE `session` (
  `id` char(32) NOT NULL,
  `expiry` int unsigned NOT NULL default '0',
  `data` TEXT NOT NULL,
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `internal_sip`;

CREATE TABLE `internal_sip` (
  `id` int(11) NOT NULL auto_increment,
  `data` text NOT NULL default '',
  `update_date` varchar(25) default '',
  PRIMARY KEY  (`id`)
) ;


DROP TABLE IF EXISTS `site_family_settings`;

CREATE TABLE `site_family_settings` (
  `id` int(11) NOT NULL auto_increment,
  `site_family_id` int(11) NOT NULL,
  `locale_id` int(11) NOT NULL default '0',
  `title` varchar(255) default '',
  `description` text NOT NULL default '',
  `image` varchar(255) default '',
  PRIMARY KEY  (`id`)
) ;

DROP TABLE IF EXISTS `task`;

CREATE TABLE `task` (
  `id` int(11) NOT NULL auto_increment,
  `action` text NOT NULL default '',
  `type` int(1) NOT NULL default 0,
  `time` int unsigned NOT NULL default '0',
  `period` int unsigned default '0',
  PRIMARY KEY  (`id`)
) ;
