From 8a3dfbe9f06c4bb207c198adff378cc2d03467fa Mon Sep 17 00:00:00 2001 From: igor Date: Thu, 29 May 2025 23:13:18 +0200 Subject: [PATCH] added info to README about Maintenance --- README.md | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ test/test4.php | 8 ++++++++ 2 files changed, 64 insertions(+) diff --git a/README.md b/README.md index fd17211..8d2113e 100644 --- a/README.md +++ b/README.md @@ -195,3 +195,59 @@ print_r($result); ?> ``` + +## Maintenance of database schema + +When developing a project, a situation arises where the DB schema needs to be modified and distributed to all installations. To ease these problems, the Maintenance class was created, which contains several useful methods. These first check if the schema contains the requested change and if it doesn't, they apply a FIX. Here is a short demonstration of what we can fix automatically in the project when changing the DB schema. + +```php +checkDBTable('users', ' + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(255) DEFAULT NULL, + `email` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +'); +echo "\nCheckDBTable: "; var_dump($suc); + +// Check if existing column, if not add it by definition +$suc = $maintenance->checkDBAdd('users', 'password', 'varchar(255) DEFAULT NULL'); +echo "\nCheckDBAdd: "; var_dump($suc); + +// Check if existing column +$suc = $maintenance->existsColumn('users', 'email'); +echo "\nExistsColumn: "; var_dump($suc); + +// Check if existing column in new name, if not rename it +$suc = $maintenance->checkDBRename('users', 'email', 'email_new', 'varchar(255) DEFAULT NULL'); +echo "\nCheckDBRename: "; var_dump($suc); + +// Chceck type of column, if not matched change it +$suc = $maintenance->checkDBRetype('users', 'email_new', 'varchar(128)', 'varchar(128) DEFAULT NULL AFTER `password`'); +echo "\nCheckDBRetype: "; var_dump($suc); + +// Remove column from table +$suc = $maintenance->checkDBRemove('users', 'email_new'); +echo "\nCheckDBRemove: "; var_dump($suc); + +// Add index for column +$suc = $maintenance->addKey('users', 'name'); +echo "\nAddKey: "; var_dump($suc); + +// Drop index by column name +$suc = $maintenance->dropKey('users', 'name'); +echo "\nDropKey: "; var_dump($suc); + +echo "\nDone\n\n"; + +?> +``` diff --git a/test/test4.php b/test/test4.php index 7a995ec..7d62a29 100644 --- a/test/test4.php +++ b/test/test4.php @@ -7,6 +7,7 @@ $db = new \TPsoft\DBmodel\DBmodel('mysql:host=127.0.0.1;dbname=test;charset=utf8 $maintenance = new \TPsoft\DBmodel\Maintenance($db); +// Check if existing table, if not create it by definition $suc = $maintenance->checkDBTable('users', ' `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, @@ -15,24 +16,31 @@ $suc = $maintenance->checkDBTable('users', ' '); echo "\nCheckDBTable: "; var_dump($suc); +// Check if existing column, if not add it by definition $suc = $maintenance->checkDBAdd('users', 'password', 'varchar(255) DEFAULT NULL'); echo "\nCheckDBAdd: "; var_dump($suc); +// Check if existing column $suc = $maintenance->existsColumn('users', 'email'); echo "\nExistsColumn: "; var_dump($suc); +// Check if existing column in new name, if not rename it $suc = $maintenance->checkDBRename('users', 'email', 'email_new', 'varchar(255) DEFAULT NULL'); echo "\nCheckDBRename: "; var_dump($suc); +// Chceck type of column, if not matched change it $suc = $maintenance->checkDBRetype('users', 'email_new', 'varchar(128)', 'varchar(128) DEFAULT NULL AFTER `password`'); echo "\nCheckDBRetype: "; var_dump($suc); +// Remove column from table $suc = $maintenance->checkDBRemove('users', 'email_new'); echo "\nCheckDBRemove: "; var_dump($suc); +// Add index for column $suc = $maintenance->addKey('users', 'name'); echo "\nAddKey: "; var_dump($suc); +// Drop index by column name $suc = $maintenance->dropKey('users', 'name'); echo "\nDropKey: "; var_dump($suc);