Blame scripts/migration/1.11-1.16.3/schema-upgrade-1.15-1.16.sql

Thomas Oulevey 4cda50
-- upgrade script to migrate the Koji database schema
Thomas Oulevey 4cda50
-- from version 1.14 to 1.16
Thomas Oulevey 4cda50
Thomas Oulevey 4cda50
Thomas Oulevey 4cda50
BEGIN;
Thomas Oulevey 4cda50
Thomas Oulevey 4cda50
-- create host_config table
Thomas Oulevey 4cda50
SELECT 'Creating table host_config';
Thomas Oulevey 4cda50
CREATE TABLE host_config (
Thomas Oulevey 4cda50
        host_id INTEGER NOT NULL REFERENCES host(id),
Thomas Oulevey 4cda50
        arches TEXT,
Thomas Oulevey 4cda50
        capacity FLOAT CHECK (capacity > 1) NOT NULL DEFAULT 2.0,
Thomas Oulevey 4cda50
        description TEXT,
Thomas Oulevey 4cda50
        comment TEXT,
Thomas Oulevey 4cda50
        enabled BOOLEAN NOT NULL DEFAULT 'true',
Thomas Oulevey 4cda50
-- versioned - see desc above
Thomas Oulevey 4cda50
        create_event INTEGER NOT NULL REFERENCES events(id) DEFAULT get_event(),
Thomas Oulevey 4cda50
        revoke_event INTEGER REFERENCES events(id),
Thomas Oulevey 4cda50
        creator_id INTEGER NOT NULL REFERENCES users(id),
Thomas Oulevey 4cda50
        revoker_id INTEGER REFERENCES users(id),
Thomas Oulevey 4cda50
        active BOOLEAN DEFAULT 'true' CHECK (active),
Thomas Oulevey 4cda50
        CONSTRAINT active_revoke_sane CHECK (
Thomas Oulevey 4cda50
                (active IS NULL AND revoke_event IS NOT NULL AND revoker_id IS NOT NULL)
Thomas Oulevey 4cda50
                OR (active IS NOT NULL AND revoke_event IS NULL AND revoker_id IS NULL)),
Thomas Oulevey 4cda50
        PRIMARY KEY (create_event, host_id),
Thomas Oulevey 4cda50
        UNIQUE (host_id, active)
Thomas Oulevey 4cda50
) WITHOUT OIDS;
Thomas Oulevey 4cda50
CREATE INDEX host_config_by_active_and_enabled ON host_config(active, enabled);
Thomas Oulevey 4cda50
Thomas Oulevey 4cda50
-- copy starting data
Thomas Oulevey 4cda50
-- CREATE FUNCTION pg_temp.user() returns INTEGER as $$ select id from users where name='nobody' $$ language SQL;
Thomas Oulevey 4cda50
CREATE FUNCTION pg_temp.user() returns INTEGER as $$ select 1 $$ language SQL;
Thomas Oulevey 4cda50
-- If you would like to use an existing user instead, then:
Thomas Oulevey 4cda50
--   1. edit the temporary function to look for the alternate user name
Thomas Oulevey 4cda50
Thomas Oulevey 4cda50
SELECT 'Copying data from host to host_config';
Thomas Oulevey 4cda50
INSERT INTO host_config (host_id, arches, capacity, description, comment, enabled, creator_id)
Thomas Oulevey 4cda50
        SELECT id, arches, capacity, description, comment, enabled, pg_temp.user() FROM host;
Thomas Oulevey 4cda50
Thomas Oulevey 4cda50
-- alter original table
Thomas Oulevey 4cda50
SELECT 'Dropping moved columns';
Thomas Oulevey 4cda50
ALTER TABLE host DROP COLUMN arches;
Thomas Oulevey 4cda50
ALTER TABLE host DROP COLUMN capacity;
Thomas Oulevey 4cda50
ALTER TABLE host DROP COLUMN description;
Thomas Oulevey 4cda50
ALTER TABLE host DROP COLUMN comment;
Thomas Oulevey 4cda50
ALTER TABLE host DROP COLUMN enabled;
Thomas Oulevey 4cda50
Thomas Oulevey 4cda50
-- history for host_channels
Thomas Oulevey 4cda50
SELECT 'Adding versions to host_channels';
Thomas Oulevey 4cda50
ALTER TABLE host_channels ADD COLUMN create_event INTEGER NOT NULL REFERENCES events(id) DEFAULT get_event();
Thomas Oulevey 4cda50
ALTER TABLE host_channels ADD COLUMN revoke_event INTEGER REFERENCES events(id);
Thomas Oulevey 4cda50
-- we need some default for alter table, but drop it after
Thomas Oulevey 4cda50
ALTER TABLE host_channels ADD COLUMN creator_id INTEGER NOT NULL REFERENCES users(id) DEFAULT pg_temp.user();
Thomas Oulevey 4cda50
ALTER TABLE host_channels ALTER COLUMN creator_id DROP DEFAULT;
Thomas Oulevey 4cda50
ALTER TABLE host_channels ADD COLUMN revoker_id INTEGER REFERENCES users(id);
Thomas Oulevey 4cda50
ALTER TABLE host_channels ADD COLUMN active BOOLEAN DEFAULT 'true' CHECK (active);
Thomas Oulevey 4cda50
ALTER TABLE host_channels ADD CONSTRAINT active_revoke_sane CHECK (
Thomas Oulevey 4cda50
                                         (active IS NULL AND revoke_event IS NOT NULL AND revoker_id IS NOT NULL)
Thomas Oulevey 4cda50
                                         OR (active IS NOT NULL AND revoke_event IS NULL AND revoker_id IS NULL));
Thomas Oulevey 4cda50
ALTER TABLE host_channels ADD PRIMARY KEY (create_event, host_id, channel_id);
Thomas Oulevey 4cda50
ALTER TABLE host_channels ADD UNIQUE (host_id, channel_id, active);
Thomas Oulevey 4cda50
-- ALTER TABLE host_channels DROP CONSTRAINT host_channels_host_id_channel_id_key;
Thomas Oulevey 4cda50
Thomas Oulevey 4cda50
COMMIT;