Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 877ca38c0b | |||
| 8a3dfbe9f0 | |||
| d055bd1132 |
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";
|
||||
|
||||
?>
|
||||
```
|
||||
|
||||
@ -1,65 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
TPsoft.org 2000-2025
|
||||
file for controlers/*.php
|
||||
|
||||
Molestones:
|
||||
2025-05-27 21:36 Created
|
||||
*/
|
||||
|
||||
class Test extends \TPsoft\DBmodel\DBmodel {
|
||||
|
||||
public $tables = array(
|
||||
'test' => array(
|
||||
'name' => 'test',
|
||||
'primary_key_name' => 'id',
|
||||
'allow_attributes' => array(
|
||||
'name' => 'varchar(255)',
|
||||
'created' => 'datetime'
|
||||
)
|
||||
),
|
||||
);
|
||||
|
||||
public function exist($primary_key = null) {
|
||||
return $this->existRecord('test', $primary_key);
|
||||
}
|
||||
|
||||
public function test($primary_key = null, $data = array()) {
|
||||
return $this->record('test', $primary_key, $data);
|
||||
}
|
||||
|
||||
public function testBy($colname, $colvalue) {
|
||||
return $this->recordBy('test', $colname, $colvalue);
|
||||
}
|
||||
|
||||
public function testSave($data = array()) {
|
||||
return $this->test($this->exist($data) ? $data : null, $data);
|
||||
}
|
||||
|
||||
public function testEmpty() {
|
||||
return $this->recordEmpty('test');
|
||||
}
|
||||
|
||||
public function testAttributes() {
|
||||
return $this->typesAttributes('test');
|
||||
}
|
||||
|
||||
public function testCount() {
|
||||
return $this->count('test');
|
||||
}
|
||||
|
||||
public function getList($search = array(), $reverse = false, $concat_or = false) {
|
||||
return $this->search('test')
|
||||
->where($search, $concat_or)
|
||||
->order(array('id' => $reverse ? 'DESC' : 'ASC'))
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public function testCombo($col_key, $col_value, $add_empty = false) {
|
||||
return $this->search('test')
|
||||
->toCombo($col_key, $col_value, $add_empty);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@ -4,10 +4,19 @@ namespace TPsoft\DBmodel;
|
||||
|
||||
class Creator {
|
||||
|
||||
private $dbh;
|
||||
private DBmodel $dbh;
|
||||
private string $rootDir = '';
|
||||
|
||||
public function __construct($dbh) {
|
||||
$this->dbh = $dbh;
|
||||
public function __construct(?DBmodel $dbh = null) {
|
||||
if (is_null($dbh)) {
|
||||
if (!is_null(DBmodel::$instance)) {
|
||||
$this->dbh = DBmodel::$instance;
|
||||
} else {
|
||||
throw new \Exception('DB handler is null');
|
||||
}
|
||||
} else {
|
||||
$this->dbh = $dbh;
|
||||
}
|
||||
}
|
||||
|
||||
public function interact() {
|
||||
@ -97,7 +106,7 @@ class ".$name." extends \TPsoft\DBmodel\DBmodel {
|
||||
|
||||
public $"."tables = array(
|
||||
'".$entity."' => array(
|
||||
'name' => '".$entity."',
|
||||
'name' => '".$tablename."',
|
||||
'primary_key_name' => ".$primary_key_name.",
|
||||
'allow_attributes' => array(
|
||||
".implode(",\n\t\t\t\t", $allowed_attributes)."
|
||||
@ -166,11 +175,14 @@ class ".$name." extends \TPsoft\DBmodel\DBmodel {
|
||||
return $line;
|
||||
}
|
||||
|
||||
public function rootDir() {
|
||||
public function rootDir(?string $dir = null) {
|
||||
if (!is_null($dir)) {
|
||||
$this->rootDir = $dir;
|
||||
}
|
||||
if (strlen($this->rootDir) > 0) return $this->rootDir;
|
||||
return dirname(dirname(__FILE__));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
|
||||
@ -674,12 +674,12 @@ class DBmodel
|
||||
foreach ($data[$column] as $key => $val) {
|
||||
$prefix = $this->extractPrefix($val, $where_prefixes);
|
||||
$n_val = str_replace($prefix, '', $val);
|
||||
$data[$column] = $prefix . date('Y-m-d H:i:s', strtotime($n_val));
|
||||
$data[$column] = $prefix . date('Y-m-d H:i:s', is_numeric($n_val) ? $n_val : strtotime($n_val));
|
||||
}
|
||||
} else {
|
||||
$prefix = $this->extractPrefix($data[$column], $where_prefixes);
|
||||
$n_val = str_replace($prefix, '', $data[$column]);
|
||||
$data[$column] = $prefix . date('Y-m-d H:i:s', strtotime($n_val));
|
||||
$data[$column] = $prefix . date('Y-m-d H:i:s', is_numeric($n_val) ? $n_val : strtotime($n_val));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ class Maintenance
|
||||
|
||||
public string $lastMessage = '';
|
||||
|
||||
private DBmodel $dbh;
|
||||
protected DBmodel $dbh;
|
||||
|
||||
public function __construct(?DBmodel $dbh)
|
||||
{
|
||||
@ -79,6 +79,14 @@ class Maintenance
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function existsTable($table_name)
|
||||
{
|
||||
return $this->testDB(
|
||||
'SHOW TABLES LIKE "' . $table_name . '"',
|
||||
$table_name
|
||||
);
|
||||
}
|
||||
|
||||
public function checkDBTable($table_name, $definition)
|
||||
{
|
||||
if (strlen($table_name) <= 0) {
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user