Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/BaseSeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ public function shouldExecute(): bool
public function call(string $seeder, array $options = []): void
{
$io = $this->getIo();
assert($io !== null, 'Requires ConsoleIo');
if ($io === null) {
throw new RuntimeException('ConsoleIo is required for calling other seeders.');
}
$io->out('');
$io->out(
' ====' .
Expand Down Expand Up @@ -245,7 +247,9 @@ protected function runCall(string $seeder, array $options = []): void
'connection' => $options['connection'] ?? $connection,
]);
$io = $this->getIo();
assert($io !== null, 'Missing ConsoleIo instance');
if ($io === null) {
throw new RuntimeException('ConsoleIo is required for running seeders.');
}
$manager = $factory->createManager($io);
$manager->seed($seeder);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int

return self::CODE_SUCCESS;
}
$io->error("An error occurred while writing dump file `{$filePath}`");
$io->err("<error>An error occurred while writing dump file `{$filePath}`</error>");

return self::CODE_ERROR;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,9 @@ protected function getRenameColumnInstructions(string $tableName, string $column
foreach ($rows as $row) {
if (strcasecmp($row['Field'], $columnName) === 0) {
$null = $row['Null'] === 'NO' ? 'NOT NULL' : 'NULL';
$comment = isset($row['Comment']) ? ' COMMENT ' . '\'' . addslashes($row['Comment']) . '\'' : '';
$comment = isset($row['Comment']) && $row['Comment'] !== ''
? ' COMMENT ' . $this->getConnection()->getDriver()->schemaValue($row['Comment'])
: '';

// create the extra string by also filtering out the DEFAULT_GENERATED option (MySQL 8 fix)
$extras = array_filter(
Expand Down
15 changes: 10 additions & 5 deletions src/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,11 @@ public function getPhinxType(string $sqlType): string
public function createDatabase(string $name, array $options = []): void
{
$charset = $options['charset'] ?? 'utf8';
$this->execute(sprintf("CREATE DATABASE %s WITH ENCODING = '%s'", $name, $charset));
$this->execute(sprintf(
'CREATE DATABASE %s WITH ENCODING = %s',
$this->quoteSchemaName($name),
$this->quoteString($charset),
));
}

/**
Expand All @@ -995,7 +999,7 @@ public function hasDatabase(string $name): bool
public function dropDatabase($name): void
{
$this->disconnect();
$this->execute(sprintf('DROP DATABASE IF EXISTS %s', $name));
$this->execute(sprintf('DROP DATABASE IF EXISTS %s', $this->quoteSchemaName($name)));
$this->createdTables = [];
$this->connect();
}
Expand Down Expand Up @@ -1091,10 +1095,11 @@ protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, string $ta
$constraintName = $foreignKey->getName() ?: (
$parts['table'] . '_' . implode('_', $foreignKey->getColumns()) . '_fkey'
);
$columnList = implode(', ', array_map($this->quoteColumnName(...), $foreignKey->getColumns()));
$refColumnList = implode(', ', array_map($this->quoteColumnName(...), $foreignKey->getReferencedColumns()));
$def = ' CONSTRAINT ' . $this->quoteColumnName($constraintName) .
' FOREIGN KEY ("' . implode('", "', $foreignKey->getColumns()) . '")' .
" REFERENCES {$this->quoteTableName($foreignKey->getReferencedTable()->getName())} (\"" .
implode('", "', $foreignKey->getReferencedColumns()) . '")';
' FOREIGN KEY (' . $columnList . ')' .
' REFERENCES ' . $this->quoteTableName($foreignKey->getReferencedTable()->getName()) . ' (' . $refColumnList . ')';
if ($foreignKey->getOnDelete()) {
$def .= " ON DELETE {$foreignKey->getOnDelete()}";
}
Expand Down
65 changes: 37 additions & 28 deletions src/Db/Adapter/SqlserverAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ protected function getRenameTableInstructions(string $tableName, string $newTabl
{
$this->updateCreatedTableName($tableName, $newTableName);
$sql = sprintf(
"EXEC sp_rename '%s', '%s'",
$tableName,
$newTableName,
'EXEC sp_rename %s, %s',
$this->quoteString($tableName),
$this->quoteString($newTableName),
);

return new AlterInstructions([], [$sql]);
Expand Down Expand Up @@ -404,23 +404,21 @@ protected function getRenameColumnInstructions(string $tableName, string $column

$oldConstraintName = "DF_{$tableName}_{$columnName}";
$newConstraintName = "DF_{$tableName}_{$newColumnName}";
$sql = <<<SQL
IF (OBJECT_ID('$oldConstraintName', 'D') IS NOT NULL)
$sql = sprintf(
'IF (OBJECT_ID(%s, \'D\') IS NOT NULL)
BEGIN
EXECUTE sp_rename N'%s', N'%s', N'OBJECT'
END
SQL;
$instructions->addPostStep(sprintf(
$sql,
$oldConstraintName,
$newConstraintName,
));
EXECUTE sp_rename %s, %s, N\'OBJECT\'
END',
$this->quoteString($oldConstraintName),
$this->quoteString($oldConstraintName),
$this->quoteString($newConstraintName),
);
$instructions->addPostStep($sql);

$instructions->addPostStep(sprintf(
"EXECUTE sp_rename N'%s.%s', N'%s', 'COLUMN' ",
$tableName,
$columnName,
$newColumnName,
'EXECUTE sp_rename %s, %s, N\'COLUMN\'',
$this->quoteString($tableName . '.' . $columnName),
$this->quoteString($newColumnName),
));

return $instructions;
Expand Down Expand Up @@ -970,12 +968,17 @@ public function getPhinxType(string $sqlType): string
*/
public function createDatabase(string $name, array $options = []): void
{
$quotedName = $this->quoteSchemaName($name);
if (isset($options['collation'])) {
$this->execute(sprintf('CREATE DATABASE [%s] COLLATE [%s]', $name, $options['collation']));
$this->execute(sprintf(
'CREATE DATABASE %s COLLATE %s',
$quotedName,
$this->quoteSchemaName($options['collation']),
));
} else {
$this->execute(sprintf('CREATE DATABASE [%s]', $name));
$this->execute(sprintf('CREATE DATABASE %s', $quotedName));
}
$this->execute(sprintf('USE [%s]', $name));
$this->execute(sprintf('USE %s', $quotedName));
}

/**
Expand All @@ -997,12 +1000,16 @@ public function hasDatabase(string $name): bool
*/
public function dropDatabase(string $name): void
{
$sql = <<<SQL
USE master;
IF EXISTS(select * from sys.databases where name=N'$name')
ALTER DATABASE [$name] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [$name];
SQL;
$quotedName = $this->quoteSchemaName($name);
$sql = sprintf(
'USE master;
IF EXISTS(select * from sys.databases where name=%s)
ALTER DATABASE %s SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE %s;',
$this->quoteString($name),
$quotedName,
$quotedName,
);
$this->execute($sql);
$this->createdTables = [];
}
Expand Down Expand Up @@ -1061,10 +1068,12 @@ protected function getIndexSqlDefinition(Index $index, string $tableName): strin
protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, string $tableName): string
{
$constraintName = $foreignKey->getName() ?: $tableName . '_' . implode('_', $foreignKey->getColumns());
$columnList = implode(', ', array_map($this->quoteColumnName(...), $foreignKey->getColumns()));
$refColumnList = implode(', ', array_map($this->quoteColumnName(...), $foreignKey->getReferencedColumns()));

$def = ' CONSTRAINT ' . $this->quoteColumnName($constraintName);
$def .= ' FOREIGN KEY ("' . implode('", "', $foreignKey->getColumns()) . '")';
$def .= " REFERENCES {$this->quoteTableName($foreignKey->getReferencedTable()->getName())} (\"" . implode('", "', $foreignKey->getReferencedColumns()) . '")';
$def .= ' FOREIGN KEY (' . $columnList . ')';
$def .= ' REFERENCES ' . $this->quoteTableName($foreignKey->getReferencedTable()->getName()) . ' (' . $refColumnList . ')';
if ($foreignKey->getOnDelete()) {
$def .= " ON DELETE {$foreignKey->getOnDelete()}";
}
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ public function saveData(): void
$c = array_keys($row);
foreach ($this->getData() as $row) {
$k = array_keys($row);
if ($k != $c) {
if ($k !== $c) {
$bulk = false;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Migrations
*
* @var string
*/
protected string $command;
protected string $command = '';

/**
* Stub input to feed the manager class since we might not have an input ready when we get the Manager using
Expand Down
2 changes: 1 addition & 1 deletion src/TestSuite/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ protected function shouldDropTables(Migrations $migrations, array $options): boo
if (!empty($messages['missing'])) {
$hasProblems = true;
$output[] = 'Applied but missing migrations:';
$output = array_merge($output, array_map($itemize, $messages['down']));
$output = array_merge($output, array_map($itemize, $messages['missing']));
$output[] = '';
}
if ($output) {
Expand Down