polo icon indicating copy to clipboard operation
polo copied to clipboard

Handle id sequences with care

Open volkanunsal opened this issue 8 years ago • 9 comments

I realized that after restoring the dump file generated by Polo, I still had to reset the id sequences postgres uses to determine the next id for a new record. This entails looking up the highest id number in the imported rows, and running the following command for every table:

ALTER SEQUENCE users_id_seq RESTART WITH 208

It would be great if Polo did this automatically, though.

volkanunsal avatar Jul 08 '16 16:07 volkanunsal

hey, @volkanunsal. Long time no PR 😂. We missed you!

This is an interesting idea. Though I must say this is pretty PostgreSQL specific, so I'm not sure how it fits in the scope of Polo.

At the same time, I think this is something we could try to squeeze in the postgres adapter. What do you think?

Wanna take a stab at it?

nettofarah avatar Jul 08 '16 21:07 nettofarah

@nettofarah Ha, ok, I'll give it a try.

volkanunsal avatar Jul 11 '16 15:07 volkanunsal

This is a feature I would also find very useful

ghost avatar Jul 20 '16 21:07 ghost

Yeah, I was thinking about this today. I think we could write a class for data ingestion.

We could add an import method to Polo that you could run in development/test. That method could then delegate to some adapter specific classes.

What do you guys think?

nettofarah avatar Jul 20 '16 22:07 nettofarah

Depends on if you want to bake this into the default Polo.explore for postgres or not. If we always want the 'ALTER SEQUENCE' statement in the end, then you can just modify the existing #ignore_transform so it tacks on the 'ALTER SEQUENCE' statement at the end.

I'm already doing something similar:

File.open('filename') do |f|
   f << Polo.explore(klass, ids).join(";\n")
   f << ";\nALTER SEQUENCE #{klass.table_name}_id_seq RESTART WITH #{ids.max+1};\n"
end

ghost avatar Jul 20 '16 22:07 ghost

You can see how we handle this in Brillo (a Polo wrapper with some extra conventions) here

bessey avatar Feb 23 '17 00:02 bessey

@bessey maybe we can join forces and try to bring this feature over to Polo?

nettofarah avatar Feb 23 '17 00:02 nettofarah

Indeed, this is one of the parts of Brillo that would probably fit better in Polo. It doesn't seem like the API would be affected at all either, since you already have a notion of DB specific adapters in Polo.

Potentially we'd just need something like

https://github.com/IFTTT/polo/blob/master/lib/polo/sql_translator.rb#L23

    def to_sql
      case @configuration.on_duplicate_strategy
      when :ignore
        @adapter.ignore_transform(inserts, records)
      when :override
        @adapter.on_duplicate_key_update(inserts, records)
      else inserts
--    end
++    end + @adapter.table_footer(record.class.arel_table)
    end

bessey avatar Feb 23 '17 01:02 bessey

niice! I like that. We could even be a little extra defensive and do something like:

if @adapter.respond_to?(:table_footer)
  ...
end

nettofarah avatar Feb 23 '17 05:02 nettofarah