Flarum to Discourse Migration Script

Hey All,

Just figured I’d make a quick post with a little bit on how I migrated our 25,000+ member, 250,000+ post forum from Flarum to Discourse.

As I’m sure you know if you’re reading this, there’s no official import script for Flarum → Discourse, however @koen360 was kind enough to share his work with getting the FluxBB importer modified to import users. However, we needed to also import posts as a large part of our forums success rides on users searching for very specific terms that appear in posts. So that’s what we did…with several hours of failed migrations, headaches, and some bloodshed.

I’m by no means an expert in writing migration scripts, but this script works well enough for the current version of Flarum at the time of writing (V0.1.0-beta.16). The script (attached below) will need access to your MySQL/MariaDB database, preferably a copy of it so you don’t wreck your production site if something goes wrong. Just replace the dummy values in the MySQL connection and you should be set.

Just make sure you have a full backup of your Flarum database before you start this process, if something goes wrong it’s likely irreversible.

If you need assistance with your migration from Flarum → Discourse feel free to reply to this thread, I’m by no means an expert, but I’ll try to help out however I can.

You can get the importer here, apparently I can’t upload a file since this is my first post

12 Likes

Thanks for making the script @Lecter! Can you submit it as a PR to discourse/discourse? We don’t have a Flarum script yet, so this would be helpful to have as a base.

7 Likes

Absolutely! PR Incoming…

7 Likes

This is great, thank you for taking this importer a step further! :sparkling_heart: I do hope in time it can also be expanded to include Tags, if not Attachments.

How much work might it be to add Tag support? I see vBulletin 5 and maybe a couple others have possible models of how it works, but I have no idea how much that would need to be adjusted for Flarum. Thanks for any insight anyone can provide, and no worries if it’s not something that can really be answered without doing it. :grinning_face_with_smiling_eyes:

1 Like

Hello @Lecter !
Does your script still valid?
Can you share again? Because your link is dead. :frowning:

Thanks for considering my request.

The script written by @Lecter now lives at

4 Likes

Can someone help me with migration, please?

I have Discourse with empty posts and with 3 default Categories.
When I tried to import SQL from Flarum to Discourse I’ve got this error

su discourse -c 'bundle exec ruby script/import_scripts/flarum_import.rb'
Loading existing groups...
Loading existing users...
Loading existing categories...
Loading existing posts...
Loading existing topics...

creating users
       13 / 13 (100.0%)  [281 items/min]  n]  
importing top level categories...
       39 / 39 (100.0%)  [420 items/min]  n]  
importing children categories...
       39 / 39 (100.0%)  [290 items/min]  
creating topics and posts
Traceback (most recent call last):
	19: from script/import_scripts/flarum_import.rb:162:in `<main>'
	18: from /var/www/discourse/script/import_scripts/base.rb:47:in `perform'
	17: from script/import_scripts/flarum_import.rb:32:in `execute'
	16: from script/import_scripts/flarum_import.rb:101:in `import_posts'
	15: from /var/www/discourse/script/import_scripts/base.rb:916:in `batches'
	14: from /var/www/discourse/script/import_scripts/base.rb:916:in `loop'
	13: from /var/www/discourse/script/import_scripts/base.rb:917:in `block in batches'
	12: from script/import_scripts/flarum_import.rb:122:in `block in import_posts'
	11: from /var/www/discourse/script/import_scripts/base.rb:224:in `all_records_exist?'
	10: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3/lib/active_record/transactions.rb:209:in `transaction'
	 9: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:316:in `transaction'
	 8: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3/lib/active_record/connection_adapters/abstract/transaction.rb:317:in `within_new_transaction'
	 7: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activesupport-7.0.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
	 6: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activesupport-7.0.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
	 5: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activesupport-7.0.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
	 4: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activesupport-7.0.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
	 3: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3/lib/active_record/connection_adapters/abstract/transaction.rb:319:in `block in within_new_transaction'
	 2: from /var/www/discourse/script/import_scripts/base.rb:231:in `block in all_records_exist?'
	 1: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rack-mini-profiler-3.0.0/lib/patches/db/pg.rb:56:in `exec'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rack-mini-profiler-3.0.0/lib/patches/db/pg.rb:56:in `exec': ERROR:  duplicate key value violates unique constraint "import_ids_pkey" (PG::UniqueViolation)
DETAIL:  Key (val)=(7) already exists.
	20: from script/import_scripts/flarum_import.rb:162:in `<main>'
	19: from /var/www/discourse/script/import_scripts/base.rb:47:in `perform'
	18: from script/import_scripts/flarum_import.rb:32:in `execute'
	17: from script/import_scripts/flarum_import.rb:101:in `import_posts'
	16: from /var/www/discourse/script/import_scripts/base.rb:916:in `batches'
	15: from /var/www/discourse/script/import_scripts/base.rb:916:in `loop'
	14: from /var/www/discourse/script/import_scripts/base.rb:917:in `block in batches'
	13: from script/import_scripts/flarum_import.rb:122:in `block in import_posts'
	12: from /var/www/discourse/script/import_scripts/base.rb:224:in `all_records_exist?'
	11: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3/lib/active_record/transactions.rb:209:in `transaction'
	10: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:316:in `transaction'
	 9: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3/lib/active_record/connection_adapters/abstract/transaction.rb:317:in `within_new_transaction'
	 8: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activesupport-7.0.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
	 7: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activesupport-7.0.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
	 6: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activesupport-7.0.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
	 5: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activesupport-7.0.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
	 4: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-7.0.3/lib/active_record/connection_adapters/abstract/transaction.rb:319:in `block in within_new_transaction'
	 3: from /var/www/discourse/script/import_scripts/base.rb:243:in `block in all_records_exist?'
	 2: from /var/www/discourse/script/import_scripts/base.rb:243:in `ensure in block in all_records_exist?'
	 1: from /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rack-mini-profiler-3.0.0/lib/patches/db/pg.rb:56:in `exec'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rack-mini-profiler-3.0.0/lib/patches/db/pg.rb:56:in `exec': ERROR:  current transaction is aborted, commands ignored until end of transaction block (PG::InFailedSqlTransaction)

The problem is with Key (val)=(7) already exists.

`exec': ERROR:  duplicate key value violates unique constraint "import_ids_pkey" (PG::UniqueViolation)
DETAIL:  Key (val)=(7) already exists.
root@raspberrypi-app:/var/www/discourse# rake db:validate_indexes
Starting postgres on port: 11000
Waiting for PG server to start...
PG server is ready and DB is loaded
Running migrations on blank database!

Testing indexes on the default database

No missing indexes

No extra indexes

How can I fix this ?

This SQL code breaks the script

 SELECT p.id id,
               d.id topic_id,
               d.title title,
               d.first_post_id first_post_id,
               p.user_id user_id,
               p.content raw,
               p.created_at created_at,
               t.tag_id category_id
        FROM posts p,
             discussions d,
             discussion_tag t
        WHERE p.discussion_id = d.id
          AND t.discussion_id = d.id
        ORDER BY p.created_at

I found the problem. In my case one post has multiple tags. After I deleted such posts import script processed without any error.

2 Likes

I got this error

ubuntu@ip-172-26-1-78:~/discourse$ bundle exec ruby script/import_scripts/flarum_import.rb
Loading existing groups...
Loading existing users...
Loading existing categories...
Loading existing posts...
Loading existing topics...

creating users
Skipping 1000 already imported users
Skipping 826 already imported users

importing top level categories...
       34 / 34 (100.0%)  [5728165 items/min]  
importing children categories...
       34 / 34 (100.0%)  [3194 items/min]  
creating topics and posts
Traceback (most recent call last):
	19: from script/import_scripts/flarum_import.rb:162:in `<main>'
	18: from /home/ubuntu/discourse/script/import_scripts/base.rb:47:in `perform'
	17: from script/import_scripts/flarum_import.rb:32:in `execute'
	16: from script/import_scripts/flarum_import.rb:101:in `import_posts'
	15: from /home/ubuntu/discourse/script/import_scripts/base.rb:916:in `batches'
	14: from /home/ubuntu/discourse/script/import_scripts/base.rb:916:in `loop'
	13: from /home/ubuntu/discourse/script/import_scripts/base.rb:917:in `block in batches'
	12: from script/import_scripts/flarum_import.rb:122:in `block in import_posts'
	11: from /home/ubuntu/discourse/script/import_scripts/base.rb:224:in `all_records_exist?'
	10: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/transactions.rb:209:in `transaction'
	 9: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/connection_adapters/abstract/database_statements.rb:316:in `transaction'
	 8: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/connection_adapters/abstract/transaction.rb:317:in `within_new_transaction'
	 7: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
	 6: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
	 5: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
	 4: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
	 3: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/connection_adapters/abstract/transaction.rb:319:in `block in within_new_transaction'
	 2: from /home/ubuntu/discourse/script/import_scripts/base.rb:231:in `block in all_records_exist?'
	 1: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rack-mini-profiler-3.0.0/lib/patches/db/pg.rb:56:in `exec'
/home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rack-mini-profiler-3.0.0/lib/patches/db/pg.rb:56:in `exec': ERROR:  duplicate key value violates unique constraint "import_ids_pkey" (PG::UniqueViolation)
DETAIL:  Key (val)=(53) already exists.
	20: from script/import_scripts/flarum_import.rb:162:in `<main>'
	19: from /home/ubuntu/discourse/script/import_scripts/base.rb:47:in `perform'
	18: from script/import_scripts/flarum_import.rb:32:in `execute'
	17: from script/import_scripts/flarum_import.rb:101:in `import_posts'
	16: from /home/ubuntu/discourse/script/import_scripts/base.rb:916:in `batches'
	15: from /home/ubuntu/discourse/script/import_scripts/base.rb:916:in `loop'
	14: from /home/ubuntu/discourse/script/import_scripts/base.rb:917:in `block in batches'
	13: from script/import_scripts/flarum_import.rb:122:in `block in import_posts'
	12: from /home/ubuntu/discourse/script/import_scripts/base.rb:224:in `all_records_exist?'
	11: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/transactions.rb:209:in `transaction'
	10: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/connection_adapters/abstract/database_statements.rb:316:in `transaction'
	 9: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/connection_adapters/abstract/transaction.rb:317:in `within_new_transaction'
	 8: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
	 7: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
	 6: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
	 5: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
	 4: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/connection_adapters/abstract/transaction.rb:319:in `block in within_new_transaction'
	 3: from /home/ubuntu/discourse/script/import_scripts/base.rb:243:in `block in all_records_exist?'
	 2: from /home/ubuntu/discourse/script/import_scripts/base.rb:243:in `ensure in block in all_records_exist?'
	 1: from /home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rack-mini-profiler-3.0.0/lib/patches/db/pg.rb:56:in `exec'
/home/ubuntu/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rack-mini-profiler-3.0.0/lib/patches/db/pg.rb:56:in `exec': ERROR:  current transaction is aborted, commands ignored until end of transaction block (PG::InFailedSqlTransaction)

Hi there,

Can you explain how you did it? Cause I am facing the same error as you but instead of (val)=(7) I get:

`exec’: ERROR: duplicate key value violates unique constraint “import_ids_pkey” (PG::UniqueViolation)
DETAIL: Key (val)=(1) already exists.