-- Generated by CompileSchemaScripts.pl from:
-- 20180503-mbs-9708-drop-area-containment-view.sql
-- 20190317-mbs-9941-mbs-10062.sql
-- 20190422-mbs-7480.sql
-- 20190422-mbs-9973-collection-added-date.sql
-- 20190422-mbs-9428-collection-collaborators.sql
-- 20190422-mbs-5818-collection-position.sql
-- 20190422-mbs-1658-collection-text.sql
-- 20190423-mbs-10052-eaa.sql
-- 20190423-mbs-5387.sql
\set ON_ERROR_STOP 1
BEGIN;
SET search_path = musicbrainz, public;
SET LOCAL statement_timeout = 0;
--------------------------------------------------------------------------------
SELECT '20180503-mbs-9708-drop-area-containment-view.sql';

DROP VIEW IF EXISTS area_containment;

--------------------------------------------------------------------------------
SELECT '20190317-mbs-9941-mbs-10062.sql';

CREATE TABLE genre ( -- replicate (verbose)
    id                  SERIAL, -- PK
    gid                 UUID NOT NULL,
    name                VARCHAR NOT NULL,
    comment             VARCHAR(255) NOT NULL DEFAULT '',
    edits_pending       INTEGER NOT NULL DEFAULT 0 CHECK (edits_pending >=0),
    last_updated        TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

CREATE TABLE genre_alias ( -- replicate (verbose)
    id                  SERIAL,
    genre               INTEGER NOT NULL, -- references genre.id
    name                VARCHAR NOT NULL,
    locale              TEXT,
    edits_pending       INTEGER NOT NULL DEFAULT 0 CHECK (edits_pending >= 0),
    last_updated        TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    primary_for_locale  BOOLEAN NOT NULL DEFAULT FALSE,
    CONSTRAINT primary_check CHECK ((locale IS NULL AND primary_for_locale IS FALSE) OR (locale IS NOT NULL))
);

ALTER TABLE genre ADD CONSTRAINT genre_pkey PRIMARY KEY (id);

ALTER TABLE genre_alias ADD CONSTRAINT genre_alias_pkey PRIMARY KEY (id);

CREATE UNIQUE INDEX genre_idx_gid ON genre (gid);
CREATE UNIQUE INDEX genre_idx_name ON genre (LOWER(name));

CREATE INDEX genre_alias_idx_genre ON genre_alias (genre);
CREATE UNIQUE INDEX genre_alias_idx_primary ON genre_alias (genre, locale) WHERE primary_for_locale = TRUE AND locale IS NOT NULL;

INSERT INTO genre (gid, name)
VALUES  ('54c01942-22fd-4184-9877-1db0089b18f1', 'acid house'),
        ('7dc2b20f-3953-4874-b9bf-41b8ba06d20c', 'acid jazz'),
        ('ba64013e-27bb-4f14-a530-8d25b296e0da', 'acid techno'),
        ('37f85b9c-c3fc-4b5a-8545-51aeb78c8786', 'acoustic blues'),
        ('00055e8b-b951-46e2-af1e-58b5624e7952', 'acoustic rock'),
        ('fcc58a18-9326-4c92-8b29-c294d44379c3', 'afrobeat'),
        ('5f9cba3d-1a9f-46cd-8c49-7ed78d1f3354', 'alternative country'),
        ('8301f73c-9166-4108-bfeb-4fd22dc19083', 'alternative dance'),
        ('0b48a36c-630f-4ee7-8cf3-480e3dd8be65', 'alternative folk'),
        ('924943cd-73c8-45c0-96eb-74f2a15e5d6e', 'alternative hip hop'),
        ('7983ff25-ddf9-411e-a7f9-6cca238bff79', 'alternative metal'),
        ('d3ebe947-7c03-43f5-b333-09379881b0f6', 'alternative pop'),
        ('e096efc6-6c9c-4240-b627-d7bbb5e8fa2a', 'alternative punk'),
        ('ceeaa283-5d7b-4202-8d1d-e25d116b2a18', 'alternative rock'),
        ('608b0471-7531-4854-a348-e698c69cb699', 'ambient'),
        ('cc2327cf-88a3-476b-950c-296c382eabd9', 'ambient house'),
        ('1c6e4eae-915d-4662-a7ff-038920b67d9e', 'ambient techno'),
        ('2c765e49-a12e-45eb-b628-f8b9cc0a0250', 'americana'),
        ('e12fe025-4c13-485e-a2fe-ad5bf2cd75eb', 'anarcho-punk'),
        ('aca1caab-09d1-44b5-ae84-590ff1d218fc', 'aor'),
        ('26af3760-750a-4b5e-a699-78c87697585a', 'arena rock'),
        ('b7ef058e-6d83-4ca4-8123-9724bff4648b', 'art rock'),
        ('d14a4c4a-bea5-4b08-b945-59b048acd2d5', 'atmospheric black metal'),
        ('966d3d2d-15bd-4f6f-a830-15ab2c5e662f', 'audiobook'),
        ('b1c0c2ad-e5ae-4bb6-a1be-d66e195fd2d7', 'avant-garde'),
        ('8d901306-d4a3-436c-a1c3-75550e5b6eaf', 'avant-garde jazz'),
        ('77c0160b-8090-4493-b229-890addfee1df', 'avant-garde metal'),
        ('0e7a3c6a-4fbc-4b66-8201-9196032768ac', 'avant-garde pop'),
        ('b413acf5-112a-4d44-a61f-9abe28f0dfac', 'bachata'),
        ('17b58d38-1110-4f5a-9412-b0314329622b', 'ballad'),
        ('f8f6157d-5ace-4844-90c2-2e2a829b8932', 'barbershop'),
        ('9e44c2c9-8c44-4aa6-9de3-0f3e75b49f1b', 'baroque'),
        ('ac30756b-55a7-4dc3-8908-8f94c93318d0', 'bebop'),
        ('59a849e1-73fe-428d-8a61-3788875f9644', 'bhangra'),
        ('e7bb9a21-9556-4b85-bc78-5f69a3d0577d', 'big band'),
        ('aac07ae0-8acf-4249-b5c0-2762b53947a2', 'big beat'),
        ('a687ea57-487a-440d-b5fd-2f1b2f8b1f57', 'blackgaze'),
        ('12568698-97cc-49a9-80ab-9c5e3fe4dd33', 'black metal'),
        ('f976d48b-965e-4fe9-aaa6-105aab988c7c', 'blackened death metal'),
        ('650bda08-20ca-4012-b873-3b68c5768bdf', 'blue-eyed soul'),
        ('a1a7b40d-f6e6-48c1-a5fc-b15af73d7381', 'bluegrass'),
        ('fe12b346-a10e-450f-bf81-fa20894b5ea2', 'blues'),
        ('58e325d5-54fd-4e98-b39a-3aa6bc319273', 'blues rock'),
        ('49fa872d-561c-4f8a-aea4-fbe7f67e1bd6', 'bolero'),
        ('ae4e05d3-6ae6-41b2-9b63-7c9e27784e27', 'bolero son'),
        ('d8048b43-9edb-4e1a-aa88-bb91fa847d4a', 'boom bap'),
        ('573a284f-2ae3-4636-af36-10fc1078d295', 'bossa nova'),
        ('3b8f3f65-6caa-47c8-b076-fa59dfb69c43', 'breakbeat'),
        ('8aad8a03-be38-48e4-aa43-5ca0f826c569', 'breakcore'),
        ('63eddc75-dbad-4a05-a365-e30d1e87bca8', 'breaks'),
        ('f1c9b874-7ef2-4e79-9419-c0bcb5f33a30', 'britpop'),
        ('cf9daa9d-74e2-4eff-b0eb-7269bbca6501', 'broken beat'),
        ('12d11d01-cecc-4f9b-8d3a-99e4590952fe', 'brutal death metal'),
        ('6fd83b83-9b63-47fa-a208-5f9e762f40ab', 'bubblegum pop'),
        ('0af301ff-3e33-4f5f-934f-5916cfc04157', 'cajun'),
        ('b44a8ffd-e717-4d73-99ea-9fe5d245339d', 'calypso'),
        ('78caea3b-84fd-4664-878a-fe70a6e1d9d1', 'cantopop'),
        ('9c2f8b08-9bac-4bfd-b9ed-6381c65791e9', 'canterbury scene'),
        ('34edc6ac-d431-4b36-be15-ecd17533e5e2', 'celtic'),
        ('36c0f210-b6c1-4481-bc7c-33de031a4046', 'celtic punk'),
        ('f0843e91-c025-4dd7-8aef-6fe3d35470d9', 'chamber pop'),
        ('55d05e59-4829-4e98-8cb7-5d769cafb576', 'champeta'),
        ('b7864789-29e6-4965-84e4-463baaa869df', 'chanson'),
        ('f1e26a78-3e85-4365-a34d-dc4b5389b29d', 'chicago blues'),
        ('7232e5df-8466-421f-ad5a-ce2048e5522d', 'chillout'),
        ('103a65bb-35d9-4d27-9c9b-f35a458621ba', 'chiptune'),
        ('c02cd2c5-9316-4046-a241-f74b773fee06', 'christian rock'),
        ('31aee5bc-c1e8-43d7-a7bc-902df802ccbb', 'christmas music'),
        ('5fef47af-772b-48e2-8e49-855f7d999e81', 'city pop'),
        ('15f9b448-6f2d-45a4-b11b-1fcdf45be203', 'classic blues'),
        ('9cc9ea3b-e189-4def-9e47-86e9dc643eee', 'classic country'),
        ('f0e90b41-4499-493f-b6e3-05d3c3ea1b20', 'classic jazz'),
        ('93244085-20e5-4f16-9067-1d19143b3810', 'classic rock'),
        ('bf9955d6-c3eb-466d-95ca-30f3848fc9ea', 'classical'),
        ('bab69b07-8bb9-4415-b666-c37609cef80b', 'club'),
        ('119ecf69-7d87-4f63-bc83-2959a0111a55', 'comedy'),
        ('c0e73bb6-37a4-485e-aaea-11c63b40c9f3', 'conscious hip hop'),
        ('2b4f0036-79a0-4ce0-a38c-4c0c8e61c579', 'contemporary christian'),
        ('a190bb32-e0a7-4c56-b142-5cf9cfcfa713', 'contemporary classical'),
        ('c11e4253-a888-4a2a-adde-c274b748d0b3', 'contemporary folk'),
        ('6aeacd9a-3964-4ca4-9e3c-23ec8bd8848d', 'contemporary gospel'),
        ('e2b06d21-13df-4a48-aa81-6ddd99344dc7', 'contemporary jazz'),
        ('4bb4043a-0ee5-4b84-93fa-6ba4567a6ba0', 'contemporary r&b'),
        ('2f7f569a-9348-4384-8508-2d0ac7d6a778', 'contra'),
        ('5fc86669-d9ba-4716-8090-eff39081090e', 'cool jazz'),
        ('5f665615-7fb3-49d8-b541-62a7b239edbe', 'country'),
        ('1a46845c-ed4d-48c5-a93f-45bb85cf56d1', 'country blues'),
        ('984e6cb2-823e-4709-a23b-233f7f2ee3aa', 'country folk'),
        ('2de61e9c-de1d-47c8-999d-5b44c0cfeca1', 'country pop'),
        ('fb07b1e3-411d-4146-a0d6-3479c0e7557b', 'country rock'),
        ('eee90717-3eee-41bd-b463-003c180544bd', 'crossover prog'),
        ('f66d7266-eb3d-4ef3-b4d8-b7cd992f918b', 'crust punk'),
        ('7cd4173c-fef8-4e99-a527-98a25b6487c0', 'cumbia'),
        ('ad41288f-0803-4e2e-859f-148e7f7b2eae', 'd-beat'),
        ('e5bba957-8c91-496a-a675-c6d0c6b51c33', 'dance'),
        ('b739a895-85ed-4ad3-8717-4e9ef5387dd8', 'dance-pop'),
        ('509c006f-67c2-4c54-a568-d2b415cb0642', 'dance-punk'),
        ('6c3503e3-bae3-42de-9e4c-7861f2053fbe', 'dancehall'),
        ('01ecf8e4-7f7a-4145-ae97-504d3109633d', 'dark ambient'),
        ('24db3574-7c54-4d9b-b238-8d17eb7e8d63', 'dark electro'),
        ('16103855-c0f1-4221-81d8-eafc53b1b41c', 'dark folk'),
        ('c4a72fcd-e291-43c8-a546-9ce1764ff31b', 'dark wave'),
        ('eacfa027-2fad-413f-a2f1-80fa43674f0b', 'death metal'),
        ('2bd1ff1a-777d-4c5c-a1cb-92792adcf10c', 'death-doom metal'),
        ('d39cbc30-7dc6-4194-8f99-6d35dd602451', 'deathcore'),
        ('619c5945-6484-4a68-96e9-a48d4a4e1f73', 'deathgrind'),
        ('d5e21e4f-f6fe-464d-b269-14cf670a6949', 'deathrock'),
        ('7fa479e7-3d35-40d1-96f7-b619a5ca9d9f', 'deep house'),
        ('b0702eb4-2951-4e1f-bbcd-dd9b0ec63c7c', 'delta blues'),
        ('bc1b8c46-5a69-4262-b909-c376f5de491a', 'desert rock'),
        ('6d12e828-40a5-4ebc-a5b0-aa5506e2efd0', 'digital hardcore'),
        ('a04d680b-b5c5-4f6a-87a3-f9f1ff98e3ea', 'disco'),
        ('1696f12e-9f01-4309-be9d-116e31ac65b7', 'doo-wop'),
        ('db815815-60fe-44a3-9859-2232df479dc9', 'doom metal'),
        ('cc38aba3-48ed-439a-83b9-f81a34a66598', 'downtempo'),
        ('1e5e18e5-4e70-4a73-942a-c6c79c1aefe2', 'drill'),
        ('75c5866f-0212-4c02-9650-de475dda157e', 'drone'),
        ('462f9321-6103-49c9-b6db-96219bce6f62', 'drum and bass'),
        ('c72a5d45-75a8-4b35-9f48-67e49eb4b5e5', 'dub'),
        ('16aab171-3dce-454e-bb96-2636f15813fc', 'dub techno'),
        ('1b50083b-1afa-4778-82c8-548b309af783', 'dubstep'),
        ('c89a76aa-2149-499e-946b-8299aa2f4ab3', 'dungeon synth'),
        ('1bafab01-c092-47bc-84ee-a6b503b3cb86', 'east coast hip hop'),
        ('f3818817-6235-48d5-82f7-b223eb0261d9', 'ebm'),
        ('8164d906-055d-4ca8-9ab2-14204b301cc3', 'electric blues'),
        ('60f00d05-df4d-496e-8f5a-c45c03a56ad4', 'electro'),
        ('05247982-e44f-4c58-96f8-a5453be69f5f', 'electro house'),
        ('9b1af4d0-2196-43d4-bbca-845a2e02d962', 'electro swing'),
        ('4f476710-cd29-400f-98fe-7782e1f00e06', 'electro-funk'),
        ('6e2e809f-8c54-4e0f-aca0-0642771ab3cf', 'electro-industrial'),
        ('cae57e98-c939-4b0e-ae49-8e0cb1bdc91d', 'electroclash'),
        ('89255676-1f14-4dd8-bbad-fca839d6aff4', 'electronic'),
        ('c4a69842-f891-4569-9506-1882aa5db433', 'electronic rock'),
        ('53a3cea3-17af-4421-a07a-5824b540aeb5', 'electronica'),
        ('f9720c77-3574-4fe8-823b-c290b117222e', 'electronicore'),
        ('3f3388b4-e36a-4bf8-a0be-66b10d92aaa4', 'electropop'),
        ('c187da0e-48a0-4de5-b759-4ca2a1503008', 'electropunk'),
        ('ef0c2769-9c8f-49fc-b66e-27b8242b004a', 'emo'),
        ('1d6489a9-5837-43c9-8155-94a126e8a951', 'emocore'),
        ('b73d4a65-091e-4f08-8422-93f1b90f95d7', 'enka'),
        ('845d2209-0b82-4a87-9405-453fc2e3086c', 'ethereal'),
        ('8f8cbcde-3948-4be5-8bf0-8073f13e4b60', 'euro house'),
        ('e9a67960-9b49-4967-a1c9-c26360e9c612', 'eurodance'),
        ('50e3ff0e-1abb-4577-9bf3-95d5c539ce2d', 'europop'),
        ('68c81274-5770-4e7b-a4bf-ab0d7d425d99', 'experimental'),
        ('65c97e89-b42b-45c2-a70e-0eca1b8f0ff7', 'experimental rock'),
        ('2fb0a213-28f9-4e67-bb02-3df6d396d2de', 'fado'),
        ('18a0d235-c8e0-4575-b0dc-e3bb0cc2ad2c', 'filk'),
        ('961c6dad-8cf0-47ab-bc09-ccd011f9ef28', 'flamenco'),
        ('a91eed3a-fdd0-494e-ade0-95ea4117f6eb', 'folk'),
        ('f5b0242a-ede0-4929-9361-4eddeb0516b5', 'folk metal'),
        ('6cec5b4d-3c64-44a6-914d-3e833432f631', 'folk punk'),
        ('445dc48f-5233-4b4f-987f-867ff9ade836', 'folk rock'),
        ('abb2374f-30e5-4d3e-b417-2caf78ef1f6c', 'folk pop'),
        ('8103927c-5f4b-4bbc-9d8c-7a1241d2bab8', 'freak folk'),
        ('6a1ba2f9-1e5b-446f-9ffa-c0b0963aa08f', 'free improvisation'),
        ('3c31635b-a5a7-44eb-ab9d-4bbde11da2d4', 'free jazz'),
        ('fe4ba6a1-9873-4fd0-a12b-a70c81818514', 'funk'),
        ('dfdf3e46-3b2b-417e-9346-86f9a5bac360', 'funk carioca'),
        ('0caaa092-27c4-413e-841c-7c4deeec57a7', 'funk metal'),
        ('eca476ac-d63b-431f-9347-c45f530dc15d', 'funk rock'),
        ('1fbd6e82-0d33-4d78-a9b1-7d145a8eefa6', 'funk soul'),
        ('62e4c7cb-a2b3-462b-8d96-1b0d66ee6cf3', 'funky house'),
        ('7af339eb-e666-4aa6-a6ca-8444ca573fe0', 'fusion'),
        ('cdc54f47-79fc-42e2-80d8-8ef58bd21122', 'future jazz'),
        ('6df6e38b-2a6f-4990-94dc-ea48eba4f8ad', 'futurepop'),
        ('b37aef4d-0029-4fc7-b634-6338197335a8', 'g-funk'),
        ('2f2118d7-315b-4241-bf40-d5d6ca11348d', 'gabber'),
        ('bc8c2f79-dcea-43f3-962b-f0663868d42c', 'gangsta rap'),
        ('60c9c6a6-1de7-4010-9f5b-7692d3231d22', 'garage'),
        ('092f40b5-9157-41f2-8243-68b704cd0ed1', 'garage house'),
        ('fd5bea65-15ef-44c4-83fb-ba15013b3596', 'garage punk'),
        ('b7bb11c3-bb83-4047-af65-1e8aba89039f', 'garage rock'),
        ('bc08993e-b188-43e8-b358-b84f2db6bfe6', 'glam'),
        ('818134e3-9392-4624-a13c-c4b19c54bccd', 'glam metal'),
        ('54d89e62-5bfb-42bc-a321-9230e6fdcd75', 'glam rock'),
        ('18b010d7-7d85-4445-a4a8-1889a4688308', 'glitch'),
        ('ade8f293-c049-438a-a5f0-b51e6776d9c5', 'goa trance'),
        ('ca315be3-733b-4630-92d9-cd8ecafe0419', 'goregrind'),
        ('08289b42-7e41-4987-9263-c28bb4c59da0', 'gospel'),
        ('d9f42dc1-3a92-4217-8ae5-e9672f595b8e', 'gothic'),
        ('c73392a9-adaf-4ee4-b911-97a5892ac917', 'gothic metal'),
        ('d9858aef-25bf-478b-b135-b900aa1f0fd1', 'gothic rock'),
        ('b9c9a69d-5fc2-4317-b25d-5a96c8c9478a', 'grebo'),
        ('51cfaac4-6696-480b-8f1b-27cfc789109c', 'grime'),
        ('093149ea-d1bc-4f93-bd5a-8fdcd055ea2f', 'grindcore'),
        ('1a85800e-945e-4a9d-824e-dbd21ca15757', 'groove metal'),
        ('3f08faa9-6c3e-4b6d-b4b8-08eea45954e2', 'grunge'),
        ('3c9097f9-10d9-4086-98e3-e8c54ef470b3', 'guaracha'),
        ('bd477a3e-9235-4597-9ce5-3caa994baef7', 'happy hardcore'),
        ('8c083468-2dce-4a31-ae77-433af3deafd3', 'hard bop'),
        ('9c5052e0-36ff-4b92-9447-58f44ffdf682', 'hard house'),
        ('51cb9f91-e6a2-41bf-891f-e78e3f1e52ab', 'hard rock'),
        ('c8acb78a-8337-4eae-9d19-48220dba03ac', 'hard trance'),
        ('3308d0b0-3c7f-45e2-b835-5c2a0d433f02', 'hardcore techno'),
        ('055a6e4d-d929-42ac-ba7a-9d063b254ea5', 'hardcore punk'),
        ('4a74a604-f702-424c-984f-93c5b04360ea', 'hardstyle'),
        ('a6719055-99c4-47eb-beaa-71081f2376f9', 'heavy metal'),
        ('52faa157-6bad-4d86-a0ab-d4dec7d2513c', 'hip hop'),
        ('59b8a806-8bdd-45b5-978a-b5e404c910aa', 'honky tonk'),
        ('66d01f06-21dc-4216-ac63-c5efb7b6338e', 'horrorcore'),
        ('36471baf-3fd6-459d-918c-40dc64adea36', 'horror punk'),
        ('a2782cb6-1cd0-477c-a61d-b3f8b42dd1b3', 'house'),
        ('8eb583f1-4fd7-460c-8246-dcdccc0e3ef9', 'idm'),
        ('da52c9d1-73f2-456e-a0d0-94bd854cb338', 'illbient'),
        ('538c905b-642d-43c9-b071-439af11cc471', 'indie'),
        ('36917620-240a-4fed-abcb-7be611ac3834', 'indie folk'),
        ('f390be72-360b-41bb-a310-6a2e638779d2', 'indie pop'),
        ('ccd19ebf-052c-4afe-8ad9-fbb0a73f94a7', 'indie rock'),
        ('9e7ae1de-d5f2-46a5-b105-11c7367dc70d', 'indietronica'),
        ('fde4b353-2d97-4b57-af38-e7c53efa84f3', 'indorock'),
        ('060beed7-e597-4c42-8e25-5bf8bd5dd3cb', 'industrial'),
        ('d4df54b5-67b4-4fb7-8f73-79e71717a501', 'industrial metal'),
        ('ffbc9907-c9be-4ace-876b-b7fd5b9d51f9', 'industrial rock'),
        ('4ae63770-68af-4fae-b0f1-5e53f372c743', 'instrumental'),
        ('c1a4860e-720f-4f4c-8a54-a8032aac08fa', 'instrumental jazz'),
        ('6b3903aa-8abe-4645-b363-558483b1586f', 'instrumental rock'),
        ('3d88ed41-624d-4be9-bf9d-ca11991eb250', 'irish folk'),
        ('97a4a75a-6e49-4519-9684-ae2ccb2fc0f9', 'italo-disco'),
        ('eba7715e-ee26-4989-8d49-9db382955419', 'j-pop'),
        ('d181e978-c908-4f33-b2a2-f7454d0cb2c1', 'j-rock'),
        ('a715278f-1580-409f-8078-4ffbc800e08b', 'jazz'),
        ('9702d99a-823f-47ba-b8c2-af0538c9640b', 'jazz blues'),
        ('2377ff19-83e7-4873-b425-dce6d1ad4ef4', 'jazz fusion'),
        ('6061fb1b-d733-4643-a3d4-940c9a5e2ab8', 'jazz rap'),
        ('5eea976c-1ab2-4d94-9b63-7d1297d5dba5', 'jazz rock'),
        ('f3c43ae8-839a-4523-b295-cfa4eba6c911', 'jazz-funk'),
        ('ea9b6ec3-fc8f-4c55-b2f9-19bd4511a986', 'jungle'),
        ('b74b3b6c-0700-46b1-aa55-1f2869a3bd1a', 'k-pop'),
        ('7054e8e2-7c7b-4432-83b9-93423b361792', 'kayōkyoku'),
        ('5c8b654c-0f2b-40d5-990b-e070a527b1df', 'kizomba'),
        ('06eeec6e-f389-4783-b601-909bb10bd04e', 'klezmer'),
        ('ba318056-9ddf-46cd-8b95-61fc993b962d', 'krautrock'),
        ('7369bb8b-3399-4180-8cb2-4e37e0e7b0e7', 'latin'),
        ('48095aaf-c31d-4bfc-9578-8755c71d44af', 'latin jazz'),
        ('2b9b7ea8-cb90-428c-adb2-d55b2faa4f1d', 'latin pop'),
        ('b07b5eec-c7f0-4529-a876-e0dd6b350084', 'latin rock'),
        ('32bea60c-e201-4241-8ed4-228636d34a97', 'leftfield'),
        ('e1e37f00-9bd9-4510-b6ca-9bb128b665f4', 'line dance'),
        ('65e2f3ad-e0bd-4627-bd83-cba7c8b6d011', 'lo-fi'),
        ('97b15117-c3e2-47ec-9d64-79f90b528a9c', 'lovers rock'),
        ('de2785b2-f282-4718-a422-37dc93964bdd', 'lounge'),
        ('013161b9-3f21-468f-aa1f-28adf1eae085', 'madchester'),
        ('caec8f8d-d862-4b62-9942-5622c2c18446', 'mainstream rock'),
        ('f6fb8e67-cd02-4fb4-b256-2970000dcb46', 'mambo'),
        ('489cf2f9-0edd-4fbc-aa05-ae248706cb3e', 'mandopop'),
        ('482ff4ed-fc37-49fb-9858-92f955a19e6c', 'martial industrial'),
        ('a6b55d73-bba3-49fc-a361-ca4bde004717', 'math rock'),
        ('8df1241b-2ba1-40b8-bb8f-2dc15ce6dc1e', 'mathcore'),
        ('4fc769b7-acac-4061-9129-ee8c6875b4c3', 'medieval'),
        ('74ed98e0-d2f4-4d34-9a51-0c18c1303726', 'melodic black metal'),
        ('5eeb0431-8160-40a7-ae44-c2ad01fed156', 'melodic death metal'),
        ('4f2cd717-7259-48df-8d0a-c17ccd9203b9', 'melodic metalcore'),
        ('e6d5e340-d969-4bb1-8342-18abc1d8c611', 'melodic rock'),
        ('13e409d0-fe4a-4dd4-a957-6e06462b085f', 'melodic trance'),
        ('ce41a887-cdb3-4c52-bbce-604287d74573', 'mento'),
        ('3f5f955d-f505-484a-b397-d0bf0760d8f3', 'merengue'),
        ('cc28f5aa-5e19-487c-88e1-e56b022f1fdd', 'metal'),
        ('62f88295-0605-4ce8-958b-2e0063ece47e', 'metalcore'),
        ('bbce54eb-a477-4b23-ac81-d3b9aac43756', 'microhouse'),
        ('7588308f-e08b-444e-8d6b-55549c58cf3c', 'milonga'),
        ('b0ac2e92-b41d-46e9-8c88-cd254f3a86e0', 'min''yō'),
        ('d024e2e6-842c-43f7-b46e-8d0b87a4cd8e', 'mincecore'),
        ('8d9a00db-0b8b-41ff-9af7-d0f96793bdc5', 'minimal'),
        ('6fb5fe60-b6c2-414f-8011-4f8dc023039b', 'modern blues'),
        ('e328c306-8e35-46d2-93c3-623412ec7c13', 'modern classical'),
        ('0582ba9e-320b-431d-a925-e1b22cad22d8', 'modern country'),
        ('0c44a04f-f7ba-46a0-9dbe-1a21359cfa1e', 'motown'),
        ('7b447f98-1e94-4e57-9bb8-373743fbcdd8', 'mpb'),
        ('ff6d73e8-bf1a-431e-9911-88ae7ffcfdfb', 'musical'),
        ('286f9168-9c46-4e41-94a1-1aa4aeb93b8c', 'neo soul'),
        ('d2327987-362f-4d70-8bfd-a8b47c3b0a54', 'neo-progressive rock'),
        ('66366e89-590e-424f-b13d-0d0564eb4f27', 'neo-rockabilly'),
        ('e94c28db-21a2-43de-9e58-8113fba2c732', 'neofolk'),
        ('9c8ba153-740e-4b88-b7ff-31d004944c95', 'nerdcore'),
        ('6c76d9ec-0924-4fc9-bb22-91a695ca09d6', 'new age'),
        ('dd497eac-e915-49a9-b1e4-482792acf397', 'new jack swing'),
        ('391a797d-50d7-46df-b380-39ccacd52a09', 'new romantic'),
        ('56407f9d-3398-4bf3-bbbd-ea372fa5adeb', 'new wave'),
        ('3188a961-88e0-4b12-9b9e-47f79e53d780', 'no wave'),
        ('89c7c9b3-215e-482f-bce3-3219ffd0a222', 'noise'),
        ('0cd76818-fdde-4f56-93a1-040f09ca7f23', 'noisecore'),
        ('1208a8e7-e5bd-4891-9f81-9a9fdfceeb39', 'noise pop'),
        ('018b7630-8941-4966-90e4-3774daea4e04', 'non-music'),
        ('3e226375-1286-40e3-92b3-d257e3a1babb', 'norteño'),
        ('a18a254b-67ca-4c8b-a13c-87154055e47e', 'northern soul'),
        ('848ecccf-e7fa-4439-a8ab-9dd3c31b4cf6', 'nu jazz'),
        ('bbad9cda-bde2-448e-821c-7ebf85a54020', 'nu metal'),
        ('290afc44-ad41-4d51-9ca6-4ee88df0f4f8', 'occult rock'),
        ('221ff39c-4521-49e3-ab52-e4dde85efd1e', 'oi'),
        ('ed67ff3f-8f00-4321-badd-e9f183f7584b', 'old-time'),
        ('2692f2d7-d680-4a5c-83f0-48e6e7c3ec6b', 'old school death metal'),
        ('56e0b906-1018-4a55-8ec7-86746272116e', 'opera'),
        ('f3fe6570-1e79-4ed5-8eb9-9ed64d631871', 'orchestral'),
        ('f30d5ce4-559c-45c2-897c-089599ac9a83', 'outlaw country'),
        ('fadab481-3ced-466e-b828-74aed72d5c2f', 'p-funk'),
        ('bbe70cad-e184-42e5-9b90-e14438ba4363', 'pachanga'),
        ('911c7bbb-172d-4df8-9478-dbff4296e791', 'pop'),
        ('77365c92-f25b-4ed3-8d02-03aae8b6df0e', 'pop metal'),
        ('60e21d01-ce21-4127-86b7-aada79558dab', 'pop punk'),
        ('da2fc3b4-b97d-491a-ac0f-8a731bee7237', 'pop rap'),
        ('797e2e85-5ffd-495c-a757-8b4079052f0e', 'pop rock'),
        ('64e0bd76-822f-4549-a6fe-9858561282d1', 'pop soul'),
        ('aef2077e-6cbc-47ba-9698-9eb5f842a412', 'pornogrind'),
        ('94ce0ba0-9043-4b5b-87d6-3c1a8458f75d', 'post-bop'),
        ('842008b8-1c67-4c8c-b450-7f5adf20fb7f', 'post-classical'),
        ('bbb1310c-24cb-4fc9-97c4-f93e4549f1e2', 'post-grunge'),
        ('aec382b7-b635-470c-9051-2f84e0b5ea1e', 'post-hardcore'),
        ('e214700e-dae1-4b69-81e9-276e4e88fa32', 'post-metal'),
        ('f6d8cadd-6a5e-4ec9-9c2b-996b5332a639', 'post-punk'),
        ('0bfffa19-4e4a-4bf7-bf53-6ec44f0a2f8e', 'post-rock'),
        ('4e6e388c-a9d2-45cb-80ad-bf64db4a0759', 'powerviolence'),
        ('18527512-634e-48bb-81b2-6bfcf1818b95', 'power electronics'),
        ('f0b0aa5f-ed81-456b-85ab-492002a29eeb', 'power metal'),
        ('618704fc-daac-44a2-92c9-89e3b1a2fc8c', 'power pop'),
        ('5a56d32f-81e4-4301-be70-4a060ab3b4a0', 'production music'),
        ('8d275a8d-154a-4bcb-8b5a-f56306ad4bcb', 'progressive'),
        ('34d08b64-7426-42e1-959e-48af44352fcd', 'progressive folk'),
        ('70adb285-e1f7-458a-823f-5cbda5e291c4', 'progressive house'),
        ('5c7785e6-a0b4-4441-a1e6-4fd4c0066a5f', 'progressive metal'),
        ('ae9b8279-3959-48d8-8a88-741a7f6d4a48', 'progressive rock'),
        ('6bd228be-3374-4c4e-bc0a-46779fafe8f1', 'progressive trance'),
        ('d1091878-2332-4f82-9a5c-f6d67cf4b7ef', 'psy-trance'),
        ('d698ece0-6fed-4262-b446-42c98b73b65a', 'psychedelic'),
        ('507757f0-a63c-422b-93f1-f1d77462033f', 'psychedelic folk'),
        ('2aeb5340-c474-4677-b9a6-35ddac9b6a58', 'psychedelic pop'),
        ('146ef761-5ad9-48b4-b0b3-483104f7da48', 'psychedelic rock'),
        ('8a14f570-7297-438d-996d-8797f9b8cfcc', 'psychobilly'),
        ('b3cccf71-e843-4546-87fa-728055ba3147', 'psytrance'),
        ('8cc9b280-230b-4a3a-b1e2-8acab0744dd3', 'punk'),
        ('bd7e1d40-43b1-4ba5-97b3-05b891466962', 'punk rock'),
        ('2481fbdc-05ed-458d-9928-46f3336c343e', 'queercore'),
        ('31be54b2-4d0c-42df-aa44-c496c7b4c3c3', 'r&b'),
        ('0d15e596-3374-4590-bdb0-0a95d38ab4dc', 'ragga'),
        ('4080e55b-ac2e-4907-a724-491b3bfdba63', 'ragga hip-hop'),
        ('7aae791f-dfff-4910-b133-ba1b9e9c1560', 'ragga jungle'),
        ('deec5f2f-ccfc-4e0c-b1ac-8e5a17cd5cb7', 'raï'),
        ('d017dd87-704b-4ad4-8b67-39fb401b8339', 'ragtime'),
        ('ca20f188-68d2-4af5-966e-8e2e0f848758', 'ranchera'),
        ('b4b184b7-6224-478e-a273-f9da9ee30136', 'rap rock'),
        ('2cd7891d-c012-45f1-b1a1-86e34793f7fa', 'rapcore'),
        ('660e755a-1b66-440b-9b7c-fbcf245f4415', 'rave'),
        ('02b2f720-d06e-42ce-85c2-1ecd4191ffcb', 'reggae'),
        ('bb14fb18-af6b-4547-9130-47fc4b208f48', 'reggaeton'),
        ('9d4f50e8-5056-435e-8786-2d09e3407622', 'rhythmic noise'),
        ('0e3fc579-2d24-4f20-9dae-736e1ec78798', 'rock'),
        ('e4581b76-a842-440b-9b0f-1aa7a00a5c57', 'rock and roll'),
        ('f1a053eb-61bb-448a-964d-35784c4ae8c1', 'rockabilly'),
        ('81638671-c67c-47cf-b21d-e92261068cf6', 'rocksteady'),
        ('f2d4991d-97fa-4c1c-8e09-15ec7668aa43', 'roots reggae'),
        ('190bba5b-909f-4501-9a00-f96969983459', 'rumba'),
        ('717f442d-59d1-4349-a14e-c4874acd3509', 'salsa'),
        ('2447a0ec-e65f-48c7-b402-05cfe5c62cb7', 'samba'),
        ('7edde935-8094-409e-ba33-34779b4668f8', 'schlager'),
        ('8fea0cb0-4205-49cb-95e8-bc1122857c7d', 'screamo'),
        ('9864d280-a987-466f-bac2-ce103721c3e2', 'shibuya-kei'),
        ('a4012b2e-5c27-4f81-8781-9ae5998d882c', 'shoegaze'),
        ('455f264b-db00-4716-991d-fbd32dc24523', 'singer-songwriter'),
        ('65877068-2116-4af6-89a2-838179a7e103', 'ska'),
        ('89e34d8e-6562-412f-8da3-d040e159e6cd', 'ska punk'),
        ('7aabc697-7c79-4f12-bb6f-ee5d70298a3f', 'skacore'),
        ('a4704a31-7c69-44d9-a590-8fcc2b258f77', 'slow waltz'),
        ('66544dbb-08a8-44c2-a21a-4d251481d2b6', 'sludge metal'),
        ('a1f1dcc8-6078-4c12-90b7-47e3080e330c', 'smooth jazz'),
        ('d4eafdc7-1a86-422b-b598-79e6bb81745c', 'smooth soul'),
        ('b251cd9a-990c-49f6-8da3-db3457aa2114', 'soca'),
        ('7d0c6a6b-5d56-4b1e-a8c7-e32194f246b9', 'soft rock'),
        ('db48f46a-6467-49b0-a6b2-beb059584d9d', 'son cubano'),
        ('7f0d2b34-d0ea-4ab5-a169-934f131bc40c', 'son montuno'),
        ('1b5e7b16-336b-473e-a364-5120413a9827', 'soul'),
        ('e70f0aa3-9531-448c-8182-7ff34e3d74f8', 'soul jazz'),
        ('905c1899-3027-4d29-8d9c-bbeef2c8628a', 'southern rock'),
        ('bd61d502-8906-4073-a4ea-18c04e946dbc', 'southern soul'),
        ('532dcf83-5641-446d-b007-51292b401650', 'space rock'),
        ('59c7776f-fd87-4cfb-a7a5-9fe66ec02b1b', 'speed garage'),
        ('13b9a3d0-cadd-483d-9bf5-9c2b32a946a7', 'speed metal'),
        ('c774791e-1b78-4794-a109-befb712ef44d', 'spoken word'),
        ('5c76eb54-7342-4d1f-9d98-a93f2f6aa7f6', 'stoner metal'),
        ('aa5f0254-403e-496a-a388-c01abf62f39a', 'stoner rock'),
        ('c2ca3275-7e83-40cc-8f13-7ccae588144d', 'street punk'),
        ('b76ee78d-ec97-49e9-8ea6-cfc46ecc3b23', 'surf rock'),
        ('063d0461-1e12-4246-87bf-69c5ca611cc2', 'swing'),
        ('86a383d3-a4cc-4690-9a0f-f8ee83f7492d', 'symphonic black metal'),
        ('2a4cae89-5a5a-44b7-9cf8-bf92c38a31dc', 'symphonic metal'),
        ('166be36f-febb-4523-a005-1fb3603bd3f6', 'symphonic prog'),
        ('f729e6f8-30dc-4b81-9ff4-f4e7de82225d', 'symphonic rock'),
        ('50294a4d-61f0-4dc8-a20f-4708d52fe78a', 'symphony'),
        ('19565d07-8460-4773-8d54-149208a61bb4', 'synthwave'),
        ('988e91a3-3341-416d-b7f8-7dbef6848dac', 'synth-pop'),
        ('267b14aa-3e3c-4615-803c-0255a81c8203', 'tango'),
        ('1f9c4b4a-c43d-40bd-b806-357153641fd0', 'tech house'),
        ('db37c56d-e6a7-4809-b549-6f1cb559b559', 'technical death metal'),
        ('41fe3260-fcc1-450b-bd5a-803886c56912', 'techno'),
        ('4b7c8e31-462b-4e06-acfd-fa508ad26e0c', 'teen pop'),
        ('e8a3ec42-4427-4365-8180-f1e05b46459e', 'thrashcore'),
        ('cc4cf136-5690-4ee3-a62b-b172febfe322', 'thrash metal'),
        ('6b8e3c88-add3-4b27-b2ec-a2135edf6e95', 'timba'),
        ('037150a6-d83b-4307-908a-84201867186d', 'traditional country'),
        ('2afc2320-384f-43a4-a71e-1b763c485045', 'trance'),
        ('9a339aa8-a0e5-450c-95a7-b11517bb508f', 'trap'),
        ('a21ea484-efba-4119-8120-429b26f98728', 'trap edm'),
        ('57159d7c-faa8-4266-abe1-7504e312fc88', 'tribal house'),
        ('45eb1d9c-588c-4dc8-9394-a14b7c8f02bc', 'trip hop'),
        ('cfe47781-460e-498a-92f5-399d68076727', 'turntablism'),
        ('ca5d2892-93bf-4aae-85d5-a4589a1680b9', 'uk drill'),
        ('95c57cd7-7fb8-4405-ade1-775c2d27e54e', 'uk garage'),
        ('03a8749c-93e6-4b71-8204-b33de116d8fd', 'underground hip hop'),
        ('bff33e5d-d591-498f-927f-b26c5527d817', 'vallenato'),
        ('e837fa08-f314-428f-af96-ceed9858e3b2', 'vaporwave'),
        ('fc736acd-9413-4b86-b8d4-47e9ce61d561', 'viking metal'),
        ('823e4a7f-ade7-4d34-927a-721c60469b81', 'visual kei'),
        ('9405d427-8b33-4670-a41a-040e23cef8ce', 'vocal house'),
        ('782dc157-d1a5-42b9-9174-d329c24a7ef3', 'vocal jazz'),
        ('f4dec9c1-abe2-4f3f-adee-5aa8f395d5ce', 'vocal trance'),
        ('149a3524-0fd3-4bdc-9876-0361dfee8908', 'west coast hip hop'),
        ('bb071d13-8c80-411b-9c0b-16b37bf71ae6', 'west coast swing'),
        ('e6d6a58f-6c00-484e-9bc7-12e2c4d024e2', 'yé-yé'),
        ('281e2c3f-80ad-4bc4-8b30-54222dee3b9c', 'zamrock'),
        ('498229df-4956-4ed7-91f7-a9d4b1877af1', 'zydeco')
;

CREATE OR REPLACE FUNCTION unique_primary_genre_alias()
RETURNS trigger AS $$
BEGIN
    IF NEW.primary_for_locale THEN
      UPDATE genre_alias SET primary_for_locale = FALSE
      WHERE locale = NEW.locale AND id != NEW.id
        AND genre = NEW.genre;
    END IF;
    RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';

--------------------------------------------------------------------------------
SELECT '20190422-mbs-7480.sql';

ALTER TABLE cover_art_archive.cover_art ADD COLUMN filesize INTEGER;
ALTER TABLE cover_art_archive.cover_art ADD COLUMN thumb_250_filesize INTEGER;
ALTER TABLE cover_art_archive.cover_art ADD COLUMN thumb_500_filesize INTEGER;
ALTER TABLE cover_art_archive.cover_art ADD COLUMN thumb_1200_filesize INTEGER;

--------------------------------------------------------------------------------
SELECT '20190422-mbs-9973-collection-added-date.sql';

ALTER TABLE editor_collection_area ADD COLUMN added TIMESTAMP WITH TIME ZONE DEFAULT NOW();
ALTER TABLE editor_collection_artist ADD COLUMN added TIMESTAMP WITH TIME ZONE DEFAULT NOW();
ALTER TABLE editor_collection_event ADD COLUMN added TIMESTAMP WITH TIME ZONE DEFAULT NOW();
ALTER TABLE editor_collection_instrument ADD COLUMN added TIMESTAMP WITH TIME ZONE DEFAULT NOW();
ALTER TABLE editor_collection_label ADD COLUMN added TIMESTAMP WITH TIME ZONE DEFAULT NOW();
ALTER TABLE editor_collection_place ADD COLUMN added TIMESTAMP WITH TIME ZONE DEFAULT NOW();
ALTER TABLE editor_collection_recording ADD COLUMN added TIMESTAMP WITH TIME ZONE DEFAULT NOW();
ALTER TABLE editor_collection_release ADD COLUMN added TIMESTAMP WITH TIME ZONE DEFAULT NOW();
ALTER TABLE editor_collection_release_group ADD COLUMN added TIMESTAMP WITH TIME ZONE DEFAULT NOW();
ALTER TABLE editor_collection_series ADD COLUMN added TIMESTAMP WITH TIME ZONE DEFAULT NOW();
ALTER TABLE editor_collection_work ADD COLUMN added TIMESTAMP WITH TIME ZONE DEFAULT NOW();
ALTER TABLE editor_collection_deleted_entity ADD COLUMN added TIMESTAMP WITH TIME ZONE DEFAULT NOW();

UPDATE editor_collection_area SET added = NULL;
UPDATE editor_collection_artist SET added = NULL;
UPDATE editor_collection_event SET added = NULL;
UPDATE editor_collection_instrument SET added = NULL;
UPDATE editor_collection_label SET added = NULL;
UPDATE editor_collection_place SET added = NULL;
UPDATE editor_collection_recording SET added = NULL;
UPDATE editor_collection_release SET added = NULL;
UPDATE editor_collection_release_group SET added = NULL;
UPDATE editor_collection_series SET added = NULL;
UPDATE editor_collection_work SET added = NULL;
UPDATE editor_collection_deleted_entity SET added = NULL;

--------------------------------------------------------------------------------
SELECT '20190422-mbs-9428-collection-collaborators.sql';

CREATE TABLE editor_collection_collaborator (
    collection INTEGER NOT NULL, -- PK, references editor_collection.id
    editor INTEGER NOT NULL -- references editor.id
);

ALTER TABLE editor_collection_collaborator ADD CONSTRAINT editor_collection_collaborator_pkey PRIMARY KEY (collection, editor);

--------------------------------------------------------------------------------
SELECT '20190422-mbs-5818-collection-position.sql';

ALTER TABLE editor_collection_area ADD COLUMN position INTEGER NOT NULL DEFAULT 0 CHECK (position >= 0);
ALTER TABLE editor_collection_artist ADD COLUMN position INTEGER NOT NULL DEFAULT 0 CHECK (position >= 0);
ALTER TABLE editor_collection_event ADD COLUMN position INTEGER NOT NULL DEFAULT 0 CHECK (position >= 0);
ALTER TABLE editor_collection_instrument ADD COLUMN position INTEGER NOT NULL DEFAULT 0 CHECK (position >= 0);
ALTER TABLE editor_collection_label ADD COLUMN position INTEGER NOT NULL DEFAULT 0 CHECK (position >= 0);
ALTER TABLE editor_collection_place ADD COLUMN position INTEGER NOT NULL DEFAULT 0 CHECK (position >= 0);
ALTER TABLE editor_collection_recording ADD COLUMN position INTEGER NOT NULL DEFAULT 0 CHECK (position >= 0);
ALTER TABLE editor_collection_release ADD COLUMN position INTEGER NOT NULL DEFAULT 0 CHECK (position >= 0);
ALTER TABLE editor_collection_release_group ADD COLUMN position INTEGER NOT NULL DEFAULT 0 CHECK (position >= 0);
ALTER TABLE editor_collection_series ADD COLUMN position INTEGER NOT NULL DEFAULT 0 CHECK (position >= 0);
ALTER TABLE editor_collection_work ADD COLUMN position INTEGER NOT NULL DEFAULT 0 CHECK (position >= 0);
ALTER TABLE editor_collection_deleted_entity ADD COLUMN position INTEGER NOT NULL DEFAULT 0 CHECK (position >= 0);

--------------------------------------------------------------------------------
SELECT '20190422-mbs-1658-collection-text.sql';

ALTER TABLE editor_collection_area ADD COLUMN comment TEXT DEFAULT '' NOT NULL;
ALTER TABLE editor_collection_artist ADD COLUMN comment TEXT DEFAULT '' NOT NULL;
ALTER TABLE editor_collection_event ADD COLUMN comment TEXT DEFAULT '' NOT NULL;
ALTER TABLE editor_collection_instrument ADD COLUMN comment TEXT DEFAULT '' NOT NULL;
ALTER TABLE editor_collection_label ADD COLUMN comment TEXT DEFAULT '' NOT NULL;
ALTER TABLE editor_collection_place ADD COLUMN comment TEXT DEFAULT '' NOT NULL;
ALTER TABLE editor_collection_recording ADD COLUMN comment TEXT DEFAULT '' NOT NULL;
ALTER TABLE editor_collection_release ADD COLUMN comment TEXT DEFAULT '' NOT NULL;
ALTER TABLE editor_collection_release_group ADD COLUMN comment TEXT DEFAULT '' NOT NULL;
ALTER TABLE editor_collection_series ADD COLUMN comment TEXT DEFAULT '' NOT NULL;
ALTER TABLE editor_collection_work ADD COLUMN comment TEXT DEFAULT '' NOT NULL;
ALTER TABLE editor_collection_deleted_entity ADD COLUMN comment TEXT DEFAULT '' NOT NULL;

--------------------------------------------------------------------------------
SELECT '20190423-mbs-10052-eaa.sql';

CREATE SCHEMA event_art_archive;

SET search_path = event_art_archive, musicbrainz;

CREATE TYPE musicbrainz.event_art_presence AS ENUM ('absent', 'present', 'darkened');

ALTER TABLE musicbrainz.event_meta
ADD COLUMN event_art_presence musicbrainz.event_art_presence NOT NULL DEFAULT 'absent';

-- Tables

CREATE TABLE art_type ( -- replicate (verbose)
    id                  SERIAL NOT NULL, -- PK
    name                TEXT NOT NULL,
    parent              INTEGER, -- references event_art_archive.art_type.id
    child_order         INTEGER NOT NULL DEFAULT 0,
    description         TEXT,
    gid                 uuid NOT NULL
);

INSERT INTO art_type VALUES
    (1, 'Poster', NULL, 0, NULL, generate_uuid_v3('6ba7b8119dad11d180b400c04fd430c8', 'even_art_archive.art_type' || 1));

CREATE TABLE event_art ( -- replicate (verbose)
    id BIGINT NOT NULL, -- PK
    event INTEGER NOT NULL, -- references musicbrainz.event.id CASCADE
    comment TEXT NOT NULL DEFAULT '',
    edit INTEGER NOT NULL, -- separately references musicbrainz.edit.id
    ordering INTEGER NOT NULL CHECK (ordering > 0),
    date_uploaded TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL,
    edits_pending INTEGER NOT NULL DEFAULT 0 CHECK (edits_pending >= 0),
    mime_type TEXT NOT NULL, -- references cover_art_archive.image_type.mime_type
    filesize INTEGER,
    thumb_250_filesize INTEGER,
    thumb_500_filesize INTEGER,
    thumb_1200_filesize INTEGER
);

CREATE TABLE event_art_type ( -- replicate (verbose)
    id BIGINT NOT NULL, -- PK, references event_art_archive.event_art.id CASCADE,
    type_id INTEGER NOT NULL -- PK, references event_art_archive.art_type.id,
);

-- Primary keys

ALTER TABLE art_type ADD CONSTRAINT art_type_pkey PRIMARY KEY (id);
ALTER TABLE event_art ADD CONSTRAINT event_art_pkey PRIMARY KEY (id);
ALTER TABLE event_art_type ADD CONSTRAINT event_art_type_pkey PRIMARY KEY (id, type_id);

-- Indexes

CREATE INDEX event_art_idx_event ON event_art (event);
CREATE UNIQUE INDEX art_type_idx_gid ON art_type (gid);

-- Functions

CREATE OR REPLACE FUNCTION materialize_eaa_presence() RETURNS trigger AS $$
    BEGIN
        -- On delete, set the presence flag to 'absent' if there's no more
        -- event art
        IF TG_OP = 'DELETE' THEN
            IF NOT EXISTS (
                SELECT TRUE FROM event_art_archive.event_art
                WHERE event = OLD.event
            ) THEN
                UPDATE musicbrainz.event_meta
                SET event_art_presence = 'absent'
                WHERE id = OLD.event;
            END IF;
        END IF;

        -- On insert, set the presence flag to 'present' if it was previously
        -- 'absent'
        IF TG_OP = 'INSERT' THEN
            CASE (
                SELECT event_art_presence FROM musicbrainz.event_meta
                WHERE id = NEW.event
            )
                WHEN 'absent' THEN
                    UPDATE musicbrainz.event_meta
                    SET event_art_presence = 'present'
                    WHERE id = NEW.event;
                WHEN 'darkened' THEN
                    RAISE EXCEPTION 'This event has been darkened and cannot have new event art';
                ELSE
            END CASE;
        END IF;

        RETURN NULL;
    END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION resequence_positions(event_id INT) RETURNS void AS $$
    BEGIN
        UPDATE event_art_archive.event_art
        SET ordering = recalculated.row_number
        FROM (
            SELECT *,
              row_number() OVER (PARTITION BY event ORDER BY ordering ASC)
            FROM event_art_archive.event_art
            WHERE event_art.event = event_id
        ) recalculated
        WHERE recalculated.id = event_art.id AND
          recalculated.row_number != event_art.ordering;
   END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION resequence_event_art_trigger() RETURNS trigger AS $$
    BEGIN
        IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
            PERFORM event_art_archive.resequence_positions(NEW.event);
        END IF;

        IF (TG_OP = 'DELETE') OR
           (TG_OP = 'UPDATE' AND NEW.event != OLD.event)
        THEN
            PERFORM event_art_archive.resequence_positions(OLD.event);
        END IF;

        RETURN NULL;
    END;
$$ LANGUAGE 'plpgsql';

-- Views

CREATE OR REPLACE VIEW index_listing AS
SELECT event_art.*,
  (edit.close_time IS NOT NULL) AS approved,
  coalesce(event_art.id = (SELECT id FROM event_art_archive.event_art_type
                   JOIN event_art_archive.event_art ea_front USING (id)
                   WHERE ea_front.event = event_art.event
                   AND type_id = 1
                   AND mime_type != 'application/pdf'
                   ORDER BY ea_front.ordering
                   LIMIT 1), FALSE) AS is_front,
  array(SELECT art_type.name
        FROM event_art_archive.event_art_type
        JOIN event_art_archive.art_type ON event_art_type.type_id = art_type.id
        WHERE event_art_type.id = event_art.id) AS types
FROM event_art_archive.event_art
LEFT JOIN musicbrainz.edit ON edit.id = event_art.edit;

--------------------------------------------------------------------------------
SELECT '20190423-mbs-5387.sql';

ALTER TABLE artist_credit
ADD COLUMN edits_pending INTEGER NOT NULL DEFAULT 0
CHECK (edits_pending >= 0);

COMMIT;
