DEV: Add import script for Yammer (#20074)

Co-authored-by: Jay Pfaffman <jay@literatecomputing.com>
This commit is contained in:
Gerhard Schlager
2023-01-31 10:12:01 +01:00
committed by GitHub
parent 0dbb089b47
commit c3e978ada9
2 changed files with 628 additions and 0 deletions

View File

@ -14,10 +14,13 @@ module ImportScripts
configure_database
create_category_table
create_upload_table
create_like_table
create_user_table
create_topic_table
create_post_table
create_pm_topic_table
create_pm_post_table
end
def insert_category(category)
@ -27,6 +30,15 @@ module ImportScripts
SQL
end
def insert_upload(upload)
@db.execute(<<-SQL, prepare(upload))
INSERT OR REPLACE INTO upload (id, user_id, original_filename,
filename, description, url)
VALUES (:id, :user_id, :original_filename,
:filename, :description, :url)
SQL
end
def insert_user(user)
@db.execute(<<-SQL, prepare(user))
INSERT OR REPLACE
@ -69,6 +81,16 @@ module ImportScripts
end
end
def insert_pm_topic(topic)
attachments = topic.delete(:attachments)
topic[:upload_count] = attachments&.size || 0
@db.execute(<<-SQL, prepare(topic))
INSERT OR REPLACE INTO pm_topic (id, title, raw, category_id, closed, user_id, created_at, url, upload_count, target_users)
VALUES (:id, :title, :raw, :category_id, :closed, :user_id, :created_at, :url, :upload_count, :target_users)
SQL
end
def insert_post(post)
like_user_ids = post.delete(:like_user_ids)
attachments = post.delete(:attachments)
@ -92,6 +114,16 @@ module ImportScripts
end
end
def insert_pm_post(post)
attachments = post.delete(:attachments)
post[:upload_count] = attachments&.size || 0
@db.execute(<<-SQL, prepare(post))
INSERT OR REPLACE INTO pm_post (id, raw, topic_id, user_id, created_at, reply_to_post_id, url, upload_count)
VALUES (:id, :raw, :topic_id, :user_id, :created_at, :reply_to_post_id, :url, :upload_count)
SQL
end
def sort_posts_by_created_at
@db.execute "DELETE FROM post_order"
@ -160,6 +192,13 @@ module ImportScripts
SQL
end
def count_pm_topics
@db.get_first_value(<<-SQL)
SELECT COUNT(*)
FROM pm_topic
SQL
end
def fetch_topics(last_id)
rows = @db.execute(<<-SQL, last_id)
SELECT *
@ -172,6 +211,18 @@ module ImportScripts
add_last_column_value(rows, "id")
end
def fetch_pm_topics(last_id)
rows = @db.execute(<<-SQL, last_id)
SELECT *
FROM pm_topic
WHERE id > :last_id
ORDER BY id
LIMIT #{@batch_size}
SQL
add_last_column_value(rows, "id")
end
def fetch_topic_attachments(topic_id)
@db.execute(<<-SQL, topic_id)
SELECT path
@ -187,6 +238,21 @@ module ImportScripts
SQL
end
def count_pm_posts
@db.get_first_value(<<-SQL)
SELECT COUNT(*)
FROM pm_post
SQL
end
def fetch_upload(id)
@db.execute(<<-SQL, id)
SELECT *
FROM upload
WHERE id = :id
SQL
end
def fetch_posts(last_row_id)
rows = @db.execute(<<-SQL, last_row_id)
SELECT ROWID AS rowid, *
@ -199,6 +265,18 @@ module ImportScripts
add_last_column_value(rows, "rowid")
end
def fetch_pm_posts(last_row_id)
rows = @db.execute(<<-SQL, last_row_id)
SELECT ROWID AS rowid, *
FROM pm_post
WHERE ROWID > :last_row_id
ORDER BY ROWID
LIMIT #{@batch_size}
SQL
add_last_column_value(rows, "rowid")
end
def fetch_sorted_posts(last_row_id)
rows = @db.execute(<<-SQL, last_row_id)
SELECT o.ROWID AS rowid, p.*
@ -270,6 +348,19 @@ module ImportScripts
SQL
end
def create_upload_table
@db.execute <<-SQL
CREATE TABLE IF NOT EXISTS upload (
id #{key_data_type} NOT NULL PRIMARY KEY,
user_id INTEGER,
original_filename TEXT,
filename TEXT,
description TEXT,
url TEXT
)
SQL
end
def create_like_table
@db.execute <<-SQL
CREATE TABLE IF NOT EXISTS like (
@ -325,6 +416,23 @@ module ImportScripts
@db.execute "CREATE UNIQUE INDEX IF NOT EXISTS topic_upload_unique ON topic_upload(topic_id, path)"
end
def create_pm_topic_table
@db.execute <<-SQL
CREATE TABLE IF NOT EXISTS pm_topic (
id #{key_data_type} NOT NULL PRIMARY KEY,
title TEXT,
raw TEXT,
category_id #{key_data_type},
closed BOOLEAN NOT NULL DEFAULT false,
user_id #{key_data_type} NOT NULL,
target_users TEXT,
created_at DATETIME,
url TEXT,
upload_count INTEGER DEFAULT 0
)
SQL
end
def create_post_table
@db.execute <<-SQL
CREATE TABLE IF NOT EXISTS post (
@ -357,6 +465,21 @@ module ImportScripts
@db.execute "CREATE UNIQUE INDEX IF NOT EXISTS post_upload_unique ON post_upload(post_id, path)"
end
def create_pm_post_table
@db.execute <<-SQL
CREATE TABLE IF NOT EXISTS pm_post (
id #{key_data_type} NOT NULL PRIMARY KEY,
raw TEXT,
topic_id #{key_data_type} NOT NULL,
user_id #{key_data_type} NOT NULL,
created_at DATETIME,
reply_to_post_id #{key_data_type},
url TEXT,
upload_count INTEGER DEFAULT 0
)
SQL
end
def prepare(hash)
hash.each do |key, value|
if value.is_a?(TrueClass) || value.is_a?(FalseClass)