In order to script rearranging a large Discourse with working with my site leadership, I wrote a simple framework for packaging up a bunch of ruby scriptlets to be driven from a YAML configuration file. This way, I can restore a backup on a staging site, run my script, get feedback, change a few lines of YAML, restore the backup, run the script with new configuration, and repeat until satisfied.
I have almost 600 lines of configuration for my site, and doing that by manual manipulation through the UI would just not happen. Not even once, let alone many time to get it right. I know this because last time I proposed making sweeping changes I quite literally gave up trying. By contrast, with this script I can now complete the whole cycle in just a few minutes per iteration, even though the site has about half a million posts and over 100 categories. This lets me get fast feedback from my site leadership, and I’ll be prepared to migrate my site quickly.
More detailed documentation is in the README file in the repository with the code:
This script does no error checking. It’s pretty much crazy to run it on your live site. It is intended to run on a staging site, validate the result, and then be taken live. As the author I still intend to run it this way. If you run it directly on a live site you get to keep all the pieces when it breaks.
From the documentation, a configuration file might look like this:
---
- describe:
context: Old Name
category: 7
name: New Name
description: New description of category
slug: new-slug
- movePosts:
context: move only faq posts from the Support category to the Documentation category
source: 3 # Support category ID
target: 6 # Documentation category ID
withTag: faq
hide: false # do not hide the Support category when done
- movePosts:
context: consolidate How-To category into documentation with how-to tag
source: 8 # How-To category ID
target: 6 # Documentation category ID
addTag: how-to
hide: true # hide the old How-To category, visible only to Admin
The progress output while it’s running might then look like this.
==========
Move hidden categories out of the way so they don't clutter admin view
setHiddenCategory: {:category=>11}
==========
Rename Old Name to New Name
describe: {:category=>7, :name=>"New Name", :description=>"New description of category", :slug=>"new-slug"}
==========
move only faq posts from the Support category to the Documentation category
movePosts: {:source=>3, :withTag=>"faq", :target=>6}
==========
To use this, put your YAML file in /var/discourse/shared/app/tmp/rearrange.yaml
and then:
cd /var/discourse
./launcher enter app
git clone https://github.com/johnsonm/discourse-site-rearranger.git script/discourse-site-rearranger
ruby script/discourse-site-rearranger/rearrange.rb /shared/tmp/rearrange.yaml
However, it is reasonably likely that this script won’t do quite everything you need. It’s really a framework that makes it very easy to drop in new actions that automate more aspects of a scripted site modification with just a few lines of code. All you need to do is define one method with a few lines of code, and you’ll be able to invoke it properly from the YAML file.
Have you been thinking about improving the organization of your site, but shrinking from using the UI, or wondering how to trust that you’ll be able to replicate your testing on a staging site for making changes live? Take a look and tell me what you think!