sqlalchemy / alembic

A database migrations tool for SQLAlchemy.
MIT License
2.85k stars 247 forks source link

No time zone data on Windows, error setting `timezone` in `alembic.ini` #1527

Closed floxay closed 2 months ago

floxay commented 2 months ago

Describe the bug Setting timezone to --for example-- UTC on Windows will fail with the error Can't locate timezone: UTC as Windows does not have time zone data. Related docs: https://docs.python.org/3/library/zoneinfo.html#data-sources

Expected behavior Setting timezone in alembic.ini should work on Windows "out of the box". Or alembic should at least provide a better error message in this case.

To Reproduce On Windows use this config and attempt to create a new migration.

timezone = UTC

Error

Traceback (most recent call last):
  File "C:\Program Files\Python312\Lib\runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "c:\Users\floxay\.vscode\extensions\ms-python.debugpy-2024.11.2024082901-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 71, in <module>
    cli.main()
  File "c:\Users\floxay\.vscode\extensions\ms-python.debugpy-2024.11.2024082901-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 486, in main
    run()
  File "c:\Users\floxay\.vscode\extensions\ms-python.debugpy-2024.11.2024082901-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 369, in run_module
    run_module_as_main(options.target, alter_argv=True)
  File "c:\Users\floxay\.vscode\extensions\ms-python.debugpy-2024.11.2024082901-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 238, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\.vscode\extensions\ms-python.debugpy-2024.11.2024082901-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "c:\Users\floxay\x\.venv\Lib\site-packages\litestar\__main__.py", line 10, in <module>
    run_cli()
  File "c:\Users\floxay\x\.venv\Lib\site-packages\litestar\__main__.py", line 6, in run_cli
    litestar_group()
  File "c:\Users\floxay\x\.venv\Lib\site-packages\rich_click\rich_command.py", line 367, in __call__
    return super().__call__(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\x\.venv\Lib\site-packages\click\core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\x\.venv\Lib\site-packages\rich_click\rich_command.py", line 152, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\x\.venv\Lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\x\.venv\Lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\x\.venv\Lib\site-packages\click\core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\x\.venv\Lib\site-packages\click\core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\x\.venv\Lib\site-packages\click\decorators.py", line 33, in new_func
    return f(get_current_context(), *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\x\.venv\Lib\site-packages\litestar\cli\_utils.py", line 254, in wrapped
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\x\.venv\Lib\site-packages\click\decorators.py", line 33, in new_func
    return f(get_current_context(), *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\x\.venv\Lib\site-packages\litestar\cli\_utils.py", line 254, in wrapped
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\x\.venv\Lib\site-packages\advanced_alchemy\extensions\litestar\cli.py", line 226, in create_revision
    alembic_commands.revision(
  File "c:\Users\floxay\x\.venv\Lib\site-packages\advanced_alchemy\alembic\commands.py", line 176, in revision
    return migration_command.revision(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\x\.venv\Lib\site-packages\alembic\command.py", line 243, in revision
    scripts = [script for script in revision_context.generate_scripts()]
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\x\.venv\Lib\site-packages\alembic\autogenerate\api.py", line 650, in generate_scripts
    yield self._to_script(generated_revision)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\x\.venv\Lib\site-packages\alembic\autogenerate\api.py", line 555, in _to_script
    return self.script_directory.generate_revision(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\x\.venv\Lib\site-packages\alembic\script\base.py", line 689, in generate_revision
    create_date = self._generate_create_date()
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\floxay\x\.venv\Lib\site-packages\alembic\script\base.py", line 624, in _generate_create_date
    raise util.CommandError(
alembic.util.exc.CommandError: Can't locate timezone: UTC

Versions.

Additional context Adding tzdata as a dependency on Windows as recommended in the zoneinfo documentation solves the issue.

CaselIT commented 2 months ago

Hi,

I can't reproduce. I don't know what python version you have, but trying in an empty env works without issues:

$ pip list
Package    Version
---------- -------
pip        24.2
setuptools 72.1.0
wheel      0.43.0

$ python
Python 3.12.4 | packaged by Anaconda, Inc. | (main, Jun 18 2024, 15:03:56) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import zoneinfo
>>> zoneinfo.ZoneInfo('UTC')
zoneinfo.ZoneInfo(key='UTC')
>>>