Skip to content

Read the default schema from the server with the ibm_db driver - #203

Open
aminghadersohi wants to merge 1 commit into
ibmdb:masterfrom
aminghadersohi:fix-default-schema-name
Open

aminghadersohi wants to merge 1 commit into
ibmdb:masterfrom
aminghadersohi:fix-default-schema-name

Conversation

@aminghadersohi

Copy link
Copy Markdown

Problem

On current master, DB2Dialect_ibm_db.default_schema_name is '' for ordinary host/port URLs. As a result, reflection calls without an explicit schema (inspect(conn).get_table_names(), get_columns(table), Table(..., autoload_with=conn)) find nothing.

_get_default_schema_name calls ibm_db_dbi's get_current_schema(). That returns SQL_ATTR_CURRENT_SCHEMA only when it was set explicitly; otherwise it falls back to the user argument passed to connect(). Since the create_connect_args refactor (#199), credentials go in the DSN and user is passed as "", so the fallback, and therefore the default schema, is empty. Released 0.4.4 is unaffected.

Fix

Ask the server with VALUES CURRENT SCHEMA, which reports the session's actual current schema however the connection was made (DSN credentials, CURRENTSCHEMA, or a later SET SCHEMA). It uses exec_driver_sql on SQLAlchemy 1.4+ and execute on older versions. The query runs once, at dialect initialization.

Tests

test/test_default_schema.py:

  • Connectionless: the dialect uses the server's current schema even when the DBAPI's cached value is ''.
  • Live: default_schema_name matches VALUES CURRENT SCHEMA, and a table created without a schema is found by get_table_names() and get_columns() without a schema argument.

Same test file, Python 3.11, SQLAlchemy 2.0.52, ibm-db 3.2.3, local throwaway DB2 Community Edition 11.5.9.0; only ibm_db_sa/ibm_db.py differs from master 7f3866e:

Before After
Connectionless + live 3 FAIL ('' != 'db2inst1', table not found) 3 PASS

The connectionless test also passes on SQLAlchemy 1.4.54. Ruff 0.5.0 check/format on the new test and git diff --check pass. test/test_out_parameters.py errors identically before and after (it calls Engine.execute, removed in SQLAlchemy 2.0). The full legacy suite was not run.

_get_default_schema_name used ibm_db_dbi's get_current_schema(), which
falls back to the user argument passed to connect(). create_connect_args
now puts the credentials in the DSN and passes an empty user, so the
default schema became '' and reflection without an explicit schema found
no tables or columns.

Query VALUES CURRENT SCHEMA instead, which reports the session's actual
current schema regardless of how the connection was made.

Signed-off-by: Amin Ghadersohi <amin.ghadersohi@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant