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

#-----------------------------------------------------------------------------
#-- album
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `album`;


CREATE TABLE `album`
(
	`aid` INTEGER(11)  NOT NULL AUTO_INCREMENT,
	`albumtype` INTEGER(11)  NOT NULL,
	`objectid` INTEGER(11)  NOT NULL,
	`visibility` INTEGER(11) default 0 NOT NULL,
	`status` INTEGER(11) default 0 NOT NULL,
	`title` VARCHAR(255)  NOT NULL,
	`description` TEXT,
	`count_media` INTEGER(11) default 0 NOT NULL,
	`count_media_videos` INTEGER(11) default 0 NOT NULL,
	`count_media_photos` INTEGER(11) default 0 NOT NULL,
	`count_media_audios` INTEGER(11) default 0 NOT NULL,
	`count_media_documents` INTEGER(11) default 0 NOT NULL,
	`count_media_youtube` INTEGER(11) default 0 NOT NULL,
	`count_media_vimeo` INTEGER(11) default 0 NOT NULL,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	PRIMARY KEY (`aid`),
	KEY `a_i_album`(`albumtype`, `objectid`),
	KEY `a_i_album_visibility`(`albumtype`, `objectid`, `visibility`),
	KEY `a_i_albumtype`(`albumtype`),
	KEY `a_i_objectid`(`objectid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- album_media
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `album_media`;


CREATE TABLE `album_media`
(
	`aid` INTEGER(11)  NOT NULL,
	`mid` INTEGER(11)  NOT NULL,
	`weight` INTEGER(11) default 0 NOT NULL,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	PRIMARY KEY (`aid`,`mid`),
	KEY `am_i_aid`(`aid`),
	KEY `am_i_mid`(`mid`),
	CONSTRAINT `am_fk_aid`
		FOREIGN KEY (`aid`)
		REFERENCES `album` (`aid`),
	CONSTRAINT `am_fk_mid`
		FOREIGN KEY (`mid`)
		REFERENCES `media` (`mid`)
)Engine=InnoDB;

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