|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +require "thor" |
| 4 | + |
| 5 | +class DiscourseCLI < Thor |
| 6 | + class_option :verbose, default: false, aliases: :v |
| 7 | + |
| 8 | + desc "migrate", "Make sure all the posts are pointing to the new domain" |
| 9 | + option :from, type: :array, required: true, banner: "http://previous.domain.com" |
| 10 | + option :database, default: "default", aliases: :db |
| 11 | + def migrate |
| 12 | + verbose = options[:verbose] |
| 13 | + database = options[:database] |
| 14 | + from = options[:from].map { |f| schemaless f } |
| 15 | + |
| 16 | + begin |
| 17 | + puts "loading rails..." if verbose |
| 18 | + load_rails |
| 19 | + |
| 20 | + puts "connecting to #{database}..." if verbose |
| 21 | + RailsMultisite::ConnectionManagement.establish_connection(db: database) |
| 22 | + |
| 23 | + base_url = schemaless Discourse.base_url_no_prefix |
| 24 | + |
| 25 | + puts "updating #{Post.count} posts to #{base_url}" if verbose |
| 26 | + Post.find_each do |post| |
| 27 | + raw, cooked = post.raw.dup, post.cooked.dup |
| 28 | + from.each do |f| |
| 29 | + raw.gsub!(f, base_url) |
| 30 | + cooked.gsub!(f, base_url) |
| 31 | + end |
| 32 | + if raw != post.raw || cooked != post.cooked |
| 33 | + Post.where(id: post.id).update_all(raw: raw, cooked: cooked) |
| 34 | + putc "#" if verbose |
| 35 | + else |
| 36 | + putc "." |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + rescue => e |
| 41 | + puts "Cannot connect to database: #{database}" |
| 42 | + puts e |
| 43 | + puts e.backtrace.join("\n") |
| 44 | + end |
| 45 | + |
| 46 | + puts "", "done!" if verbose |
| 47 | + end |
| 48 | + |
| 49 | + private |
| 50 | + |
| 51 | + def load_rails |
| 52 | + require File.expand_path(File.dirname(__FILE__) + "/../config/environment") |
| 53 | + end |
| 54 | + |
| 55 | + def schemaless(url) |
| 56 | + url.gsub(/^https?:/, "") |
| 57 | + end |
| 58 | + |
| 59 | +end |
| 60 | + |
| 61 | +DiscourseCLI.start(ARGV) |
0 commit comments