Indexes and foreign keys added

This commit is contained in:
Michael 2021-06-14 08:53:37 +00:00
parent 2cd0c1d30d
commit 96f07c7288
73 changed files with 1366 additions and 8 deletions

View file

@ -166,6 +166,12 @@ class DBStructure
$tables = [];
foreach (self::definition(null) as $name => $definition) {
$indexes = [];
foreach ($definition['indexes'] as $key => $value) {
$indexes[] = ['name' => $key, 'fields' => implode(', ', $value)];
}
$foreign = [];
$fields = [[
'name' => 'Field',
'comment' => 'Description',
@ -174,7 +180,7 @@ class DBStructure
'primary' => 'Key',
'default' => 'Default',
'extra' => 'Extra',
],
],
[
'name' => '-',
'comment' => '-',
@ -202,33 +208,43 @@ class DBStructure
$field['primary'] = ($value['primary'] ?? false) ? 'PRI' : '';
$field['default'] = $value['default'] ?? 'NULL';
$field['extra'] = $value['extra'] ?? '';
foreach ($field as $fieldname => $fieldvalue) {
$lengths[$fieldname] = max($lengths[$fieldname] ?? 0, strlen($fieldvalue));
}
$fields[] = $field;
if (!empty($value['foreign'])) {
$foreign[] = [
'field' => $key,
'targettable' => array_keys($value['foreign'])[0],
'targetfield' => array_values($value['foreign'])[0]
];
}
}
array_walk_recursive($fields, function(&$value, $key) use ($lengths)
{
$value = str_pad($value, $lengths[$key], $value === '-' ? '-' : ' ');
});
$tables[] = ['name' => $name, 'comment' => $definition['comment']];
$content = Renderer::replaceMacros(Renderer::getMarkupTemplate('structure.tpl'), [
'$name' => $name,
'$comment' => $definition['comment'],
'$fields' => $fields,
'$indexes' => $indexes,
'$foreign' => $foreign,
]);
$filename = DI::basePath() . '/doc/database/db_' . $name . '.md';
file_put_contents($filename, $content);
}
asort($tables);
$content = Renderer::replaceMacros(Renderer::getMarkupTemplate('tables.tpl'), [
'$tables' => $tables,
'$tables' => $tables,
]);
$filename = DI::basePath() . '/doc/database.md';
file_put_contents($filename, $content);
file_put_contents($filename, $content);
}
public static function printStructure($basePath)
@ -1278,7 +1294,7 @@ class DBStructure
if ($verbose) {
echo "Zero contact added\n";
}
}
}
} elseif (self::existsTable('contact') && $verbose) {
echo "Zero contact already added\n";
} elseif ($verbose) {
@ -1302,7 +1318,7 @@ class DBStructure
if (self::existsTable('permissionset')) {
if (!DBA::exists('permissionset', ['id' => 0])) {
DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']);
DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']);
$lastid = DBA::lastInsertId();
if ($lastid != 0) {
DBA::update('permissionset', ['id' => 0], ['id' => $lastid]);
@ -1337,7 +1353,7 @@ class DBStructure
} elseif ($verbose) {
echo "permissionset: Table not found\n";
}
if (!self::existsForeignKeyForField('tokens', 'client_id')) {
$tokens = DBA::p("SELECT `tokens`.`id` FROM `tokens`
LEFT JOIN `clients` ON `clients`.`client_id` = `tokens`.`client_id`