set FOREIGN_KEY_CHECKS = 0;
DROP TABlE Campaign;
CREATE TABLE `Campaign` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(50) NOT NULL,
  `type` VARCHAR(50) NULL DEFAULT NULL,
  `code` VARCHAR(50) NOT NULL,
  `defaultCpc` DOUBLE NOT NULL DEFAULT '0',
  `marketplaceAccountId` INT(10) UNSIGNED NULL DEFAULT NULL,
  `marketplaceId` INT(10) UNSIGNED NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `Campaign_code` (`code`),
  INDEX `FK1_campaign_marketplaceAccountId` (`marketplaceAccountId`, `marketplaceId`),
  CONSTRAINT `FK1_campaign_marketplaceAccountId` FOREIGN KEY (`marketplaceAccountId`, `marketplaceId`) REFERENCES `MarketplaceAccount` (`id`, `marketplaceId`) ON UPDATE NO ACTION ON DELETE NO ACTION
)
  COLLATE='utf8_general_ci'
  ENGINE=InnoDB
  AUTO_INCREMENT=27
;

DROP TABlE CampaignVisit;
CREATE TABLE `CampaignVisit` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `campaignId` INT(10) UNSIGNED NOT NULL,
  `timestamp` TIMESTAMP NULL DEFAULT NULL,
  `cost` DOUBLE NULL DEFAULT NULL,
  PRIMARY KEY (`id`, `campaignId`),
  INDEX `timestamp` (`timestamp`),
  INDEX `FK1_campaignVisits_campaign` (`campaignId`),
  CONSTRAINT `FK1_campaignVisits_campaign` FOREIGN KEY (`campaignId`) REFERENCES `Campaign` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
)
  COLLATE='utf8_general_ci'
  ENGINE=InnoDB
  AUTO_INCREMENT=510339
;

DROP TABlE CampaignVisitHasOrder;
CREATE TABLE `CampaignVisitHasOrder` (
  `campaignVisitId` INT(10) UNSIGNED NOT NULL,
  `campaignId` INT(10) UNSIGNED NOT NULL,
  `orderId` INT(10) UNSIGNED NOT NULL,
  PRIMARY KEY (`campaignVisitId`, `campaignId`, `orderId`),
  INDEX `campaignVisitId_campaignId` (`campaignVisitId`, `campaignId`),
  INDEX `orderId` (`orderId`),
  CONSTRAINT `FK1_toVisit` FOREIGN KEY (`campaignVisitId`, `campaignId`) REFERENCES `CampaignVisit` (`id`, `campaignId`) ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT `FK1_visit_to_order` FOREIGN KEY (`orderId`) REFERENCES `Order` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
)
  COLLATE='utf8_general_ci'
  ENGINE=InnoDB
;

DROP TABlE CampaignVisitHasProduct;
CREATE TABLE `CampaignVisitHasProduct` (
  `campaignVisitId` INT(10) UNSIGNED NOT NULL,
  `campaignId` INT(10) UNSIGNED NOT NULL,
  `productId` INT(10) UNSIGNED NOT NULL,
  `productVariantId` INT(10) UNSIGNED NOT NULL,
  PRIMARY KEY (`campaignVisitId`, `campaignId`, `productId`, `productVariantId`),
  INDEX `productId_productVariantId` (`productId`, `productVariantId`),
  INDEX `campaignVisitId_campaignId` (`campaignVisitId`, `campaignId`),
  CONSTRAINT `FK1_toCampaignVisit` FOREIGN KEY (`campaignVisitId`, `campaignId`) REFERENCES `CampaignVisit` (`id`, `campaignId`) ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT `FK2_toProduct` FOREIGN KEY (`productId`, `productVariantId`) REFERENCES `Product` (`id`, `productVariantId`) ON UPDATE NO ACTION ON DELETE NO ACTION
)
  COLLATE='utf8_general_ci'
  ENGINE=InnoDB
;
SET FOREIGN_KEY_CHECKS = 1;