DROP VIEW IF EXISTS v_order_emails;
CREATE VIEW v_order_emails AS
select
    orders.id order_id,
    orders.inserted::date order_date,
    billing.email billing_email,
    billing.first_name billing_firstname,
    billing.last_name billing_lastname
from
    orders
    left join addresses billing on billing_address_id = billing.id
where
    orders.paid = 1
;

-- If we want to read this with direct access from BigQuery to Postgres:
-- CREATE USER bigquery WITH PASSWORD 'secret';
-- GRANT SELECT ON v_order_emails TO bigquery;
-- GRANT USAGE ON SCHEMA public TO bigquery;
-- We don't need this now as we will access the view ourselves instead and get the results to BigQuery via S3.
