added table check/creation

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-01-23 16:31:18 +01:00
parent 28b2738c3e
commit aa706c9e6a
1 changed files with 67 additions and 3 deletions

View File

@ -12,11 +12,11 @@ class DatabaseConnection
{ {
private PDO $dbConnection; private PDO $dbConnection;
const TABLE_PREFIX = ""; const TABLE_PREFIX = '';
const TABLE_DOMAINS = self::TABLE_PREFIX . "domains"; const TABLE_DOMAINS = self::TABLE_PREFIX . "domains";
const TABLE_NAMESERVERS = self::TABLE_PREFIX . "nameservers"; const TABLE_NAMESERVERS = self::TABLE_PREFIX . "nameservers";
const TABLE_PANELS = self::TABLE_PREFIX . "panels"; const TABLE_PANELS = self::TABLE_PREFIX . "panels";
const TABLE_USER = self::TABLE_PREFIX . "user"; const TABLE_USER = self::TABLE_PREFIX . "apikeys";
public function __construct(private array $config) public function __construct(private array $config)
{ {
@ -28,8 +28,72 @@ class DatabaseConnection
username: $dbUser, username: $dbUser,
password: $dbPassword password: $dbPassword
); );
$sql = "SHOW TABLES";
$statement = $this->dbConnection->prepare($sql);
if (!$statement->execute()) {
echo 'Error: Cannot find tables.' . PHP_EOL;
if (confirm('Should I try to create them?')) {
$sql = "
CREATE TABLE `apikeys` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`api_token_prefix` varchar(13) COLLATE utf8mb4_unicode_ci NOT NULL,
`api_token` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$statement = $this->dbConnection->prepare($sql);
$statement->execute();
$sql = "
CREATE TABLE `domains` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`a` varbinary(255) DEFAULT NULL,
`aaaa` varbinary(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$statement = $this->dbConnection->prepare($sql);
$statement->execute();
$sql = "
CREATE TABLE `nameservers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`a` varbinary(255) DEFAULT NULL,
`aaaa` varbinary(255) DEFAULT NULL,
`apikey` varbinary(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$statement = $this->dbConnection->prepare($sql);
$statement->execute();
$sql = "
CREATE TABLE `panels` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`a` varbinary(255) DEFAULT NULL,
`aaaa` varbinary(255) DEFAULT NULL,
`apikey` varbinary(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$statement = $this->dbConnection->prepare($sql);
$statement->execute();
echo 'Tables have been created.' . PHP_EOL;
}
exit(1);
}
} catch (PDOException $exception) { } catch (PDOException $exception) {
exit($exception->getMessage()); echo $exception->getMessage() . PHP_EOL;
echo 'Did you create the database and adjust the config file?' . PHP_EOL;
echo PHP_EOL . 'You can create database an user via a panel or manually in mysql shell:' . PHP_EOL;
echo 'CREATE DATABASE databasename;' . PHP_EOL;
echo "CREATE USER 'user'@'localhost' IDENTIFIED BY 'secret';" . PHP_EOL;
echo "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, DROP, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON databasename.* TO 'user'@'localhost';" . PHP_EOL;
echo 'There is no need to run FLUSH PRIVILEGES when using GRANT!' . PHP_EOL;
exit(1);
} }
} }