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

#-----------------------------------------------------------------------------
#-- api_auth
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `api_auth`;


CREATE TABLE `api_auth`
(
	`apiid` INTEGER(11)  NOT NULL AUTO_INCREMENT,
	`apikey` VARCHAR(255)  NOT NULL,
	`name` VARCHAR(255)  NOT NULL,
	`lifetime` INTEGER(11) default -1 NOT NULL,
	`status` INTEGER(11)  NOT NULL,
	`created_at` DATETIME  NOT NULL,
	PRIMARY KEY (`apiid`),
	KEY `aa_i_username`(`apikey`)
)Engine=InnoDB;

#-----------------------------------------------------------------------------
#-- api_token
#-----------------------------------------------------------------------------

DROP TABLE IF EXISTS `api_token`;


CREATE TABLE `api_token`
(
	`token` VARCHAR(255)  NOT NULL,
	`uid` INTEGER(11)  NOT NULL,
	`sessid` VARCHAR(128)  NOT NULL,
	`created_at` DATETIME  NOT NULL,
	`expired_at` DATETIME,
	PRIMARY KEY (`token`,`uid`,`sessid`),
	KEY `at_i_token`(`token`),
	KEY `at_i_uid`(`uid`),
	KEY `at_i_sessid`(`sessid`),
	CONSTRAINT `at_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;
