
# This is a fix for InnoDB in MySQL >= 4.1.x
# It "suspends judgement" for fkey relationships until are tables are set.
SET FOREIGN_KEY_CHECKS = 0;

#-----------------------------------------------------------------------------
#-- media_version
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `media_version`;


CREATE TABLE `media_version`
(
	`mvid` INTEGER(11)  NOT NULL AUTO_INCREMENT,
	`mid` INTEGER(11)  NOT NULL,
	`uid` INTEGER(11)  NOT NULL,
	`mediakey` VARCHAR(32)  NOT NULL,
	`mediahash` VARCHAR(128)  NOT NULL,
	`mediatype` INTEGER(11)  NOT NULL,
	`mediasubtype` INTEGER(11)  NOT NULL,
	`spritemap` VARCHAR(255)  NOT NULL,
	`spritemap_count` INTEGER(11) default 0 NOT NULL,
	`first` TINYINT(1) default 0 NOT NULL,
	`hidden` TINYINT(1) default 0 NOT NULL,
	`secret` TINYINT(1) default 0 NOT NULL,
	`status` INTEGER(11) default 0 NOT NULL,
	`featured` TINYINT(1) default 0 NOT NULL,
	`culture` VARCHAR(7)  NOT NULL,
	`properties` TEXT  NOT NULL,
	`childwelfare` TINYINT(1) default 0,
	`title` VARCHAR(255)  NOT NULL,
	`title_clean` VARCHAR(255)  NOT NULL,
	`sid` INTEGER(11),
	`duration` INTEGER(11) default 0 NOT NULL,
	`filesize` INTEGER(11) default 0 NOT NULL,
	`thumbnail_filename` VARCHAR(255),
	`thumbnail_custom_filename` VARCHAR(255),
	`source_filename` VARCHAR(255),
	`original_filename` VARCHAR(255),
	`scrambled_filename` VARCHAR(255),
	`server` VARCHAR(255),
	`transcoder` VARCHAR(255),
	`file_extension` VARCHAR(4)  NOT NULL,
	`meta_title` VARCHAR(255),
	`meta_description` TEXT,
	`meta_keywords` TEXT,
	`meta_author` VARCHAR(255),
	`meta_copyright` VARCHAR(255),
	`conversion_notice` VARCHAR(255),
	`sum_rating` FLOAT(8,2) default 0 NOT NULL,
	`sum_likes` INTEGER(11) default 0 NOT NULL,
	`count_views` INTEGER(11) default 0 NOT NULL,
	`count_views_embed` INTEGER(11) default 0 NOT NULL,
	`count_views_webtv` INTEGER(11) default 0 NOT NULL,
	`count_rating` INTEGER(11) default 0 NOT NULL,
	`count_likes` INTEGER(11) default 0 NOT NULL,
	`count_favorites` INTEGER(11) default 0 NOT NULL,
	`count_comments` INTEGER(11) default 0 NOT NULL,
	`count_flags` INTEGER(11) default 0 NOT NULL,
	`accept_comment` TINYINT(1) default 1 NOT NULL,
	`accept_rating` TINYINT(1) default 1 NOT NULL,
	`exported_to_youtube` TINYINT(1) default 0,
	`price` FLOAT(8,2) default 0,
	`with_costs` TINYINT(1) default 0 NOT NULL,
	`preview_length` INTEGER(11) default 0,
	`enable_download` TINYINT(1) default 0,
	`enable_download_for_users` TINYINT(1) default 1,
	`priority_transcode` TINYINT(1) default 0,
	`smil_exists` TINYINT(1) default 0,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	`uploaded_at` DATETIME  NOT NULL,
	`converted_at` DATETIME,
	PRIMARY KEY (`mvid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- media_category_version
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `media_category_version`;


CREATE TABLE `media_category_version`
(
	`mvid` INTEGER(11)  NOT NULL,
	`mid` INTEGER(11)  NOT NULL,
	`cid` INTEGER(11)  NOT NULL,
	PRIMARY KEY (`mvid`,`mid`,`cid`),
	CONSTRAINT `mcv_fk_mvid`
		FOREIGN KEY (`mvid`)
		REFERENCES `media_version` (`mvid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- media_chapter_version
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `media_chapter_version`;


CREATE TABLE `media_chapter_version`
(
	`mvid` INTEGER(11)  NOT NULL,
	`id` INTEGER(11)  NOT NULL,
	`mid` INTEGER(11)  NOT NULL,
	`language` VARCHAR(128)  NOT NULL,
	`title` VARCHAR(128)  NOT NULL,
	`time` INTEGER(11)  NOT NULL,
	`description` TEXT,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	PRIMARY KEY (`mvid`,`id`),
	CONSTRAINT `mchv_fk_mvid`
		FOREIGN KEY (`mvid`)
		REFERENCES `media_version` (`mvid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- media_field_value_version
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `media_field_value_version`;


CREATE TABLE `media_field_value_version`
(
	`mvid` INTEGER(11)  NOT NULL,
	`mid` INTEGER(11)  NOT NULL,
	`mfid` INTEGER(11)  NOT NULL,
	`value` TEXT,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	PRIMARY KEY (`mvid`,`mid`,`mfid`),
	CONSTRAINT `mfvv_fk_mvid`
		FOREIGN KEY (`mvid`)
		REFERENCES `media_version` (`mvid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- media_file_version
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `media_file_version`;


CREATE TABLE `media_file_version`
(
	`mvid` INTEGER(11)  NOT NULL,
	`mfiid` INTEGER(11)  NOT NULL,
	`mid` INTEGER(11)  NOT NULL,
	`filetype` VARCHAR(20)  NOT NULL,
	`mfoid` INTEGER(11),
	`status` INTEGER(11) default 0 NOT NULL,
	`preview` TINYINT(1) default 0 NOT NULL,
	`title` VARCHAR(255)  NOT NULL,
	`filename` VARCHAR(255)  NOT NULL,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	`converted_at` DATETIME,
	PRIMARY KEY (`mvid`,`mfiid`),
	CONSTRAINT `mfv_fk_mvid`
		FOREIGN KEY (`mvid`)
		REFERENCES `media_version` (`mvid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- media_permission_version
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `media_permission_version`;


CREATE TABLE `media_permission_version`
(
	`mvid` INTEGER(11)  NOT NULL,
	`mid` INTEGER(11)  NOT NULL,
	`rid` INTEGER(11)  NOT NULL,
	PRIMARY KEY (`mvid`,`mid`,`rid`),
	CONSTRAINT `mpv_fk_mvid`
		FOREIGN KEY (`mvid`)
		REFERENCES `media_version` (`mvid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- media_tag_version
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `media_tag_version`;


CREATE TABLE `media_tag_version`
(
	`mvid` INTEGER(11)  NOT NULL,
	`mid` INTEGER(11)  NOT NULL,
	`tid` INTEGER(11)  NOT NULL,
	`pos` INTEGER(3)  NOT NULL,
	PRIMARY KEY (`mvid`,`mid`,`tid`),
	CONSTRAINT `mtv_fk_mvid`
		FOREIGN KEY (`mvid`)
		REFERENCES `media_version` (`mvid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- media_youtube_version
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `media_youtube_version`;


CREATE TABLE `media_youtube_version`
(
	`mvid` INTEGER(11)  NOT NULL,
	`mid` INTEGER(11)  NOT NULL,
	`youtube` VARCHAR(1024)  NOT NULL,
	`id` VARCHAR(255)  NOT NULL,
	`title` VARCHAR(255),
	`description` TEXT,
	`keywords` TEXT,
	`thumbnail` VARCHAR(1024),
	`url` VARCHAR(1024),
	`duration` INTEGER(11),
	`owner` VARCHAR(255),
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	`related` TINYINT(1) default 0 NOT NULL,
	PRIMARY KEY (`mvid`,`mid`),
	CONSTRAINT `myv_fk_mvid`
		FOREIGN KEY (`mvid`)
		REFERENCES `media_version` (`mvid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- media_vimeo_version
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `media_vimeo_version`;


CREATE TABLE `media_vimeo_version`
(
	`mvid` INTEGER(11)  NOT NULL,
	`mid` INTEGER(11)  NOT NULL,
	`vimeo` VARCHAR(1024)  NOT NULL,
	`id` VARCHAR(255)  NOT NULL,
	`title` VARCHAR(255),
	`description` TEXT,
	`keywords` TEXT,
	`thumbnail` VARCHAR(1024),
	`url` VARCHAR(1024),
	`duration` INTEGER(11),
	`owner` VARCHAR(255),
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	PRIMARY KEY (`mvid`,`mid`),
	CONSTRAINT `mvv_fk_mvid`
		FOREIGN KEY (`mvid`)
		REFERENCES `media_version` (`mvid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- statictext_version
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `statictext_version`;


CREATE TABLE `statictext_version`
(
	`mvid` INTEGER(11)  NOT NULL,
	`sid` INTEGER(11)  NOT NULL,
	`culture` VARCHAR(7)  NOT NULL,
	`title` VARCHAR(255),
	`content` TEXT  NOT NULL,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	PRIMARY KEY (`mvid`,`sid`),
	CONSTRAINT `sv_fk_mv`
		FOREIGN KEY (`mvid`)
		REFERENCES `media_version` (`mvid`)
)Engine=InnoDB;

# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;
