# Trouble with \`discourse remap\` remapping \`topic\_links url\`

**URL:** https://meta.discourse.org/t/trouble-with-discourse-remap-remapping-topic-links-url/110052
**Category:** Bug
**Created:** [2월 25, 2019, 5:31오후 UTC](https://meta.discourse.org/t/trouble-with-discourse-remap-remapping-topic-links-url/110052 "2019-02-25T17:31:20Z")
**Posts on this page:** 8
**Page:** 1

<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: [2월 25, 2019, 5:31오후 UTC](https://meta.discourse.org/t/trouble-with-discourse-remap-remapping-topic-links-url/110052/1 "2019-02-25T17:31:20Z")

</div>

I’m trying to

```
discourse remap community.x.com staging.community.x.com

```

as described [here](https://meta.discourse.org/t/replace-a-string-in-all-posts/48729) (edit: Hmm. Maybe I should have made this a post over there?) and am getting this error:

```plaintext
Remapping topic_links url
Error: ERROR: value too long for type character varying(500)
The remap has only been partially applied due to the error above. Please re-run the script again.

```

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [2월 25, 2019, 5:33오후 UTC](https://meta.discourse.org/t/trouble-with-discourse-remap-remapping-topic-links-url/110052/2 "2019-02-25T17:33:55Z")

</div>

Can you share the value for the column `url` in the table topic\_links where the length is that big? Is even a valid url?

```plaintext
psql
\x
SELECT * FROM topic_links WHERE length(url) > 450;

```

---

<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: [2월 25, 2019, 5:40오후 UTC](https://meta.discourse.org/t/trouble-with-discourse-remap-remapping-topic-links-url/110052/3 "2019-02-25T17:40:59Z")

</div>

(Thanks for the `SELECT`!)

Looks like it’s a long [http://site/new-topic?body=a%20bunch%20of%20stuff](http://site/new-topic?body=a%20bunch%20of%20stuff) that’s to blame. Looks like it go truncated to 500 chars and so when we try to replace `HOSTNAME` with staging.`HOSTNAME` we’re out of luck.

P.S. If someone else wants to do that select, first do this:

```plaintext
./launcher enter app
su discourse

```

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [2월 25, 2019, 5:42오후 UTC](https://meta.discourse.org/t/trouble-with-discourse-remap-remapping-topic-links-url/110052/4 "2019-02-25T17:42:46Z")

</div>

We could move that column to `character varying(2000)` (or even `text` and keep the validation on Ruby) but I’m not sure if it’s worth the hassle of a migration.

Since the remap is smart enough to just skip this line, I guess all is ok in the end?

---

<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: [2월 25, 2019, 5:45오후 UTC](https://meta.discourse.org/t/trouble-with-discourse-remap-remapping-topic-links-url/110052/5 "2019-02-25T17:45:05Z")

</div>

Unless `topic_links` is the last table it’s handling, it looks like it’s quitting rather than just skipping.

---

<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: [2월 28, 2019, 3:47오후 UTC](https://meta.discourse.org/t/trouble-with-discourse-remap-remapping-topic-links-url/110052/6 "2019-02-28T15:47:22Z")

</div>

Hey @Falco, would truncating the URL in line 120 fix this?

change it to

```
    uri = UrlHelper.relaxed_parse(u.url[0..self.max_url_length])

```

> <https://github.com/discourse/discourse/blob/main/app/models/topic_link.rb#L120>

EDIT: well, on my first attempt, this yielded

```plaintext
Remapping topic_links url
Error: ERROR: duplicate key value violates unique constraint "unique_post_links"
DETAIL: Key (topic_id, post_id, url)=(19484, 61049, https://staging.community.rstudio.com/t/birds-of-a-feather-bof-at-rstudio-conf-2019l/19328) already exists.
The remap has only been partially applied due to the error above. Please re-run the script again.

```

So short of a database migration to make the URL longer. . . maybe add another `rescue` on duplicate URLs and let them pass? So also add a

```
    rescue PG::UniqueViolation

```

that does nothing?

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [2월 28, 2019, 4:43오후 UTC](https://meta.discourse.org/t/trouble-with-discourse-remap-remapping-topic-links-url/110052/7 "2019-02-28T16:43:50Z")

</div>

Since this is the very first time someone hits this, I would go with a simple approach of fixing the few affected rows.

Something like:

```SQL
UPDATE topic_links
SET url = substring(url FOR 450)
WHERE length(url) > 450;

```

---

<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: [2월 28, 2019, 5:09오후 UTC](https://meta.discourse.org/t/trouble-with-discourse-remap-remapping-topic-links-url/110052/8 "2019-02-28T17:09:22Z")

</div>

I’ll give that a shot. My solution was to add a `next if parsed.length > 500` to `self.extract_from(post) in `topic\_link.rb`. It seems like that would fix it for everyone. I think that it worked on the site where I am doing the remapping, but then I’m getting

```plaintext
Error: ERROR: duplicate key value violates unique constraint "unique_post_links"
DETAIL: Key (topic_id, post_id, url)=(19484, 61049, https://staging.community.rstudio.com/t/birds-of-a-feather-bof-at-rstudio-conf-2019l/19328) already exists.
The remap has only been partially applied due to the error above. Please re-run the script again.

```

More on this over at [Migrate\_to\_s3 for Digital Ocean Spaces woes - #12 by pfaffman](https://meta.discourse.org/t/migrate-to-s3-for-digital-ocean-spaces-woes/109662/12). I hit the same `topic_link` problem doing the S3 migrate on another site.
