Skip to content

[8.x] SqlServer schema fixes - #32957

Closed
joelharkes wants to merge 5 commits into
laravel:masterfrom
joelharkes:sql-server-fixes
Closed

joelharkes wants to merge 5 commits into
laravel:masterfrom
joelharkes:sql-server-fixes

Conversation

@joelharkes

Copy link
Copy Markdown
Contributor

Fixes #32883 #31229

summary

  • adds unit testing based on docker-compose file
  • fixes using schema's in migrations and table prefix
  • fixes user default schema issues.

Run sql server based unit integration tests (only docker required), can be added to CI easily:

docker-compose -f sqlsrv.docker-compose.yml up --exit-code-from laravel --abort-on-container-exit

open questions:

  • where do you want me to put the dockerfile and compose file, is root ok?
  • any missing tests?
  • is skip tests like i do ok?

- fix hasTable and hasColumn uses schema or uses user default schema (just like sql would)
- fix dropping default constraints only on table in right schema.
@joelharkes

Copy link
Copy Markdown
Contributor Author

Contribution guide says styleCI will be automatically fixed, is that true if not how can i fix it?

@driesvints

Copy link
Copy Markdown
Member

@joelharkes only on this repo, not on forks. It'll be automatically fixed when it's merged.

@driesvints driesvints changed the title SqlServer schema fixes [8.x] SqlServer schema fixes May 25, 2020
@driesvints

Copy link
Copy Markdown
Member

@staudenmeir your review on this would be appreciated :)

@kadevland

Copy link
Copy Markdown

@joelharkes Thx for this PR.

@GrahamCampbell

Copy link
Copy Markdown
Collaborator

If this is a bug fix PR, it should probably target 6.x, not 8.x.

@joelharkes

joelharkes commented May 26, 2020 •

Copy link
Copy Markdown
Contributor Author

@GrahamCampbell it does also slightly change behavior.

say: user default schema: test
and there is a table: dbo.users.

before:

$schema->hasTable('users') === true;
$schema->hasTable('dbo.users') === false; 
$schema->hasTable('test.users') === false; 

now:

$schema->hasTable('users') === false;
$schema->hasTable('dbo.users') === true; 
$schema->hasTable('test.users') === false; 

It might be unexpected change so probably better to take it along a new bumped version.

also tablePrefix can be a schema, eg: test.

in this case hasTable() and hasColumn() will only check tables within this schema

@taylorotwell

Copy link
Copy Markdown
Member

What exactly are the breaking changes here? How likely are the to effect the average Laravel application using SQL Server?

@joelharkes

Copy link
Copy Markdown
Contributor Author

@taylorotwell

hasTable() could now return false where it did return true before when the table is in another schema than the user schema.

After this PR:

// sqlUser schema: test, table: another.users
$schema->hasTable('users') === false;

Before:
After this PR:

// sqlUser schema: test, table: another.users
$schema->hasTable('users') === true;

you can consider it as a bug or change of behaviour.

in Sql server its possible to have same table names in different schema's so $schema->hasTable('users') could be ambiguous if you have tables: schema.users another_schema.users

@taylorotwell

Copy link
Copy Markdown
Member

I get the following errors running your docker command:

image

@taylorotwell

Copy link
Copy Markdown
Member

The second time I ran the command it worked fine.

$connection->insert($sql, $values);

if ($connection->getConfig('odbc') === true) {
$id = $this->processInsertGetIdForOdbc($connection);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is now no longer needed? This doesn't break anything?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea i'm pretty sure my solution will work for ODBC too but i havent tested it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there someone who can test this?

on obj.schema_id = schem.schema_id
where obj.type = 'U'
and obj.name = ?
and schem.name = "; // schema concatenated by builder.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this comment mean? Where does this happen?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the processor. found not better way todo this since if default schema should be used parameter is not possble (needs to user sql method)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if no schema is provided SCHEMA_NAME() constant/sql server method is used, this cannot be parameterized.

would be nice if this compile method could just return a string + array of bindings but that is not how the interface is setup.

@taylorotwell taylorotwell Jun 11, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where in the processor? What file and line number? Please be VERY specific and detailed.

}
if(!$table instanceof Blueprint){
// fix infinite loop when $tablePrefix = 'schema.';
return $this->wrapValue($table);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would there be an infinite loop?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had infinte loop while unit testing.
every time it concatenates the schema. prefix
and then in wrapSegment() it splits on the dot. if it has 2 or more segments it will call wrapTable() method and thus the loop starts over again. => you can test by removing this line and running docker compose

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example exception my tests threw before adding this part:

Error : Maximum function nesting level of '256' reached, aborting!
 E:\git\laravel-framework\src\Illuminate\Database\Grammar.php:173
 E:\git\laravel-framework\src\Illuminate\Database\Grammar.php:38
 E:\git\laravel-framework\src\Illuminate\Database\Schema\Grammars\Grammar.php:216
 E:\git\laravel-framework\src\Illuminate\Database\Schema\Grammars\SqlServerGrammar.php:891
 E:\git\laravel-framework\src\Illuminate\Database\Grammar.php:99
 E:\git\laravel-framework\src\Illuminate\Collections\Collection.php:656
 E:\git\laravel-framework\src\Illuminate\Database\Grammar.php:101
 E:\git\laravel-framework\src\Illuminate\Database\Grammar.php:65
 E:\git\laravel-framework\src\Illuminate\Database\Schema\Grammars\Grammar.php:230
 E:\git\laravel-framework\src\Illuminate\Database\Grammar.php:39
 E:\git\laravel-framework\src\Illuminate\Database\Schema\Grammars\Grammar.php:216
 E:\git\laravel-framework\src\Illuminate\Database\Schema\Grammars\SqlServerGrammar.php:891
 E:\git\laravel-framework\src\Illuminate\Database\Grammar.php:99
 E:\git\laravel-framework\src\Illuminate\Collections\Collection.php:656
 E:\git\laravel-framework\src\Illuminate\Database\Grammar.php:101
 E:\git\laravel-framework\src\Illuminate\Database\Grammar.php:65
 E:\git\laravel-framework\src\Illuminate\Database\Schema\Grammars\Grammar.php:230
 E:\git\laravel-framework\src\Illuminate\Database\Grammar.php:39
 E:\git\laravel-framework\src\Illuminate\Database\Schema\Grammars\Grammar.php:216
 E:\git\laravel-framework\src\Illuminate\Database\Schema\Grammars\SqlServerGrammar.php:891
 E:\git\laravel-framework\src\Illuminate\Database\Grammar.php:99

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current setup does not allow x. prefix it will fall in wrapSegments() funciton thinking the first part is the table second part is column. the first table part will 'again' be prefixed and thus the loop.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still have no idea what we are talking about to be honest. It feels like we are hacking around some deeper issue. Please try explain why this happens very clearly with a walk through of the code with screenshots.

public function parseSchemaAndTable($table){
$parts = explode(".", $table);
if(count($parts) == 1){
return [null, $parts[0]]; // expect dbo as default schema as it is in sql server

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this comment mean? "dbo" appears nowhere near this code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes sorry, comment should be removed. it is outdated from older code where it depended on dbo as default schema

@joelharkes

Copy link
Copy Markdown
Contributor Author

Thanks for all your feedback guys i will update the PR shortly, anything else i should be checking?

@GrahamCampbell

Copy link
Copy Markdown
Collaborator

Can you make sure the tests run when ./bin/test.sh is run?

@kadevland

Copy link
Copy Markdown

Thx a lot @joelharkes for this PR

@joelharkes

Copy link
Copy Markdown
Contributor Author

@GrahamCampbell

i would disadvise against it, or is it ok if I add the following at the end of test.sh script?

docker-compose -f sqlsrv.docker-compose.yml up --exit-code-from laravel --abort-on-container-exit

i will not be able to integrate in your existing setup:

  • would have to add sql server to your docker compose file (doable)
  • duplicate database environment variables for mysql and sqlsrv (not possible or hack in unit tests)
  • your docker image does not have the necessary Sql server drivers

also it would slow down your process. the sql server tests are relatively slow.

are you ok with that?

@GrahamCampbell

GrahamCampbell commented Jun 2, 2020 •

Copy link
Copy Markdown
Collaborator

Why do we need the extra compose file?

@GrahamCampbell

Copy link
Copy Markdown
Collaborator

It doesn't matter if that test script is slow. It's purpose is to test everything. Most of the time it won't be run, just the standard phpunit run. We need that test script to genuinely test everything.

@joelharkes

Copy link
Copy Markdown
Contributor Author

@GrahamCampbell couldnt put test in same docker-compose file because the same DB_* environment variables are used for both mysql and sqlsrv (by default).

i can try to make it work and hack my tests to depend on alternative env variables if preferred?

I also preferred my setup so i could easily run also the tests in docker-compose and not need a separate test.sh script for this. what do you think?

@joelharkes

Copy link
Copy Markdown
Contributor Author

I Have an addtional issue:
when eager loading i get the following error:

Tried to bind parameter number 2101.  SQL Server supports a maximum of 2100 parameters.

Happens when:

  • id column is not an integer.
  • more than 2100 entities are being loaded in.

How to solve:

  • can be done by chunking or something

problem:

Would need to build sqlserver specific login in the RelationShip class. to chunk by 2000 results when id column is a string. How to best solve this?

@taylorotwell

Copy link
Copy Markdown
Member

I'm not sure we should worry about working around that at the moment. It's a SQL Server limitation apparently.

@GrahamCampbell

Copy link
Copy Markdown
Collaborator

@GrahamCampbell couldnt put test in same docker-compose file because the same DB_* environment variables are used for both mysql and sqlsrv (by default).

Can you update the tests to avoid that? It definitely can be done.

if ($table instanceof Blueprint && $table->temporary) {
$this->setTablePrefix('#');
}
if(!$table instanceof Blueprint){

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove double space here after $table

@taylorotwell

Copy link
Copy Markdown
Member

Haven't heard from PR submitter in a couple weeks. Closing pending inactivity. Anyone can feel free to pick this up if they want and resubmit.

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.

8 participants