
# 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;

#-----------------------------------------------------------------------------
#-- livestream
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `livestream`;


CREATE TABLE `livestream`
(
	`lsid` INTEGER(11)  NOT NULL AUTO_INCREMENT,
	`streamtype` INTEGER(11) default 0 NOT NULL,
	`hidden` TINYINT(1) default 0 NOT NULL,
	`verify` TINYINT(1) default 0 NOT NULL,
	`status` INTEGER(11) default 0 NOT NULL,
	`uid` INTEGER(11)  NOT NULL,
	`picture_filename` VARCHAR(255),
	`picture_menu_filename` VARCHAR(255),
	`picture_cover_filename` VARCHAR(255),
	`picture_placeholder_filename` VARCHAR(255),
	`font_color_title` VARCHAR(16) default '',
	`font_color_content` VARCHAR(16) default '',
	`stream_rtmp` VARCHAR(255),
	`stream_hls` VARCHAR(255),
	`stream_rtp` VARCHAR(255),
	`stream_hds` VARCHAR(255),
	`stream_silverlight` VARCHAR(255),
	`stream_mp3` VARCHAR(255),
	`stream_aac` VARCHAR(255),
	`weight` INTEGER(11) default 0 NOT NULL,
	`price` FLOAT(8,2) default 0 NOT NULL,
	`with_costs` TINYINT(1) default 0 NOT NULL,
	`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_users` 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,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	PRIMARY KEY (`lsid`),
	KEY `ls_i_uid`(`uid`),
	KEY `ls_i_created_at`(`created_at`),
	KEY `ls_i_status`(`status`),
	KEY `ls_i_status_created`(`status`, `created_at`),
	CONSTRAINT `ls_fk_uid`
		FOREIGN KEY (`uid`)
		REFERENCES `user` (`uid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- livestream_i18n
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `livestream_i18n`;


CREATE TABLE `livestream_i18n`
(
	`lsid` INTEGER(11)  NOT NULL,
	`culture` VARCHAR(7)  NOT NULL,
	`name` VARCHAR(255)  NOT NULL,
	`description` TEXT,
	PRIMARY KEY (`lsid`,`culture`),
	KEY `lsi_i_lsid`(`lsid`),
	KEY `lsi_i_culture`(`culture`),
	CONSTRAINT `lsi_fk_lsid`
		FOREIGN KEY (`lsid`)
		REFERENCES `livestream` (`lsid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- livestream_user
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `livestream_user`;


CREATE TABLE `livestream_user`
(
	`lsid` INTEGER(11)  NOT NULL,
	`uid` INTEGER(11)  NOT NULL,
	`status` INTEGER(11) default 0 NOT NULL,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	PRIMARY KEY (`lsid`,`uid`),
	KEY `lsu_i_lsid`(`lsid`),
	KEY `lsu_i_uid`(`uid`),
	CONSTRAINT `lsu_fk_lsid`
		FOREIGN KEY (`lsid`)
		REFERENCES `livestream` (`lsid`),
	CONSTRAINT `lsu_fk_uid`
		FOREIGN KEY (`uid`)
		REFERENCES `user` (`uid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- livestream_category
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `livestream_category`;


CREATE TABLE `livestream_category`
(
	`lsid` INTEGER(11)  NOT NULL,
	`cid` INTEGER(11)  NOT NULL,
	PRIMARY KEY (`lsid`,`cid`),
	KEY `lsc_i_lsid`(`lsid`),
	KEY `lsc_i_cid`(`cid`),
	CONSTRAINT `lsc_fk_lsid`
		FOREIGN KEY (`lsid`)
		REFERENCES `livestream` (`lsid`),
	CONSTRAINT `lsc_fk_cid`
		FOREIGN KEY (`cid`)
		REFERENCES `category` (`cid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- livestream_view
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `livestream_view`;


CREATE TABLE `livestream_view`
(
	`lsvid` INTEGER(11)  NOT NULL AUTO_INCREMENT,
	`lsid` INTEGER(11)  NOT NULL,
	`uid` INTEGER(11)  NOT NULL,
	`sessid` VARCHAR(128)  NOT NULL,
	`hostname` VARCHAR(244)  NOT NULL,
	`external` TINYINT(1) default 0 NOT NULL,
	`external_url` VARCHAR(1024),
	`created_at` DATETIME  NOT NULL,
	PRIMARY KEY (`lsvid`),
	KEY `lsv_i_lsid`(`lsid`),
	KEY `lsv_i_uid`(`uid`),
	CONSTRAINT `lsv_fk_lsid`
		FOREIGN KEY (`lsid`)
		REFERENCES `livestream` (`lsid`),
	CONSTRAINT `lsv_fk_uid`
		FOREIGN KEY (`uid`)
		REFERENCES `user` (`uid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- livestream_tag
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `livestream_tag`;


CREATE TABLE `livestream_tag`
(
	`lsid` INTEGER(11)  NOT NULL,
	`tid` INTEGER(11)  NOT NULL,
	`pos` INTEGER(3)  NOT NULL,
	PRIMARY KEY (`lsid`,`tid`),
	KEY `lst_i_lsid`(`lsid`),
	KEY `lst_i_tid`(`tid`),
	CONSTRAINT `lst_fk_lsid`
		FOREIGN KEY (`lsid`)
		REFERENCES `livestream` (`lsid`),
	CONSTRAINT `lst_fk_tid`
		FOREIGN KEY (`tid`)
		REFERENCES `tag` (`tid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- livestream_rating
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `livestream_rating`;


CREATE TABLE `livestream_rating`
(
	`lsid` INTEGER(11)  NOT NULL,
	`uid` INTEGER(11)  NOT NULL,
	`hostname` VARCHAR(255)  NOT NULL,
	`rating` INTEGER(3)  NOT NULL,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	PRIMARY KEY (`lsid`,`uid`,`hostname`,`created_at`),
	KEY `lsr_i_lsid`(`lsid`),
	KEY `lsr_i_uid`(`uid`),
	CONSTRAINT `lsr_fk_lsid`
		FOREIGN KEY (`lsid`)
		REFERENCES `livestream` (`lsid`),
	CONSTRAINT `lsr_fk_uid`
		FOREIGN KEY (`uid`)
		REFERENCES `user` (`uid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- livestream_like
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `livestream_like`;


CREATE TABLE `livestream_like`
(
	`lsid` INTEGER(11)  NOT NULL,
	`uid` INTEGER(11)  NOT NULL,
	`hostname` VARCHAR(255)  NOT NULL,
	`like` INTEGER(3)  NOT NULL,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	PRIMARY KEY (`lsid`,`uid`,`hostname`,`created_at`),
	KEY `lsl_i_lsid`(`lsid`),
	KEY `lsl_i_uid`(`uid`),
	CONSTRAINT `lsl_fk_lsid`
		FOREIGN KEY (`lsid`)
		REFERENCES `livestream` (`lsid`),
	CONSTRAINT `lsl_fk_uid`
		FOREIGN KEY (`uid`)
		REFERENCES `user` (`uid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- livestream_comment
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `livestream_comment`;


CREATE TABLE `livestream_comment`
(
	`lscid` INTEGER(11)  NOT NULL AUTO_INCREMENT,
	`lsid` INTEGER(11)  NOT NULL,
	`pid` INTEGER(11) default 0 NOT NULL,
	`uid` INTEGER(11)  NOT NULL,
	`ano_username` VARCHAR(255),
	`text` TEXT  NOT NULL,
	`status` INTEGER(11) default 0 NOT NULL,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	PRIMARY KEY (`lscid`),
	KEY `lsco_i_lsid`(`lsid`),
	KEY `lsco_i_pid`(`pid`),
	KEY `lsco_i_uid`(`uid`),
	CONSTRAINT `lsco_fk_lsid`
		FOREIGN KEY (`lsid`)
		REFERENCES `livestream` (`lsid`),
	CONSTRAINT `lsco_fk_uid`
		FOREIGN KEY (`uid`)
		REFERENCES `user` (`uid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- livestream_flag
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `livestream_flag`;


CREATE TABLE `livestream_flag`
(
	`flagid` INTEGER(11)  NOT NULL AUTO_INCREMENT,
	`lsid` INTEGER(11)  NOT NULL,
	`uid` INTEGER(11)  NOT NULL,
	`reason` TEXT  NOT NULL,
	`status` INTEGER(11) default 0 NOT NULL,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	PRIMARY KEY (`flagid`),
	KEY `lsf_i_lsid`(`lsid`),
	KEY `lsf_i_uid`(`uid`),
	KEY `lsf_i_status`(`status`),
	CONSTRAINT `lsf_fk_lsid`
		FOREIGN KEY (`lsid`)
		REFERENCES `livestream` (`lsid`),
	CONSTRAINT `lsf_fk_uid`
		FOREIGN KEY (`uid`)
		REFERENCES `user` (`uid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- livestream_favorite
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `livestream_favorite`;


CREATE TABLE `livestream_favorite`
(
	`uid` INTEGER(11)  NOT NULL,
	`lsid` INTEGER(11)  NOT NULL,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	PRIMARY KEY (`uid`,`lsid`),
	KEY `lsfav_i_uid_lsid`(`uid`, `lsid`),
	KEY `lsfav_i_uid`(`uid`),
	KEY `lsfav_i_lsid`(`lsid`),
	CONSTRAINT `lsfav_fk_uid`
		FOREIGN KEY (`uid`)
		REFERENCES `user` (`uid`),
	CONSTRAINT `lsfav_fk_lsid`
		FOREIGN KEY (`lsid`)
		REFERENCES `livestream` (`lsid`)
)Engine=InnoDB;

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