-- 2024-10-25

/**
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 * FOREIGN TABLES CREATION
 *
 *
 *
 *   - [x] external_b2_files
 *   - [x] external_mux_assets
 *   - [x] external_streams
 *   - [x] external_streams_vtuber_links
 *   - [x] external_tag_vod_relations
 *   - [x] external_tag_vod_relations_tag_links
 *   - [x] external_tag_vod_relations_vod_links
 *   - [x] external_tags
 *   - [x] external_tags_toy_links
 *   - [x] external_tags_vods_links
 *   - [x] external_timestamps
 *   - [x] external_timestamps_tag_links
 *   - [x] external_timestamps_vod_links
 *   - [x] external_toys
 *   - [x] external_toys_link_tag_links
 *   - [x] external_vods
 *   - [x] external_vods_mux_asset_links
 *   - [x] external_vods_stream_links
 *   - [x] external_vods_video_src_b_2_links
 *   - [x] external_vods_vtuber_links
 *   - [x] external_vods_thumbnail_links
 *   - [x] external_vods_uploader_links
 *   - [x] external_vtubers
 *   - [x] external_vtubers_toy_links
 *   - [x] external_vtubers_toys_links
 *
 *
 * 
 */

SET search_path TO 'public';

CREATE FOREIGN TABLE external_b2_files
(
    id INT,
    url CHARACTER VARYING(255),
    key CHARACTER VARYING(255),
    upload_id CHARACTER VARYING(255),
    created_at TIMESTAMP(6) WITHOUT TIME ZONE,
    updated_at TIMESTAMP(6) WITHOUT TIME ZONE,
    created_by_id INT,
    updated_by_id INT,
    cdn_url CHARACTER VARYING(255)
)
SERVER futureporn_old
OPTIONS (table_name 'b2_files');



CREATE FOREIGN TABLE external_mux_assets
(
    id INT,
    playback_id CHARACTER VARYING(255),
    asset_id CHARACTER VARYING(255),
    created_at TIMESTAMP(6) WITHOUT TIME ZONE,
    updated_at TIMESTAMP(6) WITHOUT TIME ZONE,
    created_by_id INT,
    updated_by_id INT
)
SERVER futureporn_old
OPTIONS (table_name 'mux_assets');



CREATE FOREIGN TABLE external_streams
(
	id INT,
    date_str CHARACTER VARYING(255),
	date TIMESTAMP WITHOUT TIME ZONE,
    created_by_id INT,
    updated_by_id INT,
    created_at TIMESTAMP WITHOUT TIME ZONE,
    updated_at TIMESTAMP WITHOUT TIME ZONE,
    cuid CHARACTER VARYING(255),
    date_2 CHARACTER VARYING(255),
    archive_status CHARACTER VARYING(255),
    is_chaturbate_stream BOOLEAN,
    is_fansly_stream BOOLEAN
)
SERVER futureporn_old
OPTIONS (table_name 'streams');



CREATE FOREIGN TABLE external_streams_vtuber_links
(
    id INT,
	stream_id INT,
	vtuber_id INT,
    stream_order DOUBLE PRECISION
)
SERVER futureporn_old
OPTIONS (table_name 'streams_vtuber_links');



CREATE FOREIGN TABLE external_tag_vod_relations
(
    id INT,
    votes INT,
    creator_id INT,
    created_at TIMESTAMP(6) WITHOUT TIME ZONE,
    updated_at TIMESTAMP(6) WITHOUT TIME ZONE,
    created_by_id INT,
    updated_by_id INT
)
SERVER futureporn_old
OPTIONS (table_name 'tag_vod_relations');



CREATE FOREIGN TABLE external_tag_vod_relations_tag_links
(
    id INT,
    tag_vod_relation_id INT,
    tag_id INT
)
SERVER futureporn_old
OPTIONS (table_name 'tag_vod_relations_tag_links');



CREATE FOREIGN TABLE external_tag_vod_relations_vod_links
(
    id INT,
    tag_vod_relation_id INT,
    vod_id INT,
    tag_vod_relation_order DOUBLE PRECISION
)
SERVER futureporn_old
OPTIONS (table_name 'tag_vod_relations_vod_links');



CREATE FOREIGN TABLE external_tags
(
    id INT,
    name CHARACTER VARYING(255),
    created_at TIMESTAMP WITHOUT TIME ZONE,
    updated_at TIMESTAMP WITHOUT TIME ZONE,
    published_at TIMESTAMP WITHOUT TIME ZONE,
    created_by_id INT,
    updated_by_id INT
)
SERVER futureporn_old
OPTIONS (table_name 'tags');


CREATE FOREIGN TABLE external_tags_toy_links
(
    id INT,
    tag_id INT,
    toy_id INT,
    tag_order DOUBLE PRECISION
)
SERVER futureporn_old
OPTIONS (table_name 'tags_toy_links');



CREATE FOREIGN TABLE external_tags_vods_links
(
    id INT,
    tag_id INT,
    vod_id INT,
    tag_order DOUBLE PRECISION,
    vod_order DOUBLE PRECISION
)
SERVER futureporn_old
OPTIONS (table_name 'tags_vods_links');



CREATE FOREIGN TABLE external_timestamps
(
    id INT,
    time INT,
    creator_id INT,
    created_at TIMESTAMP(6) WITHOUT TIME ZONE,
    updated_at TIMESTAMP(6) WITHOUT TIME ZONE,
    created_by_id INT,
    updated_by_id INT
)
SERVER futureporn_old
OPTIONS (table_name 'timestamps');



CREATE FOREIGN TABLE external_timestamps_tag_links
(
    id INT,
    timestamp_id INT,
    tag_id INT
)
SERVER futureporn_old
OPTIONS (table_name 'timestamps_tag_links');



CREATE FOREIGN TABLE external_timestamps_vod_links
(
    id INT,
    timestamp_id INT,
    vod_id INT,
    timestamp_order DOUBLE PRECISION
)
SERVER futureporn_old
OPTIONS (table_name 'timestamps_vod_links');


CREATE FOREIGN TABLE external_toys
(
    id INT,
    make CHARACTER VARYING(255),
    model CHARACTER VARYING(255),
    created_at TIMESTAMP WITHOUT TIME ZONE,
    updated_at TIMESTAMP WITHOUT TIME ZONE,
    created_by_id INT,
    updated_by_id INT,
    image_2 CHARACTER VARYING(255)
)
SERVER futureporn_old
OPTIONS (table_name 'toys');



CREATE FOREIGN TABLE external_toys_link_tag_links
(
    id INT,
    toy_id INT,
    tag_id INT
)
SERVER futureporn_old
OPTIONS (table_name 'toys_link_tag_links');






CREATE FOREIGN TABLE external_vods
(
    id integer,
    created_at timestamp without time zone,
    updated_at timestamp without time zone,
    published_at date,
    title text,
    date date NOT NULL,
    date_2 timestamp without time zone,
    mux_asset INT,
    thumbnail INT,
    vtuber INT,
    ipfs_cid text,
	video_src_hash CHARACTER VARYING(255), 
    s3_file INT,
    torrent text,
    announce_title text,
    announce_url text,
    note text,
    url text,
    discord_message_id text,
    status text COLLATE pg_catalog."default" DEFAULT 'pending_recording'::text,
    recording_id INT,
    stream_id INT
)
SERVER futureporn_old
OPTIONS (table_name 'vods');



CREATE FOREIGN TABLE external_vods_mux_asset_links
(
    id INT,
    vod_id INT,
    mux_asset_id INT
)
SERVER futureporn_old
OPTIONS (table_name 'vods_mux_asset_links');





CREATE FOREIGN TABLE external_vods_stream_links
(
    id INT,
    vod_id INT,
    stream_id INT,
    vod_order DOUBLE PRECISION
)
SERVER futureporn_old
OPTIONS (table_name 'vods_stream_links');


CREATE FOREIGN TABLE external_vods_vtuber_links
(
    id INT,
    vod_id INT,
    vtuber_id INT,
    vod_order DOUBLE PRECISION
)
SERVER futureporn_old
OPTIONS (table_name 'vods_vtuber_links');



CREATE FOREIGN TABLE external_vods_thumbnail_links
(
    id INT,
    vod_id INT,
    b_2_file_id INT
)
SERVER futureporn_old
OPTIONS (table_name 'vods_thumbnail_links');


CREATE FOREIGN TABLE external_vods_uploader_links
(
    id INT,
    vod_id INT,
    user_id INT
)
SERVER futureporn_old
OPTIONS (table_name 'vods_uploader_links');



CREATE FOREIGN TABLE external_vods_video_src_b_2_links
(
    id INT,
    vod_id INT,
    b_2_file_id INT
)
SERVER futureporn_old
OPTIONS (table_name 'vods_video_src_b_2_links');



CREATE FOREIGN TABLE external_vtubers
(
    id INT,
    chaturbate CHARACTER VARYING(255),
    twitter CHARACTER VARYING(255),
    patreon CHARACTER VARYING(255),
    twitch CHARACTER VARYING(255),
    tiktok CHARACTER VARYING(255),
    onlyfans CHARACTER VARYING(255),
    youtube CHARACTER VARYING(255),
    linktree CHARACTER VARYING(255),
    carrd CHARACTER VARYING(255),
    fansly CHARACTER VARYING(255),
    pornhub CHARACTER VARYING(255),
    discord CHARACTER VARYING(255),
    reddit CHARACTER VARYING(255),
    throne CHARACTER VARYING(255),
    instagram CHARACTER VARYING(255),
    facebook CHARACTER VARYING(255),
    merch CHARACTER VARYING(255),
    slug CHARACTER VARYING(255),
    image CHARACTER VARYING(255),
    description_1 TEXT,
    description_2 TEXT,
    created_at TIMESTAMP(6) WITHOUT TIME ZONE,
    updated_at TIMESTAMP(6) WITHOUT TIME ZONE,
    published_at TIMESTAMP(6) WITHOUT TIME ZONE,
    created_by_id INT,
    updated_by_id INT,
    theme_color CHARACTER VARYING(255),
    display_name CHARACTER VARYING(255),
    image_blur CHARACTER VARYING(255)
)
SERVER futureporn_old
OPTIONS (table_name 'vtubers');


CREATE FOREIGN TABLE external_vtubers_toy_links
(
    id INT,
    vtuber_id INT,
    toy_id INT,
    vtuber_order DOUBLE PRECISION
)
SERVER futureporn_old
OPTIONS (table_name 'vtubers_toy_links');



CREATE FOREIGN TABLE external_vtubers_toys_links
(
    id INT,
    vtuber_id INT,
    toy_id INT,
    toy_order DOUBLE PRECISION
)
SERVER futureporn_old
OPTIONS (table_name 'vtubers_toys_links');



/**
 *
 *
 *
 *
 *
 *
 *
 * Migrations
 *
 *
 *
 *
 * The tables we need to copy are as follows. 
 * Note: this is not alphabetical on purpose, because some tables depend on others.
 *
 *   - [x] vtubers
 *   - [x] tags
 *   - [x] b2_files
 *   - [x] mux_assets
 *   - [x] streams
 *   - [x] streams_vtuber_links
 *   - [x] vods
 *   - [x] toys
 *   - [x] tag_vod_relations
 *   - [x] tag_vod_relations_tag_links
 *   - [x] tag_vod_relations_vod_links
 *   - [x] tags_toy_links
 *   - [x] tags_vods_links
 *   - [x] timestamps
 *   - [x] timestamps_tag_links
 *   - [x] timestamps_vod_links
 *   - [x] toys_link_tag_links
 *   - [x] vods_mux_asset_links
 *   - [x] vods_stream_links
 *   - [x] vods_thumbnail_links
 *   - [x] vods_uploader_links
 *   - [x] vods_video_src_b_2_links
 *   - [x] vods_vtuber_links
 *   - [x] vtubers_toy_links
 *   - [x] vtubers_toys_links
 *
 *
 *
 */


INSERT INTO api.vtubers
(
    id,
    display_name,
    chaturbate,
    twitter,
    patreon,
    twitch,
    tiktok,
    onlyfans,
    youtube,
    linktree,
    carrd,
    fansly,
    pornhub,
    discord,
    reddit,
    throne,
    instagram,
    facebook,
    merch,
    slug,
    description_1,
    description_2,
    image,
    theme_color,
    image_blur,
    created_at,
    updated_at
)
OVERRIDING SYSTEM VALUE
SELECT
    vtubers.id,
    display_name,
    chaturbate,
    twitter,
    patreon,
    twitch,
    tiktok,
    onlyfans,
    youtube,
    linktree,
    carrd,
    fansly,
    pornhub,
    discord,
    reddit,
    throne,
    instagram,
    facebook,
    merch,
    slug,
    description_1,
    description_2,
    image,
    theme_color,
    image_blur,
    created_at,
    updated_at
FROM public.external_vtubers AS vtubers;






INSERT INTO api.b2_files (
	id,
    url,
    key,
	upload_id,
	created_at,
	updated_at,
    created_by_id,
    updated_by_id,
	cdn_url
)
OVERRIDING SYSTEM VALUE
SELECT 
	id,
    url,
	key,
	upload_id,
	created_at,
	updated_at,
    created_by_id,
    updated_by_id,
	cdn_url
FROM public.external_b2_files;


INSERT INTO api.mux_assets (
	id,
	asset_id,
	playback_id,
	created_at,
	updated_at
)
OVERRIDING SYSTEM VALUE
SELECT 
	id,
	asset_id,
	playback_id,
	created_at,
	updated_at
FROM public.external_mux_assets;



INSERT INTO api.streams (
    id,
    platform_notification_type,
    date,
    date_2,
    created_at,
    updated_at,
    vtuber_num,
    archive_status,
    is_chaturbate_stream,
    is_fansly_stream
)
OVERRIDING SYSTEM VALUE
SELECT 
    streams.id,
    NULL AS platform_notification_type, -- Modify if necessary
    streams.date_2::TIMESTAMP WITH TIME ZONE AS date,
    streams.date_2::TIMESTAMP WITH TIME ZONE AS date_2,
    streams.created_at,
    streams.updated_at,
    links.vtuber_id AS vtuber_num,
    streams.archive_status,
    streams.is_chaturbate_stream,
    streams.is_fansly_stream
FROM public.external_streams AS streams
LEFT JOIN public.external_streams_vtuber_links AS links
    ON streams.id = links.stream_id;



INSERT INTO api.streams_vtuber_links (
    id,
	stream_id,
	vtuber_id,
    stream_order
)
OVERRIDING SYSTEM VALUE
SELECT
    id,
    stream_id,
    vtuber_id,
    stream_order
FROM public.external_streams_vtuber_links;



INSERT INTO api.vods (
    id,
    created_at,
    updated_at,
    published_at,
    title, 
    date,
    date_2,
    note, 
    ipfs_cid,
    announce_title, 
    announce_url, 
    status
)
OVERRIDING SYSTEM VALUE
SELECT
    vods.id,
    vods.created_at,
    vods.updated_at,
    vods.published_at,
    vods.title,
    vods.date::TIMESTAMP WITH TIME ZONE,
    vods.date_2::TIMESTAMP WITH TIME ZONE AS date_2,
    vods.note,
    vods.video_src_hash AS ipfs_cid,
    vods.announce_title,
    vods.announce_url,
    'pending_recording'
FROM public.external_vods AS vods;



INSERT INTO api.toys (
    id,
    make,
    model,
    image,
    created_at,
    updated_at
)
OVERRIDING SYSTEM VALUE
SELECT
    toys.id,
    toys.make,
    toys.model,
    toys.image_2 AS image,
    toys.created_at,
    toys.updated_at
FROM public.external_toys AS toys;



INSERT INTO api.tags (
    id,
    name,
	created_at,
	updated_at,
	created_by_id,
	updated_by_id
)
OVERRIDING SYSTEM VALUE
SELECT
    id,
    name,
    created_at,
    updated_at,
    created_by_id,
    updated_by_id
FROM public.external_tags;



INSERT INTO api.tag_vod_relations (
    id,
    votes,
    creator_id,
    created_at,
    updated_at,
    created_by_id,
    updated_by_id
)
SELECT
    id,
    votes,
    creator_id,
    created_at,
    updated_at,
    created_by_id,
    updated_by_id
FROM public.external_tag_vod_relations;



INSERT INTO api.tag_vod_relations_tag_links (
    id,
    tag_vod_relation_id,
    tag_id
)
SELECT
    id,
    tag_vod_relation_id,
    tag_id
FROM public.external_tag_vod_relations_tag_links;



INSERT INTO api.tag_vod_relations_vod_links
(
    id,
    tag_vod_relation_id,
    vod_id,
    tag_vod_relation_order
)
SELECT
    id,
    tag_vod_relation_id,
    vod_id,
    tag_vod_relation_order
FROM public.external_tag_vod_relations_vod_links;



INSERT INTO api.tags_toy_links
(
    id,
    tag_id,
    toy_id,
    tag_order
)
SELECT
    id,
    tag_id,
    toy_id,
    tag_order
FROM public.external_tags_toy_links;



INSERT INTO api.tags_vods_links
(
    id,
    tag_id,
    vod_id,
    tag_order,
    vod_order
)
SELECT
    id,
    tag_id,
    vod_id,
    tag_order,
    vod_order
FROM public.external_tags_vods_links;



INSERT INTO api.timestamps
(
    id,
    time,
    creator_id,
    created_at,
    updated_at,
    created_by_id,
    updated_by_id
)
SELECT
    id,
    time,
    creator_id,
    created_at,
    updated_at,
    created_by_id,
    updated_by_id
FROM public.external_timestamps;



INSERT INTO api.timestamps_tag_links
(
    id,
    timestamp_id,
    tag_id
)
SELECT
    id,
    timestamp_id,
    tag_id
FROM public.external_timestamps_tag_links;



INSERT INTO api.timestamps_vod_links
(
    id,
    timestamp_id,
    vod_id,
    timestamp_order
)
SELECT
    id,
    timestamp_id,
    vod_id,
    timestamp_order
FROM public.external_timestamps_vod_links;



INSERT INTO api.toys_link_tag_links
(
    id,
    toy_id,
    tag_id
)
SELECT
    id,
    toy_id,
    tag_id
FROM public.external_toys_link_tag_links;



INSERT INTO api.vods_mux_asset_links (
    id,
    vod_id,
	mux_asset_id
)
SELECT
    vmal.id,
    vmal.vod_id,
    vmal.mux_asset_id
FROM public.external_vods_mux_asset_links AS vmal;



INSERT INTO api.vods_stream_links (
    id,
    vod_id,
    stream_id,
    vod_order
)
SELECT
    vsl.id,
    vsl.vod_id,
    vsl.stream_id,
    vsl.vod_order
FROM public.external_vods_stream_links AS vsl;



INSERT INTO api.vods_thumbnail_links (
    id,
    vod_id,
	b_2_file_id
)
SELECT
    vtl.id,
    vtl.vod_id,
    vtl.b_2_file_id
FROM public.external_vods_thumbnail_links AS vtl;



INSERT INTO api.vods_uploader_links (
    id,
    vod_id,
	user_id
)
SELECT
    vul.id,
    vul.vod_id,
    vul.user_id
FROM public.external_vods_uploader_links AS vul;



INSERT INTO api.vods_video_src_b_2_links (
    id,
    vod_id,
	b_2_file_id
)
SELECT
    v.id,
    v.vod_id,
    v.b_2_file_id
FROM public.external_vods_video_src_b_2_links AS v;



INSERT INTO api.vods_vtuber_links (
    id,
    vod_id,
	vtuber_id,
    vod_order
)
SELECT
    v.id,
    v.vod_id,
    v.vtuber_id,
    v.vod_order
FROM public.external_vods_vtuber_links AS v;



INSERT INTO api.vtubers_toy_links
(
    id,
    vtuber_id,
    toy_id,
    vtuber_order
)
SELECT
    id,
    vtuber_id,
    toy_id,
    vtuber_order
FROM public.external_vtubers_toy_links;


INSERT INTO api.vtubers_toys_links
(
    id,
    vtuber_id,
    toy_id,
    toy_order
)
SELECT
    id,
    vtuber_id,
    toy_id,
    toy_order
FROM public.external_vtubers_toys_links;






























