# Merge script error when copying tags

**URL:** https://meta.discourse.org/t/merge-script-error-when-copying-tags/97360
**Category:** Bug
**Created:** [September 15, 2018, 5:26pm UTC](https://meta.discourse.org/t/merge-script-error-when-copying-tags/97360 "2018-09-15T17:26:16Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jrgen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jrgen/32/81165_2.png) [@jrgen](https://meta.discourse.org/u/jrgen)
#### Post date: [September 15, 2018, 5:26pm UTC](https://meta.discourse.org/t/merge-script-error-when-copying-tags/97360/1 "2018-09-15T17:26:16Z")

</div>

Looks like a very cool and useful script, but I encountered an error when I attempted this.  
Any pointers to how I can resolve it? (no ruby experience)  
Error message:

```plaintext
...
copying post_stats...
copying incoming_emails...
copying post_details...
copying post_revisions...
merging tags...
Traceback (most recent call last):
	4: from script/bulk_import/discourse_merger.rb:797:in `<main>'
	3: from script/bulk_import/discourse_merger.rb:65:in `start'
	2: from /home/xxxxx/discourse/script/bulk_import/base.rb:81:in `run'
	1: from script/bulk_import/discourse_merger.rb:81:in `execute'
script/bulk_import/discourse_merger.rb:271:in `copy_tags': undefined method `+' for nil:NilClass (NoMethodError)

```

Using the latest version of Discourse ( v2.2.0.beta2 +5 ) for both the sites.

---

<div class="post-metadata">

### Author: ![neil](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neil/32/102150_2.png) [@neil](https://meta.discourse.org/u/neil)
#### Post date: [September 17, 2018, 1:58pm UTC](https://meta.discourse.org/t/merge-script-error-when-copying-tags/97360/2 "2018-09-17T13:58:11Z")

</div>

@jrgen I committed a fix for that problem:

[https://github.com/discourse/discourse/commit/b9891c26417df1777b0cfc89ec41466436e10801](https://github.com/discourse/discourse/commit/b9891c26417df1777b0cfc89ec41466436e10801)

---

<div class="post-metadata">

### Author: ![jrgen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jrgen/32/81165_2.png) [@jrgen](https://meta.discourse.org/u/jrgen)
#### Post date: [September 17, 2018, 8:25pm UTC](https://meta.discourse.org/t/merge-script-error-when-copying-tags/97360/3 "2018-09-17T20:25:12Z")

</div>

Thanks for that! I found a couple more errors while trying to merge two (small-ish) forums;

1. Duplicate slug:

```plaintext
[...]
copying user_archived_messages...
copying unsubscribe_keys...
copying group_mentions...
copying user_histories...
copying user_warnings...
copying group_archived_messages...
copying notifications...
copying category_groups...
copying group_histories...
copying badge_groupings...
merging badges...
copying user_badges...
updating foreign keys in the users table...
updating category description topic ids...
Traceback (most recent call last):
	19: from script/bulk_import/discourse_merger.rb:798:in `<main>'
	18: from script/bulk_import/discourse_merger.rb:66:in `start'
	17: from /home/u/discourse/script/bulk_import/base.rb:81:in `run'
	16: from script/bulk_import/discourse_merger.rb:88:in `execute'
	15: from script/bulk_import/discourse_merger.rb:219:in `fix_category_descriptions'
	14: from script/bulk_import/discourse_merger.rb:219:in `each'
	13: from script/bulk_import/discourse_merger.rb:223:in `block in fix_category_descriptions'
	12: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/suppressor.rb:48:in `save!'
	11: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/transactions.rb:315:in `save!'
	10: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/transactions.rb:383:in `with_transaction_returning_status'
	 9: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/transactions.rb:212:in `transaction'
	 8: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:254:in `transaction'
	 7: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:227:in `within_new_transaction'
	 6: from /home/u/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
	 5: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:230:in `block in within_new_transaction'
	 4: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:254:in `block in transaction'
	 3: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/transactions.rb:386:in `block in with_transaction_returning_status'
	 2: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/transactions.rb:315:in `block in save!'
	 1: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/validations.rb:52:in `save!'
/home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/validations.rb:80:in `raise_validation_error': Validation failed: Slug is already in use (ActiveRecord::RecordInvalid)

```

1. Double nesting of categories (not sure how this can happen):

```plaintext
[...]
copying notifications...
copying category_groups...
copying group_histories...
copying badge_groupings...
merging badges...
copying user_badges...
updating foreign keys in the users table...
updating category description topic ids...
Traceback (most recent call last):
	19: from script/bulk_import/discourse_merger.rb:799:in `<main>'
	18: from script/bulk_import/discourse_merger.rb:66:in `start'
	17: from /home/u/discourse/script/bulk_import/base.rb:81:in `run'
	16: from script/bulk_import/discourse_merger.rb:88:in `execute'
	15: from script/bulk_import/discourse_merger.rb:219:in `fix_category_descriptions'
	14: from script/bulk_import/discourse_merger.rb:219:in `each'
	13: from script/bulk_import/discourse_merger.rb:224:in `block in fix_category_descriptions'
	12: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/suppressor.rb:48:in `save!'
	11: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/transactions.rb:315:in `save!'
	10: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/transactions.rb:383:in `with_transaction_returning_status'
	 9: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/transactions.rb:212:in `transaction'
	 8: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:254:in `transaction'
	 7: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:227:in `within_new_transaction'
	 6: from /home/u/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
	 5: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:230:in `block in within_new_transaction'
	 4: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:254:in `block in transaction'
	 3: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/transactions.rb:386:in `block in with_transaction_returning_status'
	 2: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/transactions.rb:315:in `block in save!'
	 1: from /home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/validations.rb:52:in `save!'
/home/u/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/validations.rb:80:in `raise_validation_error': Validation failed: You can't nest a subcategory under another (ActiveRecord::RecordInvalid)

```

---

<div class="post-metadata">

### Author: ![neil](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neil/32/102150_2.png) [@neil](https://meta.discourse.org/u/neil)
#### Post date: [September 19, 2018, 9:55pm UTC](https://meta.discourse.org/t/merge-script-error-when-copying-tags/97360/6 "2018-09-19T21:55:03Z")

</div>

> [@jrgen](#):
>
> Slug is already in use

Do you have two categories with the same slug but different names? 🤔 I guess the script should be merging categories based on slug (`/c/:category/:subcategory`) instead of name.

---

<div class="post-metadata">

### Author: ![jrgen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jrgen/32/81165_2.png) [@jrgen](https://meta.discourse.org/u/jrgen)
#### Post date: [September 20, 2018, 5:35am UTC](https://meta.discourse.org/t/merge-script-error-when-copying-tags/97360/8 "2018-09-20T05:35:59Z")

</div>

Yes, probably.  
One of the sites is imported from vbulletin, the other from a new discourse-install. Most of the categories exist in both sites, I will try to align all names and slugs before trying again

---

<div class="post-metadata">

### Author: ![Walker\_Blackwell](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/walker_blackwell/32/119568_2.png) [@Walker\_Blackwell](https://meta.discourse.org/u/Walker_Blackwell)
#### Post date: [September 25, 2018, 7:40pm UTC](https://meta.discourse.org/t/merge-script-error-when-copying-tags/97360/9 "2018-09-25T19:40:16Z")

</div>

This may have been what I was seeing as well (previous topic thread) as I was merging from a bbepress-to-discourse into a vb4-to-discourse.

best,  
Walker

---

<div class="post-metadata">

### Author: ![neil](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neil/32/102150_2.png) [@neil](https://meta.discourse.org/u/neil)
#### Post date: [September 26, 2018, 2:34am UTC](https://meta.discourse.org/t/merge-script-error-when-copying-tags/97360/10 "2018-09-26T02:34:23Z")

</div>

I made the merge tool smarter when merging/copying categories. It was only looking at category names ☹ but now looks at full category url’s to distinguish parent categories too.

> <https://github.com/discourse/discourse/commit/8af6d8189186e9a504488ae219845c40c987c718>

---

<div class="post-metadata">

### Author: ![neil](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neil/32/102150_2.png) [@neil](https://meta.discourse.org/u/neil)
#### Post date: [March 29, 2019, 8:26pm UTC](https://meta.discourse.org/t/merge-script-error-when-copying-tags/97360/11 "2019-03-29T20:26:41Z")

</div>


