From 9d5d6dd057cf957e29b012e8806a6d0d87de27b5 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sat, 11 Nov 2023 20:26:33 -0800 Subject: [PATCH] import pinboard archive --- db.rb | 5 +++++ db/migrations/001_create_entities.rb | 4 ++-- db/migrations/002_pinboard_archive.rb | 13 +++++++++++++ .../003_enforce_unique_entities_on_pinboard.rb | 7 +++++++ scripts/20231111_archive_pinboard.rb | 15 +++++++++++++++ 5 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 db.rb create mode 100644 db/migrations/002_pinboard_archive.rb create mode 100644 db/migrations/003_enforce_unique_entities_on_pinboard.rb create mode 100644 scripts/20231111_archive_pinboard.rb diff --git a/db.rb b/db.rb new file mode 100644 index 0000000..c6f3a6c --- /dev/null +++ b/db.rb @@ -0,0 +1,5 @@ +require "logger" + +require "sequel" + +DB = Sequel.connect(ENV.fetch("DATABASE_URL"), loggers: [Logger.new($stdout)]) diff --git a/db/migrations/001_create_entities.rb b/db/migrations/001_create_entities.rb index d93bb2e..7319ea9 100644 --- a/db/migrations/001_create_entities.rb +++ b/db/migrations/001_create_entities.rb @@ -3,8 +3,8 @@ Sequel.migration do create_table(:entities) do primary_key :id - DateTime :created_at - DateTime :modified_at + DateTime :created_at, null: false + DateTime :modified_at, null: false end end end diff --git a/db/migrations/002_pinboard_archive.rb b/db/migrations/002_pinboard_archive.rb new file mode 100644 index 0000000..1b366ea --- /dev/null +++ b/db/migrations/002_pinboard_archive.rb @@ -0,0 +1,13 @@ +Sequel.migration do + change do + create_table(:pinboard) do + primary_key :id + foreign_key :entity_id, :entities + + String :json, text: true, null: false + + DateTime :created_at, null: false + DateTime :modified_at, null: false + end + end +end diff --git a/db/migrations/003_enforce_unique_entities_on_pinboard.rb b/db/migrations/003_enforce_unique_entities_on_pinboard.rb new file mode 100644 index 0000000..d1c3a13 --- /dev/null +++ b/db/migrations/003_enforce_unique_entities_on_pinboard.rb @@ -0,0 +1,7 @@ +Sequel.migration do + change do + alter_table(:pinboard) do + add_unique_constraint(:entity_id) + end + end +end diff --git a/scripts/20231111_archive_pinboard.rb b/scripts/20231111_archive_pinboard.rb new file mode 100644 index 0000000..512dde4 --- /dev/null +++ b/scripts/20231111_archive_pinboard.rb @@ -0,0 +1,15 @@ +require "json" + +require_relative "../db" + +entities = DB[:entities] +pinboard = DB[:pinboard] + +path = File.expand_path("~/Downloads/pinboard_export.2023.11.11_23.05.json_nd") +File.foreach(path).with_index do |line, i| + created_at = modified_at = DateTime.now + json = line.chomp + + entity_id = entities.insert(created_at:, modified_at:) + pinboard.insert(entity_id:, json: , created_at:, modified_at:) +end