# How to import category with category permissions?

**URL:** https://meta.discourse.org/t/how-to-import-category-with-category-permissions/311197
**Category:** Migration
**Tags:** rake-task
**Created:** [June 7, 2024, 11:51am UTC](https://meta.discourse.org/t/how-to-import-category-with-category-permissions/311197 "2024-06-07T11:51:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![pacharanero](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pacharanero/32/500583_2.png) [@pacharanero](https://meta.discourse.org/u/pacharanero)
#### Post date: [June 7, 2024, 11:51am UTC](https://meta.discourse.org/t/how-to-import-category-with-category-permissions/311197/1 "2024-06-07T11:51:42Z")

</div>

> [@Administrative Bulk Operations](https://meta.discourse.org/t/administrative-bulk-operations/118349/1):
>
> **Importing a category file**
> 
> Use the exported file’s name like the example below:
> 
> ```plaintext
> rake import:file["category-export-2019-05-16-052430.json"]
> 
> ```

When importing a Category from file, is there any way to ensure the uploaded Topics will remain private? I’ve done a test import into a local Discourse dev env setup and the Category security settings were reset, such that those new Topics would be viewable by Everyone.

I know I can do the `import:file[category-export.json]` and then immediately change the Category security to be viewable only by the users in a certain Group, but I’m concerned about what happens in the interim time - firstly there may be a short time that Everyone can see that content, and secondly that email notifications may go out (or be scheduled) for Everyone, even once the settings are changed.

Because the `import:file` rake task only takes one argument (the filename), there doesn’t seem to be any way to set the ‘destination Category’ in which I want the import to be stored.

I’ve tried creating the Category first, with the same Category Name and slug, but this fails with the error:

```plaintext
Failed to import category (ID = 5, name = CATEGORY_NAME): Validation failed: Category Name has already been taken, Slug is already in use

```

The single test import Topic I included in that .json file did get imported but it was created in #Uncategorized, which if anything is worse than it getting created in an openly visible Category.

I’ve thought about any other ways I can do this securely but have not got any good ideas at the moment. Any thoughts from the community?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [June 7, 2024, 2:54pm UTC](https://meta.discourse.org/t/how-to-import-category-with-category-permissions/311197/2 "2024-06-07T14:54:58Z")

</div>

I think I would edit the script by hand in the container to change the permissions.

Having it default to staff only seems like a reasonable feature request or maybe a PR.

---

<div class="post-metadata">

### Author: ![pacharanero](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pacharanero/32/500583_2.png) [@pacharanero](https://meta.discourse.org/u/pacharanero)
#### Post date: [June 7, 2024, 3:13pm UTC](https://meta.discourse.org/t/how-to-import-category-with-category-permissions/311197/3 "2024-06-07T15:13:41Z")

</div>

Thanks @pfaffman for the rapid response!

Actually, it looks like this was just my dim-wittedness front and centre here.

The default behaviour of the `import:file` rake task (which invokes `ImportExport::Importer`) is **to copy exactly the permissions over from the source Category**. I only found this out from looking over the [source code](https://github.com/discourse/discourse/blob/main/lib/import_export/importer.rb#L79).

And it did exactly this. The reason my destination category didn’t have the permissions I had been hoping for was that I had forgotten to set those permissions correctly in the source! I was moving a category in an already _private_ forum (entire forum is private so that category was See/Reply/Create to Everyone in there) into a more open forum (in which those posts needed to be in a secure category - See/Reply/Create for Admins only initially)

> [@pfaffman](#):
>
> Having it default to staff only seems like a reasonable feature request or maybe a PR.

I agree - since Category import is something that can only really be done in the real live environment, one might want the permissions to default to admin-only until the import has been checked and everyone is satisfied it’s OK. Better that than spamming users with notifications about bulk imports.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [June 7, 2024, 3:50pm UTC](https://meta.discourse.org/t/how-to-import-category-with-category-permissions/311197/4 "2024-06-07T15:50:17Z")

</div>

> [@pacharanero](#):
>
> Actually, it looks like this was just my dim-wittedness front and centre here.

Alas, I hadn’t taken that possibility into account. 🙂

> [@pacharanero](#):
>
> The default behaviour of the `import:file` rake task (which invokes `ImportExport::Importer`) is **to copy exactly the permissions over from the source Category**.

That’s exactly what you want!!! 🤣

> [@pacharanero](#):
>
> I only found this out from looking over the [source code](https://github.com/discourse/discourse/blob/main/lib/import_export/importer.rb#L79).

I was too lazy to do that myself!
