-- Lobster workflow task template copy: master template vs task-bound copy

ALTER TABLE company_workflow_lobster
    ADD COLUMN template_kind VARCHAR(20) NOT NULL DEFAULT 'MASTER' COMMENT 'MASTER=library template, TASK_COPY=task binding copy' AFTER auto_start_instance_on_tag,
    ADD COLUMN source_template_id BIGINT NULL COMMENT 'master template id when template_kind=TASK_COPY' AFTER template_kind;

UPDATE company_workflow_lobster SET template_kind = 'MASTER' WHERE template_kind IS NULL OR template_kind = '';

ALTER TABLE company_tag_template_binding
    ADD COLUMN source_template_id BIGINT NULL COMMENT 'master template id selected when creating binding' AFTER template_id,
    ADD COLUMN task_template_id BIGINT NULL COMMENT 'copied workflow template used by this binding task' AFTER source_template_id;

UPDATE company_tag_template_binding
SET source_template_id = template_id
WHERE source_template_id IS NULL AND template_id IS NOT NULL;

CREATE INDEX idx_lobster_template_kind ON company_workflow_lobster (company_id, template_kind, del_flag);
CREATE INDEX idx_tag_binding_task_template ON company_tag_template_binding (company_id, task_template_id, del_flag);
