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