diff --git a/backend/db/migrate/20260713000000_create_post_url_sanitisation_rules.rb b/backend/db/migrate/20260713000000_create_post_url_sanitisation_rules.rb index f6a63d5..77a2b0b 100644 --- a/backend/db/migrate/20260713000000_create_post_url_sanitisation_rules.rb +++ b/backend/db/migrate/20260713000000_create_post_url_sanitisation_rules.rb @@ -1,44 +1,58 @@ class CreatePostUrlSanitisationRules < ActiveRecord::Migration[8.0] + class PostUrlSanitisationRule < ActiveRecord::Base + self.table_name = 'post_url_sanitisation_rules' + end + def up - create_table :post_url_sanitisation_rules, - id: :integer, - primary_key: :priority do |t| + create_table :post_url_sanitisation_rules, id: :integer, primary_key: :priority do |t| t.string :source_pattern, null: false t.string :replacement, null: false t.timestamps t.datetime :discarded_at + t.index :source_pattern, unique: true t.index :discarded_at end - now = ActiveRecord::Base.connection.quote(Time.current) - execute <<~SQL - INSERT INTO - post_url_sanitisation_rules(priority, source_pattern, replacement, created_at, updated_at) - VALUES - (10, '\\Ahttps?://youtu\\\\.be/([^/?#]+)(?:[?#].*)?\\z', - 'https://www.youtube.com/watch?v=\\\\1', - #{ now }, #{ now }) - , (20, '\\Ahttps?://(?:www\\\\.|m\\\\.)?youtube\\\\.com/live/([^/?#]+)(?:[?#].*)?\\z', - 'https://www.youtube.com/watch?v=\\\\1', - #{ now }, #{ now }) - , (30, '\\Ahttps?://(?:www\\\\.|m\\\\.)?youtube\\\\.com/shorts/([^/?#]+)(?:[?#].*)?\\z', - 'https://www.youtube.com/watch?v=\\\\1', - #{ now }, #{ now }) - , (40, '\\Ahttps?://(?:www\\\\.|m\\\\.)?youtube\\\\.com/embed/([^/?#]+)(?:[?#].*)?\\z', - 'https://www.youtube.com/watch?v=\\\\1', - #{ now }, #{ now }) - , (50, '\\Ahttps?://(?:www\\\\.|m\\\\.)?youtube\\\\.com/watch\\\\?(?:[^#&]+&)*v=([^&#]+)(?:[&#].*)?\\z', - 'https://www.youtube.com/watch?v=\\\\1', - #{ now }, #{ now }) - , (60, '\\Ahttps?://nico\\\\.ms/([^/?#]+)(?:[?#].*)?\\z', - 'https://www.nicovideo.jp/watch/\\\\1', - #{ now }, #{ now }) - , (70, '\\Ahttps?://(?:www\\\\.)?nicovideo\\\\.jp/watch/([^?#/]+)(?:[?#].*)?\\z', - 'https://www.nicovideo.jp/watch/\\\\1', - #{ now }, #{ now }) - ; - SQL + now = Time.current + + PostUrlSanitisationRule.insert_all!([ + { priority: 10, + source_pattern: '\Ahttps?://youtu\.be/([^/?#]+)(?:[?#].*)?\z', + replacement: 'https://www.youtube.com/watch?v=\1', + created_at: now, + updated_at: now }, + { priority: 20, + source_pattern: '\Ahttps?://(?:www\.|m\.)?youtube\.com/live/([^/?#]+)(?:[?#].*)?\z', + replacement: 'https://www.youtube.com/watch?v=\1', + created_at: now, + updated_at: now }, + { priority: 30, + source_pattern: '\Ahttps?://(?:www\.|m\.)?youtube\.com/shorts/([^/?#]+)(?:[?#].*)?\z', + replacement: 'https://www.youtube.com/watch?v=\1', + created_at: now, + updated_at: now }, + { priority: 40, + source_pattern: '\Ahttps?://(?:www\.|m\.)?youtube\.com/embed/([^/?#]+)(?:[?#].*)?\z', + replacement: 'https://www.youtube.com/watch?v=\1', + created_at: now, + updated_at: now }, + { priority: 50, + source_pattern: + '\Ahttps?://(?:www\.|m\.)?youtube\.com/watch\?(?:[^#&]+&)*v=([^&#]+)(?:[&#].*)?\z', + replacement: 'https://www.youtube.com/watch?v=\1', + created_at: now, + updated_at: now }, + { priority: 60, + source_pattern: '\Ahttps?://nico\.ms/([^/?#]+)(?:[?#].*)?\z', + replacement: 'https://www.nicovideo.jp/watch/\1', + created_at: now, + updated_at: now }, + { priority: 70, + source_pattern: '\Ahttps?://(?:www\.)?nicovideo\.jp/watch/([^?#/]+)(?:[?#].*)?\z', + replacement: 'https://www.nicovideo.jp/watch/\1', + created_at: now, + updated_at: now }]) end def down