This post is part of the work on a project to migrate a forum from Discourse to Flarum. See the related post here: Migrating a forum from Discourse to Flarum

Of course, in your real life scenario, you will have a database already. But for contributing to the script discourse_to_flarum.php, we need a test database, for reproducing specific situations.

It took a while to figure out how to create a test database…

Initial Setup of a test installation

First, follow the instructions at https://github.com/discourse/discourse/blob/master/docs/INSTALL-cloud.md to setup Discourse with a docker container.

On Fedora, I did:

wget -qO- https://get.docker.com/ | sh
mkdir /var/discourse
git clone https://github.com/discourse/discourse_docker.git /var/discourse
cd /var/discourse
./discourse-setup

I changed the hostname from discourse.example.com to localhost, and used admin@example.com as the E-mail address for the admin account. For the SMTP settings, you must enter values different than the defaults, but I use dummy values and create the admin account as described below.

My values for discourse-setup for a local test installation of Discourse:

Hostname for your Discourse? [discourse.example.com]: localhost
Email address for admin account(s)? [me@example.com,you@example.com]: admin@example.com
SMTP server address? [smtp.example.com]: my.smtp.example.com
SMTP port? [587]: 
SMTP user name? [user@example.com]: my.user@example.com
SMTP password? [pa$$word]: mypassword
Optional email address for setting up Let's Encrypt? (ENTER to skip) [me@example.com]:

To activate the admin user, type these commands:

cd /var/discourse
./launcher enter app
rake admin:create

I use the Email address for admin account specified above, eg. admin@example.com, and I am using the password Demo1234Demo1234! for this test database.

root@localhost-app:/var/www/discourse# rake admin:create
Email:  admin@example.com
Password:  
Repeat password:  
 
Ensuring account is active!
 
Account created successfully with username example
Do you want to grant Admin privileges to this account? (Y/n)  y
 
Your account now has Admin privileges!

Now the Discourse instance is running at http://localhost on my Fedora machine, and I can login with user admin@example.com and the password Demo1234Demo1234! (including the exclamation mark).

Just a note, how to start a fresh database again:

launcher stop app
launcher destroy app
rm -Rf shared/standalone/*

Then start again with ./discourse-setup as described above!

Creating sample data

To create some sample data, you can use the script https://github.com/discourse/discourse/blob/master/script/profile_db_generator.rb

I have modified that script a little bit, you can see my changes at https://gist.github.com/tpokorra/b2e34238dea0243572e822f649a35bab/revisions.

To execute this script:

./launcher enter app
cd /var/www/discourse
vi script/profile_db_generator.rb
# make some modifications as described in the gist linked above:
#     unless Rails.env == "profile"  becomes: unless Rails.env == "production"
#      if User.count > 2 becomes if User.count > 3
#      users = 100.times.map    becomes: users = 10.times.map
#      topic_ids = 100.times.map do becomes:  topic_ids = 50.times.map do
#   and for replies: 2000.times do becomes:   200.times do
RAILS_ENV=production sudo -H -E -u discourse bundle exec ruby script/profile_db_generator.rb

This is the output:

root@localhost-app:/var/www/discourse# RAILS_ENV=production sudo -H -E -u discourse bundle exec ruby script/profile_db_generator.rb
installing gabbler gem
Successfully installed gabbler-0.1.0
1 gem installed
Creating 100 users
..........
Creating 10 categories
..........
Creating 100 topics
..................................................
creating 200 replies
........................................................................................................................................................................................................

Creating a backup

To create a backup of the Postgresql Database:

./launcher enter app
sudo -u discourse pg_dump discourse | gzip > /shared/postgres_backup/discourse_pg`date '+%Y-%m-%d_%H-%M-%S'`.sql.gz

You find the result on your host machine, in directory /var/discourse/shared/standalone/postgres_backup.

I have uploaded this backup of the PostgreSQL database into the discourse_to_flarum repository.

Creating a test database for Discourse with dummy data
Tagged on: