class CreatePostUrlSanitisationRules < ActiveRecord::Migration[8.0] def up 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 end def down drop_table :post_url_sanitisation_rules end end