Command line option to check initial values

This commit is contained in:
Michael 2020-11-23 18:58:18 +00:00
parent 794f98f479
commit 61ff99c410
2 changed files with 58 additions and 11 deletions

View file

@ -55,6 +55,7 @@ Commands
update Update database schema update Update database schema
dumpsql Dump database schema dumpsql Dump database schema
toinnodb Convert all tables from MyISAM or InnoDB in the Antelope file format to InnoDB in the Barracuda file format toinnodb Convert all tables from MyISAM or InnoDB in the Antelope file format to InnoDB in the Barracuda file format
initial Set needed initial values in the tables
version Set the database to a given number version Set the database to a given number
Options Options
@ -123,7 +124,11 @@ HELP;
DBStructure::setDatabaseVersion($this->getArgument(1)); DBStructure::setDatabaseVersion($this->getArgument(1));
$output = ob_get_clean(); $output = ob_get_clean();
break; break;
case "initial":
ob_start();
DBStructure::checkInitialValues(true);
$output = ob_get_clean();
break;
default: default:
$output = 'Unknown command: ' . $this->getArgument(0); $output = 'Unknown command: ' . $this->getArgument(0);
} }

View file

@ -1046,20 +1046,34 @@ class DBStructure
/** /**
* Check if initial database values do exist - or create them * Check if initial database values do exist - or create them
*/ */
public static function checkInitialValues() public static function checkInitialValues(bool $verbose = false)
{ {
if (self::existsTable('verb') && !DBA::exists('verb', ['id' => 1])) { if (self::existsTable('verb')) {
foreach (Item::ACTIVITIES as $index => $activity) { if (!DBA::exists('verb', ['id' => 1])) {
DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], Database::INSERT_IGNORE); foreach (Item::ACTIVITIES as $index => $activity) {
DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], Database::INSERT_IGNORE);
}
if ($verbose) {
echo "verb: activities added\n";
}
} elseif ($verbose) {
echo "verb: activities already added\n";
} }
}
if (self::existsTable('verb') && !DBA::exists('verb', ['id' => 0])) { if (!DBA::exists('verb', ['id' => 0])) {
DBA::insert('verb', ['name' => '']); DBA::insert('verb', ['name' => '']);
$lastid = DBA::lastInsertId(); $lastid = DBA::lastInsertId();
if ($lastid != 0) { if ($lastid != 0) {
DBA::update('verb', ['id' => 0], ['id' => $lastid]); DBA::update('verb', ['id' => 0], ['id' => $lastid]);
if ($verbose) {
echo "Zero verb added\n";
}
}
} elseif ($verbose) {
echo "Zero verb already added\n";
} }
} elseif ($verbose) {
echo "verb: Table not found\n";
} }
if (self::existsTable('user') && !DBA::exists('user', ['uid' => 0])) { if (self::existsTable('user') && !DBA::exists('user', ['uid' => 0])) {
@ -1072,7 +1086,14 @@ class DBStructure
$lastid = DBA::lastInsertId(); $lastid = DBA::lastInsertId();
if ($lastid != 0) { if ($lastid != 0) {
DBA::update('user', ['uid' => 0], ['uid' => $lastid]); DBA::update('user', ['uid' => 0], ['uid' => $lastid]);
if ($verbose) {
echo "Zero user added\n";
}
} }
} elseif (self::existsTable('user')) {
echo "Zero user already added\n";
} else {
echo "user: Table not found\n";
} }
if (self::existsTable('contact') && !DBA::exists('contact', ['id' => 0])) { if (self::existsTable('contact') && !DBA::exists('contact', ['id' => 0])) {
@ -1080,7 +1101,14 @@ class DBStructure
$lastid = DBA::lastInsertId(); $lastid = DBA::lastInsertId();
if ($lastid != 0) { if ($lastid != 0) {
DBA::update('contact', ['id' => 0], ['id' => $lastid]); DBA::update('contact', ['id' => 0], ['id' => $lastid]);
if ($verbose) {
echo "Zero contact added\n";
}
} }
} elseif (self::existsTable('contact')) {
echo "Zero contact already added\n";
} else {
echo "contact: Table not found\n";
} }
if (self::existsTable('permissionset')) { if (self::existsTable('permissionset')) {
@ -1089,7 +1117,12 @@ class DBStructure
$lastid = DBA::lastInsertId(); $lastid = DBA::lastInsertId();
if ($lastid != 0) { if ($lastid != 0) {
DBA::update('permissionset', ['id' => 0], ['id' => $lastid]); DBA::update('permissionset', ['id' => 0], ['id' => $lastid]);
if ($verbose) {
echo "Zero tag added\n";
}
} }
} elseif ($verbose) {
echo "Zero permissionset already added\n";
} }
if (!self::existsForeignKeyForField('item', 'psid')) { if (!self::existsForeignKeyForField('item', 'psid')) {
$sets = DBA::p("SELECT `psid`, `item`.`uid`, `item`.`private` FROM `item` $sets = DBA::p("SELECT `psid`, `item`.`uid`, `item`.`private` FROM `item`
@ -1112,6 +1145,8 @@ class DBStructure
} }
DBA::close($sets); DBA::close($sets);
} }
} elseif ($verbose) {
echo "permissionset: Table not found\n";
} }
if (self::existsTable('tag') && !DBA::exists('tag', ['id' => 0])) { if (self::existsTable('tag') && !DBA::exists('tag', ['id' => 0])) {
@ -1119,7 +1154,14 @@ class DBStructure
$lastid = DBA::lastInsertId(); $lastid = DBA::lastInsertId();
if ($lastid != 0) { if ($lastid != 0) {
DBA::update('tag', ['id' => 0], ['id' => $lastid]); DBA::update('tag', ['id' => 0], ['id' => $lastid]);
if ($verbose) {
echo "Zero tag added\n";
}
} }
} elseif (self::existsTable('tag')) {
echo "Zero tag already added\n";
} else {
echo "tag: Table not found\n";
} }
if (!self::existsForeignKeyForField('tokens', 'client_id')) { if (!self::existsForeignKeyForField('tokens', 'client_id')) {