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

#-----------------------------------------------------------------------------
#-- newsletter
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `newsletter`;


CREATE TABLE `newsletter`
(
	`nlid` INTEGER(11)  NOT NULL AUTO_INCREMENT,
	`status` INTEGER(11) default 0 NOT NULL,
	`name` VARCHAR(255)  NOT NULL,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	PRIMARY KEY (`nlid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- newsletter_issue
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `newsletter_issue`;


CREATE TABLE `newsletter_issue`
(
	`nliid` INTEGER(11)  NOT NULL AUTO_INCREMENT,
	`nlid` INTEGER(11)  NOT NULL,
	`status` INTEGER(11) default 0 NOT NULL,
	`subject` VARCHAR(255)  NOT NULL,
	`message_html` TEXT  NOT NULL,
	`message_text` TEXT  NOT NULL,
	`from_email` VARCHAR(255)  NOT NULL,
	`from_name` VARCHAR(255)  NOT NULL,
	`reply_email` VARCHAR(255)  NOT NULL,
	`reply_name` VARCHAR(255)  NOT NULL,
	`test_users` TEXT  NOT NULL,
	`last_receiver_id` INTEGER(11) default 0 NOT NULL,
	`count_send` INTEGER(11) default 0 NOT NULL,
	`execute_at` DATETIME  NOT NULL,
	`started_at` DATETIME,
	`finished_at` DATETIME,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	PRIMARY KEY (`nliid`),
	INDEX `FI__fk_nlid` (`nlid`),
	CONSTRAINT `ncn_fk_nlid`
		FOREIGN KEY (`nlid`)
		REFERENCES `newsletter` (`nlid`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- newsletter_subscription
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `newsletter_subscription`;


CREATE TABLE `newsletter_subscription`
(
	`nlid` INTEGER(11)  NOT NULL,
	`uid` INTEGER(11)  NOT NULL,
	`created_at` DATETIME  NOT NULL,
	`updated_at` DATETIME  NOT NULL,
	PRIMARY KEY (`nlid`,`uid`),
	CONSTRAINT `un_fk_nlid`
		FOREIGN KEY (`nlid`)
		REFERENCES `newsletter` (`nlid`),
	INDEX `FI_fk_uid` (`uid`),
	CONSTRAINT `un_fk_uid`
		FOREIGN KEY (`uid`)
		REFERENCES `user` (`uid`)
)Engine=InnoDB;

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