From 8a62381268a2fd6cc31e87ae5cae018ce1ec52fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 27 Nov 2013 19:17:14 +0100 Subject: [PATCH] migration script --- script/discourse | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 script/discourse diff --git a/script/discourse b/script/discourse new file mode 100755 index 00000000000..677764fbc40 --- /dev/null +++ b/script/discourse @@ -0,0 +1,61 @@ +#!/usr/bin/env ruby + +require "thor" + +class DiscourseCLI < Thor + class_option :verbose, default: false, aliases: :v + + desc "migrate", "Make sure all the posts are pointing to the new domain" + option :from, type: :array, required: true, banner: "http://previous.domain.com" + option :database, default: "default", aliases: :db + def migrate + verbose = options[:verbose] + database = options[:database] + from = options[:from].map { |f| schemaless f } + + begin + puts "loading rails..." if verbose + load_rails + + puts "connecting to #{database}..." if verbose + RailsMultisite::ConnectionManagement.establish_connection(db: database) + + base_url = schemaless Discourse.base_url_no_prefix + + puts "updating #{Post.count} posts to #{base_url}" if verbose + Post.find_each do |post| + raw, cooked = post.raw.dup, post.cooked.dup + from.each do |f| + raw.gsub!(f, base_url) + cooked.gsub!(f, base_url) + end + if raw != post.raw || cooked != post.cooked + Post.where(id: post.id).update_all(raw: raw, cooked: cooked) + putc "#" if verbose + else + putc "." + end + end + + rescue => e + puts "Cannot connect to database: #{database}" + puts e + puts e.backtrace.join("\n") + end + + puts "", "done!" if verbose + end + + private + + def load_rails + require File.expand_path(File.dirname(__FILE__) + "/../config/environment") + end + + def schemaless(url) + url.gsub(/^https?:/, "") + end + +end + +DiscourseCLI.start(ARGV)