added info to README about Maintenance
This commit is contained in:
parent
d055bd1132
commit
8a3dfbe9f0
56
README.md
56
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
|
||||
<?php
|
||||
|
||||
require_once __DIR__.'/../src/DBmodel.php';
|
||||
require_once __DIR__.'/../src/Maintenance.php';
|
||||
|
||||
$db = new \TPsoft\DBmodel\DBmodel('mysql:host=127.0.0.1;dbname=test;charset=utf8mb4', 'test', 'test');
|
||||
|
||||
$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,
|
||||
`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";
|
||||
|
||||
?>
|
||||
```
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user