466 lines
12 KiB
PL/PgSQL
466 lines
12 KiB
PL/PgSQL
BEGIN;
|
|
|
|
|
|
-- raw store events
|
|
|
|
CREATE TABLE mock.raw_shop_events (
|
|
platform TEXT NOT NULL,
|
|
store_id TEXT NOT NULL,
|
|
event_timestamp TIMESTAMPTZ NOT NULL,
|
|
event_id TEXT NOT NULL,
|
|
raw_payload JSONB NOT NULL,
|
|
parsed BOOLEAN NOT NULL DEFAULT FALSE,
|
|
|
|
PRIMARY KEY (platform, store_id, event_id, event_timestamp)
|
|
);
|
|
|
|
|
|
-- processed event tables
|
|
|
|
CREATE TABLE mock.shop_amazon_events (
|
|
platform TEXT NOT NULL DEFAULT 'amazon' CHECK (platform = 'amazon'),
|
|
store_id TEXT NOT NULL,
|
|
event_timestamp TIMESTAMPTZ NOT NULL,
|
|
event_id TEXT NOT NULL,
|
|
|
|
PRIMARY KEY (store_id, event_id, event_timestamp),
|
|
FOREIGN KEY (platform, store_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
|
|
);
|
|
|
|
CREATE TABLE mock.shop_big_cartel_events (
|
|
platform TEXT NOT NULL DEFAULT 'big_cartel' CHECK (platform = 'big_cartel'),
|
|
store_id TEXT NOT NULL,
|
|
event_timestamp TIMESTAMPTZ NOT NULL,
|
|
event_id TEXT NOT NULL,
|
|
|
|
PRIMARY KEY (store_id, event_id, event_timestamp),
|
|
FOREIGN KEY (platform, store_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
|
|
);
|
|
|
|
CREATE TABLE mock.shop_ebay_events (
|
|
platform TEXT NOT NULL DEFAULT 'ebay' CHECK (platform = 'ebay'),
|
|
store_id TEXT NOT NULL,
|
|
event_timestamp TIMESTAMPTZ NOT NULL,
|
|
event_id TEXT NOT NULL,
|
|
|
|
PRIMARY KEY (store_id, event_id, event_timestamp),
|
|
FOREIGN KEY (platform, store_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
|
|
);
|
|
|
|
CREATE TABLE mock.shop_ecwid_events (
|
|
platform TEXT NOT NULL DEFAULT 'ecwid' CHECK (platform = 'ecwid'),
|
|
store_id TEXT NOT NULL,
|
|
event_timestamp TIMESTAMPTZ NOT NULL,
|
|
event_id TEXT NOT NULL,
|
|
|
|
PRIMARY KEY (store_id, event_id, event_timestamp),
|
|
FOREIGN KEY (platform, store_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
|
|
);
|
|
|
|
CREATE TABLE mock.shop_etsy_events (
|
|
platform TEXT NOT NULL DEFAULT 'etsy' CHECK (platform = 'etsy'),
|
|
store_id TEXT NOT NULL,
|
|
event_timestamp TIMESTAMPTZ NOT NULL,
|
|
event_id TEXT NOT NULL,
|
|
|
|
PRIMARY KEY (store_id, event_id, event_timestamp),
|
|
FOREIGN KEY (platform, store_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
|
|
);
|
|
|
|
CREATE TABLE mock.shop_shopify_events (
|
|
platform TEXT NOT NULL DEFAULT 'shopify' CHECK (platform = 'shopify'),
|
|
store_id TEXT NOT NULL,
|
|
event_timestamp TIMESTAMPTZ NOT NULL,
|
|
event_id TEXT NOT NULL,
|
|
|
|
PRIMARY KEY (store_id, event_id, event_timestamp),
|
|
FOREIGN KEY (platform, store_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
|
|
);
|
|
|
|
CREATE TABLE mock.shop_square_online_events (
|
|
platform TEXT NOT NULL DEFAULT 'square_online' CHECK (platform = 'square_online'),
|
|
store_id TEXT NOT NULL,
|
|
event_timestamp TIMESTAMPTZ NOT NULL,
|
|
event_id TEXT NOT NULL,
|
|
|
|
PRIMARY KEY (store_id, event_id, event_timestamp),
|
|
FOREIGN KEY (platform, store_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
|
|
);
|
|
|
|
CREATE TABLE mock.shop_squarespace_events (
|
|
platform TEXT NOT NULL DEFAULT 'squarespace' CHECK (platform = 'squarespace'),
|
|
store_id TEXT NOT NULL,
|
|
event_timestamp TIMESTAMPTZ NOT NULL,
|
|
event_id TEXT NOT NULL,
|
|
|
|
PRIMARY KEY (store_id, event_id, event_timestamp),
|
|
FOREIGN KEY (platform, store_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
|
|
);
|
|
|
|
CREATE TABLE mock.shop_tiktok_events (
|
|
platform TEXT NOT NULL DEFAULT 'tiktok' CHECK (platform = 'tiktok'),
|
|
store_id TEXT NOT NULL,
|
|
event_timestamp TIMESTAMPTZ NOT NULL,
|
|
event_id TEXT NOT NULL,
|
|
|
|
PRIMARY KEY (store_id, event_id, event_timestamp),
|
|
FOREIGN KEY (platform, store_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
|
|
);
|
|
|
|
CREATE TABLE mock.shop_walmart_marketplace_events (
|
|
platform TEXT NOT NULL DEFAULT 'walmart_marketplace' CHECK (platform = 'walmart_marketplace'),
|
|
store_id TEXT NOT NULL,
|
|
event_timestamp TIMESTAMPTZ NOT NULL,
|
|
event_id TEXT NOT NULL,
|
|
|
|
PRIMARY KEY (store_id, event_id, event_timestamp),
|
|
FOREIGN KEY (platform, store_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
|
|
);
|
|
|
|
CREATE TABLE mock.shop_wix_events (
|
|
platform TEXT NOT NULL DEFAULT 'wix' CHECK (platform = 'wix'),
|
|
store_id TEXT NOT NULL,
|
|
event_timestamp TIMESTAMPTZ NOT NULL,
|
|
event_id TEXT NOT NULL,
|
|
|
|
PRIMARY KEY (store_id, event_id, event_timestamp),
|
|
FOREIGN KEY (platform, store_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
|
|
);
|
|
|
|
CREATE TABLE mock.shop_woo_commerce_events (
|
|
platform TEXT NOT NULL DEFAULT 'woo_commerce' CHECK (platform = 'woo_commerce'),
|
|
store_id TEXT NOT NULL,
|
|
event_timestamp TIMESTAMPTZ NOT NULL,
|
|
event_id TEXT NOT NULL,
|
|
|
|
PRIMARY KEY (store_id, event_id, event_timestamp),
|
|
FOREIGN KEY (platform, store_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
|
|
);
|
|
|
|
CREATE TABLE mock.shop_zoho_events (
|
|
platform TEXT NOT NULL DEFAULT 'zoho' CHECK (platform = 'zoho'),
|
|
store_id TEXT NOT NULL,
|
|
event_timestamp TIMESTAMPTZ NOT NULL,
|
|
event_id TEXT NOT NULL,
|
|
|
|
PRIMARY KEY (store_id, event_id, event_timestamp),
|
|
FOREIGN KEY (platform, store_id, event_id, event_timestamp) REFERENCES mock.raw_shop_events
|
|
);
|
|
|
|
|
|
--- event processing
|
|
|
|
CREATE OR REPLACE FUNCTION mock.process_raw_amazon_event() RETURNS TRIGGER AS $process_raw_amazon_event$
|
|
BEGIN
|
|
INSERT INTO mock.shop_amazon_events (
|
|
platform,
|
|
store_id,
|
|
event_timestamp,
|
|
event_id
|
|
)
|
|
SELECT
|
|
NEW.platform,
|
|
NEW.store_id,
|
|
NEW.event_timestamp,
|
|
NEW.event_id;
|
|
RETURN NULL;
|
|
END;
|
|
$process_raw_amazon_event$ LANGUAGE plpgsql;
|
|
|
|
CREATE OR REPLACE TRIGGER amazon_store_events
|
|
AFTER INSERT ON mock.raw_shop_events
|
|
FOR EACH ROW
|
|
WHEN (NEW.platform = 'amazon')
|
|
EXECUTE FUNCTION mock.process_raw_amazon_event();
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION mock.process_raw_big_cartel_event() RETURNS TRIGGER AS $process_raw_big_cartel_event$
|
|
BEGIN
|
|
INSERT INTO mock.shop_big_cartel_events (
|
|
platform,
|
|
store_id,
|
|
event_timestamp,
|
|
event_id
|
|
)
|
|
SELECT
|
|
NEW.platform,
|
|
NEW.store_id,
|
|
NEW.event_timestamp,
|
|
NEW.event_id;
|
|
RETURN NULL;
|
|
END;
|
|
$process_raw_big_cartel_event$ LANGUAGE plpgsql;
|
|
|
|
CREATE OR REPLACE TRIGGER big_cartel_store_events
|
|
AFTER INSERT ON mock.raw_shop_events
|
|
FOR EACH ROW
|
|
WHEN (NEW.platform = 'big_cartel')
|
|
EXECUTE FUNCTION mock.process_raw_big_cartel_event();
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION mock.process_raw_ebay_event() RETURNS TRIGGER AS $process_raw_ebay_event$
|
|
BEGIN
|
|
INSERT INTO mock.shop_ebay_events (
|
|
platform,
|
|
store_id,
|
|
event_timestamp,
|
|
event_id
|
|
)
|
|
SELECT
|
|
NEW.platform,
|
|
NEW.store_id,
|
|
NEW.event_timestamp,
|
|
NEW.event_id;
|
|
RETURN NULL;
|
|
END;
|
|
$process_raw_ebay_event$ LANGUAGE plpgsql;
|
|
|
|
CREATE OR REPLACE TRIGGER ebay_store_events
|
|
AFTER INSERT ON mock.raw_shop_events
|
|
FOR EACH ROW
|
|
WHEN (NEW.platform = 'ebay')
|
|
EXECUTE FUNCTION mock.process_raw_ebay_event();
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION mock.process_raw_ecwid_event() RETURNS TRIGGER AS $process_raw_ecwid_event$
|
|
BEGIN
|
|
INSERT INTO mock.shop_ecwid_events (
|
|
platform,
|
|
store_id,
|
|
event_timestamp,
|
|
event_id
|
|
)
|
|
SELECT
|
|
NEW.platform,
|
|
NEW.store_id,
|
|
NEW.event_timestamp,
|
|
NEW.event_id;
|
|
RETURN NULL;
|
|
END;
|
|
$process_raw_ecwid_event$ LANGUAGE plpgsql;
|
|
|
|
CREATE OR REPLACE TRIGGER ecwid_store_events
|
|
AFTER INSERT ON mock.raw_shop_events
|
|
FOR EACH ROW
|
|
WHEN (NEW.platform = 'ecwid')
|
|
EXECUTE FUNCTION mock.process_raw_ecwid_event();
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION mock.process_raw_etsy_event() RETURNS TRIGGER AS $process_raw_etsy_event$
|
|
BEGIN
|
|
INSERT INTO mock.shop_etsy_events (
|
|
platform,
|
|
store_id,
|
|
event_timestamp,
|
|
event_id
|
|
)
|
|
SELECT
|
|
NEW.platform,
|
|
NEW.store_id,
|
|
NEW.event_timestamp,
|
|
NEW.event_id;
|
|
RETURN NULL;
|
|
END;
|
|
$process_raw_etsy_event$ LANGUAGE plpgsql;
|
|
|
|
CREATE OR REPLACE TRIGGER etsy_store_events
|
|
AFTER INSERT ON mock.raw_shop_events
|
|
FOR EACH ROW
|
|
WHEN (NEW.platform = 'etsy')
|
|
EXECUTE FUNCTION mock.process_raw_etsy_event();
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION mock.process_raw_shopify_event() RETURNS TRIGGER AS $process_raw_shopify_event$
|
|
BEGIN
|
|
INSERT INTO mock.shop_shopify_events (
|
|
platform,
|
|
store_id,
|
|
event_timestamp,
|
|
event_id
|
|
)
|
|
SELECT
|
|
NEW.platform,
|
|
NEW.store_id,
|
|
NEW.event_timestamp,
|
|
NEW.event_id;
|
|
RETURN NULL;
|
|
END;
|
|
$process_raw_shopify_event$ LANGUAGE plpgsql;
|
|
|
|
CREATE OR REPLACE TRIGGER shopify_store_events
|
|
AFTER INSERT ON mock.raw_shop_events
|
|
FOR EACH ROW
|
|
WHEN (NEW.platform = 'shopify')
|
|
EXECUTE FUNCTION mock.process_raw_shopify_event();
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION mock.process_raw_square_online_event() RETURNS TRIGGER AS $process_raw_square_online_event$
|
|
BEGIN
|
|
INSERT INTO mock.shop_square_online_events (
|
|
platform,
|
|
store_id,
|
|
event_timestamp,
|
|
event_id
|
|
)
|
|
SELECT
|
|
NEW.platform,
|
|
NEW.store_id,
|
|
NEW.event_timestamp,
|
|
NEW.event_id;
|
|
RETURN NULL;
|
|
END;
|
|
$process_raw_square_online_event$ LANGUAGE plpgsql;
|
|
|
|
CREATE OR REPLACE TRIGGER square_online_store_events
|
|
AFTER INSERT ON mock.raw_shop_events
|
|
FOR EACH ROW
|
|
WHEN (NEW.platform = 'square_online')
|
|
EXECUTE FUNCTION mock.process_raw_square_online_event();
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION mock.process_raw_squarespace_event() RETURNS TRIGGER AS $process_raw_squarespace_event$
|
|
BEGIN
|
|
INSERT INTO mock.shop_squarespace_events (
|
|
platform,
|
|
store_id,
|
|
event_timestamp,
|
|
event_id
|
|
)
|
|
SELECT
|
|
NEW.platform,
|
|
NEW.store_id,
|
|
NEW.event_timestamp,
|
|
NEW.event_id;
|
|
RETURN NULL;
|
|
END;
|
|
$process_raw_squarespace_event$ LANGUAGE plpgsql;
|
|
|
|
CREATE OR REPLACE TRIGGER squarespace_store_events
|
|
AFTER INSERT ON mock.raw_shop_events
|
|
FOR EACH ROW
|
|
WHEN (NEW.platform = 'squarespace')
|
|
EXECUTE FUNCTION mock.process_raw_squarespace_event();
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION mock.process_raw_tiktok_event() RETURNS TRIGGER AS $process_raw_tiktok_event$
|
|
BEGIN
|
|
INSERT INTO mock.shop_tiktok_events (
|
|
platform,
|
|
store_id,
|
|
event_timestamp,
|
|
event_id
|
|
)
|
|
SELECT
|
|
NEW.platform,
|
|
NEW.store_id,
|
|
NEW.event_timestamp,
|
|
NEW.event_id;
|
|
RETURN NULL;
|
|
END;
|
|
$process_raw_tiktok_event$ LANGUAGE plpgsql;
|
|
|
|
CREATE OR REPLACE TRIGGER tiktok_store_events
|
|
AFTER INSERT ON mock.raw_shop_events
|
|
FOR EACH ROW
|
|
WHEN (NEW.platform = 'tiktok')
|
|
EXECUTE FUNCTION mock.process_raw_tiktok_event();
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION mock.process_raw_walmart_marketplace_event() RETURNS TRIGGER AS $process_raw_walmart_marketplace_event$
|
|
BEGIN
|
|
INSERT INTO mock.shop_walmart_marketplace_events (
|
|
platform,
|
|
store_id,
|
|
event_timestamp,
|
|
event_id
|
|
)
|
|
SELECT
|
|
NEW.platform,
|
|
NEW.store_id,
|
|
NEW.event_timestamp,
|
|
NEW.event_id;
|
|
RETURN NULL;
|
|
END;
|
|
$process_raw_walmart_marketplace_event$ LANGUAGE plpgsql;
|
|
|
|
CREATE OR REPLACE TRIGGER walmart_marketplace_store_events
|
|
AFTER INSERT ON mock.raw_shop_events
|
|
FOR EACH ROW
|
|
WHEN (NEW.platform = 'walmart_marketplace')
|
|
EXECUTE FUNCTION mock.process_raw_walmart_marketplace_event();
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION mock.process_raw_wix_event() RETURNS TRIGGER AS $process_raw_wix_event$
|
|
BEGIN
|
|
INSERT INTO mock.shop_wix_events (
|
|
platform,
|
|
store_id,
|
|
event_timestamp,
|
|
event_id
|
|
)
|
|
SELECT
|
|
NEW.platform,
|
|
NEW.store_id,
|
|
NEW.event_timestamp,
|
|
NEW.event_id;
|
|
RETURN NULL;
|
|
END;
|
|
$process_raw_wix_event$ LANGUAGE plpgsql;
|
|
|
|
CREATE OR REPLACE TRIGGER wix_store_events
|
|
AFTER INSERT ON mock.raw_shop_events
|
|
FOR EACH ROW
|
|
WHEN (NEW.platform = 'wix')
|
|
EXECUTE FUNCTION mock.process_raw_wix_event();
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION mock.process_raw_woo_commerce_event() RETURNS TRIGGER AS $process_raw_woo_commerce_event$
|
|
BEGIN
|
|
INSERT INTO mock.shop_woo_commerce_events (
|
|
platform,
|
|
store_id,
|
|
event_timestamp,
|
|
event_id
|
|
)
|
|
SELECT
|
|
NEW.platform,
|
|
NEW.store_id,
|
|
NEW.event_timestamp,
|
|
NEW.event_id;
|
|
RETURN NULL;
|
|
END;
|
|
$process_raw_woo_commerce_event$ LANGUAGE plpgsql;
|
|
|
|
CREATE OR REPLACE TRIGGER woo_commerce_store_events
|
|
AFTER INSERT ON mock.raw_shop_events
|
|
FOR EACH ROW
|
|
WHEN (NEW.platform = 'woo_commerce')
|
|
EXECUTE FUNCTION mock.process_raw_woo_commerce_event();
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION mock.process_raw_zoho_event() RETURNS TRIGGER AS $process_raw_zoho_event$
|
|
BEGIN
|
|
INSERT INTO mock.shop_zoho_events (
|
|
platform,
|
|
store_id,
|
|
event_timestamp,
|
|
event_id
|
|
)
|
|
SELECT
|
|
NEW.platform,
|
|
NEW.store_id,
|
|
NEW.event_timestamp,
|
|
NEW.event_id;
|
|
RETURN NULL;
|
|
END;
|
|
$process_raw_zoho_event$ LANGUAGE plpgsql;
|
|
|
|
CREATE OR REPLACE TRIGGER zoho_store_events
|
|
AFTER INSERT ON mock.raw_shop_events
|
|
FOR EACH ROW
|
|
WHEN (NEW.platform = 'zoho')
|
|
EXECUTE FUNCTION mock.process_raw_zoho_event();
|
|
|
|
|
|
COMMIT;
|