ALTER TABLE `datawarehouse` ADD `erp_ref` VARCHAR(32)  NULL  DEFAULT NULL  AFTER `own_ref`;
ALTER TABLE `datawarehouse` ADD `ext_ref` VARCHAR(32)  NULL  DEFAULT NULL  AFTER `erp_ref`;
ALTER TABLE `datawarehouse` ADD COLUMN `erp_output` TEXT NULL DEFAULT NULL AFTER `edit`;

ALTER TABLE `datawarehouse_history` ADD `erp_ref` VARCHAR(32)  NULL  DEFAULT NULL  AFTER `own_ref`;
ALTER TABLE `datawarehouse_history` ADD `ext_ref` VARCHAR(32)  NULL  DEFAULT NULL  AFTER `erp_ref`;


ALTER TABLE `resource` ADD `erp_ref` VARCHAR(32)  NULL  DEFAULT NULL  AFTER `field_billable`;
ALTER TABLE `fields` ADD `erp_ref` VARCHAR(32)  NULL  DEFAULT NULL  AFTER `position`;
ALTER TABLE `product_field` ADD `erp_ref` VARCHAR(32)  NULL  DEFAULT NULL  AFTER `externalRef`;

ALTER TABLE `datawarehouse_submit` ADD `datawarehouse_id` VARCHAR(1000) NULL AFTER `filter`;
ALTER TABLE `datawarehouse_quote` ADD `datawarehouse_id` VARCHAR(1000) NULL AFTER `file`;

CREATE TABLE `external_apps` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `app` VARCHAR(45) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

INSERT INTO external_apps VALUES (NULL, 'laborders');

CREATE TABLE `external_tokens` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `app` INT NOT NULL,
  `user` INT NOT NULL,
  `token` TEXT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_ext_token_app_idx` (`app` ASC),
  INDEX `fk_ext_token_user_idx` (`user` ASC),
  CONSTRAINT `fk_ext_token_app`
    FOREIGN KEY (`app`)
    REFERENCES `external_apps` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_ext_token_user`
    FOREIGN KEY (`user`)
    REFERENCES `users` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

INSERT INTO notification_target VALUES (NULL, 'users_inactivation', 'A user account is <b>inactivated</b> (Requires EDIT permissions for user accounts)', 5);
UPDATE notification_target SET description = '<b>Real-usage</b> notifications' WHERE label = 'calendar_pgina';

ALTER TABLE `datawarehouse_history` ADD `erp_ref` VARCHAR(32)  NULL  DEFAULT NULL  AFTER `own_ref`;
ALTER TABLE `datawarehouse_history` ADD `ext_ref` VARCHAR(32)  NULL  DEFAULT NULL  AFTER `erp_ref`;

ALTER TABLE `datawarehouse_history` ADD COLUMN `erp_output` TEXT NULL DEFAULT NULL AFTER `edit`;
