-- Generated by CompileSchemaScripts.pl from:
-- 20150209-extend-aliases-to-more-entities.sql
-- 20150307-other-entities-collections.sql
-- 20150317-medium-name-not-null.sql
-- 20150331-relationship-credits.sql
-- 20150429-tag-upvote.sql
-- 20150403-drop-empty-functions.sql
\set ON_ERROR_STOP 1
BEGIN;
SET search_path = musicbrainz, public;
SET LOCAL statement_timeout = 0;
--------------------------------------------------------------------------------
SELECT '20150209-extend-aliases-to-more-entities.sql';

CREATE TABLE recording_alias_type ( -- replicate
    id SERIAL, -- PK,
    name TEXT NOT NULL,
    parent              INTEGER, -- references recording_alias_type.id
    child_order         INTEGER NOT NULL DEFAULT 0,
    description         TEXT
);

CREATE TABLE recording_alias ( -- replicate (verbose)
    id                  SERIAL, --PK
    recording           INTEGER NOT NULL, -- references recording.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(),
    type                INTEGER, -- references recording_alias_type.id
    sort_name           VARCHAR NOT NULL,
    begin_date_year     SMALLINT,
    begin_date_month    SMALLINT,
    begin_date_day      SMALLINT,
    end_date_year       SMALLINT,
    end_date_month      SMALLINT,
    end_date_day        SMALLINT,
    primary_for_locale  BOOLEAN NOT NULL DEFAULT false,
    ended               BOOLEAN NOT NULL DEFAULT FALSE
      CHECK (
        (
          -- If any end date fields are not null, then ended must be true
          (end_date_year IS NOT NULL OR
           end_date_month IS NOT NULL OR
           end_date_day IS NOT NULL) AND
          ended = TRUE
        ) OR (
          -- Otherwise, all end date fields must be null
          (end_date_year IS NULL AND
           end_date_month IS NULL AND
           end_date_day IS NULL)
        )
      ),
             CONSTRAINT primary_check
                 CHECK ((locale IS NULL AND primary_for_locale IS FALSE) OR (locale IS NOT NULL)));

CREATE TABLE release_alias_type ( -- replicate
    id SERIAL, -- PK,
    name TEXT NOT NULL,
    parent              INTEGER, -- references release_alias_type.id
    child_order         INTEGER NOT NULL DEFAULT 0,
    description         TEXT
);

CREATE TABLE release_alias ( -- replicate (verbose)
    id                  SERIAL, --PK
    release             INTEGER NOT NULL, -- references release.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(),
    type                INTEGER, -- references release_alias_type.id
    sort_name           VARCHAR NOT NULL,
    begin_date_year     SMALLINT,
    begin_date_month    SMALLINT,
    begin_date_day      SMALLINT,
    end_date_year       SMALLINT,
    end_date_month      SMALLINT,
    end_date_day        SMALLINT,
    primary_for_locale  BOOLEAN NOT NULL DEFAULT false,
    ended               BOOLEAN NOT NULL DEFAULT FALSE
      CHECK (
        (
          -- If any end date fields are not null, then ended must be true
          (end_date_year IS NOT NULL OR
           end_date_month IS NOT NULL OR
           end_date_day IS NOT NULL) AND
          ended = TRUE
        ) OR (
          -- Otherwise, all end date fields must be null
          (end_date_year IS NULL AND
           end_date_month IS NULL AND
           end_date_day IS NULL)
        )
      ),
             CONSTRAINT primary_check
                 CHECK ((locale IS NULL AND primary_for_locale IS FALSE) OR (locale IS NOT NULL)));

CREATE TABLE release_group_alias_type ( -- replicate
    id SERIAL, -- PK,
    name TEXT NOT NULL,
    parent              INTEGER, -- references release_group_alias_type.id
    child_order         INTEGER NOT NULL DEFAULT 0,
    description         TEXT
);

CREATE TABLE release_group_alias ( -- replicate (verbose)
    id                  SERIAL, --PK
    release_group       INTEGER NOT NULL, -- references release_group.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(),
    type                INTEGER, -- references release_group_alias_type.id
    sort_name           VARCHAR NOT NULL,
    begin_date_year     SMALLINT,
    begin_date_month    SMALLINT,
    begin_date_day      SMALLINT,
    end_date_year       SMALLINT,
    end_date_month      SMALLINT,
    end_date_day        SMALLINT,
    primary_for_locale  BOOLEAN NOT NULL DEFAULT false,
    ended               BOOLEAN NOT NULL DEFAULT FALSE
      CHECK (
        (
          -- If any end date fields are not null, then ended must be true
          (end_date_year IS NOT NULL OR
           end_date_month IS NOT NULL OR
           end_date_day IS NOT NULL) AND
          ended = TRUE
        ) OR (
          -- Otherwise, all end date fields must be null
          (end_date_year IS NULL AND
           end_date_month IS NULL AND
           end_date_day IS NULL)
        )
      ),
             CONSTRAINT primary_check
                 CHECK ((locale IS NULL AND primary_for_locale IS FALSE) OR (locale IS NOT NULL)));


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

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

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


CREATE INDEX recording_alias_idx_recording ON recording_alias (recording);
CREATE UNIQUE INDEX recording_alias_idx_primary ON recording_alias (recording, locale) WHERE primary_for_locale = TRUE AND locale IS NOT NULL;

CREATE INDEX release_alias_idx_release ON release_alias (release);
CREATE UNIQUE INDEX release_alias_idx_primary ON release_alias (release, locale) WHERE primary_for_locale = TRUE AND locale IS NOT NULL;

CREATE INDEX release_group_alias_idx_release_group ON release_group_alias (release_group);
CREATE UNIQUE INDEX release_group_alias_idx_primary ON release_group_alias (release_group, locale) WHERE primary_for_locale = TRUE AND locale IS NOT NULL;


ALTER TABLE recording_alias ADD CONSTRAINT recording_alias_pkey PRIMARY KEY (id);
ALTER TABLE recording_alias_type ADD CONSTRAINT recording_alias_type_pkey PRIMARY KEY (id);
ALTER TABLE release_alias ADD CONSTRAINT release_alias_pkey PRIMARY KEY (id);
ALTER TABLE release_alias_type ADD CONSTRAINT release_alias_type_pkey PRIMARY KEY (id);
ALTER TABLE release_group_alias ADD CONSTRAINT release_group_alias_pkey PRIMARY KEY (id);
ALTER TABLE release_group_alias_type ADD CONSTRAINT release_group_alias_type_pkey PRIMARY KEY (id);

 
CREATE INDEX release_alias_idx_txt ON release_alias USING gin(to_tsvector('mb_simple', name));
CREATE INDEX release_alias_idx_txt_sort ON release_alias USING gin(to_tsvector('mb_simple', sort_name));

CREATE INDEX release_group_alias_idx_txt ON release_group_alias USING gin(to_tsvector('mb_simple', name));
CREATE INDEX release_group_alias_idx_txt_sort ON release_group_alias USING gin(to_tsvector('mb_simple', sort_name));

CREATE INDEX recording_alias_idx_txt ON recording_alias USING gin(to_tsvector('mb_simple', name));
CREATE INDEX recording_alias_idx_txt_sort ON recording_alias USING gin(to_tsvector('mb_simple', sort_name));

INSERT INTO recording_alias_type (name) VALUES ('Recording name'), ('Search hint');
INSERT INTO release_alias_type (name) VALUES ('Release name'), ('Search hint');
INSERT INTO release_group_alias_type (name) VALUES ('Release group name'), ('Search hint');

SELECT setval('recording_alias_type_id_seq', (SELECT MAX(id) FROM recording_alias_type));
SELECT setval('release_alias_type_id_seq', (SELECT MAX(id) FROM release_alias_type));
SELECT setval('release_group_alias_type_id_seq', (SELECT MAX(id) FROM release_group_alias_type));

--------------------------------------------------------------------------------
SELECT '20150307-other-entities-collections.sql';

ALTER TABLE editor_collection_type
      DROP CONSTRAINT IF EXISTS allowed_collection_entity_type;

-- Commented-out because code was not ready for this yet.
--INSERT INTO editor_collection_type (id, name, entity_type, parent, child_order) VALUES
--        (7, 'Area', 'area', NULL, 2),
--        (8, 'Artist', 'artist', NULL, 2),
--        (9, 'Instrument', 'instrument', NULL, 2),
--        (10, 'Label', 'label', NULL, 2),
--        (11, 'Place', 'place', NULL, 2),
--        (12, 'Recording', 'recording', NULL, 2),
--        (13, 'Release group', 'release_group', NULL, 2),
--        (14, 'Series', 'series', NULL, 2),
--        (15, 'Work', 'work', NULL, 2);

CREATE TABLE editor_collection_area (
    collection INTEGER NOT NULL,
    area INTEGER NOT NULL
);

ALTER TABLE editor_collection_area ADD CONSTRAINT editor_collection_area_pkey PRIMARY KEY (collection, area);


CREATE TABLE editor_collection_artist (
    collection INTEGER NOT NULL,
    artist INTEGER NOT NULL
);

ALTER TABLE editor_collection_artist ADD CONSTRAINT editor_collection_artist_pkey PRIMARY KEY (collection, artist);


CREATE TABLE editor_collection_instrument (
    collection INTEGER NOT NULL,
    instrument INTEGER NOT NULL
);

ALTER TABLE editor_collection_instrument ADD CONSTRAINT editor_collection_instrument_pkey PRIMARY KEY (collection, instrument);


CREATE TABLE editor_collection_label (
    collection INTEGER NOT NULL,
    label INTEGER NOT NULL
);

ALTER TABLE editor_collection_label ADD CONSTRAINT editor_collection_label_pkey PRIMARY KEY (collection, label);


CREATE TABLE editor_collection_place (
    collection INTEGER NOT NULL,
    place INTEGER NOT NULL
);

ALTER TABLE editor_collection_place ADD CONSTRAINT editor_collection_place_pkey PRIMARY KEY (collection, place);


CREATE TABLE editor_collection_recording (
    collection INTEGER NOT NULL,
    recording INTEGER NOT NULL
);

ALTER TABLE editor_collection_recording ADD CONSTRAINT editor_collection_recording_pkey PRIMARY KEY (collection, recording);


CREATE TABLE editor_collection_release_group (
    collection INTEGER NOT NULL,
    release_group INTEGER NOT NULL
);

ALTER TABLE editor_collection_release_group ADD CONSTRAINT editor_collection_release_group_pkey PRIMARY KEY (collection, release_group);


CREATE TABLE editor_collection_series (
    collection INTEGER NOT NULL,
    series INTEGER NOT NULL
);

ALTER TABLE editor_collection_series ADD CONSTRAINT editor_collection_series_pkey PRIMARY KEY (collection, series);


CREATE TABLE editor_collection_work (
    collection INTEGER NOT NULL,
    work INTEGER NOT NULL
);

ALTER TABLE editor_collection_work ADD CONSTRAINT editor_collection_work_pkey PRIMARY KEY (collection, work);

--------------------------------------------------------------------------------
SELECT '20150317-medium-name-not-null.sql';

ALTER TABLE medium
    ALTER COLUMN name SET DATA TYPE VARCHAR USING COALESCE(name, ''),
    ALTER COLUMN name SET NOT NULL,
    ALTER COLUMN name SET DEFAULT '';

--------------------------------------------------------------------------------
SELECT '20150331-relationship-credits.sql';

ALTER TABLE l_area_area ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_area_area ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_area_artist ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_area_artist ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_area_event ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_area_event ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_area_instrument ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_area_instrument ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_area_label ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_area_label ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_area_place ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_area_place ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_area_recording ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_area_recording ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_area_release ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_area_release ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_area_release_group ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_area_release_group ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_area_series ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_area_series ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_area_url ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_area_url ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_area_work ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_area_work ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_artist_artist ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_artist_artist ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_artist_event ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_artist_event ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_artist_instrument ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_artist_instrument ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_artist_label ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_artist_label ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_artist_place ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_artist_place ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_artist_recording ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_artist_recording ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_artist_release ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_artist_release ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_artist_release_group ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_artist_release_group ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_artist_series ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_artist_series ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_artist_url ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_artist_url ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_artist_work ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_artist_work ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_event_event ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_event_event ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_event_instrument ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_event_instrument ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_event_label ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_event_label ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_event_place ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_event_place ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_event_recording ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_event_recording ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_event_release ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_event_release ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_event_release_group ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_event_release_group ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_event_series ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_event_series ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_event_url ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_event_url ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_event_work ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_event_work ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_label_label ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_label_label ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_instrument_instrument ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_instrument_instrument ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_instrument_label ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_instrument_label ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_instrument_place ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_instrument_place ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_instrument_recording ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_instrument_recording ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_instrument_release ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_instrument_release ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_instrument_release_group ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_instrument_release_group ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_instrument_series ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_instrument_series ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_instrument_url ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_instrument_url ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_instrument_work ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_instrument_work ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_label_place ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_label_place ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_label_recording ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_label_recording ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_label_release ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_label_release ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_label_release_group ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_label_release_group ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_label_series ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_label_series ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_label_url ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_label_url ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_label_work ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_label_work ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_place_place ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_place_place ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_place_recording ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_place_recording ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_place_release ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_place_release ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_place_release_group ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_place_release_group ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_place_series ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_place_series ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_place_url ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_place_url ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_place_work ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_place_work ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_recording_recording ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_recording_recording ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_recording_release ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_recording_release ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_recording_release_group ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_recording_release_group ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_recording_series ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_recording_series ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_recording_url ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_recording_url ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_recording_work ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_recording_work ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_release_release ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_release_release ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_release_release_group ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_release_release_group ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_release_series ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_release_series ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_release_url ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_release_url ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_release_work ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_release_work ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_release_group_release_group ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_release_group_release_group ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_release_group_series ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_release_group_series ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_release_group_url ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_release_group_url ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_release_group_work ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_release_group_work ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_series_series ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_series_series ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_series_url ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_series_url ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_series_work ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_series_work ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_url_url ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_url_url ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_url_work ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_url_work ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

ALTER TABLE l_work_work ADD COLUMN entity0_credit TEXT NOT NULL DEFAULT '';
ALTER TABLE l_work_work ADD COLUMN entity1_credit TEXT NOT NULL DEFAULT '';

--------------------------------------------------------------------------------
SELECT '20150429-tag-upvote.sql';

ALTER TABLE area_tag_raw ADD COLUMN is_upvote BOOLEAN NOT NULL DEFAULT TRUE;
ALTER TABLE artist_tag_raw ADD COLUMN is_upvote BOOLEAN NOT NULL DEFAULT TRUE;
ALTER TABLE event_tag_raw ADD COLUMN is_upvote BOOLEAN NOT NULL DEFAULT TRUE;
ALTER TABLE instrument_tag_raw ADD COLUMN is_upvote BOOLEAN NOT NULL DEFAULT TRUE;
ALTER TABLE label_tag_raw ADD COLUMN is_upvote BOOLEAN NOT NULL DEFAULT TRUE;
ALTER TABLE place_tag_raw ADD COLUMN is_upvote BOOLEAN NOT NULL DEFAULT TRUE;
ALTER TABLE recording_tag_raw ADD COLUMN is_upvote BOOLEAN NOT NULL DEFAULT TRUE;
ALTER TABLE release_tag_raw ADD COLUMN is_upvote BOOLEAN NOT NULL DEFAULT TRUE;
ALTER TABLE release_group_tag_raw ADD COLUMN is_upvote BOOLEAN NOT NULL DEFAULT TRUE;
ALTER TABLE series_tag_raw ADD COLUMN is_upvote BOOLEAN NOT NULL DEFAULT TRUE;
ALTER TABLE work_tag_raw ADD COLUMN is_upvote BOOLEAN NOT NULL DEFAULT TRUE;

--------------------------------------------------------------------------------
SELECT '20150403-drop-empty-functions.sql';

DROP FUNCTION IF EXISTS empty_artists();
DROP FUNCTION IF EXISTS empty_events();
DROP FUNCTION IF EXISTS empty_labels();
DROP FUNCTION IF EXISTS empty_places();
DROP FUNCTION IF EXISTS empty_release_groups();
DROP FUNCTION IF EXISTS empty_series();
DROP FUNCTION IF EXISTS empty_works();

COMMIT;
