0

I'm in the process of migrating a Ruby on Rails application from MySQL to Postgres. Is there a recommended way to keep the deleted data, like all the deleted records (their IDs at least) from MySQL?

In testing a dump-and-restore didn't seem to keep deleted records.

Also, in the event that I manage to keep the records where they are, what'll happen with the blank ones in Postgres? Will they be skipped over or used?

Example

Say I have a user with an ID of 101 and I've deleted users up to 100. I need 101 to stay at 101.

2
  • 1
    If data is deleted, it doesn't exist in the DB anymore, so you can't keep it. This doesn't make sense. Are you talking about AUTO_INCREMENT keys and PostgreSQL SERIAL columns? Commented Sep 19, 2014 at 1:17
  • I'll update the question with an example. Commented Sep 19, 2014 at 1:18

1 Answer 1

2

So you don't want to reassign the IDs assigned to records where you generated keys.

That should be the default in any sane migration. When you copy the data rows over - say, exporting from MySQL with SELECT ... INTO OUTFILE and importing into PostgreSQL with COPY tablename FROM 'filename.csv' WITH (FORMAT CSV), the IDs won't change.

All you'll need to do is to set the next ID to be generated in the sequence on the PostgreSQL table afterwards. So, say you have the table:

CREATE TABLE users
(
    id serial primary key,
    name text not null,
    ...
);

and you've just copied a user with id = 101 into it.

You'll now just assign a new value to the key generation sequence for the table, e.g.:

SELECT setval('users_id_seq', (SELECT max(id) FROM users)+1);

To learn more about sequences and key generation in PostgreSQL, see SERIAL in the numeric types documentation, the documentation for CREATE SEQUENCE, the docs for setval, etc. The default name for a key generation sequence is tablename_columnname_seq.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh good! I started to panic when I thought this might be a problem. Will I have to assign new values to the key generation sequence for every table individually?
@emm Yep. I'd script it personally. The above assumes you're migrating the data table-by-table as CSV. If you're using some kind of Rails-level data migration then everything might be different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.