cakephp icon indicating copy to clipboard operation
cakephp copied to clipboard

Fixture could not be created in SQL Server when using schema

Open stmeyer opened this issue 10 years ago • 6 comments

Preamble

This is a (multiple allowed):

  • [x] bug
  • [ ] enhancement
  • [ ] feature-discussion (RFC)

Fixture could not be created in SQL Server when using schema

CakePHP Version: 3.2.3 Platform and Target: IIS 7.5, SQL Server 2014, PHP 5.6

What you did

I wrote a fixture with a different schema than 'dbo':

 public $table = 'factories.assign_components';

The table fixture could not be created, due to schema. Writing a fixture, without schema works as expected. The bug seems to be in Cake\Database\Schema\SqlserverSchema::truncateTableSql

Expected Behavior

The method should generate a SQL like

DBCC CHECKIDENT('factories.assign_components', RESEED, 0)

There sould be a single quote around the table name

Actual Behavior

the generated query look like this:

DBCC CHECKIDENT(factories.assign_components, RESEED, 0)

So the single quotes are missing around the table name. In SQL Server documentation (https://msdn.microsoft.com/de-de/library/ms176057%28v=sql.120%29.aspx) the single quotes for the table name are also used.

Fix

Add single quotes to the query:


    /**
     * {@inheritDoc}
     */
    public function truncateTableSql(Table $table)
    {
        $name = $this->_driver->quoteIdentifier($table->name());
        $queries = [
            sprintf('DELETE FROM %s', $name)
        ];

        // Restart identity sequences
        $pk = $table->primaryKey();
        if (count($pk) === 1) {
            $column = $table->column($pk[0]);
            if (in_array($column['type'], ['integer', 'biginteger'])) {
                $queries[] = sprintf(
                    "DBCC CHECKIDENT('%s', RESEED, 0)",
                    $name
                );
            }
        }
        return $queries;
    }

Sorry, but i have an other pull request open, so that i can not create a new one.

stmeyer avatar Feb 22 '16 15:02 stmeyer

~~@steefaan~~ @stmeyer for future fun with PRs, you can create a new branch (say, other-issue from master) and PR that branch as a separate PR against master. Changes to other-issue branch will only show up to that PR, likewise for the other. Think of PRs like branches (because they are, essentially!).

Not saying you need to do that here, but just wanted to let you know! (Also, if your git skills aren't up to the task, no worries! The core team will always help out :smile:.)

jeremyharris avatar Feb 22 '16 15:02 jeremyharris

@jeremyharris Why you mentoined me here? :)

steefaan avatar Feb 22 '16 15:02 steefaan

@steefaan because I'm a dummy that believed Github's autocomplete would be smarter than it is :P sorry man

jeremyharris avatar Feb 22 '16 15:02 jeremyharris

@stmeyer would you mind sending a pull request with your fix?

lorenzo avatar Feb 22 '16 18:02 lorenzo

I can take care of a pull request for this. @stmeyer Mentioned he was a bit stuck as he's already made changes to his master branch that haven't been merged in.

markstory avatar Feb 22 '16 19:02 markstory

Pull request open.

markstory avatar Feb 24 '16 02:02 markstory