Skip to content

Commit dc7c95d

Browse files
Adopt last discussion (#63)
* Add `RuntimeException`, if table exists. * Rename `DbHelper::class`, add more test, update README.md.
1 parent 22abb9b commit dc7c95d

37 files changed

+558
-369
lines changed

README.md

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,52 @@ The package could be installed with composer:
3838
composer require yiisoft/cache-db --prefer-dist
3939
```
4040

41+
## Create database connection
42+
43+
For more information see [yiisoft/db](https://github.com/yiisoft/db/tree/master/docs/en#create-connection).
44+
4145
## Database Preparing
4246

4347
Package provides two way for preparing database:
4448

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

47-
- [MSSQL](/docs/en/migration/schema-mssql.sql),
48-
- [MySQL / MariaDB](/docs/en/migration/schema-mysql.sql),
49-
- [Oracle](/docs/en/migration/schema-oci.sql),
50-
- [PostgreSQL](/docs/en/migration/schema-pgsql.sql),
51-
- [SQLite](/docs/en/migration/schema-sqlite.sql),
51+
- Ensure tables:
52+
- [MSSQL](/sql/sqlsrv-up.sql),
53+
- [MySQL / MariaDB](/sql/mysql-up.sql),
54+
- [Oracle](/sql/oci-up.sql),
55+
- [PostgreSQL](/sql/pgsql-up.sql)
56+
- [SQLite](/sql/sqlite-up.sql)
57+
58+
- Ensure no tables:
59+
- [MSSQL](/sql/sqlsrv-down.sql),
60+
- [MySQL / MariaDB](/sql/mysql-down.sql),
61+
- [Oracle](/sql/oci-down.sql),
62+
- [PostgreSQL](/sql/pgsql-down.sql)
63+
- [SQLite](/sql/sqlite-down.sql)
5264

53-
2. `DbHelper` for create/drop cache table (by default `{{%cache}}`).
65+
2. `DbSchemaManager` for `ensureTable()`, `ensureNoTable()` methods for cache table (by default `{{%yii_cache}}`).
5466

5567
```php
56-
// Create table with default name
57-
DbHelper::ensureTable($db);
68+
// Create db schema manager
69+
$dbSchemaManager = new DbSchemaManager($db);
70+
71+
// Ensure table with default name
72+
$dbSchemaManager->ensureTable();
5873

59-
// Create table with custom name
60-
DbHelper::ensureTable($db, '{{%custom_cache_table}}');
74+
// Ensure table with custom name
75+
$dbSchemaManager->ensureTable('{{%custom_cache_table}}');
6176

62-
// Drop table with default name
63-
DbHelper::dropTable($db);
77+
// Ensure no table with default name
78+
$dbSchemaManager->ensureNoTable();
6479

65-
// Drop table with custom name
66-
DbHelper::dropTable($db, '{{%custom_cache_table}}');
80+
// Ensure no table with custom name
81+
$dbSchemaManager->ensureNoTable('{{%custom_cache_table}}');
6782
```
6883

6984
## Configuration
7085

71-
When creating an instance of `\Yiisoft\Cache\Db\DbCache`, you must pass an instance of the database connection,
72-
for more information see [yiisoft/db](https://github.com/yiisoft/db/tree/master/docs/en#create-connection).
86+
When creating an instance of `\Yiisoft\Cache\Db\DbCache`, you must pass an instance of the database connection.
7387

7488
```php
7589
$cache = new \Yiisoft\Cache\Db\DbCache($db, $table, $gcProbability);

docs/en/migration/schema-mssql.sql

Lines changed: 0 additions & 9 deletions
This file was deleted.

docs/en/migration/schema-mysql.sql

Lines changed: 0 additions & 9 deletions
This file was deleted.

docs/en/migration/schema-oci.sql

Lines changed: 0 additions & 9 deletions
This file was deleted.

docs/en/migration/schema-pgsql.sql

Lines changed: 0 additions & 9 deletions
This file was deleted.

docs/en/migration/schema-sqlite.sql

Lines changed: 0 additions & 9 deletions
This file was deleted.

sql/mysql-down.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE `yii_cache`;

sql/mysql-up.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE `yii_cache` (
2+
`id` varchar(128) NOT NULL,
3+
`data` blob,
4+
`expire` int(11),
5+
CONSTRAINT `PK_yii_cache` PRIMARY KEY (`id`)
6+
);

sql/oci-down.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE "yii_cache";

sql/oci-up.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE "yii_cache" (
2+
"id" VARCHAR2(128) NOT NULL,
3+
"data" BLOB,
4+
"expire" NUMBER(10),
5+
CONSTRAINT "PK_yii_cache" PRIMARY KEY ("id")
6+
);

0 commit comments

Comments
 (0)