Skip to content
Snippets Groups Projects
Verified Commit f9aa16d7 authored by Damien's avatar Damien
Browse files

FEAT #13671 TIME 3:00 Database creation

parent 7d4bbdac
No related branches found
No related tags found
No related merge requests found
......@@ -311,6 +311,7 @@ $app->delete('/indexingModels/{id}', \IndexingModel\controllers\IndexingModelCon
//Installer
$app->get('/installer/prerequisites', \SrcCore\controllers\InstallerController::class . ':getPrerequisites');
$app->get('/installer/databaseConnection', \SrcCore\controllers\InstallerController::class . ':checkDatabaseConnection');
$app->post('/installer/database', \SrcCore\controllers\InstallerController::class . ':setDatabase');
//ListInstances
$app->put('/listinstances', \Entity\controllers\ListInstanceController::class . ':update');
......
......@@ -45,8 +45,7 @@ class InstallerController
$writable = is_writable('.') && is_readable('.');
$displayErrors = (ini_get('display_errors') == '1');
$shortOpenTag = (ini_get('short_open_tag') == '1');
$errorReporting = (ini_get('error_reporting') == 22519);
$errorReporting = (ini_get('error_reporting') >= 22519);
exec('whereis netcat', $outputNetcat, $returnNetcat);
$outputNetcat = explode(':', $outputNetcat[0]);
......@@ -72,7 +71,6 @@ class InstallerController
'zip' => $zip,
'writable' => $writable,
'displayErrors' => $displayErrors,
'shortOpenTag' => $shortOpenTag,
'errorReporting' => $errorReporting,
'netcatOrNmap' => $netcatOrNmap
];
......@@ -92,8 +90,9 @@ class InstallerController
return $response->withStatus(400)->withJson(['errors' => 'Body user is empty or not a string']);
} elseif (!Validator::stringType()->notEmpty()->validate($queryParams['password'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body password is empty or not a string']);
} elseif (!Validator::stringType()->notEmpty()->validate($queryParams['name'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body name is empty or not a string']);
}
if (empty($queryParams['name'])) {
$queryParams['name'] = 'postgres';
}
$connection = "host={$queryParams['server']} port={$queryParams['port']} user={$queryParams['user']} password={$queryParams['password']} dbname={$queryParams['name']}";
......@@ -104,9 +103,57 @@ class InstallerController
$request = "select datname from pg_database where datname = '{$queryParams['name']}'";
$result = @pg_query($request);
if (!$result) {
return $response->withStatus(400)->withJson(['errors' => 'Request failed']);
}
return $response->withStatus(204);
}
public function setDatabase(Request $request, Response $response)
{
$body = $request->getParsedBody();
if (!Validator::stringType()->notEmpty()->validate($body['server'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body server is empty or not a string']);
} elseif (!Validator::intVal()->notEmpty()->validate($body['port'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body port is empty or not an integer']);
} elseif (!Validator::stringType()->notEmpty()->validate($body['user'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body user is empty or not a string']);
} elseif (!Validator::stringType()->notEmpty()->validate($body['password'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body password is empty or not a string']);
} elseif (!Validator::stringType()->notEmpty()->validate($body['name'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body name is empty or not a string']);
}
$connection = "host={$body['server']} port={$body['port']} user={$body['user']} password={$body['password']} dbname={$body['name']}";
if (!@pg_connect($connection)) {
return $response->withStatus(400)->withJson(['errors' => 'Connexion failed']);
}
$request = "CREATE DATABASE '{$body['name']}' WITH TEMPLATE template0 ENCODING = 'UTF8'";
$result = pg_query($request);
if (!$result) {
return $response->withStatus(400)->withJson(['errors' => 'Request failed']);
}
@pg_query("ALTER DATABASE '{$body['name']}' SET DateStyle =iso, dmy");
pg_close();
$options = [
\PDO::ATTR_PERSISTENT => true,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_CASE => \PDO::CASE_NATURAL
];
$dsn = "pgsql:host={$body['server']};port={$body['port']};dbname={$body['name']}";
$db = new \PDO($dsn, $body['user'], $body['password'], $options);
$fileContent = file_get_contents('sql/structure.sql');
$result = $db->query($fileContent, null, true, true);
if (!$result) {
return $response->withStatus(400)->withJson(['errors' => 'Request failed : run structure.sql']);
}
return $response->withStatus(204);
}
}
This diff is collapsed.
......@@ -165,6 +165,7 @@ export const LANG_NL = {
"city" : "Gemeente",
"classifying" : "STANDEN",
"clause" : "Clausule",
"fingerprintsDoNotMatch": "Fingerprints do not match",
"clauseGroup" : "Documentaire perimeter",
"clickOn" : "Klik op",
"close" : "Afsluiten",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment