-- +migrate Up
CREATE TABLE IF NOT EXISTS `workflows` (
	`id` BIGINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY COMMENT 'workflowの内部ID',
	`account_id` BIGINT UNSIGNED NOT NULL COMMENT 'アカウントID',
	`author_id` BIGINT UNSIGNED NOT NULL COMMENT 'authorユーザーの内部ID',	
	`worktype_id`BIGINT UNSIGNED COMMENT 'Worktypeの内部ID',
	`template_id` BIGINT UNSIGNED COMMENT 'テンプレートファイルの内部ID',
	`created_by` VARCHAR(255) COMMENT '作成者',
	`created_at` TIMESTAMP DEFAULT now() COMMENT '作成時刻',
	`updated_by` VARCHAR(255) COMMENT '更新者',
	`updated_at` TIMESTAMP DEFAULT now() on UPDATE now() COMMENT '更新時刻',
	UNIQUE worktype_id_index (account_id, author_id, worktype_id),
	CONSTRAINT `workflows_fk_account_id` FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE, 
	CONSTRAINT `workflows_fk_author_id` FOREIGN KEY (author_id) REFERENCES users(id),
	CONSTRAINT `workflows_fk_worktype_id` FOREIGN KEY (worktype_id) REFERENCES worktypes(id),
	CONSTRAINT `workflows_fk_template_id` FOREIGN KEY (template_id) REFERENCES template_files(id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci;

CREATE TABLE IF NOT EXISTS `workflow_typists` (
	`id` BIGINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY COMMENT 'worktypeの内部ID',
	`workflow_id` BIGINT UNSIGNED NOT NULL COMMENT 'workflowの内部ID',
	`typist_id` BIGINT UNSIGNED COMMENT 'タイピストユーザーの内部ID',
	`typist_group_id` BIGINT UNSIGNED COMMENT 'タイピストグループの内部ID',
	`created_by` VARCHAR(255) COMMENT '作成者',
	`created_at` TIMESTAMP DEFAULT now() COMMENT '作成時刻',
	`updated_by` VARCHAR(255) COMMENT '更新者',
	`updated_at` TIMESTAMP DEFAULT now() on UPDATE now() COMMENT '更新時刻',
	CONSTRAINT `workflow_typists_fk_workflow_id` FOREIGN KEY (workflow_id) REFERENCES workflows(id) ON DELETE CASCADE,
	CONSTRAINT `workflow_typists_fk_typist_id` FOREIGN KEY (typist_id) REFERENCES users(id),
	CONSTRAINT `workflow_typists_fk_typist_group_id` FOREIGN KEY (typist_group_id) REFERENCES user_group(id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci;



-- +migrate Down
ALTER TABLE `workflows` DROP FOREIGN KEY `workflows_fk_account_id`;
ALTER TABLE `workflows` DROP FOREIGN KEY `workflows_fk_author_id`;
ALTER TABLE `workflows` DROP FOREIGN KEY `workflows_fk_worktype_id`;
ALTER TABLE `workflows` DROP FOREIGN KEY `workflows_fk_template_id`;
ALTER TABLE `workflow_typists` DROP FOREIGN KEY `workflow_typists_fk_workflow_id`;
ALTER TABLE `workflow_typists` DROP FOREIGN KEY `workflow_typists_fk_typist_id`;
ALTER TABLE `workflow_typists` DROP FOREIGN KEY `workflow_typists_fk_typist_group_id`;
DROP TABLE IF EXISTS `workflows`;
DROP TABLE IF EXISTS `workflow_typists`;