New table "post-user" and more foreign keys

This commit is contained in:
Michael 2020-11-17 22:33:44 +00:00
parent ba0d3b2435
commit eaa58da25b
7 changed files with 274 additions and 46 deletions

View file

@ -170,6 +170,34 @@ class DBStructure
return $definition;
}
/**
* Get field data for the given table
*
* @param string $table
* @param array $data data fields
* @return array fields for the given
*/
public static function getFieldsForTable(string $table, array $data = [])
{
$definition = DBStructure::definition('', false);
if (empty($definition[$table])) {
return [];
}
$fieldnames = array_keys($definition[$table]['fields']);
$fields = [];
// Assign all field that are present in the table
foreach ($fieldnames as $field) {
if (isset($data[$field])) {
$fields[$field] = $data[$field];
}
}
return $fields;
}
private static function createTable($name, $structure, $verbose, $action)
{
$r = true;
@ -1073,6 +1101,28 @@ class DBStructure
}
}
if (self::existsTable('user') && !DBA::exists('user', ['uid' => 0])) {
$system = User::getSystemAccount();
$user = [
"username" => $system['name'],
"nickname" => $system['nick'],
"register_date" => $system['created'],
"pubkey" => $system['pubkey'],
"prvkey" => $system['prvkey'],
"spubkey" => $system['spubkey'],
"sprvkey" => $system['sprvkey'],
"verified" => true,
"page-flags" => User::PAGE_FLAGS_SOAPBOX,
"account-type" => User::ACCOUNT_TYPE_RELAY,
];
DBA::insert('user', $user);
$lastid = DBA::lastInsertId();
if ($lastid != 0) {
DBA::update('user', ['uid' => 0], ['uid' => $lastid]);
}
}
if (!self::existsForeignKeyForField('tokens', 'client_id')) {
$tokens = DBA::p("SELECT `tokens`.`id` FROM `tokens`
LEFT JOIN `clients` ON `clients`.`client_id` = `tokens`.`client_id`