Skip to content

Commit

Permalink
Create database if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
piyushwebkul committed Nov 1, 2019
1 parent 37738ba commit 7e1f131
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
25 changes: 25 additions & 0 deletions public/css/wizard.css
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,29 @@ ul.button-groups > li:last-child {

.systemCriteria-Info-Message {
margin-left: 30px;
}

.wizard-container .database-integration .checkbox {
display: flex;
flex-flow: row nowrap;
align-items: center;
justify-content: flex-start;
}

.wizard-container .database-integration .checkbox input[type="checkbox"] {
width: 20px;
height: 20px;
margin: 0;
padding: 0;
}

.wizard-container .database-integration .checkbox .checkbox-info {
font-size: 15px;
color: #333333;
padding-left: 5px;
user-select: none;
}

.wizard-container .database-integration .checkbox-form-field {
margin-top: -10px;
}
9 changes: 6 additions & 3 deletions public/scripts/wizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@
username: 'root',
password: null,
database: null,
ifNotExists: 1,
}
},
initialize: function (attributes) {
Expand All @@ -346,10 +347,11 @@
isProcedureCompleted: function (callback) {
this.set('credentials', {
serverName: this.view.$el.find('input[name="serverName"]').val(),
serverPort: this.view.$el.find('input[name="port"]').val(),
serverPort: this.view.$el.find('input[name="serverPort"]').val(),
username: this.view.$el.find('input[name="username"]').val(),
password: this.view.$el.find('input[name="password"]').val(),
database: this.view.$el.find('input[name="database"]').val(),
ifNotExists: this.view.$el.find('input[name="ifNotExists"]').prop("checked") ? 1 : 0,
});

let wizard = this.view.wizard;
Expand All @@ -364,7 +366,7 @@
element.parentNode.removeChild(element);
}

this.view.$el.find('.form-content input[name="database"]').parent().append("<span id='wizard-error-id' class='wizard-form-notice'>Details are incorrect ! Connection not established.</span>");
this.view.$el.find('.form-content input[name="ifNotExists"]').parents('.form-content').append("<span id='wizard-error-id' class='wizard-form-notice'>Details are incorrect ! Connection not established.</span>");
wizard.disableNextStep();
}
}.bind(this)).fail(function(response) {
Expand All @@ -382,6 +384,7 @@
database_configuration_template: _.template($("#installationWizard-DatabaseConfigurationTemplate").html()),
events: {
"keyup #wizard-configureDatabase .form-content input" : "validateForm",
"change #wizard-configureDatabase .form-content input[type='checkbox']" : "validateForm",
},
initialize: function(params) {
if (params.existingModel instanceof UVDeskCommunityDatabaseConfigurationModel) {
Expand Down Expand Up @@ -412,7 +415,7 @@

let credentials = {
hostname: this.$el.find('input[name="serverName"]').val(),
port: this.$el.find('input[name="port"]').val(),
serverPort: this.$el.find('input[name="serverPort"]').val(),
username: this.$el.find('input[name="username"]').val(),
password: this.$el.find('input[name="password"]').val(),
database: this.$el.find('input[name="database"]').val(),
Expand Down
31 changes: 24 additions & 7 deletions src/Controller/ConfigureHelpdesk.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\DBAL\DriverManager;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -86,16 +87,32 @@ public function verifyDatabaseCredentials(Request $request)

// unset($_SESSION['DB_CONFIG']);

// Get entity manager
$entityManager = EntityManager::create([
$dbParams = [
'driver' => 'pdo_mysql',
"host" => $request->request->get('serverName'),
"port" => $request->request->get('port'),
"port" => $request->request->get('serverPort'),
'user' => $request->request->get('username'),
'password' => $request->request->get('password'),
'dbname' => $request->request->get('database'),
], Setup::createAnnotationMetadataConfiguration(['src/Entity'], false));

];
try {
$ifNotExists = (bool) $request->request->get('ifNotExists');
$tmpConnection = DriverManager::getConnection($dbParams);
$dbName = $request->request->get('database');
$dbParams = array_merge($dbParams, ['dbname' => $dbName]);
$shouldCreateDatabase = $ifNotExists && !in_array($dbName, $tmpConnection->getSchemaManager()->listDatabases());

if ($shouldCreateDatabase) {
$tmpConnection->getSchemaManager()->createDatabase($tmpConnection->getDatabasePlatform()->quoteSingleIdentifier($dbName));
}

} catch (\Exception $e) {
// Unable to create database
// @TODO: Error reporting to user.
return new Response(json_encode(['status' => false]), 200, self::DEFAULT_JSON_HEADERS);
}

// Get entity manager
$entityManager = EntityManager::create($dbParams, Setup::createAnnotationMetadataConfiguration(['src/Entity'], false));
$databaseConnection = $entityManager->getConnection();
$connectionResponse = [
'status' => $databaseConnection->isConnected(),
Expand All @@ -105,7 +122,7 @@ public function verifyDatabaseCredentials(Request $request)
if (false == $connectionResponse['status']) {
try {
$databaseConnection->connect();

$connectionResponse['status'] = true;

$port = $request->request->get('port') ? ':' . $request->request->get('port') : '';
Expand Down
12 changes: 11 additions & 1 deletion templates/installation-wizard/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
<div class="form-field">
<label class="form-label">Port</label>
<div class="form-content">
<input name="port" type="text" value="<%- credentials.serverPort %>" placeholder="3306" />
<input name="serverPort" type="text" value="<%- credentials.serverPort %>" placeholder="3306" />
<p class="wizard-form-info">Port number of the database (on which port database is hosted).</p>
</div>
</div>
Expand Down Expand Up @@ -174,6 +174,16 @@
<p class="wizard-form-info">Name of the database/schema to connect to.</p>
</div>
</div>
<div class="form-field checkbox-form-field">
<label class="form-label"></label>
<div class="form-content">
<div class="checkbox">
<input id="ifNotExists" name="ifNotExists" type="checkbox" <%= credentials.ifNotExists == 1 ? "checked" : "" %> />
<label for="ifNotExists" class="checkbox-info">Create database if not exists.</label>
</div>
</div>
</div>
</form>
</div>
</script>
Expand Down

0 comments on commit 7e1f131

Please sign in to comment.