From 94dc7b57967f8e8cff5bf2bf1080cdce5e161408 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Mon, 13 Nov 2023 11:48:25 -0800 Subject: [PATCH] use updated_at for timestamps --- db/migrations/001_create_entities.rb | 2 +- db/migrations/002_pinboard_archive.rb | 8 ++++---- db/migrations/003_create_tags.rb | 12 ++++++------ db/migrations/004_create_urls.rb | 6 +++--- db/migrations/005_create_sources_and_notes.rb | 4 ++-- .../006_extract_pinboard_title_notes.rb | 8 ++++---- lib/alphadex.rb | 19 +++++++++++++++++++ models.rb | 8 ++++++++ 8 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 lib/alphadex.rb diff --git a/db/migrations/001_create_entities.rb b/db/migrations/001_create_entities.rb index 7319ea9..8215b1b 100644 --- a/db/migrations/001_create_entities.rb +++ b/db/migrations/001_create_entities.rb @@ -4,7 +4,7 @@ Sequel.migration do primary_key :id DateTime :created_at, null: false - DateTime :modified_at, null: false + DateTime :updated_at end end end diff --git a/db/migrations/002_pinboard_archive.rb b/db/migrations/002_pinboard_archive.rb index f0a530f..c8e8dbe 100644 --- a/db/migrations/002_pinboard_archive.rb +++ b/db/migrations/002_pinboard_archive.rb @@ -9,15 +9,15 @@ Sequel.migration do String :json, text: true, null: false DateTime :created_at, null: false - DateTime :modified_at, null: false + DateTime :updated_at end File.foreach(pinboard_export).with_index do |line, i| - created_at = modified_at = DateTime.now + created_at = updated_at = DateTime.now json = line.chomp - entity_id = DB[:entities].insert(created_at:, modified_at:) - DB[:pinboard].insert(entity_id:, json: , created_at:, modified_at:) + entity_id = DB[:entities].insert(created_at:, updated_at:) + DB[:pinboard].insert(entity_id:, json: , created_at:, updated_at:) end # Remove duplicate entry diff --git a/db/migrations/003_create_tags.rb b/db/migrations/003_create_tags.rb index 9339850..a3ba0fa 100644 --- a/db/migrations/003_create_tags.rb +++ b/db/migrations/003_create_tags.rb @@ -6,7 +6,7 @@ Sequel.migration do String :name, unique: true, null: false DateTime :created_at, null: false - DateTime :modified_at, null: false + DateTime :updated_at end create_table(:entities_tags) do @@ -15,7 +15,7 @@ Sequel.migration do foreign_key :tag_id, :tags, null: false DateTime :created_at, null: false - DateTime :modified_at, null: false + DateTime :updated_at unique [:entity_id, :tag_id] end @@ -33,11 +33,11 @@ Sequel.migration do tag_names.each do |tag_name| tag_id = DB[:tags].where(name: tag_name).get(:id) - created_at = modified_at = DateTime.now - tag_id ||= DB[:tags].insert(name: tag_name, created_at:, modified_at:) + created_at = updated_at = DateTime.now + tag_id ||= DB[:tags].insert(name: tag_name, created_at:, updated_at:) - created_at = modified_at = DateTime.now - DB[:entities_tags].insert(entity_id:, tag_id:, created_at:, modified_at:) + created_at = updated_at = DateTime.now + DB[:entities_tags].insert(entity_id:, tag_id:, created_at:, updated_at:) end end end diff --git a/db/migrations/004_create_urls.rb b/db/migrations/004_create_urls.rb index 90d01af..abdcc55 100644 --- a/db/migrations/004_create_urls.rb +++ b/db/migrations/004_create_urls.rb @@ -7,7 +7,7 @@ Sequel.migration do String :url, unique: true, null: false DateTime :created_at, null: false - DateTime :modified_at, null: false + DateTime :updated_at end json = Sequel.sqlite_json_op(:json) @@ -18,9 +18,9 @@ Sequel.migration do .each do |pin| entity_id = pin.fetch(:entity_id) url = pin.fetch(:href) - created_at = modified_at = DateTime.now + created_at = updated_at = DateTime.now - DB[:urls].insert(entity_id:, url:, created_at:, modified_at:) + DB[:urls].insert(entity_id:, url:, created_at:, updated_at:) end end diff --git a/db/migrations/005_create_sources_and_notes.rb b/db/migrations/005_create_sources_and_notes.rb index 2fcc8db..d4cbcd1 100644 --- a/db/migrations/005_create_sources_and_notes.rb +++ b/db/migrations/005_create_sources_and_notes.rb @@ -5,7 +5,7 @@ Sequel.migration do foreign_key :source_id, :entities, null: false DateTime :created_at, null: false - DateTime :modified_at, null: false + DateTime :updated_at unique %i[entity_id source_id] end @@ -16,7 +16,7 @@ Sequel.migration do String :text, text: true DateTime :created_at, null: false - DateTime :modified_at, null: false + DateTime :updated_at end end end diff --git a/db/migrations/006_extract_pinboard_title_notes.rb b/db/migrations/006_extract_pinboard_title_notes.rb index fd54206..ac2be47 100644 --- a/db/migrations/006_extract_pinboard_title_notes.rb +++ b/db/migrations/006_extract_pinboard_title_notes.rb @@ -6,7 +6,7 @@ Sequel.migration do String :title DateTime :created_at, null: false - DateTime :modified_at, null: false + DateTime :updated_at end json = Sequel.sqlite_json_op(:json) @@ -19,10 +19,10 @@ Sequel.migration do entity_id = pin.fetch(:entity_id) title = pin.fetch(:description) text = pin.fetch(:extended) - created_at = modified_at = DateTime.now + created_at = updated_at = DateTime.now - DB[:titles].insert(entity_id:, title:, created_at:, modified_at:) - DB[:notes].insert(entity_id:, text:, created_at:, modified_at:) + DB[:titles].insert(entity_id:, title:, created_at:, updated_at:) + DB[:notes].insert(entity_id:, text:, created_at:, updated_at:) end end diff --git a/lib/alphadex.rb b/lib/alphadex.rb new file mode 100644 index 0000000..408a7de --- /dev/null +++ b/lib/alphadex.rb @@ -0,0 +1,19 @@ +require_relative "../models" + +module Alphadex + def self.add_bookmark(url, title, note, tags) + fail "url already exists" unless Url.where(url:).empty? + + DB.transaction do + entity = Entity.create + entity.url = Url.new(url:) + entity.title = Title.new(title:) + entity.add_note(text: note) + tags.map { Tag.find_or_create(name: _1) }.each do |tag| + entity.add_tag(tag) + end + + entity + end + end +end diff --git a/models.rb b/models.rb index 3eb8d72..2f1c217 100644 --- a/models.rb +++ b/models.rb @@ -1,11 +1,19 @@ +require "date" + require_relative "db" +Sequel::Model.plugin :timestamps + class Entity < Sequel::Model one_to_many :notes one_to_many :sources one_to_one :title many_to_many :tags one_to_one :url + + def add_tag(tag) + DB[:entities_tags].insert(entity_id: self.id, tag_id: tag.id, created_at: DateTime.now) + end end class Note < Sequel::Model