-
-
Notifications
You must be signed in to change notification settings - Fork 263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Batch migration on SQLite truncate TIMESTAMP values just to the year #391
Comments
Jiri Bajer (@sarimak) wrote: alembic (0.8.6), SQLAlchemy (1.0.12), Python 3.5.2, Ubuntu 16.04.1 |
Changes by Jiri Bajer (@sarimak):
|
Michael Bayer (@zzzeek) wrote: Well, SQLite thinks this makes sense:
Which of course is a bug in SQLite, just like the fact that it does not support ALTER is a bug, and neither will ever be fixed. Why are you changing the datatype on SQLite? If you already have TIMESTAMP, there's no need to specify type_. Works fine if you just omit it. I can see documentation notes here and maybe at some point the ability to inject custom casting functions, but that's about it. |
Jiri Bajer (@sarimak) wrote: Aha, I thought that type_ is just a hint for Alembic. I had to use it because I use a custom subclass of TIMESTAMP (via TypeDecorator) with redefined process_result_value and process_bind_param so I can use timezone-aware timestamps in my code and they are persisted as converted to local timezone and timezone-naiive. My primary database is MariaDB and it choked if I didn't specify the type:
BTW: I know that persisting in any other timezone than UTC is a bad practice but I was forced to do so by an existing PHP GUI which relies on it... |
Jiri Bajer (@sarimak) wrote: The problem can be worked around in the migration script by detecting the dialect and conditionally adding the type_ cast only for MySQL. Far from elegant but at least works:
|
Michael Bayer (@zzzeek) wrote: im assuming you're doing batch to avoid production table locking in MySQL, right? if sqlite is not your production platform I'd just forego doing migrations with it. |
Michael Bayer (@zzzeek) wrote: it's possible that batch mode shouldn't do CAST() for sqlite at all. SQLite doesn't really care about the data in the column. |
Jiri Bajer (@sarimak) wrote: My original motivation for the batch mode (and for the SQLAlchemy/Alembic combo as well) was: I want to develop and test primarily on local SQLite before each commit and re-test on remote MySQL before finishing the two week increment. I strongly prefer having a single codebase which handles both backends reasonably well. As my migration scripts need to do ALTER TABLE and SQLite lacks a native support for non-trivial changes, I followed http://alembic.zzzcomputing.com/en/latest/batch.html and switched my migration code to batch mode so SQLite is handled and MySQL doesn't care. I don't care that much about MySQL locking because I can afford to migrate offline (with the web application shut down). But I have 200k rows now in the MySQL (SQLite DB is tiny) and will have much more (~10M) so migrating the DDL under a minute is fine for my use case. |
Michael Bayer (@zzzeek) wrote: yeah I don't like that use case which is why I resisted having any kind of special SQLite feature for a long time. SQLite really causes lots more trouble than it's worth. mysql installs easily. |
Jiri Bajer (@sarimak) wrote: Thank you for the clarification. I am currently using my workaround and works fine. Note: I have realized that I need it whenever I call alter_column() within a batch_op. This also means whenever I am adding a column with nullable=False to an existing table (existing rows do not have any value for it so I have to add it with nullable=True, update the data and updathe the column back to nullable=False). Which happens quite often in my case. |
Michael Bayer (@zzzeek) wrote: cast() types in batch only if type_affinity is different Batch mode will not use CAST() to copy data if type_ is given, however Change-Id: I20f7b399cd3cd7740d67ff7d624aa1da874ebc71 → 3926ac6 |
Changes by Michael Bayer (@zzzeek):
|
Migrated issue, originally created by Jiri Bajer (@sarimak)
Value stored in TIMESTAMP column of SQLite DB gets truncated to its year after migration in batch mode:
model.py:
init.py:
env.py:
revisions/123456789123_v1.py
The text was updated successfully, but these errors were encountered: