-- +goose Up
-- +goose StatementBegin
CREATE TABLE device_subscriptions(
    id INTEGER PRIMARY KEY,
    device VARCHAR(250) NOT NULL COLLATE NOCASE,
    subreddit VARCHAR(255) NOT NULL COLLATE NOCASE,
    created_at BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
    CONSTRAINT fk_device_subscriptions_device_slug
        FOREIGN KEY (device)
        REFERENCES devices(slug)
        ON DELETE CASCADE,
    CONSTRAINT fk_device_subscriptions_subreddit_name
        FOREIGN KEY (subreddit)
        REFERENCES subreddits(name)
        ON DELETE CASCADE
);

CREATE UNIQUE INDEX
    idx_unique_device_subscriptions_per_device ON
    device_subscriptions(device, subreddit);

CREATE INDEX
    idx_device_subscriptions_subreddit ON
    device_subscriptions(subreddit);

CREATE INDEX
    idx_device_subscriptions_device ON
    device_subscriptions(device);

-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE device_subscriptions;
-- +goose StatementEnd
