Skip to content

Commit

Permalink
Adopt last discussion.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed May 6, 2023
1 parent d5f3e87 commit f5c9505
Show file tree
Hide file tree
Showing 39 changed files with 867 additions and 783 deletions.
56 changes: 24 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,48 +39,40 @@ The package could be installed with composer:
composer require yiisoft/translator-message-db --prefer-dist
```

## Configuration

### Quick start

**Step 1.** You need a configured database connection for create migration and use in `yiisoft/translator-message-db` package,
for more information see [yiisoft/db](https://github.com/yiisoft/db/tree/master/docs/en#create-connection).
## Create database connection

**Step 2.** Create migration for `source_message` and `message` tables:
For more information see [yiisoft/db](https://github.com/yiisoft/db/tree/master/docs/en#create-connection).

For default tables:
## Database Preparing

```php
DbHelper::ensureTable($db);
```
Package provides two way for preparing database:

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

```php
DbHelper::ensureTables($db, '{{%custom_source_message_table}}', '{{%custom_message_table}}');
```
- 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)

For dropping tables:
2. `DbSchemaManager` for `ensureTables()`, `ensureNoTables()` methods for translator tables
(by default `{{%yii_source_message}}` and `{{%yii_message}}`).

```php
DbHelper::dropTables($db);
```

For custom tables:
## Configuration

```php
DbHelper::dropTable($db, '{{%custom_source_message_table}}', '{{%custom_message_table}}');
```
### Quick start

> Note: Additionally you can import the `RAW SQL` directly to create the tables.
>
>- [schema-mssql](/docs/en/migration/schema-mssql.sql).
>- [schema-mysql](/docs/en/migration/schema-mysql.sql).
>- [schema-oracle](/docs/en/migration/schema-oci.sql).
>- [schema-pgsql](/docs/en/migration/schema-pgsql.sql).
>- [schema-sqlite](/docs/en/migration/schema-sqlite.sql).

**Step 3.** In case you use [`yiisoft/config`](http://github.com/yiisoft/config), you will get configuration automatically. If not, the following DI container configuration is necessary:
In case you use [`yiisoft/config`](http://github.com/yiisoft/config), you will get configuration automatically.
If not, the following DI container configuration is necessary:

```php
<?php
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"require-dev": {
"maglnet/composer-require-checker": "^4.2",
"phpunit/phpunit": "^9.6|^10.1",
"rector/rector": "^0.15.20",
"rector/rector": "^0.16",
"roave/infection-static-analysis-plugin": "^1.25|^1.29",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.8|^5.8"
Expand Down
23 changes: 0 additions & 23 deletions docs/en/migration/schema-mssql.sql

This file was deleted.

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

This file was deleted.

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

This file was deleted.

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

This file was deleted.

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

This file was deleted.

3 changes: 3 additions & 0 deletions sql/mysql-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE `yii_message` DROP FOREIGN KEY `FK_yii_source_message_yii_message`;
DROP TABLE `yii_message`;
DROP TABLE `yii_source_message`;
15 changes: 15 additions & 0 deletions sql/mysql-up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE `yii_source_message` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`category` varchar(255),
`message_id` text,
`comment` text
);
CREATE TABLE `yii_message` (
`id` int(11) NOT NULL,
`locale` varchar(16) NOT NULL,
`translation` text,
CONSTRAINT `PK_yii_message_id_locale` PRIMARY KEY (`id`, `locale`),
CONSTRAINT `FK_yii_source_message_yii_message` FOREIGN KEY (`id`) REFERENCES `yii_source_message` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT
);
CREATE INDEX `IDX_yii_source_message_category` ON `yii_source_message` (`category`);
CREATE INDEX `IDX_yii_message_locale` ON `yii_message` (`locale`);
8 changes: 8 additions & 0 deletions sql/oci-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* STATEMENTS */
ALTER TABLE "yii_message" DROP CONSTRAINT "FK_yii_source_message_yii_message";
DROP TRIGGER "yii_message_TRG";
DROP SEQUENCE "yii_message_SEQ";
DROP TABLE "yii_message";
DROP TRIGGER "yii_source_message_TRG";
DROP SEQUENCE "yii_source_message_SEQ";
DROP TABLE "yii_source_message";
30 changes: 30 additions & 0 deletions sql/oci-up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* STATEMENTS */
CREATE TABLE "yii_source_message" (
"id" NUMBER(10) NOT NULL PRIMARY KEY,
"category" VARCHAR2(255),
"message_id" CLOB,
"comment" CLOB
);
CREATE TABLE "yii_message" (
"id" NUMBER(10) NOT NULL,
"locale" VARCHAR2(16) NOT NULL,
"translation" CLOB,
CONSTRAINT "PK_yii_message_id_locale" PRIMARY KEY ("id", "locale"),
CONSTRAINT "FK_yii_source_message_yii_message" FOREIGN KEY ("id") REFERENCES "yii_source_message" ("id") ON DELETE CASCADE
);
CREATE INDEX "IDX_yii_source_message_category" ON "yii_source_message" ("category");
CREATE INDEX "IDX_yii_message_locale" ON "yii_message" ("locale");
CREATE SEQUENCE "yii_source_message_SEQ" START WITH 1 INCREMENT BY 1 NOMAXVALUE;
CREATE SEQUENCE "yii_message_SEQ" START WITH 1 INCREMENT BY 1 NOMAXVALUE;

/* TRIGGERS */
CREATE TRIGGER "yii_source_message_TRG" BEFORE INSERT ON "yii_source_message" FOR EACH ROW BEGIN <<COLUMN_SEQUENCES>> BEGIN
IF INSERTING AND :NEW."id" IS NULL THEN SELECT "yii_source_message_SEQ".NEXTVAL INTO :NEW."id" FROM SYS.DUAL; END IF;
END COLUMN_SEQUENCES;
END;
/
CREATE TRIGGER "yii_message_TRG" BEFORE INSERT ON "yii_message" FOR EACH ROW BEGIN <<COLUMN_SEQUENCES>> BEGIN
IF INSERTING AND :NEW."id" IS NULL THEN SELECT "yii_message_SEQ".NEXTVAL INTO :NEW."id" FROM SYS.DUAL; END IF;
END COLUMN_SEQUENCES;
END;
/
3 changes: 3 additions & 0 deletions sql/pgsql-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE "yii_message" DROP CONSTRAINT "FK_yii_source_message_yii_message";
DROP TABLE "yii_message";
DROP TABLE "yii_source_message";
15 changes: 15 additions & 0 deletions sql/pgsql-up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE "yii_source_message" (
"id" serial NOT NULL PRIMARY KEY,
"category" varchar(255),
"message_id" text,
"comment" text
);
CREATE TABLE "yii_message" (
"id" integer NOT NULL,
"locale" varchar(16) NOT NULL,
"translation" text,
CONSTRAINT "PK_yii_message_id_locale" PRIMARY KEY ("id", "locale"),
CONSTRAINT "FK_yii_source_message_yii_message" FOREIGN KEY ("id") REFERENCES "yii_source_message" ("id") ON DELETE CASCADE ON UPDATE RESTRICT
);
CREATE INDEX "IDX_yii_source_message_category" ON "yii_source_message" ("category");
CREATE INDEX "IDX_yii_message_locale" ON "yii_message" ("locale");
2 changes: 2 additions & 0 deletions sql/sqlite-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE `yii_message`;
DROP TABLE `yii_source_message`;
16 changes: 16 additions & 0 deletions sql/sqlite-up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE TABLE `yii_source_message` (
`id` integer NOT NULL,
`category` varchar(255),
`message_id` text,
`comment` text,
CONSTRAINT `PK_yii_source_message` PRIMARY KEY (`id`)
);
CREATE TABLE `yii_message` (
`id` integer NOT NULL,
`locale` varchar(16) NOT NULL,
`translation` text,
PRIMARY KEY (`id`, `locale`),
CONSTRAINT `FK_yii_message_yii_source_message` FOREIGN KEY (`id`) REFERENCES `yii_source_message` (`id`) ON DELETE CASCADE
);
CREATE INDEX `IDX_yii_source_message_category` ON `yii_source_message` (`category`);
CREATE INDEX `IDX_yii_message_locale` ON `yii_message` (`locale`);
3 changes: 3 additions & 0 deletions sql/sqlsrv-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE [yii_message] DROP CONSTRAINT [FK_yii_source_message_yii_message];
DROP TABLE [yii_message];
DROP TABLE [yii_source_message];
15 changes: 15 additions & 0 deletions sql/sqlsrv-up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE [yii_source_message] (
[id] int IDENTITY PRIMARY KEY,
[category] nvarchar(255),
[message_id] nvarchar(max),
[comment] nvarchar(max)
);
CREATE TABLE [yii_message] (
[id] int NOT NULL,
[locale] nvarchar(16) NOT NULL,
[translation] nvarchar(max),
CONSTRAINT [PK_yii_message_id_locale] PRIMARY KEY ([id], [locale]),
CONSTRAINT [FK_yii_source_message_yii_message] FOREIGN KEY ([id]) REFERENCES [yii_source_message] ([id]) ON DELETE CASCADE ON UPDATE NO ACTION
);
CREATE INDEX [IDX_yii_source_message_category] ON [yii_source_message] ([category]);
CREATE INDEX [IDX_yii_message_locale] ON [yii_message] ([locale]);
Loading

0 comments on commit f5c9505

Please sign in to comment.