parent
9d5d6dd057
commit
69193b862d
@ -0,0 +1,48 @@
|
||||
Sequel.migration do
|
||||
up do
|
||||
create_table(:tags) do
|
||||
primary_key :id
|
||||
|
||||
String :name, unique: true, null: false
|
||||
|
||||
DateTime :created_at, null: false
|
||||
DateTime :modified_at, null: false
|
||||
end
|
||||
|
||||
create_table(:entities_tags) do
|
||||
foreign_key :entity_id, :entities
|
||||
foreign_key :tag_id, :tags
|
||||
|
||||
DateTime :created_at, null: false
|
||||
DateTime :modified_at, null: false
|
||||
|
||||
unique [:entity_id, :tag_id]
|
||||
end
|
||||
|
||||
json = Sequel.sqlite_json_op(:json)
|
||||
tags = json.extract("$.tags")
|
||||
|
||||
DB[:pinboard]
|
||||
.select_append(tags.as(:tags))
|
||||
.exclude(tags => "")
|
||||
.each do |pin|
|
||||
entity_id = pin.fetch(:entity_id)
|
||||
tag_names = pin.fetch(:tags).split(/\s+/)
|
||||
|
||||
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 = modified_at = DateTime.now
|
||||
DB[:entities_tags].insert(entity_id:, tag_id:, created_at:, modified_at:)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
down do
|
||||
drop_table(:tags)
|
||||
drop_table(:entities_tags)
|
||||
end
|
||||
end
|
Loading…
Reference in new issue