0 follower

Final Class Yiisoft\Log\Target\Db\DbSchemaManager

InheritanceYiisoft\Log\Target\Db\DbSchemaManager

Manages the log table schema in the database.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Log\Target\Db\DbSchemaManager
ensureNoTable() Ensures that the log table does not exist in the database. Yiisoft\Log\Target\Db\DbSchemaManager
ensureTable() Ensures that the log table exists in the database. Yiisoft\Log\Target\Db\DbSchemaManager

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\Db\Connection\ConnectionInterface $db )
$db \Yiisoft\Db\Connection\ConnectionInterface

                public function __construct(private readonly ConnectionInterface $db)
{
}

            
ensureNoTable() public method

Ensures that the log table does not exist in the database.

public void ensureNoTable ( string $table '{{%yii_log}}' )
$table string

The name of the log table. Defaults to '{{%yii_log}}'.

throws \Yiisoft\Db\Exception\Exception
throws \Yiisoft\Db\Exception\InvalidConfigException
throws Throwable

                public function ensureNoTable(string $table = '{{%yii_log}}'): void
{
    /** @var Quoter $quoter */
    $quoter = $this->db->getQuoter();
    $tableRawName = $quoter->getRawTableName($table);
    // drop table
    if ($this->db->getTableSchema($table, true) !== null) {
        $this->db->createCommand()->dropTable($tableRawName)->execute();
        // drop sequence oracle
        if ($this->db->getDriverName() === 'oci') {
            $this->db
                ->createCommand()
                ->setSql(
                    <<<SQL
                    DROP SEQUENCE {{{$tableRawName}_SEQ}}
                    SQL,
                )
                ->execute();
        }
    }
}

            
ensureTable() public method

Ensures that the log table exists in the database.

public void ensureTable ( string $table '{{%yii_log}}' )
$table string

The name of the log table. Defaults to '{{%yii_log}}'.

throws \Yiisoft\Db\Exception\Exception
throws \Yiisoft\Db\Exception\InvalidArgumentException
throws \Yiisoft\Db\Exception\InvalidConfigException
throws \Yiisoft\Db\Exception\NotSupportedException
throws Throwable

                public function ensureTable(string $table = '{{%yii_log}}'): void
{
    $driverName = $this->db->getDriverName();
    $schema = $this->db->getSchema();
    /** @var Quoter $quoter */
    $quoter = $this->db->getQuoter();
    $tableRawName = $quoter->getRawTableName($table);
    if ($this->hasTable($table)) {
        return;
    }
    // `log_Time` Default value custom for all dbms
    $defaultValue = match ($driverName) {
        'mysql' => new Expression('CURRENT_TIMESTAMP(6)'),
        'sqlite' => new Expression("(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW', 'UTC'))"),
        default => new Expression('CURRENT_TIMESTAMP'),
    };
    // `log_Time` Type custom for all dbms
    $logTimeType = match ($driverName) {
        'sqlsrv' => $schema->createColumn('DATETIME2(6)')->defaultValue($defaultValue),
        default => $schema->createColumn(SchemaInterface::TYPE_TIMESTAMP, 6)->defaultValue($defaultValue),
    };
    // `id` AutoIncrement custom for all dbms
    $id = match ($driverName) {
        'mysql' => $schema->createColumn(SchemaInterface::TYPE_BIGINT)->notNull()->append('AUTO_INCREMENT'),
        'pgsql' => $schema->createColumn('BIGSERIAL')->notNull(),
        'sqlsrv' => $schema->createColumn(SchemaInterface::TYPE_BIGINT)->notNull()->append('IDENTITY'),
        default => $schema->createColumn(SchemaInterface::TYPE_INTEGER)->notNull(),
    };
    // create table
    $this->db->createCommand()->createTable(
        $table,
        [
            'id' => $id,
            'level' => $schema->createColumn(SchemaInterface::TYPE_STRING, 16),
            'category' => $schema->createColumn(SchemaInterface::TYPE_STRING),
            'log_time' => $logTimeType,
            'message' => $schema->createColumn(SchemaInterface::TYPE_TEXT),
            "CONSTRAINT [[PK_$tableRawName]] PRIMARY KEY ([[id]])",
        ],
    )->execute();
    if ($driverName === 'oci') {
        $this->addSequenceAndTrigger($tableRawName);
    }
    $this->db->createCommand()->createIndex($table, "IDX_{$tableRawName}-category", 'category')->execute();
    $this->db->createCommand()->createIndex($table, "IDX_{$tableRawName}-level", 'level')->execute();
    $this->db->createCommand()->createIndex($table, "IDX_{$tableRawName}-time", 'log_time')->execute();
}