From 08b2840141a59d78acf0a742b91ef277dfe3d822 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sun, 7 May 2023 09:11:28 -0400 Subject: [PATCH 1/3] Add phpdoc, minor improvements. --- src/DbSchemaManager.php | 339 ++++++++++++++++++++++++---------------- 1 file changed, 205 insertions(+), 134 deletions(-) diff --git a/src/DbSchemaManager.php b/src/DbSchemaManager.php index 8ae0500..1c51af7 100644 --- a/src/DbSchemaManager.php +++ b/src/DbSchemaManager.php @@ -5,7 +5,6 @@ namespace Yiisoft\Translator\Message\Db; use Throwable; -use Yiisoft\Db\Command\CommandInterface; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; @@ -13,20 +12,21 @@ use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Schema\SchemaInterface; +/** + * Manages the translator tables schema in the database. + */ final class DbSchemaManager { - private CommandInterface $command; - private string $driverName; - private SchemaInterface $schema; - public function __construct(private ConnectionInterface $db) { - $this->command = $db->createCommand(); - $this->driverName = $db->getDriverName(); - $this->schema = $db->getSchema(); } /** + * Ensures that the translator tables exists in the database. + * + * @param string $table The name of the translator tables. + * Defaults to '{{%yii_source_message}}', '{{%yii_message}}'. + * * @throws Exception * @throws InvalidConfigException * @throws Throwable @@ -35,15 +35,18 @@ public function ensureTables( string $tableSourceMessage = '{{%yii_source_message}}', string $tableMessage = '{{%yii_message}}', ): void { - $tableRawNameSourceMessage = $this->schema->getRawTableName($tableSourceMessage); - $tableRawNameMessage = $this->schema->getRawTableName($tableMessage); + $driverName = $this->db->getDriverName(); + $schema = $this->db->getSchema(); + $tableRawNameSourceMessage = $schema->getRawTableName($tableSourceMessage); + $tableRawNameMessage = $schema->getRawTableName($tableMessage); if ($this->hasTable($tableSourceMessage) && $this->hasTable($tableMessage)) { return; } - if ($this->driverName === 'sqlite') { + if ($driverName === 'sqlite') { $this->createSchemaSqlite( + $schema, $tableSourceMessage, $tableRawNameSourceMessage, $tableMessage, @@ -53,10 +56,22 @@ public function ensureTables( return; } - $this->createSchema($tableSourceMessage, $tableRawNameSourceMessage, $tableMessage, $tableRawNameMessage); + $this->createSchema( + $schema, + $driverName, + $tableSourceMessage, + $tableRawNameSourceMessage, + $tableMessage, + $tableRawNameMessage, + ); } /** + * Ensures that the translator tables does not exist in the database. + * + * @param string $table The name of the translator tables. + * Defaults to '{{%yii_source_message}}', '{{%yii_message}}'. + * * @throws Exception * @throws InvalidConfigException * @throws Throwable @@ -65,91 +80,119 @@ public function ensureNoTables( string $tableSourceMessage = '{{%yii_source_message}}', string $tableMessage = '{{%yii_message}}', ): void { - $tableRawNameSourceMessage = $this->schema->getRawTableName($tableSourceMessage); - $tableRawNameMessage = $this->schema->getRawTableName($tableMessage); + $driverName = $this->db->getDriverName(); + $schema = $this->db->getSchema(); + $tableRawNameSourceMessage = $schema->getRawTableName($tableSourceMessage); + $tableRawNameMessage = $schema->getRawTableName($tableMessage); - // drop sequence for table `source_message` and `message`. if ($this->hasTable($tableMessage)) { - // drop foreign key for table `message`. - if ($this->driverName !== 'sqlite' && $this->schema->getTableForeignKeys($tableMessage, true) !== []) { - $this->command->dropForeignKey( - $tableRawNameMessage, - "{{FK_{$tableRawNameSourceMessage}_{$tableRawNameMessage}}}", - )->execute(); + if ($driverName !== 'sqlite' && $schema->getTableForeignKeys($tableMessage, true) !== []) { + // drop foreign key for table `yii_message`. + $this->db + ->createCommand() + ->dropForeignKey( + $tableRawNameMessage, + "{{FK_{$tableRawNameSourceMessage}_{$tableRawNameMessage}}}", + ) + ->execute(); } - // drop table `message`. - $this->command->dropTable($tableRawNameMessage)->execute(); - - if ($this->driverName === 'oci') { - $this->command->setSql( - <<execute(); + // drop table `yii_message`. + $this->db->createCommand()->dropTable($tableRawNameMessage)->execute(); + + if ($driverName === 'oci') { + // drop sequence for table `yii_message`. + $this->db + ->createCommand() + ->setSql( + <<execute(); } } if ($this->hasTable($tableSourceMessage)) { - // drop table `source_message`. - $this->command->dropTable($tableRawNameSourceMessage)->execute(); - - if ($this->driverName === 'oci') { - $this->command->setSql( - <<execute(); + // drop table `yii_source_message`. + $this->db->createCommand()->dropTable($tableRawNameSourceMessage)->execute(); + + if ($driverName === 'oci') { + // drop sequence for table `yii_source_message`. + $this->db + ->createCommand() + ->setSql( + <<execute(); } } } /** + * Add sequence and trigger for tables '{{%yii_source_message}}' and '{{%yii_message}}' for Oracle. + * * @throws Exception * @throws Throwable */ private function addSequenceAndTrigger(string $tableRawNameSourceMessage, string $tableRawNameMessage): void { - // create a sequence for table `source_message` and `message`. - $this->command->setSql( - <<execute(); - - $this->command->setSql( - <<execute(); - - // create trigger for table `source_message` and `message`. - $this->command->setSql( - <<> BEGIN - IF INSERTING AND :NEW."id" IS NULL THEN SELECT {{{$tableRawNameSourceMessage}_SEQ}}.NEXTVAL INTO :NEW."id" FROM SYS.DUAL; END IF; - END COLUMN_SEQUENCES; - END; - SQL, - )->execute(); - - $this->command->setSql( - <<> BEGIN - IF INSERTING AND :NEW."id" IS NULL THEN SELECT {{{$tableRawNameMessage}_SEQ}}.NEXTVAL INTO :NEW."id" FROM SYS.DUAL; END IF; - END COLUMN_SEQUENCES; - END; - SQL, - )->execute(); + // create a sequence for table `yii_source_message` and `yii_message`. + $this->db + ->createCommand() + ->setSql( + <<execute(); + + $this->db + ->createCommand() + ->setSql( + <<execute(); + + // create trigger for table `yii_source_message` and `yii_message`. + $this->db + ->createCommand() + ->setSql( + <<> BEGIN + IF INSERTING AND :NEW."id" IS NULL THEN SELECT {{{$tableRawNameSourceMessage}_SEQ}}.NEXTVAL INTO :NEW."id" FROM SYS.DUAL; END IF; + END COLUMN_SEQUENCES; + END; + SQL, + ) + ->execute(); + + $this->db + ->createCommand() + ->setSql( + <<> BEGIN + IF INSERTING AND :NEW."id" IS NULL THEN SELECT {{{$tableRawNameMessage}_SEQ}}.NEXTVAL INTO :NEW."id" FROM SYS.DUAL; END IF; + END COLUMN_SEQUENCES; + END; + SQL, + ) + ->execute(); } /** + * Create index for tables '{{%yii_source_message}}' and '{{%yii_message}}'. + * * @throws InvalidArgumentException * @throws Exception * @throws Throwable @@ -160,15 +203,19 @@ private function createIndex( string $tableMessage, string $tableRawNameMessage ): void { - $this->command + $this->db + ->createCommand() ->createIndex($tableSourceMessage, "IDX_{$tableRawNameSourceMessage}_category", 'category') ->execute(); - $this->command + $this->db + ->createCommand() ->createIndex($tableMessage, "IDX_{$tableRawNameMessage}_locale", 'locale') ->execute(); } /** + * Create schema for tables '{{%yii_source_message}}' and '{{%yii_message}}'. + * * @throws Exception * @throws InvalidArgumentException * @throws InvalidConfigException @@ -176,6 +223,8 @@ private function createIndex( * @throws Throwable */ private function createSchema( + SchemaInterface $schema, + string $driverName, string $tableSourceMessage, string $tableRawNameSourceMessage, string $tableMessage, @@ -183,52 +232,59 @@ private function createSchema( ): void { $updateAction = 'RESTRICT'; - if ($this->driverName === 'sqlsrv') { - // 'NO ACTION' is equal to 'RESTRICT' in MSSQL + if ($driverName === 'sqlsrv') { + // 'NO ACTION' is equal to 'RESTRICT' in `MSSQL`. $updateAction = 'NO ACTION'; } - if ($this->driverName === 'oci') { + if ($driverName === 'oci') { // Oracle doesn't support action for update. $updateAction = null; } - // create table `source_message`. - $this->command->createTable( - $tableSourceMessage, - [ - 'id' => $this->schema->createColumn(SchemaInterface::TYPE_PK), - 'category' => $this->schema->createColumn(SchemaInterface::TYPE_STRING), - 'message_id' => $this->schema->createColumn(SchemaInterface::TYPE_TEXT), - 'comment' => $this->schema->createColumn(SchemaInterface::TYPE_TEXT), - ], - )->execute(); - - // create table `message`. - $this->command->createTable( - $tableMessage, - [ - 'id' => $this->schema->createColumn(SchemaInterface::TYPE_INTEGER)->notNull(), - 'locale' => $this->schema->createColumn(SchemaInterface::TYPE_STRING, 16)->notNull(), - 'translation' => $this->schema->createColumn(SchemaInterface::TYPE_TEXT), - ], - )->execute(); - - // create primary key for table `source_message` and `message`. - $this->command + // create table `yii_sorce_message`. + $this->db + ->createCommand() + ->createTable( + $tableSourceMessage, + [ + 'id' => $schema->createColumn(SchemaInterface::TYPE_PK), + 'category' => $schema->createColumn(SchemaInterface::TYPE_STRING), + 'message_id' => $schema->createColumn(SchemaInterface::TYPE_TEXT), + 'comment' => $schema->createColumn(SchemaInterface::TYPE_TEXT), + ], + ) + ->execute(); + + // create table `yii_message`. + $this->db + ->createCommand() + ->createTable( + $tableMessage, + [ + 'id' => $schema->createColumn(SchemaInterface::TYPE_INTEGER)->notNull(), + 'locale' => $schema->createColumn(SchemaInterface::TYPE_STRING, 16)->notNull(), + 'translation' => $schema->createColumn(SchemaInterface::TYPE_TEXT), + ], + ) + ->execute(); + + // create primary key for table `yii_message`. + $this->db + ->createCommand() ->addPrimaryKey($tableMessage, "PK_{$tableRawNameMessage}_id_locale", ['id', 'locale']) ->execute(); - // create index for table `source_message` and `message`. + // create index for table `yii_source_message`. $this->createIndex($tableSourceMessage, $tableRawNameSourceMessage, $tableMessage, $tableRawNameMessage); - if ($this->driverName === 'oci') { - // create sequence and trigger for table `source_message` and `message`. + if ($driverName === 'oci') { $this->addSequenceAndTrigger($tableRawNameSourceMessage, $tableRawNameMessage); } - // add foreign key for table `message`. - $this->command + // Add foreign key for table `yii_message`. + $this->db + ->createCommand() ->addForeignKey( $tableMessage, "FK_{$tableRawNameSourceMessage}_{$tableRawNameMessage}", @@ -241,6 +297,8 @@ private function createSchema( } /** + * Create schema for tables '{{%yii_source_message}}' and '{{%yii_message}}' for SQLite. + * * @throws Exception * @throws InvalidArgumentException * @throws InvalidConfigException @@ -248,39 +306,52 @@ private function createSchema( * @throws Throwable */ private function createSchemaSqlite( + SchemaInterface $schema, string $tableSourceMessage, string $tableRawNameSourceMessage, string $tableMessage, string $tableRawNameMessage ): void { - // create table `source_message`. - $this->command->createTable( - $tableSourceMessage, - [ - 'id' => $this->schema->createColumn(SchemaInterface::TYPE_INTEGER)->notNull(), - 'category' => $this->schema->createColumn(SchemaInterface::TYPE_STRING), - 'message_id' => $this->schema->createColumn(SchemaInterface::TYPE_TEXT), - 'comment' => $this->schema->createColumn(SchemaInterface::TYPE_TEXT), - "CONSTRAINT [[PK_{$tableRawNameSourceMessage}]] PRIMARY KEY ([[id]])", - ], - )->execute(); - - // create table `message`. - $this->command->createTable( - $tableMessage, - [ - 'id' => $this->schema->createColumn(SchemaInterface::TYPE_INTEGER)->notNull(), - 'locale' => $this->schema->createColumn(SchemaInterface::TYPE_STRING, 16)->notNull(), - 'translation' => $this->schema->createColumn(SchemaInterface::TYPE_TEXT), - 'PRIMARY KEY (`id`, `locale`)', - "CONSTRAINT `FK_{$tableRawNameMessage}_{$tableRawNameSourceMessage}` FOREIGN KEY (`id`) REFERENCES `$tableRawNameSourceMessage` (`id`) ON DELETE CASCADE", - ], - )->execute(); - - // create index for table `source_message` and `message`. + // create table `yii_source_message`. + $this->db + ->createCommand() + ->createTable( + $tableSourceMessage, + [ + 'id' => $schema->createColumn(SchemaInterface::TYPE_INTEGER)->notNull(), + 'category' => $schema->createColumn(SchemaInterface::TYPE_STRING), + 'message_id' => $schema->createColumn(SchemaInterface::TYPE_TEXT), + 'comment' => $schema->createColumn(SchemaInterface::TYPE_TEXT), + "CONSTRAINT [[PK_{$tableRawNameSourceMessage}]] PRIMARY KEY ([[id]])", + ], + ) + ->execute(); + + // create table `yii_message`. + $this->db + ->createCommand() + ->createTable( + $tableMessage, + [ + 'id' => $schema->createColumn(SchemaInterface::TYPE_INTEGER)->notNull(), + 'locale' => $schema->createColumn(SchemaInterface::TYPE_STRING, 16)->notNull(), + 'translation' => $schema->createColumn(SchemaInterface::TYPE_TEXT), + 'PRIMARY KEY (`id`, `locale`)', + "CONSTRAINT `FK_{$tableRawNameMessage}_{$tableRawNameSourceMessage}` FOREIGN KEY (`id`) REFERENCES `$tableRawNameSourceMessage` (`id`) ON DELETE CASCADE", + ], + ) + ->execute(); + $this->createIndex($tableSourceMessage, $tableRawNameSourceMessage, $tableMessage, $tableRawNameMessage); } + /** + * Checks if the given table exists in the database. + * + * @param string $table The name of the table to check. + * + * @return bool Whether the table exists or not. + */ private function hasTable(string $table): bool { return $this->db->getTableSchema($table, true) !== null; From bec84f3a396e4de3a0f41a2e64c5ddec2d869a8f Mon Sep 17 00:00:00 2001 From: Wilmer Arambula <42547589+terabytesoftw@users.noreply.github.com> Date: Mon, 8 May 2023 07:50:24 -0400 Subject: [PATCH 2/3] Update src/DbSchemaManager.php Co-authored-by: Sergei Predvoditelev --- src/DbSchemaManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DbSchemaManager.php b/src/DbSchemaManager.php index 1c51af7..f604e7c 100644 --- a/src/DbSchemaManager.php +++ b/src/DbSchemaManager.php @@ -297,7 +297,7 @@ private function createSchema( } /** - * Create schema for tables '{{%yii_source_message}}' and '{{%yii_message}}' for SQLite. + * Create schema for tables in SQLite database. * * @throws Exception * @throws InvalidArgumentException From 8fb75441260cdbc855ef775549637702f11dc409 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula <42547589+terabytesoftw@users.noreply.github.com> Date: Mon, 8 May 2023 07:50:32 -0400 Subject: [PATCH 3/3] Update src/DbSchemaManager.php Co-authored-by: Sergei Predvoditelev --- src/DbSchemaManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DbSchemaManager.php b/src/DbSchemaManager.php index f604e7c..6fd89d7 100644 --- a/src/DbSchemaManager.php +++ b/src/DbSchemaManager.php @@ -214,7 +214,7 @@ private function createIndex( } /** - * Create schema for tables '{{%yii_source_message}}' and '{{%yii_message}}'. + * Create schema for tables in the database. * * @throws Exception * @throws InvalidArgumentException