jonbiemond / BCIT-Flex

A website for easily navigating BCIT courses and offerings.
https://www.bcitflex.tech
MIT License
10 stars 8 forks source link

Alembic pre-commit hook fails in no-db projects #92

Open jonbiemond opened 5 months ago

jonbiemond commented 5 months ago

The Alembic pre-commit check requires a database connection or it fails. Perhaps it could skip instead?

          I was able to finish a simple error page, with a test file, but when I tried to commit to my forked repo I got this error
alembic-check............................................................Failed
- hook id: alembic-check
- exit code: 1

Traceback (most recent call last):
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 145, in __init__
    self._dbapi_connection = engine.raw_connection()
                             ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 3288, in raw_connection
    return self.pool.connect()
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 452, in connect
    return _ConnectionFairy._checkout(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 1267, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 716, in checkout
    rec = pool._do_get()
          ^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/impl.py", line 284, in _do_get
    return self._create_connection()
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 393, in _create_connection
    return _ConnectionRecord(self)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 678, in __init__
    self.__connect()
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 902, in __connect
    with util.safe_reraise():
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 147, in __exit__
    raise exc_value.with_traceback(exc_tb)
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 898, in __connect
    self.dbapi_connection = connection = pool._invoke_creator(self)
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/engine/create.py", line 637, in connect
    return dialect.connect(*cargs, **cparams)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 615, in connect
    return self.loaded_dbapi.connect(*cargs, **cparams)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/psycopg2/__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused
    Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
    Is the server running on that host and accepting TCP/IP connections?

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/bin/alembic", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/alembic/config.py", line 630, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/alembic/config.py", line 624, in main
    self.run_cmd(cfg, options)
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/alembic/config.py", line 601, in run_cmd
    fn(
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/alembic/command.py", line 289, in check
    script_directory.run_env()
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/alembic/script/base.py", line 578, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/alembic/util/pyfiles.py", line 93, in load_python_file
    module = load_module_py(module_id, path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/alembic/util/pyfiles.py", line 109, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap_external>", line 994, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/Users/amoghbantwal/Documents/GitHub/BCIT-Flex/bcitflex/alembic/env.py", line 90, in <module>
    run_migrations_online()
  File "/Users/amoghbantwal/Documents/GitHub/BCIT-Flex/bcitflex/alembic/env.py", line 75, in run_migrations_online
    with connectable.connect() as connection:
         ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 3264, in connect
    return self._connection_cls(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 147, in __init__
    Connection._handle_dbapi_exception_noconnection(
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 2426, in _handle_dbapi_exception_noconnection
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 145, in __init__
    self._dbapi_connection = engine.raw_connection()
                             ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 3288, in raw_connection
    return self.pool.connect()
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 452, in connect
    return _ConnectionFairy._checkout(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 1267, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 716, in checkout
    rec = pool._do_get()
          ^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/impl.py", line 284, in _do_get
    return self._create_connection()
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 393, in _create_connection
    return _ConnectionRecord(self)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 678, in __init__
    self.__connect()
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 902, in __connect
    with util.safe_reraise():
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 147, in __exit__
    raise exc_value.with_traceback(exc_tb)
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 898, in __connect
    self.dbapi_connection = connection = pool._invoke_creator(self)
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/engine/create.py", line 637, in connect
    return dialect.connect(*cargs, **cparams)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 615, in connect
    return self.loaded_dbapi.connect(*cargs, **cparams)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amoghbantwal/Library/Caches/pypoetry/virtualenvs/bcitflex-Q0AGhald-py3.12/lib/python3.12/site-packages/psycopg2/__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "localhost" (::1), port 5432 failed: Connection refused
    Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
    Is the server running on that host and accepting TCP/IP connections?

(Background on this error at: https://sqlalche.me/e/20/e3q8)

Originally posted by @abant07 in https://github.com/jonbiemond/BCIT-Flex/issues/91#issuecomment-1920474281

abant07 commented 5 months ago

How should I skip it? Because it won't let me commit even to my forked repo

jonbiemond commented 5 months ago

Hey @abant07, see my comment https://github.com/jonbiemond/BCIT-Flex/issues/91#issuecomment-1922037507 for a work around. I created this issue to implement a better fix in the future.