-- IHUB-WP-0019 T03 - first-class VSM hub metadata

ALTER TABLE hubs
    ADD COLUMN hub_family TEXT,
    ADD COLUMN vsm_function TEXT,
    ADD COLUMN vsm_system TEXT;

ALTER TABLE hubs
    ADD CONSTRAINT hubs_vsm_metadata_consistency CHECK (
        (hub_family IS NULL AND vsm_function IS NULL AND vsm_system IS NULL)
        OR (
            hub_family = 'vsm'
            AND vsm_function IS NOT NULL
            AND vsm_function <> ''
            AND vsm_system IN ('1', '2', '3', '3*', '4', '5', 'environment')
        )
    );

CREATE INDEX hubs_hub_family_idx ON hubs (hub_family);
CREATE INDEX hubs_vsm_system_idx ON hubs (vsm_system)
    WHERE vsm_system IS NOT NULL;
