SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,ALLOW_INVALID_DATES';


drop function if exists image_CreateByUser;
drop function if exists image_DeleteByUser;
drop function if exists image_UpdateByUser;

alter table tagT drop image;
alter table userT drop image;

alter table nodeTypeT add forceSinglePhoto ENUM('y') NULL after code;
alter table nodeT add forceSinglePhoto ENUM('y') NULL after forceSingleNode;
alter table nodeT add INDEX id_forcesinglephoto_idx (id ASC, forceSinglePhoto ASC);


update nodeTypeT set
forceSinglePhoto = if(internalTypeId = 'object', null, 'y');

update nodeT n join nodeTypeT nt on n.nodeTypeId = nt.id set
n.forceSinglePhoto = if(nt.internalTypeId = 'object', null, 'y');


DROP TABLE IF EXISTS imageT ;

CREATE TABLE IF NOT EXISTS imageT (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  nodeId INT UNSIGNED NOT NULL,
  forceSinglePhoto ENUM('y') NULL,
  name VARCHAR(255) NOT NULL,
  filenameOriginal VARCHAR(255) NOT NULL,
  filenameFs VARCHAR(255) NOT NULL,
  display ENUM('n','y') NOT NULL DEFAULT 'y',
  sorter INT NOT NULL,
  added TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
  changed TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  INDEX objectIdSorter_idx (nodeId ASC, display ASC, sorter ASC),
  UNIQUE INDEX nodeid_forcesinglephoto_uq (nodeId ASC, forceSinglePhoto ASC),
  INDEX sorter_idx (nodeId ASC, sorter ASC),
  CONSTRAINT fk_imageT_nodeT1
    FOREIGN KEY (nodeId , forceSinglePhoto)
    REFERENCES nodeT (id , forceSinglePhoto)
    ON DELETE CASCADE
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

