New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Better temporary tables support #22110

Merged
merged 1 commit into from Nov 22, 2017

Conversation

Projects
None yet
2 participants
@paulofreitas
Contributor

paulofreitas commented Nov 17, 2017

TL;DR

  • Currently we can't use $table->temporary(); option to create temporary tables when using the SQL Server even it supporting them - within this driver it's a no-op command (it's the only one that is missing this)

  • This PR adds this missing functionality to SQL Server, therefore making it usable with any database driver

  • Since SQL Server can create both local and global temporary tables and all other drivers can create only local temporary tables, we'll be offering this as a shortcut for creating local temporary tables - developers would still be allowed to create SQL Server's temporary tables in the driver-specific way (by prefixing their name)

  • This PR also adds missing tests for temporary tables creation

Proposal

Currently we have true temporary tables support for MySQL, PostgreSQL and SQLite. They're using the same CREATE TEMPORARY TABLE syntax based on SQL-92 standard although none of them follow it strictly - they're just vendor implementations. SQL Server temporary tables can be manually created by prefixing them with a single number sign # for local temporary tables and a double number sign ## for global temporary tables - so presently we could be using the existing $table->setTablePrefix() method to create temporary tables in SQL Server.

Though it does support temporary tables, using $table->temporary(); will be a no-op command when using SQL Server. I'm proposing that we start matching the developer expectation by creating temporary tables as requested. Since SQL Server have support for both local and global temporary tables and all other databases have support for local temporary tables only, it seems reasonable to follow their behavior.

Implementation

We'll be just overloading Illuminate\Database\Schema\Grammars\SqlServerGrammar->wrapTable() method to honor the $table->temporary option and call the SqlServerGrammar->setTablePrefix() method to prefix the table with a single number sign. It's being changed here because any method that create/modify/drop the table would need the prefixed table name.

Changes

I'm targeting this to 5.5 because it's an unsupported feature for SQL Server users - Laravel would allow you to use $table->temporary(); no matter what but it will be a no-op command. Implementing this in 5.5 would just match the usage expectation for those who were using it albeit being unsupported. I can however quickly promote it to 5.6 if prompted.

Code
  • Illuminate\Database\Schema\Grammars\SqlServerGrammar
    • Added wrapTable() method overloading to prefix the table name.
Tests

4 new tests and 8 new assertions

  • Illuminate\Tests\Database\DatabaseMySqlSchemaGrammarTest
  • Illuminate\Tests\Database\DatabasePostgresSchemaGrammarTest
  • Illuminate\Tests\Database\DatabaseSQLiteSchemaGrammarTest
  • Illuminate\Tests\Database\DatabaseSqlServerSchemaGrammarTest
    • Added missing tests for temporary tables creation in a new testCreateTemporaryTable() method.

@paulofreitas paulofreitas changed the title from [5.5] Complete temporary tables support to [5.5] Full temporary tables support Nov 18, 2017

@taylorotwell taylorotwell merged commit a3dc235 into laravel:5.5 Nov 22, 2017

2 checks passed

continuous-integration/styleci/pr The StyleCI analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@paulofreitas paulofreitas deleted the paulofreitas:full-temporary-tables-support branch Nov 22, 2017

@paulofreitas paulofreitas changed the title from [5.5] Full temporary tables support to [5.5] Better temporary tables support Nov 25, 2017

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