rails-sqlserver / activerecord-sqlserver-adapter

SQL Server Adapter For Rails
MIT License
968 stars 557 forks source link

Not all data sources are found #1000

Open DCrow opened 2 years ago

DCrow commented 2 years ago

Issue

When using a database with multiple schemas, not all tables are found for method

ActiveRecord::Base.connection.data_sources

It seems to stem from this line. When ActiveRecord::Base.connection.data_source_sql is called, it's called without any parameters and because of that quoted_scope(inside that method) always returns dbo. Maybe this adapter should search all schemas like postgres adapter not just dbo?

Expected behavior

I'm expecting that all tables with different schemas are found.

Actual behavior

Tables with schemas other than dbo are not found.

How to reproduce

See example

Details

KDGundermann commented 2 years ago

See also: #998 I assume the current user (sa?) has the dbo schema as default schema. Why should we enumerate all others schemas? How should it handled if there are duplicate names?

DCrow commented 2 years ago

Why should we enumerate all others schemas?

We should enumerate since other database adapters support this. For example rails postgres adapter. It also supports which schemas to enumerate using schema_search_path option in database.yml.

How should it handled if there are duplicate names?

I think data_sources should return names with schema, so that it will be possible to differentiate same table names with different schemas. Also it should be noted that truncating tables doesn't always work if you use a table which has a different schema from default.

aidanharan commented 1 year ago

Is there an SQL Server equivalent to PostgreSQL's ANY (current_schemas(false)) (https://github.com/rails/rails/blob/6-0-stable/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L749)?

DCrow commented 1 year ago

Something like this should work

ANY(SELECT DISTINCT TABLE_SCHEMA from INFORMATION_SCHEMA.TABLES)
abhishekup5338 commented 1 month ago

Hi @DCrow assign this task to me.