This commit is contained in:
2026-01-21 03:10:48 +09:00
parent 86209dcc84
commit bbdcaa9ba3
5 changed files with 93 additions and 3 deletions
+2
View File
@@ -1,4 +1,6 @@
class PostSimilarity < ApplicationRecord class PostSimilarity < ApplicationRecord
self.primary_key = :post_id, :target_post_id
belongs_to :post, class_name: 'Post', foreign_key: 'post_id' belongs_to :post, class_name: 'Post', foreign_key: 'post_id'
belongs_to :target_post, class_name: 'Post', foreign_key: 'target_post_id' belongs_to :target_post, class_name: 'Post', foreign_key: 'target_post_id'
end end
+2
View File
@@ -1,4 +1,6 @@
class TagSimilarity < ApplicationRecord class TagSimilarity < ApplicationRecord
self.primary_key = :tag_id, :target_tag_id
belongs_to :tag, class_name: 'Tag', foreign_key: 'tag_id' belongs_to :tag, class_name: 'Tag', foreign_key: 'tag_id'
belongs_to :target_tag, class_name: 'Tag', foreign_key: 'target_tag_id' belongs_to :target_tag, class_name: 'Tag', foreign_key: 'target_tag_id'
end end
@@ -0,0 +1,43 @@
class ChangePostSimilaritiesToCompositePk < ActiveRecord::Migration[8.0]
def up
execute <<~SQL
ALTER TABLE
post_similarities
MODIFY COLUMN id BIGINT NOT NULL
;
SQL
execute <<~SQL
ALTER TABLE
post_similarities
DROP PRIMARY KEY
;
SQL
remove_column :post_similarities, :id
execute <<~SQL
ALTER TABLE
post_similarities
ADD PRIMARY KEY (post_id, target_post_id)
;
SQL
end
def down
execute <<~SQL
ALTER TABLE
post_similarities
DROP PRIMARY KEY
;
SQL
execute <<~SQL
ALTER TABLE
post_similarities
ADD COLUMN id BIGINT NOT NULL AUTO_INCREMENT FIRST
, ADD PRIMARY KEY (id)
;
SQL
end
end
@@ -0,0 +1,43 @@
class ChangeTagSimilaritiesToCompositePk < ActiveRecord::Migration[8.0]
def up
execute <<~SQL
ALTER TABLE
tag_similarities
MODIFY COLUMN id BIGINT NOT NULL
;
SQL
execute <<~SQL
ALTER TABLE
tag_similarities
DROP PRIMARY KEY
;
SQL
remove_column :tag_similarities, :id
execute <<~SQL
ALTER TABLE
tag_similarities
ADD PRIMARY KEY (tag_id, target_tag_id)
;
SQL
end
def down
execute <<~SQL
ALTER TABLE
tag_similarities
DROP PRIMARY KEY
;
SQL
execute <<~SQL
ALTER TABLE
tag_similarities
ADD COLUMN id BIGINT NOT NULL AUTO_INCREMENT FIRST
, ADD PRIMARY KEY (id)
;
SQL
end
end
+3 -3
View File
@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2026_01_18_144400) do ActiveRecord::Schema[8.0].define(version: 2026_01_21_024800) do
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
t.string "record_type", null: false t.string "record_type", null: false
@@ -55,7 +55,7 @@ ActiveRecord::Schema[8.0].define(version: 2026_01_18_144400) do
t.index ["tag_id"], name: "index_nico_tag_relations_on_tag_id" t.index ["tag_id"], name: "index_nico_tag_relations_on_tag_id"
end end
create_table "post_similarities", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| create_table "post_similarities", primary_key: ["post_id", "target_post_id"], charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "post_id", null: false t.bigint "post_id", null: false
t.bigint "target_post_id", null: false t.bigint "target_post_id", null: false
t.float "cos", null: false t.float "cos", null: false
@@ -126,7 +126,7 @@ ActiveRecord::Schema[8.0].define(version: 2026_01_18_144400) do
t.index ["name"], name: "index_tag_names_on_name", unique: true t.index ["name"], name: "index_tag_names_on_name", unique: true
end end
create_table "tag_similarities", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| create_table "tag_similarities", primary_key: ["tag_id", "target_tag_id"], charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "tag_id", null: false t.bigint "tag_id", null: false
t.bigint "target_tag_id", null: false t.bigint "target_tag_id", null: false
t.float "cos", null: false t.float "cos", null: false