Skip to content

Conversation

@Namoshek
Copy link
Contributor

Sometimes it is quite convenient to check for the existence of something before actually executing code in a migration. We already may do that in our migrations for tables like so

public function up()
{
    if (!Schema::hasTable('users')) {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('email');
            $table->string('password');
        });
    }
}

As there is no counterpart that works for views (consistently across all supported DBMS) at the moment, this PR introduces a new function called Builder::hasView($viewName) which does exactly the same as Builder::hasTable($tableName) but for views. In some database grammars it just uses the query of compileTableExists() as fallback, but for SQL Server and SQLite it introduces a new query.

Table prefix

In the current implementation, a design decision has been made by me which utilizes the existing $this->connection->getTablePrefix() also for views. I'm not really sure if this makes sense as there are no methods on the schema builder which allow us to create views fluently yet. Therefore views may follow some different naming conventions which we are not yet aware of. I would suggest a change in this regard when going down the route to implement schema builder methods for view creation, though. Or maybe the introduction of such methods would solve this issue anyway as the normal table prefix could be used also for views.

Tested code base

The code has not yet been tested and I'm happy to receive reviews and feedback from the CI.

Why views anyway?

Because we need them for third-party systems and we like to maintain them with our existing code base.

@taylorotwell
Copy link
Member

No plans on adding to the core.

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.

2 participants