Skip to content

Commit

Permalink
Adopt last discussion. (#49)
Browse files Browse the repository at this point in the history
* Adopt last discussion.

* Fix error typo.

* [ci-review] Apply changes from Rector action.

* Adjust `README.md`.

* Fix all test for all versions PHP.

* Fix mutation.

* Fix minor corrections.

* Fix minor correction.

---------

Co-authored-by: rector-bot <rector@yiiframework.com>
  • Loading branch information
terabytesoftw and rector-bot committed May 7, 2023
1 parent c4e227c commit af2d3ee
Show file tree
Hide file tree
Showing 39 changed files with 931 additions and 856 deletions.
48 changes: 31 additions & 17 deletions README.md
Expand Up @@ -39,38 +39,52 @@ The package could be installed with composer:
composer require yiisoft/log-target-db --prefer-dist
```

## Create database connection

For more information see [yiisoft/db](https://github.com/yiisoft/db/tree/master/docs/en#create-connection).

## Database Preparing

Package provides two way for preparing database:

1. Raw SQL. You can use it with the migration package used in your application.

- [MSSQL](/docs/en/migration/schema-mssql.sql),
- [MySQL / MariaDB](/docs/en/migration/schema-mysql.sql),
- [Oracle](/docs/en/migration/schema-oci.sql),
- [PostgreSQL](/docs/en/migration/schema-pgsql.sql),
- [SQLite](/docs/en/migration/schema-sqlite.sql),

2. `DbHelper` for create/drop cache table (by default `{{%log}}`).
- Ensure tables:
- [MSSQL](/sql/sqlsrv-up.sql),
- [MySQL / MariaDB](/sql/mysql-up.sql),
- [Oracle](/sql/oci-up.sql),
- [PostgreSQL](/sql/pgsql-up.sql)
- [SQLite](/sql/sqlite-up.sql)

- Ensure no tables:
- [MSSQL](/sql/sqlsrv-down.sql),
- [MySQL / MariaDB](/sql/mysql-down.sql),
- [Oracle](/sql/oci-down.sql),
- [PostgreSQL](/sql/pgsql-down.sql)
- [SQLite](/sql/sqlite-down.sql)

2. `DbSchemaManager` for `ensureTable()`, `ensureNoTable()` methods for log table (by default `{{%yii_log}}`).

```php
// Create table with default name
DbHelper::ensureTable($db);
// Create db schema manager
$dbSchemaManager = new DbSchemaManager($db);

// Ensure table with default name
$dbSchemaManager->ensureTable();

// Create table with custom name
DbHelper::ensureTable($db, '{{%custom_log}}');
// Ensure table with custom name
$dbSchemaManager->ensureTable('{{%custom_log_table}}');

// Drop table with default name
DbHelper::dropTable($db);
// Ensure no table with default name
$dbSchemaManager->ensureNoTable();

// Drop table with custom name
DbHelper::dropTable($db, '{{%custom_log}}');
// Ensure no table with custom name
$dbSchemaManager->ensureNoTable('{{%custom_log_table}}');
```

## General usage

When creating an instance of `\Yiisoft\Log\Logger`, you must pass an instance of the database connection,
for more information see [yiisoft/db](https://github.com/yiisoft/db/tree/master/docs/en#create-connection).
When creating an instance of `\Yiisoft\Log\Logger`, you must pass an instance of the database connection.

Creating a target:

Expand Down
17 changes: 0 additions & 17 deletions docs/en/migration/schema-mssql.sql

This file was deleted.

15 changes: 0 additions & 15 deletions docs/en/migration/schema-mysql.sql

This file was deleted.

22 changes: 0 additions & 22 deletions docs/en/migration/schema-oci.sql

This file was deleted.

15 changes: 0 additions & 15 deletions docs/en/migration/schema-pgsql.sql

This file was deleted.

15 changes: 0 additions & 15 deletions docs/en/migration/schema-sqlite.sql

This file was deleted.

1 change: 1 addition & 0 deletions sql/mysql-down.sql
@@ -0,0 +1 @@
DROP TABLE `yii_log`;
11 changes: 11 additions & 0 deletions sql/mysql-up.sql
@@ -0,0 +1,11 @@
CREATE TABLE `yii_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`level` varchar(16),
`category` varchar(255),
`log_time` timestamp(6) DEFAULT CURRENT_TIMESTAMP(6),
`message` text,
CONSTRAINT `PK_yii_log` PRIMARY KEY (`id`)
);
CREATE INDEX `IDX_yii_log-category` ON `yii_log` (`category`);
CREATE INDEX `IDX_yii_log-level` ON `yii_log` (`level`);
CREATE INDEX `IDX_yii_log-time` ON `yii_log` (`log_time`);
4 changes: 4 additions & 0 deletions sql/oci-down.sql
@@ -0,0 +1,4 @@
/* STATEMENTS */
DROP TRIGGER "yii_log_TRG";
DROP SEQUENCE "yii_log_SEQ";
DROP TABLE "yii_log";
20 changes: 20 additions & 0 deletions sql/oci-up.sql
@@ -0,0 +1,20 @@
/* STATEMENTS */
CREATE TABLE "yii_log" (
"id" NUMBER(20) NOT NULL,
"level" VARCHAR2(16),
"category" VARCHAR2(255),
"log_time" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
"message" CLOB,
CONSTRAINT "PK_yii_log" PRIMARY KEY ("id")
);
CREATE SEQUENCE "yii_log_SEQ" START WITH 1 INCREMENT BY 1 NOMAXVALUE;
CREATE INDEX "IDX_yii_log-category" ON "yii_log" ("category");
CREATE INDEX "IDX_yii_log-level" ON "yii_log" ("level");
CREATE INDEX "IDX_yii_log-time" ON "yii_log" ("log_time");

/* TRIGGERS */
CREATE TRIGGER "yii_log_TRG" BEFORE INSERT ON "yii_log" FOR EACH ROW BEGIN <<COLUMN_SEQUENCES>> BEGIN
IF INSERTING AND :NEW."id" IS NULL THEN SELECT "yii_log_SEQ".NEXTVAL INTO :NEW."id" FROM SYS.DUAL; END IF;
END COLUMN_SEQUENCES;
END;
/
1 change: 1 addition & 0 deletions sql/pgsql-down.sql
@@ -0,0 +1 @@
DROP TABLE "yii_log";
11 changes: 11 additions & 0 deletions sql/pgsql-up.sql
@@ -0,0 +1,11 @@
CREATE TABLE "yii_log" (
"id" BIGSERIAL NOT NULL,
"level" varchar(16),
"category" varchar(255),
"log_time" timestamp(6) DEFAULT CURRENT_TIMESTAMP,
"message" text,
CONSTRAINT "PK_yii_log" PRIMARY KEY ("id")
);
CREATE INDEX "IDX_yii_log-category" ON "yii_log" ("category");
CREATE INDEX "IDX_yii_log-level" ON "yii_log" ("level");
CREATE INDEX "IDX_yii_log-time" ON "yii_log" ("log_time");
1 change: 1 addition & 0 deletions sql/sqlite-down.sql
@@ -0,0 +1 @@
DROP TABLE `yii_log`;
11 changes: 11 additions & 0 deletions sql/sqlite-up.sql
@@ -0,0 +1,11 @@
CREATE TABLE `yii_log` (
`id` integer,
`level` varchar(16),
`category` varchar(255),
`log_time` timestamp DEFAULT (STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW', 'UTC')),
`message` text,
CONSTRAINT `PK_yii_log` PRIMARY KEY (`id`)
);
CREATE INDEX `IDX_yii_log-category` ON `yii_log` (`category`);
CREATE INDEX `IDX_yii_log-level` ON `yii_log` (`level`);
CREATE INDEX `IDX_yii_log-time` ON `yii_log` (`log_time`);
1 change: 1 addition & 0 deletions sql/sqlsrv-down.sql
@@ -0,0 +1 @@
DROP TABLE [yii_log];
11 changes: 11 additions & 0 deletions sql/sqlsrv-up.sql
@@ -0,0 +1,11 @@
CREATE TABLE [yii_log] (
[id] bigint NOT NULL IDENTITY,
[level] nvarchar(16),
[category] nvarchar(255),
[log_time] DATETIME2(6) DEFAULT CURRENT_TIMESTAMP,
[message] nvarchar(max),
CONSTRAINT [PK_yii_log] PRIMARY KEY ([id])
);
CREATE INDEX [IDX_yii_log-category] ON [yii_log] ([category]);
CREATE INDEX [IDX_yii_log-level] ON [yii_log] ([level]);
CREATE INDEX [IDX_yii_log-time] ON [yii_log] ([log_time]);
131 changes: 0 additions & 131 deletions src/DbHelper.php

This file was deleted.

0 comments on commit af2d3ee

Please sign in to comment.