/* This file was generated by ODB, object-relational mapping (ORM)
 * compiler for C++.
 */

CREATE TABLE "package_build_auxiliaries" (
  "tenant" TEXT NOT NULL,
  "name" CITEXT NOT NULL,
  "version_epoch" INTEGER NOT NULL,
  "version_canonical_upstream" TEXT NOT NULL,
  "version_canonical_release" TEXT NOT NULL COLLATE "C",
  "version_revision" INTEGER NOT NULL,
  "index" BIGINT NOT NULL,
  "environment_name" TEXT NOT NULL,
  "config" TEXT NOT NULL,
  "comment" TEXT NOT NULL);

CREATE INDEX "package_build_auxiliaries_object_id_i"
  ON "package_build_auxiliaries" (
    "tenant",
    "name",
    "version_epoch",
    "version_canonical_upstream",
    "version_canonical_release",
    "version_revision");

CREATE INDEX "package_build_auxiliaries_index_i"
  ON "package_build_auxiliaries" ("index");

CREATE TABLE "package_build_config_auxiliaries" (
  "tenant" TEXT NOT NULL,
  "name" CITEXT NOT NULL,
  "version_epoch" INTEGER NOT NULL,
  "version_canonical_upstream" TEXT NOT NULL,
  "version_canonical_release" TEXT NOT NULL COLLATE "C",
  "version_revision" INTEGER NOT NULL,
  "config_index" BIGINT NOT NULL,
  "index" BIGINT NOT NULL,
  "environment_name" TEXT NOT NULL,
  "config" TEXT NOT NULL,
  "comment" TEXT NOT NULL);

CREATE INDEX "package_build_config_auxiliaries_object_id_i"
  ON "package_build_config_auxiliaries" (
    "tenant",
    "name",
    "version_epoch",
    "version_canonical_upstream",
    "version_canonical_release",
    "version_revision");

ALTER TABLE "package_build_auxiliaries"
  ADD CONSTRAINT "tenant_fk"
    FOREIGN KEY ("tenant")
    REFERENCES "tenant" ("id")
    INITIALLY DEFERRED,
  ADD CONSTRAINT "object_id_fk"
    FOREIGN KEY ("tenant",
                 "name",
                 "version_epoch",
                 "version_canonical_upstream",
                 "version_canonical_release",
                 "version_revision")
    REFERENCES "package" ("tenant",
                          "name",
                          "version_epoch",
                          "version_canonical_upstream",
                          "version_canonical_release",
                          "version_revision")
    ON DELETE CASCADE;

ALTER TABLE "package_build_config_auxiliaries"
  ADD CONSTRAINT "tenant_fk"
    FOREIGN KEY ("tenant")
    REFERENCES "tenant" ("id")
    INITIALLY DEFERRED,
  ADD CONSTRAINT "object_id_fk"
    FOREIGN KEY ("tenant",
                 "name",
                 "version_epoch",
                 "version_canonical_upstream",
                 "version_canonical_release",
                 "version_revision")
    REFERENCES "package" ("tenant",
                          "name",
                          "version_epoch",
                          "version_canonical_upstream",
                          "version_canonical_release",
                          "version_revision")
    ON DELETE CASCADE;

UPDATE "schema_version"
  SET "version" = 31, "migration" = TRUE
  WHERE "name" = 'package';

