# Reset after running migration script?

**URL:** https://meta.discourse.org/t/reset-after-running-migration-script/281224
**Category:** Migration
**Tags:** vanilla
**Created:** [October 5, 2023, 10:29pm UTC](https://meta.discourse.org/t/reset-after-running-migration-script/281224 "2023-10-05T22:29:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![MikeD](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@MikeD](https://meta.discourse.org/u/MikeD)
#### Post date: [October 5, 2023, 10:29pm UTC](https://meta.discourse.org/t/reset-after-running-migration-script/281224/1 "2023-10-05T22:29:39Z")

</div>

I’m using the migration scripts to migrate a Vanilla 3 forum to self-hosted Discourse.

The migration script is working fine:  
`RAILS_ENV=production ruby script/import_scripts/vanilla.rb /shared/uploads/export.text`

The only issue is that once I’ve done the export, I can’t seem to re-import again. The import script runs fine a second time, but any data changes I made to the import file are not applied. Also, the importer runs about 50x faster on the second run, which makes me suspect it’s not actually importing anything.

Question: is there any way to re-run the import scripts located at  
`/var/www/discourse/script/import_scripts/`  
after the first run?

Specifically, as I fix bugs in my import file format, I’d like to be able to re-import to have updates made to posts & discussions only.

So far the only solution I found was to nuke the entire Discourse install and start from scratch, which takes nearly an hour each time.

Any tips?

Here is the relevant code from vanilla.rb:

```plaintext
  def import_posts
    puts "", "importing posts..."

    create_posts(@comments) do |comment|
      next unless t = topic_lookup_from_imported_post_id("discussion#" + comment[:discussion_id])

      {
        id: "comment#" + comment[:comment_id],
        user_id:
          user_id_from_imported_user_id(comment[:insert_user_id]) || Discourse::SYSTEM_USER_ID,
        topic_id: t[:topic_id],
        raw: clean_up(comment[:body]),
        created_at: parse_date(comment[:date_inserted]),
      }
    end
  end

```

I’m a programmer but not a ruby programmer - is there any way to modify this code to force it to replace the content of a post if I do a re-import?

---

<div class="post-metadata">

### Author: ![MikeD](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@MikeD](https://meta.discourse.org/u/MikeD)
#### Post date: [October 7, 2023, 12:26am UTC](https://meta.discourse.org/t/reset-after-running-migration-script/281224/2 "2023-10-07T00:26:33Z")

</div>

I found one workaround that’s not halfway bad - as I’m improving my parser which cleans up the import file from Vanilla, I tend to focus on mistakes that occur i specific posts.

So as I improve my parser, I can stop the parser in the debugger (I’m using Xojo, for what it’s worth) and get the Raw Text.

Then, in the live discourse forum, I can simply add a new post, paste the text in, and see how it looks.

This allows me to do a test/debug/change cycle of a few seconds, rather than an hour or so.

My new plan: after I’m happy with my parser cleanup, then I’ll nuke the Discourse and re-install from scratch.

---

<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: [October 8, 2023, 4:06pm UTC](https://meta.discourse.org/t/reset-after-running-migration-script/281224/3 "2023-10-08T16:06:47Z")

</div>

It works that way on purpose. The idea is that your can do an import now and then run another with a fresh dump they will run very fast since it imports only the new data.

You need to drop, create, and migrate the database to start over.

If you have lots of users, you could stop the script after the users were imported and make a backup then and restore that backup before you try your fixes.

---

<div class="post-metadata">

### Author: ![MikeD](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@MikeD](https://meta.discourse.org/u/MikeD)
#### Post date: [October 8, 2023, 10:57pm UTC](https://meta.discourse.org/t/reset-after-running-migration-script/281224/4 "2023-10-08T22:57:51Z")

</div>

> [@pfaffman](#):
>
> It works that way on purpose. The idea is that your can do an import now and then run another with a fresh dump they will run very fast since it imports only the new data.

That makes perfect sense, thank you for explaining. It would be nice if there was a flag one could set to “force overwrite” but I dug thorough the code a bit and didn’t see anything obvious.

> [@pfaffman](#):
>
> You need to drop, create, and migrate the database to start over

Is there an easy way to do that? The only solution I’ve found are these commands, which basically are the same as starting a fresh docker install:

```plaintext
# WARNING: these commands delete your entire Discourse forum
cd /var/discourse
sudo ./launcher stop app
sudo rm -rf /var/discourse/shared/standalone
sudo ./launcher rebuild app

```

It only takes about 10 minutes, but then I have to go through the initial setup again, which is a pain.

> [@pfaffman](#):
>
> If you have lots of users, you could stop the script after the users were imported and make a backup then and restore that backup before you try your fixes.

Now that is a fantasic idea! I could even just make a backup after a fresh install, but before running the import script at all, since re-importing users/topics/posts/comments is pretty quick, and this forum is not live to the public.

---

<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: [October 9, 2023, 12:17am UTC](https://meta.discourse.org/t/reset-after-running-migration-script/281224/5 "2023-10-09T00:17:46Z")

</div>

sv stop unicorn  
rake db:drop db:create db:migrate

You have to set an env variable to drop the database, but it will tell you what it is.
