django-move-model icon indicating copy to clipboard operation
django-move-model copied to clipboard

sequences

Open eriktelepovsky opened this issue 7 years ago • 7 comments
trafficstars

Hi. Does it also rename sequences?

eriktelepovsky avatar Feb 01 '18 11:02 eriktelepovsky

I'm not sure I understand. Could you please provide more info?

alexei avatar Feb 01 '18 12:02 alexei

Let's say I have a model old_app.ModelA. Its primary key id points to sequence old_app_modela_id_seq.

localhost=# \d old_app_modela;
                                      Table "public.old_app_modela"
     Column      |           Type           |                             Modifiers                             
-----------------+--------------------------+-------------------------------------------------------------------
 id              | integer                  | not null default nextval('old_app_modela_id_seq'::regclass)

I decide to move it to new_app. So the next value of primary key should still refer to same sequence, but it should be renamed to new_app_modela_id_seq, like this:

localhost=# \d new_app_modela;
                                      Table "public.new_app_modela"
     Column      |           Type           |                             Modifiers                             
-----------------+--------------------------+-------------------------------------------------------------------
 id              | integer                  | not null default nextval('new_app_modela_id_seq'::regclass)

Basically, it should be done with statement:

ALTER SEQUENCE old_app_modela_id_seq RENAME TO new_app_modela_id_seq;

eriktelepovsky avatar Feb 01 '18 12:02 eriktelepovsky

I see. No, it doesn't. Should it?

alexei avatar Feb 01 '18 12:02 alexei

I think yes, it is convenient and more human readable.

Check this out, it may help you: http://marcela-campo.blogspot.sk/2015/01/django-migrations-moving-django-model.html

eriktelepovsky avatar Feb 01 '18 12:02 eriktelepovsky

Thanks for the link. I read that article before implementing this. It doesn't account for other DB engines. For example, SQLite stores sequence information inside a sqlite_sequence table and ALTER SEQUENCE will raise a syntax error. There's also MySQL, Oracle, SQL Server, DB2, Firebird etc.

TBH, the scope of this project is to automate the method described at https://stackoverflow.com/a/30613732/1639699 and, while I understand and agree to the reasoning, renaming sequences seems beyond that.

alexei avatar Feb 01 '18 13:02 alexei

Ideally renaming a sequence would be implemented as a db operation. Those, however, are not documented and currently I'm not sure how to do that.

alexei avatar Feb 01 '18 13:02 alexei

One way to implement an operation would be to simply inherit either RunPython or RunSQL and run the appropriate query (or do nothing?) depending on the engine.

alexei avatar Feb 01 '18 13:02 alexei