laravel-oci8 icon indicating copy to clipboard operation
laravel-oci8 copied to clipboard

hasTable() method does not consider views, but views and tables must be unique in Oracle

Open cbj4074 opened this issue 5 years ago • 4 comments

Summary of problem or feature request

It would be ideal either to modify the hasTable() method to include views (i.e., union between all_tables and all_views), or at the very least, add a separate method, hasView().

Code snippet of problem

The hasTable() method searches only for tables (not views), which makes sense, but the problem is that there's no way to create a view only if it doesn't already exist:

if (!Schema::connection('foo')->hasTable('view_users')) {
    // If "view_users" is a view and not a table, this block is executed, but the view fails
    // to be created if a table with the same name already exists, as table and view
    // names share a namespace in Oracle and must therefore be unique.
}

System details

  • Ubuntu 18.04 LTS
  • 7.3.11
  • 6.0.3
  • dev-master c6091cf

cbj4074 avatar Nov 04 '19 20:11 cbj4074

Does other database of Laravel have hasView on their schema? I prefer hasView but it might be too specific for Oracle and might cause some issues when switching database.

On the other hand, why not just use create or replace view?

yajra avatar Nov 05 '19 01:11 yajra

@yajra Thanks for taking a look!

No, Laravel core does not include a hasView method, which is unfortunate, because every database that Laravel supports implements views. Maybe a PR that adds hasView to Laravel core is preferable.

Sure, I can use a raw SQL statement to CREATE OR REPLACE VIEW ..., but I generally prefer to avoid using raw statements in migrations. That said, this approach is certainly more efficient than executing the two queries required to check if the view exists first, and then create it if not.

Do you think it makes more sense to see if Laravel will accept a PR that adds hasView()?

cbj4074 avatar Nov 05 '19 14:11 cbj4074

I currently have a use case for a hasView function (outside of a migration) so I thought I'd share what I've found while researching.

As @cbj4074 points out Laravel core does not include a hasView function at this time. A PR was created in 2018 to add this, but it was rejected.

It has recently been suggested again though, but nothing has come of it at this point.

In the suggestion it is pointed out that a package (laravel-migration-views) exists which does support it, but that does not have support for Oracle. I also don't know if @staudenmeir plans on adding support for Oracle.

There are multiple ways hasView can be added, but I'm not sure which is the best:

  • Create a PR to the Laravel core to add hasView and add the implementation for Oracle here.
  • Create a PR to laravel-migration-views with support for Oracle.
  • Include laravel-migration-views in the composer.json of this package and add the implementation for Oracle here using existing laravel-migration-views code.
  • Create hasView from scratch in this package.

What do you prefer, @yajra?

Luukvdo avatar Nov 20 '19 14:11 Luukvdo

@Luukvdo thanks for these info. I've checked laravel-migration-views and I think it would be better if we create a PR on that package to support Oracle. This way, it would not cause us some troubles incase we decided to switch database in our projects. It would also be optional since other users who needs interaction with views can just install the package as needed.

yajra avatar Nov 21 '19 00:11 yajra

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Oct 26 '22 03:10 github-actions[bot]

This issue was closed because it has been inactive for 7 days since being marked as stale.

github-actions[bot] avatar Nov 02 '22 03:11 github-actions[bot]