CREATE TABLE email_batches (
    id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
    subject VARCHAR(256) NOT NULL,
    body TEXT NOT NULL,
    from_email VARCHAR(64) NOT NULL default 'support@jamkazam.com',

    aasm_state VARCHAR(32) NOT NULL default 'pending',

    test_emails TEXT NOT NULL default 'test@jamkazam.com',

    opt_in_count INTEGER NOT NULL default 0,
    sent_count INTEGER NOT NULL default 0,

    lock_version INTEGER,

    started_at TIMESTAMP,
    completed_at TIMESTAMP,
    
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

ALTER TABLE users ALTER COLUMN subscribe_email SET DEFAULT true;
UPDATE users SET subscribe_email = true WHERE subscribe_email = false;

