use updated_at for timestamps

main
Alpha Chen 1 year ago
parent fbf67891ed
commit 94dc7b5796
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -4,7 +4,7 @@ Sequel.migration do
primary_key :id primary_key :id
DateTime :created_at, null: false DateTime :created_at, null: false
DateTime :modified_at, null: false DateTime :updated_at
end end
end end
end end

@ -9,15 +9,15 @@ Sequel.migration do
String :json, text: true, null: false String :json, text: true, null: false
DateTime :created_at, null: false DateTime :created_at, null: false
DateTime :modified_at, null: false DateTime :updated_at
end end
File.foreach(pinboard_export).with_index do |line, i| File.foreach(pinboard_export).with_index do |line, i|
created_at = modified_at = DateTime.now created_at = updated_at = DateTime.now
json = line.chomp json = line.chomp
entity_id = DB[:entities].insert(created_at:, modified_at:) entity_id = DB[:entities].insert(created_at:, updated_at:)
DB[:pinboard].insert(entity_id:, json: , created_at:, modified_at:) DB[:pinboard].insert(entity_id:, json: , created_at:, updated_at:)
end end
# Remove duplicate entry # Remove duplicate entry

@ -6,7 +6,7 @@ Sequel.migration do
String :name, unique: true, null: false String :name, unique: true, null: false
DateTime :created_at, null: false DateTime :created_at, null: false
DateTime :modified_at, null: false DateTime :updated_at
end end
create_table(:entities_tags) do create_table(:entities_tags) do
@ -15,7 +15,7 @@ Sequel.migration do
foreign_key :tag_id, :tags, null: false foreign_key :tag_id, :tags, null: false
DateTime :created_at, null: false DateTime :created_at, null: false
DateTime :modified_at, null: false DateTime :updated_at
unique [:entity_id, :tag_id] unique [:entity_id, :tag_id]
end end
@ -33,11 +33,11 @@ Sequel.migration do
tag_names.each do |tag_name| tag_names.each do |tag_name|
tag_id = DB[:tags].where(name: tag_name).get(:id) tag_id = DB[:tags].where(name: tag_name).get(:id)
created_at = modified_at = DateTime.now created_at = updated_at = DateTime.now
tag_id ||= DB[:tags].insert(name: tag_name, created_at:, modified_at:) tag_id ||= DB[:tags].insert(name: tag_name, created_at:, updated_at:)
created_at = modified_at = DateTime.now created_at = updated_at = DateTime.now
DB[:entities_tags].insert(entity_id:, tag_id:, created_at:, modified_at:) DB[:entities_tags].insert(entity_id:, tag_id:, created_at:, updated_at:)
end end
end end
end end

@ -7,7 +7,7 @@ Sequel.migration do
String :url, unique: true, null: false String :url, unique: true, null: false
DateTime :created_at, null: false DateTime :created_at, null: false
DateTime :modified_at, null: false DateTime :updated_at
end end
json = Sequel.sqlite_json_op(:json) json = Sequel.sqlite_json_op(:json)
@ -18,9 +18,9 @@ Sequel.migration do
.each do |pin| .each do |pin|
entity_id = pin.fetch(:entity_id) entity_id = pin.fetch(:entity_id)
url = pin.fetch(:href) 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
end end

@ -5,7 +5,7 @@ Sequel.migration do
foreign_key :source_id, :entities, null: false foreign_key :source_id, :entities, null: false
DateTime :created_at, null: false DateTime :created_at, null: false
DateTime :modified_at, null: false DateTime :updated_at
unique %i[entity_id source_id] unique %i[entity_id source_id]
end end
@ -16,7 +16,7 @@ Sequel.migration do
String :text, text: true String :text, text: true
DateTime :created_at, null: false DateTime :created_at, null: false
DateTime :modified_at, null: false DateTime :updated_at
end end
end end
end end

@ -6,7 +6,7 @@ Sequel.migration do
String :title String :title
DateTime :created_at, null: false DateTime :created_at, null: false
DateTime :modified_at, null: false DateTime :updated_at
end end
json = Sequel.sqlite_json_op(:json) json = Sequel.sqlite_json_op(:json)
@ -19,10 +19,10 @@ Sequel.migration do
entity_id = pin.fetch(:entity_id) entity_id = pin.fetch(:entity_id)
title = pin.fetch(:description) title = pin.fetch(:description)
text = pin.fetch(:extended) 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[:titles].insert(entity_id:, title:, created_at:, updated_at:)
DB[:notes].insert(entity_id:, text:, created_at:, modified_at:) DB[:notes].insert(entity_id:, text:, created_at:, updated_at:)
end end
end end

@ -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

@ -1,11 +1,19 @@
require "date"
require_relative "db" require_relative "db"
Sequel::Model.plugin :timestamps
class Entity < Sequel::Model class Entity < Sequel::Model
one_to_many :notes one_to_many :notes
one_to_many :sources one_to_many :sources
one_to_one :title one_to_one :title
many_to_many :tags many_to_many :tags
one_to_one :url 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 end
class Note < Sequel::Model class Note < Sequel::Model

Loading…
Cancel
Save