##
## The Antville Project
## http://code.google.com/p/antville
##
## Copyright 2001-2007 by The Antville People
##
## Licensed under the Apache License, Version 2.0 (the ``License'');
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
##    http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an ``AS IS'' BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
## $Revision:3355 $
## $LastChangedBy:piefke3000 $
## $LastChangedDate:2007-10-11 15:38:29 +0200 (Thu, 11 Oct 2007) $
## $URL:https://antville.googlecode.com/svn/trunk/updater/Updater/sql.skin $
##

# Prerequisite for running the updater
#grant all privileges on `antville`.* to 'antville'@'localhost';

<% #test %>
#!imageTable

<% #size %>
delete from AV_IMAGE where IMAGE_PROTOTYPE <> "LayoutImage" and IMAGE_CREATETIME < str_to_date('2008-01-01', '%Y-%m-%d');
delete from AV_TEXT where TEXT_CREATETIME < str_to_date('2008-01-01', '%Y-%m-%d');

<% #legacy %>
alter table AV_SITE drop column SITE_TAGLINE, drop column SITE_USERMAYCONTRIB, drop column SITE_HASDISCUSSIONS, drop column SITE_SHOWDAYS, drop column SITE_SHOWARCHIVE, drop column SITE_LANGUAGE, drop column SITE_COUNTRY, drop column SITE_TIMEZONE, drop column SITE_LONGDATEFORMAT, drop column SITE_SHORTDATEFORMAT, drop column SITE_BGCOLOR, drop column SITE_TEXTFONT, drop column SITE_TEXTCOLOR, drop column SITE_TEXTSIZE, drop column SITE_LINKCOLOR, drop column SITE_ALINKCOLOR, drop column SITE_VLINKCOLOR, drop column SITE_TITLEFONT, drop column SITE_TITLECOLOR, drop column SITE_TITLESIZE, drop column SITE_SMALLFONT, drop column SITE_SMALLCOLOR, drop column SITE_SMALLSIZE;
alter table AV_POLL drop column POLL_TITLE, drop column POLL_ISONLINE;
#delete from AV_IMAGE where IMAGE_ID = 686854;

<% #clear %>
drop table if exists tag;
drop table if exists tag_hub;
drop table if exists content;
drop table if exists image;

<% #jsonize %>
select id, xml, metadata from <% param.value1 %> order by id

<% #tag %>
#!tagTable

<% #tagTable %>
CREATE TABLE IF NOT EXISTS `tag` (
  `id` int(11) NOT NULL default '0',
  `name` varchar(255) default NULL,
  `site_id` int(11) default NULL,
  `type` enum('Story','Image') default NULL,
  PRIMARY KEY  (`id`),
  KEY `tags` (`site_id`,`type`,`name`(1)),
  KEY `name` (`name`)
)
#) ENGINE=MyISAM DEFAULT CHARSET utf8 COLLATE utf8_bin;

<% #tag_hub %>
#!tagHubTable

<% #tagHubTable %>
CREATE TABLE IF NOT EXISTS `tag_hub` (
  `id` int(11) NOT NULL default '0',
  `tag_id` int(11) default NULL,
  `tagged_id` int(11) default NULL,
  `tagged_type` enum('Story','Image') default NULL,
  `user_id` int(11) default NULL,
  PRIMARY KEY  (`id`),
  KEY `tagged` (`tagged_id`, `tagged_type`),
  KEY `tags` (`tag_id`,`tagged_type`,`tagged_id`)
)
#) ENGINE=MyISAM DEFAULT CHARSET utf8 COLLATE utf8_bin;

<% #log %>
CREATE TABLE IF NOT EXISTS `log` (
  `id` int(11) NOT NULL auto_increment,
  `context_id` mediumint(10) default NULL,
  `context_type` varchar(20) default NULL,
  `referrer` mediumtext,
  `action` varchar(255) default NULL,
  `ip` varchar(20) default NULL,
  `created` datetime default NULL,
  `creator_id` mediumint(10) default NULL,
  PRIMARY KEY  (`id`),
  KEY `entries` (`context_type`(5),`action`(5),`created`),
  KEY `requests` (`context_type`(5),`context_id`,created,`action`(5))
)
#) ENGINE=MyISAM DEFAULT CHARSET utf8 COLLATE utf8_bin;

<% #AV_ACCESSLOG %>
alter table AV_ACCESSLOG rename log;
lock tables log write;
delete from log where ACCESSLOG_DATE < curdate();
alter table log drop index IDX_ACCESSLOG_MIXED;
alter table log add column `creator_id` mediumint(10), add column `action` varchar(255), add column `context_type` varchar(20);
alter table log change column ACCESSLOG_ID id int(11), change column ACCESSLOG_REFERRER referrer mediumtext, change column ACCESSLOG_DATE created datetime, change column ACCESSLOG_F_SITE context_id mediumint(10);
#alter table log change column ACCESSLOG_IP ip varchar(20);
create index entries on log (context_type(5), action(5), created);
create index requests on log (context_type(5), context_id, created, action(5));
update log set context_type = 'Story', context_id = ACCESSLOG_F_TEXT where ACCESSLOG_F_TEXT is not null;
update log set context_type = 'Site' where ACCESSLOG_F_TEXT is null;
update log set creator_id = 0;
update log set action = 'main';
alter table log drop column ACCESSLOG_F_TEXT, drop column ACCESSLOG_BROWSER, drop column ACCESSLOG_IP;
#alter table log convert to character set utf8 collate utf8_bin;
unlock tables;

<% #AV_CHOICE %>
alter table AV_CHOICE rename choice;
lock tables choice write;
alter table choice drop index IDX_CHOICE_F_POLL;
alter table choice change column CHOICE_ID id mediumint(10), change column CHOICE_F_POLL poll_id mediumint(10), change column CHOICE_TITLE title varchar(255), change column CHOICE_CREATETIME created datetime, change column CHOICE_MODIFYTIME modified datetime;
create index poll_id on choice (poll_id);
#alter table choice convert to character set utf8 collate utf8_bin;
unlock tables;

<% #AV_FILE %>
alter table AV_FILE rename file;
lock tables file write;
alter table file drop index IDX_FILE_F_SITE, drop index IDX_FILE_ALIAS, drop index IDX_FILE_F_USER_CREATOR;
alter table file add column `metadata` mediumtext, add column `parent_id` mediumint(10), add column `parent_type` varchar(30), add column `prototype` varchar(30);
alter table file change column FILE_ID id mediumint(10), change column FILE_F_SITE site_id mediumint(10), change column FILE_ALIAS name varchar(255), change column FILE_REQUESTCNT requests mediumint(10), change column FILE_CREATETIME created datetime, change column FILE_MODIFYTIME modified datetime, change column FILE_F_USER_CREATOR creator_id mediumint(10), change column FILE_F_USER_MODIFIER modifier_id mediumint(10);
create index site_id on file (site_id);
create index creator_id on file (creator_id);
create index name on file (name(20));
#create index prototype on file (prototype(10));
create index files on file (parent_id, parent_type, created desc); 
#!files
alter table file drop column FILE_MIMETYPE, drop column FILE_NAME, drop column FILE_SIZE, drop column FILE_DESCRIPTION;
#alter table file convert to character set utf8 collate utf8_bin;
unlock tables;

<% #files %>
select id, FILE_MIMETYPE as type, FILE_SIZE as size, FILE_NAME as fileName, 
FILE_DESCRIPTION as description from file order by id

<% #AV_IMAGE %>
drop table if exists image;
#!imageTable
lock tables AV_IMAGE write, AV_IMAGE as i read, AV_IMAGE as t read, AV_SITE read, AV_SITE as site read, AV_LAYOUT read, AV_LAYOUT as l read, AV_LAYOUT as pl read, image write, tag write, tag_hub write;
alter table image change id id int(11) not null auto_increment;
#!layoutImages
#!images
alter table image change id id int(11) not null;
create index prototype on image (prototype(10));
update image set parent_type = 'Site' where prototype = 'Image';
update image set parent_type = 'Layout' where prototype = 'LayoutImage';
update image set prototype = "Image" where prototype = "LayoutImage";
create index site_id on image (site_id);
create index creator_id on image (creator_id);
create index name on image (name(20));
create index images on image (parent_id, parent_type, created desc); 
#alter table image convert to character set utf8 collate utf8_bin;
#drop table AV_IMAGE;
unlock tables;

<% #imageTable %>
CREATE TABLE IF NOT EXISTS `image` (
  `id` mediumint(10) NOT NULL default '0',
  `name` varchar(255) default NULL,
  `prototype` varchar(30) default NULL,
  `site_id` mediumint(10) default NULL,
  `parent_id` varchar(20) default NULL,
  `parent_type` varchar(30) default NULL,
  `metadata` mediumtext,
  `created` datetime default NULL,
  `creator_id` mediumint(10) default NULL,
  `modified` datetime default NULL,
  `modifier_id` mediumint(10) default NULL,
  PRIMARY KEY  (`id`)
)
#) ENGINE=MyISAM DEFAULT CHARSET utf8 COLLATE utf8_bin;

<% #AV_LAYOUT %>
alter table AV_LAYOUT rename layout;
lock tables layout write;
alter table layout add column metadata mediumtext;
alter table layout change column LAYOUT_PREFERENCES xml mediumtext, change column LAYOUT_ID id mediumint(10), change column LAYOUT_ALIAS name varchar(30), change column LAYOUT_F_SITE site_id mediumint(10), change column LAYOUT_F_LAYOUT_PARENT layout_id mediumint(10), change column LAYOUT_CREATETIME created datetime, change column LAYOUT_MODIFYTIME modified datetime, change column LAYOUT_F_USER_CREATOR creator_id mediumint(10), change column LAYOUT_F_USER_MODIFIER modifier_id mediumint(10), change column LAYOUT_SHAREABLE mode enum('default','shared');
update layout set mode = 'default';
create index site_id on layout (site_id);
#!layouts
alter table layout drop column xml, drop column LAYOUT_TITLE, drop column LAYOUT_DESCRIPTION, drop column LAYOUT_ISIMPORT;
#alter table layout convert to character set utf8 collate utf8_bin;
unlock tables;

<% #layouts %>
select id, LAYOUT_TITLE, LAYOUT_DESCRIPTION, LAYOUT_ISIMPORT, 
metadata from layout

<% #AV_MEMBERSHIP %>
alter table AV_MEMBERSHIP rename membership;
lock tables membership write;
alter table membership drop index IDX_MEMBERSHIP_F_SITE, drop index IDX_MEMBERSHIP_F_USER, drop index IDX_MEMBERSHIP_USERNAME;
alter table membership add column `role` enum('Subscriber','Contributor','Manager','Owner') not null;
alter table membership change column MEMBERSHIP_ID id mediumint(10), change column MEMBERSHIP_F_SITE site_id mediumint(10), change column MEMBERSHIP_USERNAME name varchar(100), change column MEMBERSHIP_CREATETIME created datetime, change column MEMBERSHIP_F_USER creator_id mediumint(10), change column MEMBERSHIP_MODIFYTIME modified datetime, change column MEMBERSHIP_F_USER_MODIFIER modifier_id mediumint(10);
create index site_id on membership (site_id);
#create index creator_id on membership(creator_id);
#create index name on membership (name(20));
#create index role on membership(role);
create index roles on membership (name(20), site_id, role); 
create index memberships on membership (creator_id, site_id, role);
update membership set role = 'Contributor' where MEMBERSHIP_LEVEL = 9361;
update membership set role = 'Manager' where MEMBERSHIP_LEVEL = 16383;
update membership set role = 'Owner' where MEMBERSHIP_LEVEL = 131071;
alter table membership drop column MEMBERSHIP_LEVEL;
#alter table membership convert to character set utf8 collate utf8_bin;
unlock tables;

<% #AV_POLL %>
alter table AV_POLL rename poll;
lock tables poll write;
alter table poll drop index IDX_POLL_F_SITE, drop index IDX_POLL_F_USER_CREATOR;
alter table poll add column `status` enum('closed','open');
alter table poll change column POLL_ID id mediumint(10), change column POLL_F_SITE site_id mediumint(10), change column POLL_F_USER_CREATOR creator_id mediumint(10), change column POLL_F_USER_MODIFIER modifier_id mediumint(10), change column POLL_QUESTION question mediumtext, change column POLL_CLOSETIME closed datetime, change column POLL_CREATETIME created datetime, change column POLL_MODIFYTIME modified datetime;
#create index site_id on poll (site_id);
create index creator_id on poll (creator_id);
create index status on poll (status);
create index polls on poll (site_id, creator_id, created);
update poll set status = 'closed';
update poll set status = 'open' where POLL_CLOSED = 1;
alter table poll drop column POLL_CLOSED;
#alter table poll convert to character set utf8 collate utf8_bin;
unlock tables;

<% #AV_SITE %>
alter table AV_SITE rename site;
lock tables site write;
alter table site drop index IDX_SITE_ALIAS, drop index IDX_SITE_F_USER_CREATOR;
alter table site add column metadata mediumtext, add column `mode` enum('closed','restricted','public','open') not null, add column `status` enum('blocked','regular','trusted') not null;
alter table site change column SITE_PREFERENCES xml mediumtext, change column SITE_ID id mediumint(10), change column SITE_ALIAS name varchar(30), change column SITE_F_LAYOUT layout_id mediumint(10), change column SITE_F_USER_CREATOR creator_id mediumint(10), change column SITE_F_USER_MODIFIER modifier_id mediumint(10), change column SITE_CREATETIME created datetime, change column SITE_MODIFYTIME modified datetime;
create index name on site (name(20));
create index layout_id on site (layout_id);
create index creator_id on site (creator_id);
#create index status on site (status);
#create index mode on site (mode);
create index created on site(created);
create index modified on site (modified);
create index sites on site (name(20), mode, status);
update site set mode = 'public';
update site set mode = 'restricted' where SITE_ISONLINE <> 1;
update site set status = 'regular';
update site set status = 'blocked' where SITE_ISBLOCKED = 1;
update site set status = 'trusted' where SITE_ISTRUSTED = 1;
set @timestamp = unix_timestamp(now());
update site set name = concat("www-", @timestamp) where name = "www";
#!sites
alter table site drop column xml, drop column SITE_TITLE, drop column SITE_DISKUSAGE, drop column SITE_EMAIL, drop column SITE_LASTUPDATE, drop column SITE_LASTOFFLINE, drop column SITE_LASTBLOCKWARN, drop column SITE_LASTDELWARN, drop column SITE_LASTPING, drop column SITE_ENABLEPING, drop column SITE_ISONLINE, drop column SITE_ISBLOCKED, drop column SITE_ISTRUSTED;
#alter table site convert to character set utf8 collate utf8_bin;
unlock tables;

<% #sites %>
select id, metadata, SITE_EMAIL, SITE_LASTUPDATE, SITE_LASTOFFLINE, 
SITE_LASTBLOCKWARN, SITE_LASTDELWARN, SITE_LASTPING, SITE_ENABLEPING, 
SITE_TITLE, mode from site

<% #AV_SKIN %>
alter table AV_SKIN rename skin;
lock tables skin write, skin as p_skin write, layout write, site write, site as p_site write;
alter table skin drop index IDX_SKIN_MIXED;
alter table skin add column source mediumtext;
alter table skin change column SKIN_ID id mediumint(10), change column SKIN_F_LAYOUT layout_id mediumint(10), change column SKIN_PROTOTYPE prototype varchar(30), change column SKIN_NAME name varchar(50), change column SKIN_F_USER_CREATOR creator_id mediumint(10), change column SKIN_F_USER_MODIFIER modifier_id mediumint(10), change column SKIN_CREATETIME created datetime, change column SKIN_MODIFYTIME modified datetime;
create index layout_id on skin (layout_id);
create index prototype on skin (prototype);
#create index name on skin (name);
create index skins on skin (name(10), layout_id, prototype);
update skin set prototype = "Files" where prototype = "FileMgr";
update skin set prototype = "Images" where prototype = "ImageMgr";
update skin set prototype = "Layouts" where prototype = "LayoutMgr";
update skin set prototype = "Members" where prototype = "MemberMgr";
update skin set prototype = "Polls" where prototype = "PollMgr";
update skin set prototype = "Skins" where prototype = "SkinMgr";
update skin set prototype = "Stories" where prototype = "StoryMgr";
update skin set prototype = "Archive" where prototype = "Day";
update skin set prototype = "Admin" where prototype = "SysMgr";
update skin set prototype = "LogEntry" where prototype = "SysLog";
update skin set prototype = "Tag" where prototype = "Topic";
update skin set prototype = "Tags" where prototype = "TopicMgr";
update skin set name = "main" where prototype = "Comment" and name = "toplevel";
update skin set prototype = "Membership", name = "status" where prototype = "Members" and name = "statusloggedin";
update skin set prototype = "Membership", name = "login" where prototype = "Members" and name = "statusloggedout";
update skin set name = "search" where prototype = "Site" and name = "searchbox";
update skin set name = "stylesheet" where prototype = "Site" and name = "style";
update skin set name = "date" where prototype = "Story" and name = "dayheader";
update skin set name = "content" where prototype = "Story" and name = "display";
update skin set name = "history" where prototype = "Story" and name = "historyview";
#!skins
alter table skin drop column SKIN_ISCUSTOM, drop column SKIN_SOURCE;
#alter table skin convert to character set utf8 collate utf8_bin;
unlock tables;

<% #skins %>
select skin.id, skin.prototype, skin.name, skin.SKIN_SOURCE as source, 
layout.id as layout_id, layout.name as layout_name, layout.metadata as 
layout_metadata, site.name as site_name, site.layout_id as current_layout, 
(select SKIN_SOURCE from site p_site, skin p_skin where p_site.id = site.id 
and p_skin.layout_id = layout.layout_id and p_skin.prototype = skin.prototype 
and p_skin.name = skin.name) as parent from site left join layout on 
layout.site_id = site.id left join skin on skin.layout_id = layout.id order
by site_name, layout_name, prototype, name

<% #AV_SYSLOG %>
lock tables log write, user read, site read, AV_SYSLOG read;
alter table log change id id int(11) not null auto_increment;
insert into log (context_type, context_id, created, creator_id, action) select 'User', user.id, SYSLOG_CREATETIME, SYSLOG_F_USER_CREATOR, SYSLOG_ENTRY from user, AV_SYSLOG where SYSLOG_OBJECT = user.name and SYSLOG_TYPE = 'user';
insert into log (context_type, context_id, created, creator_id, action) select 'Site', site.id, SYSLOG_CREATETIME, SYSLOG_F_USER_CREATOR, SYSLOG_ENTRY from site, AV_SYSLOG where SYSLOG_OBJECT = site.name and (SYSLOG_TYPE = 'site' or SYSLOG_TYPE = 'weblog');
insert into log (context_type, context_id, created, creator_id, action) select 'Root', 1, SYSLOG_CREATETIME, SYSLOG_F_USER_CREATOR, 'setup' from AV_SYSLOG where SYSLOG_TYPE = 'system';
alter table log change id id int(11) not null;
drop table AV_SYSLOG;
unlock tables;

<% #AV_TEXT %>
#!contentTable
lock tables AV_TEXT write, content write, tag write, tag_hub write;
#!content
#create index site_id on content(site_id);
create index story_id on content (story_id);
create index parent_id on content (parent_id);
#create index parent_type on content (parent_type);
create index creator_id on content (creator_id);
create index requests on content (requests);
#create index status on content (status);
create index content on content (site_id, prototype, status, mode);
#drop table AV_TEXT;
unlock tables;

<% #contentTable %>
CREATE TABLE IF NOT EXISTS `content` (
  `id` mediumint(10) NOT NULL default '0',
  `name` varchar(255) default '',
  `prototype` enum('Story','Comment') default NULL,
  `site_id` mediumint(10) default NULL,
  `story_id` mediumint(10) default NULL,
  `parent_id` mediumint(10) default NULL,
  `parent_type` varchar(30) default NULL,
  `requests` mediumint(10) default NULL,
  `status` enum('closed','pending','readonly','public','shared','open') default NULL,
  `mode` enum('hidden','featured') default NULL,
  `comment_mode` enum('closed','readonly','moderated','open') default NULL,
  `metadata` mediumtext,
  `created` datetime default NULL,
  `creator_id` mediumint(10) default NULL,
  `modified` datetime default NULL,
  `modifier_id` mediumint(10) default NULL,
  PRIMARY KEY  (`id`)
)
#) ENGINE=MyISAM DEFAULT CHARSET utf8 COLLATE utf8_bin;

<% #AV_USER %>
alter table AV_USER rename user;
lock tables user write;
alter table user drop index IDX_USER_NAME;
alter table user add column metadata mediumtext default NULL, add column `status` enum('blocked','regular','trusted','privileged') not null, add column hash varchar(32) default NULL, add column salt varchar(12) default NULL;
alter table user change column USER_ID id mediumint(10), change column USER_NAME name varchar(100), change column USER_EMAIL email varchar(255), change column USER_REGISTERED created datetime, change column USER_LASTVISIT modified datetime;
create index name on user (name(20));
#create index status on user (status);
create index email on user (email);
update user set status = 'regular';
update user set status = 'blocked' where USER_ISBLOCKED = 1;
update user set status = 'trusted' where USER_ISTRUSTED = 1;
update user set status = 'privileged' where USER_ISSYSADMIN = 1;
update user set salt = conv(floor(0 + (rand() * pow(2, 48))), 10, 16), hash = md5(concat(user_password, salt));
#!users
alter table user drop column hash, drop column salt, drop column USER_URL, drop column USER_PASSWORD, drop column USER_EMAIL_ISPUBLIC, drop column USER_ISBLOCKED, drop column USER_ISTRUSTED, drop column USER_ISSYSADMIN;
#alter table user convert to character set utf8 collate utf8_bin;
unlock tables;

<% #AV_VOTE %>
alter table AV_VOTE rename vote;
lock table vote write;
alter table vote drop index IDX_VOTE_F_POLL, drop index IDX_VOTE_F_USER, drop index IDX_VOTE_F_CHOICE, drop index IDX_VOTE_USERNAME;
alter table vote change column VOTE_ID id mediumint(10), change column VOTE_F_POLL poll_id mediumint(10), change column VOTE_F_CHOICE choice_id mediumint(10), change column VOTE_F_USER creator_id mediumint(10), change column VOTE_USERNAME creator_name varchar(255), change column VOTE_CREATETIME created datetime, change column VOTE_MODIFYTIME modified datetime;
create index choice_id on vote (choice_id);
create index votes on vote (creator_name, poll_id);
#alter table vote convert to character set utf8 collate utf8_bin;
unlock tables;

<% #archive %>
select * from image, layout, site where image.name = '<% param.value1 %>' and image.parent_type = "Layout" and layout.id = image.parent_id and layout.name = '<% param.value2 %>' and site.id = layout.site_id and site.name = '<% param.value3 %>'
