Skip to content

Commit

Permalink
Merge pull request #791 from cakephp/allow-underscore
Browse files Browse the repository at this point in the history
Allow column names to start with underscore
  • Loading branch information
othercorey authored Nov 1, 2021
2 parents cbeae91 + 887cafb commit e559b5f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 22 deletions.
12 changes: 6 additions & 6 deletions src/Command/FixtureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Console\Exception\StopException;
use Cake\Core\Configure;
use Cake\Database\Exception;
use Cake\Database\Schema\TableSchemaInterface;
Expand Down Expand Up @@ -168,7 +167,7 @@ protected function bake(string $model, string $useTable, Arguments $args, Consol
$data = $this->readSchema($model, $useTable);
}

$this->validateNames($data);
$this->validateNames($data, $io);

if ($modelImport === null) {
$schema = $this->_generateSchema($data);
Expand Down Expand Up @@ -214,15 +213,16 @@ public function readSchema(string $name, string $table): TableSchemaInterface
* Validates table and column names are supported.
*
* @param \Cake\Database\Schema\TableSchemaInterface $schema Table schema
* @param \Cake\Console\ConsoleIo $io Console io
* @return void
* @throws \Cake\Console\Exception\StopException When table or column names are not supported
*/
public function validateNames(TableSchemaInterface $schema): void
public function validateNames(TableSchemaInterface $schema, ConsoleIo $io): void
{
foreach ($schema->columns() as $column) {
if (!is_string($column) || !ctype_alpha($column[0])) {
throw new StopException(sprintf(
'Unable to bake table with integer column names or names that start with digits. Found `%s`.',
if (!is_string($column) || (!ctype_alpha($column[0]) && $column[0] !== '_')) {
$io->abort(sprintf(
'Unable to bake model. Table column names must start with a letter or underscore. Found `%s`.',
(string)$column
));
}
Expand Down
12 changes: 6 additions & 6 deletions src/Command/ModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Console\Exception\StopException;
use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\Driver\Sqlserver;
Expand Down Expand Up @@ -109,7 +108,7 @@ public function bake(string $name, Arguments $args, ConsoleIo $io): void
{
$table = $this->getTable($name, $args);
$tableObject = $this->getTableObject($name, $table);
$this->validateNames($tableObject->getSchema());
$this->validateNames($tableObject->getSchema(), $io);
$data = $this->getTableContext($tableObject, $table, $name, $args, $io);
$this->bakeTable($tableObject, $data, $args, $io);
$this->bakeEntity($tableObject, $data, $args, $io);
Expand All @@ -121,15 +120,16 @@ public function bake(string $name, Arguments $args, ConsoleIo $io): void
* Validates table and column names are supported.
*
* @param \Cake\Database\Schema\TableSchemaInterface $schema Table schema
* @param \Cake\Console\ConsoleIo $io Console io
* @return void
* @throws \Cake\Console\Exception\StopException When table or column names are not supported
*/
public function validateNames(TableSchemaInterface $schema): void
public function validateNames(TableSchemaInterface $schema, ConsoleIo $io): void
{
foreach ($schema->columns() as $column) {
if (!is_string($column) || !ctype_alpha($column[0])) {
throw new StopException(sprintf(
'Unable to bake table with integer column names or names that start with digits. Found `%s`.',
if (!is_string($column) || (!ctype_alpha($column[0]) && $column[0] !== '_')) {
$io->abort(sprintf(
'Unable to bake model. Table column names must start with a letter or underscore. Found `%s`.',
(string)$column
));
}
Expand Down
26 changes: 21 additions & 5 deletions tests/TestCase/Command/FixtureCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use Bake\Command\FixtureCommand;
use Bake\Test\TestCase\TestCase;
use Cake\Console\Exception\StopException;
use Cake\Console\ConsoleIo;
use Cake\Console\Shell;
use Cake\Core\Plugin;
use Cake\Database\Driver\Postgres;
Expand Down Expand Up @@ -60,17 +60,33 @@ public function setUp(): void
/**
* Tests validating supported table and column names.
*/
public function testValidateNames(): void
public function testValidateNamesWithValid(): void
{
$command = new FixtureCommand();
$command->connection = 'test';

$schema = $command->readSchema('Car', 'car');
$schema->addColumn('_valid', ['type' => 'string', 'length' => null]);

$io = $this->createMock(ConsoleIo::class);
$io->expects($this->never())->method('abort');
$command->validateNames($schema, $io);
}

/**
* Tests validating supported table and column names.
*/
public function testValidateNamesWithInvalid(): void
{
$command = new FixtureCommand();
$command->connection = 'test';

$schema = $command->readSchema('Car', 'car');
$schema->addColumn('0invalid', ['type' => 'string', 'length' => null]);

$this->expectException(StopException::class);
$this->expectExceptionMessage('Unable to bake table with integer column names or names that start with digits. Found `0invalid`');
$command->validateNames($schema);
$io = $this->createMock(ConsoleIo::class);
$io->expects($this->once())->method('abort');
$command->validateNames($schema, $io);
}

/**
Expand Down
25 changes: 20 additions & 5 deletions tests/TestCase/Command/ModelCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\Exception\StopException;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Database\Driver\Mysql;
Expand Down Expand Up @@ -164,17 +163,33 @@ public function testGetTableObjectPrefix()
/**
* Tests validating supported table and column names.
*/
public function testValidateNames(): void
public function testValidateNamesWithValid(): void
{
$command = new ModelCommand();
$command->connection = 'test';

$schema = $command->getTableObject('TodoItems', 'todo_items')->getSchema();
$schema->addColumn('_valid', ['type' => 'string', 'length' => null]);

$io = $this->createMock(ConsoleIo::class);
$io->expects($this->never())->method('abort');
$command->validateNames($schema, $io);
}

/**
* Tests validating supported table and column names.
*/
public function testValidateNamesWithInvalid(): void
{
$command = new ModelCommand();
$command->connection = 'test';

$schema = $command->getTableObject('TodoItems', 'todo_items')->getSchema();
$schema->addColumn('0invalid', ['type' => 'string', 'length' => null]);

$this->expectException(StopException::class);
$this->expectExceptionMessage('Unable to bake table with integer column names or names that start with digits. Found `0invalid`');
$command->validateNames($schema);
$io = $this->createMock(ConsoleIo::class);
$io->expects($this->once())->method('abort');
$command->validateNames($schema, $io);
}

/**
Expand Down

0 comments on commit e559b5f

Please sign in to comment.