From 26b6307630c9ac7d900d570018010458bd3dff52 Mon Sep 17 00:00:00 2001 From: Jerome Velociter Date: Sat, 5 Oct 2024 13:05:10 +0200 Subject: [PATCH] add twitter likes migration script --- package.json | 1 + scripts/migrate-likes.js | 0 scripts/migrate-likes.mjs | 111 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) delete mode 100644 scripts/migrate-likes.js create mode 100644 scripts/migrate-likes.mjs diff --git a/package.json b/package.json index 9bf4a29..dd9e0ed 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "storybook": "start-storybook -p 6006 -s ./public -c .storybook", "build-storybook": "build-storybook", "get-schema": "node scripts/get-schema.js", + "migrate-likes": "node scripts/migrate-likes.mjs", "graphql-codegen": "graphql-codegen", "update-schema": "yarn get-schema && yarn graphql-codegen" }, diff --git a/scripts/migrate-likes.js b/scripts/migrate-likes.js deleted file mode 100644 index e69de29..0000000 diff --git a/scripts/migrate-likes.mjs b/scripts/migrate-likes.mjs new file mode 100644 index 0000000..2b59f4c --- /dev/null +++ b/scripts/migrate-likes.mjs @@ -0,0 +1,111 @@ +import postgres from "postgres"; +import { readFile } from "fs/promises"; +import dotenv from "dotenv"; + +dotenv.config(); + +const { + POSTGRES_HOST, + POSTGRES_USER, + POSTGRES_PASSWORD, + POSTGRES_DATABASE, + LIKES_EXPORT_FILE, +} = process.env; + +const sql = postgres("postgres://jvelo:@localhost:5432/jvelo", { + host: POSTGRES_HOST, + database: POSTGRES_DATABASE, + username: POSTGRES_USER, + password: POSTGRES_PASSWORD, +}); + +const json = JSON.parse(await readFile(LIKES_EXPORT_FILE)); + +await sql` + create schema if not exists web; + drop table if exists web.twitter_like; + + create table web.twitter_like ( + id varchar(20) not null, + created_at timestamp not null, + text text, + truncated boolean default false, + entities jsonb default null, + extended_entities jsonb default null, + is_quote_status boolean default null, + quoted_status jsonb default null, + quoted_status_id bigint default null, + source text, + in_reply_to_status_id bigint default null, + in_reply_to_user_id bigint default null, + in_reply_to_screen_name text, + "user" jsonb default null, + retweet_count integer default null, + favorite_count integer default null, + favorited boolean default null, + retweeted boolean default null, + possibly_sensitive boolean default null, + lang varchar(10) default null, + primary key (id) + ); + + create index twitter_like_created_at_index on web.twitter_like (created_at); + create index twitter_like_lang_index on web.twitter_like (lang); +`.simple(); + +await sql + .begin(async (tx) => { + for (const entry of json) { + await tx` + insert into web.twitter_like ( + id, + created_at, + text, + truncated, + entities, + extended_entities, + is_quote_status, + quoted_status, + quoted_status_id, + source, + in_reply_to_status_id, + in_reply_to_user_id, + in_reply_to_screen_name, + "user", + retweet_count, + favorite_count, + favorited, + retweeted, + possibly_sensitive, + lang + ) values ( + ${entry.id}, + ${entry.created_at}, + ${entry.text}, + ${entry.truncated}, + ${tx.json(entry.entities)}, + ${tx.json(entry.extended_entities)}, + ${entry.is_quote_status}, + ${tx.json(entry.quoted_status)}, + ${entry.quoted_status_id}, + ${entry.source}, + ${entry.in_reply_to_status_id}, + ${entry.in_reply_to_user_id}, + ${entry.in_reply_to_screen_name}, + ${tx.json(entry.user)}, + ${entry.retweet_count}, + ${entry.favorite_count}, + ${entry.favorited}, + ${entry.retweeted}, + ${entry.possibly_sensitive}, + ${entry.lang} + ) + `; + } + }) + .then(() => console.log("insertion complete")) + .catch((error) => + console.error("Transaction failed, no data was inserted:", error) + ); + +await sql.end();