# Issue with import script

**URL:** https://meta.discourse.org/t/issue-with-import-script/84211
**Category:** Development
**Created:** [March 30, 2018, 8:49am UTC](https://meta.discourse.org/t/issue-with-import-script/84211 "2018-03-30T08:49:24Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Cara](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@Cara](https://meta.discourse.org/u/Cara)
#### Post date: [March 30, 2018, 8:49am UTC](https://meta.discourse.org/t/issue-with-import-script/84211/1 "2018-03-30T08:49:24Z")

</div>

Hi,  
I’m trying to import an old punBB. I have discourse installed and running, I can launch the script but it look fort first\_post\_id and I have only the last\_post\_id…

I tried to change the order of import but everything is upside down…

Any Idea on how I change the punBB import script to use the last\_post\_id?

Here it is the post part of the script I’m using:

```
def import_posts
    puts "", "creating topics and posts"

    total_count = mysql_query("SELECT count(*) count from boulette_posts").first["count"]

    batches(BATCH_SIZE) do |offset|
      results = mysql_query("
        SELECT p.id id,
               t.id topic_id,
               t.forum_id category_id,
               t.subject title,
               t.last_post_id first_post_id,
               p.poster_id user_id,
               p.message raw,
               p.posted created_at
        FROM boulette_posts p,
             boulette_topics t
        WHERE p.topic_id = t.id
        ORDER BY p.posted
        LIMIT #{BATCH_SIZE}
        OFFSET #{offset};
      ").to_a

      break if results.size < 1
      next if all_records_exist? :posts, results.map { |m| m['id'].to_i }

      create_posts(results, total: total_count, offset: offset) do |m|
        skip = false
        mapped = {}

        mapped[:id] = m['id']
        mapped[:user_id] = user_id_from_imported_user_id(m['user_id']) || -1
        mapped[:raw] = process_punbb_post(m['raw'], m['id'])
        mapped[:created_at] = Time.zone.at(m['created_at'])

        if m['id'] == m['first_post_id']
          mapped[:category] = category_id_from_imported_category_id("child##{m['category_id']}")
          mapped[:title] = CGI.unescapeHTML(m['title'])
        else
          parent = topic_lookup_from_imported_post_id(m['first_post_id'])
          if parent
            mapped[:topic_id] = parent[:topic_id]
          else
            puts "Parent post #{m['first_post_id']} doesn't exist. Skipping #{m["id"]}: #{m["title"][0..40]}"
            skip = true
          end
        end

        skip ? nil : mapped
      end
    end
  end

```

Thanks for your support

---

<div class="post-metadata">

### Author: ![Cara](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@Cara](https://meta.discourse.org/u/Cara)
#### Post date: [March 30, 2018, 12:14pm UTC](https://meta.discourse.org/t/issue-with-import-script/84211/2 "2018-03-30T12:14:53Z")

</div>

I finaly achieve this by updating my punbb database and add the first\_post\_id to it with this:

```
ALTER TABLE boulette_topics ADD COLUMN first_post_id int(10);

UPDATE boulette_topics t SET first_post_id = (SELECT p.id FROM boulette_posts p WHERE p.topic_id = t.id ORDER BY p.id ASC LIMIT 1);

```
