ALTER TABLE `ProductSku`
  ADD COLUMN `ean` BIGINT(13) UNSIGNED NULL AFTER `barcode`;

ALTER TABLE `ProductSku`
  ADD INDEX `productSku_barcode` (`barcode`),
  ADD INDEX `productSku_ean` (`ean`),
  ADD INDEX `productSku_productId_productVariantId` (`productId`, `productVariantId`);

UPDATE ProductSku set barcode = 0;
ALTER TABLE `ProductSku`
  ALTER `barcode` DROP DEFAULT;
ALTER TABLE `ProductSku`
  CHANGE COLUMN `barcode` `barcode` INT(10) UNSIGNED NULL AFTER `currencyId`;

DROP TRIGGER IF EXISTS ProductSku_before_insert;
CREATE TRIGGER `ProductSku_before_insert` BEFORE INSERT ON `ProductSku` FOR EACH ROW BEGIN
  if(nullif(new.barcode, '') is null)
    then
      select concat('12', LPAD((max(barcode)-1200000000) + 1,8,'0')) INTO @v from ProductSku;
      set new.barcode = @v;
  END IF;
END;

CREATE TABLE `EanBucket` (
  `ean` INT(13) UNSIGNED NOT NULL,
  `isAssigned` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
  PRIMARY KEY (`ean`)
)
  COMMENT='Contains all disposable ean code'
  COLLATE='utf8_general_ci'
  ENGINE=InnoDB
;
