Skip to content

Commit

Permalink
Supporting Rewire role schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
snoach committed Oct 23, 2014
1 parent b37369a commit 6639e13
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 46 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Requires fpm: https://github.com/jordansissel/fpm
#

release_version="1.0.4"
release_version="1.0.5"
release_dir=/tmp/propagator-release
release_files_dir=$release_dir/propagator
rm -rf $release_dir/*
Expand Down
3 changes: 3 additions & 0 deletions css/propagator.css
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,6 @@ th.database_instance_env {
color: #2c2c2c;
}

.tab-pane .tab-pane-section {
padding-bottom: 48px;
}
49 changes: 36 additions & 13 deletions lib/Propagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,11 @@ public function database_role() {
$data['instances'] = $this->data_model->get_instances_by_role($data['database_role_id']);
$data['instances_compact'] = implode("\n", array_map(function($instance) { return $instance['host'].":".$instance['port']; }, $data['instances']));
$data['assigned_instance_ids'] = array_map(function($instance) { return $instance['database_instance_id']; }, $data['instances']);


$data['all_known_schemas'] = $this->data_model->get_known_schemas();
$data['assigned_schemas'] = $this->data_model->get_known_schemas_by_role($data['database_role_id']);
$data['assigned_schemas_ids'] = array_map(function($schema) { return $schema['known_deploy_schema_id']; }, $data['assigned_schemas']);

$data['query_mappings'] = safe_presentation_query_mappings($this->data_model->get_database_role_query_mapping($data['database_role_id']));

$this->load->view("database_role", $data);
Expand Down Expand Up @@ -589,21 +593,40 @@ public function duplicate_database_role() {
public function rewire_database_role() {
$data['database_role_id'] = get_var('database_role_id');
$data['assigned_instance_ids'] = get_var('assigned_instance_ids');

try
{
if (!$this->user_is_dba()) {
throw new Exception("Unauthorized");
}
try
{
if (!$this->user_is_dba()) {
throw new Exception("Unauthorized");
}
$this->data_model->rewire_database_role($data['database_role_id'], $data['assigned_instance_ids']);
return $this->redirect("database_role", "database_role_id=" . $data['database_role_id']);
}
catch (Exception $e)
{
return $this->redirect("database_role", "database_role_id=" . $data['database_role_id'] . "&error_message=".$e->getMessage());
}
return $this->redirect("database_role", "database_role_id=" . $data['database_role_id']);
}
catch (Exception $e)
{
return $this->redirect("database_role", "database_role_id=" . $data['database_role_id'] . "&error_message=".$e->getMessage());
}
}


public function rewire_database_role_schemas() {
$data['database_role_id'] = get_var('database_role_id');
$data['assigned_schema_ids'] = get_var('assigned_schema_ids');

try
{
if (!$this->user_is_dba()) {
throw new Exception("Unauthorized");
}
$this->data_model->rewire_database_role_schemas($data['database_role_id'], $data['assigned_schema_ids']);
return $this->redirect("database_role", "database_role_id=" . $data['database_role_id']);
}
catch (Exception $e)
{
return $this->redirect("database_role", "database_role_id=" . $data['database_role_id'] . "&error_message=".$e->getMessage());
}
}

public function compare_database_role() {
if (!$this->user_is_dba()) {
throw new Exception("Unauthorized");
Expand Down
59 changes: 52 additions & 7 deletions lib/PropagatorModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,13 @@ function duplicate_database_role($database_role_id, $new_database_role_id, $new_


function rewire_database_role($database_role_id, $assigned_instance_ids) {
$database = $this->get_database();
$assigned_instance_ids = array_map(
function($e) use ($database, $database_role_id) { return "(".$database->quote($e).", ".$database->quote($database_role_id).")"; },
$assigned_instance_ids
);

$this->get_database()->query("
$database = $this->get_database();
$assigned_instance_ids = array_map(
function($e) use ($database, $database_role_id) { return "(".$database->quote($e).", ".$database->quote($database_role_id).")"; },
$assigned_instance_ids
);
$this->get_database()->query("
start transaction;
delete from
database_instance_role
Expand All @@ -393,6 +393,30 @@ function($e) use ($database, $database_role_id) { return "(".$database->quote($e
}


function rewire_database_role_schemas($database_role_id, $assigned_schema_ids) {
$database = $this->get_database();
$assigned_schema_ids = array_map(
function($e) use ($database, $database_role_id) { return "(".$database->quote($e).", ".$database->quote($database_role_id).")"; },
$assigned_schema_ids
);

$this->get_database()->query("
start transaction;
delete from
database_role_known_deploy_schema
where
database_role_id = " . $this->get_database()->quote($database_role_id) . "
;
insert into
database_role_known_deploy_schema (known_deploy_schema_id, database_role_id)
values
" . implode(",", $assigned_schema_ids) . "
;
commit;
");
}


function duplicate_database_instance($database_instance_id, $new_database_instance_host, $new_database_instance_port, $new_database_instance_description) {
$new_database_instance_host = trim($new_database_instance_host);
$new_database_instance_port = trim($new_database_instance_port);
Expand Down Expand Up @@ -543,6 +567,27 @@ function get_known_schemas() {
return $datas;
}


function get_known_schemas_by_role($database_role) {
$datas = $this->get_database()->query("
SELECT
known_deploy_schema_id, schema_name, known_deploy_schema.is_default,
count(database_instance_id) > 0 as has_mapping
FROM
known_deploy_schema
JOIN database_role_known_deploy_schema USING (known_deploy_schema_id)
LEFT JOIN database_instance_schema_mapping ON (schema_name = from_schema)
WHERE
database_role_id = " . $this->get_database()->quote($database_role) . "
GROUP BY
known_deploy_schema_id, schema_name, known_deploy_schema.is_default
ORDER BY
schema_name
")->fetchAll();
return $datas;
}


function get_database_roles_by_instance($database_instance_id) {
$datas = $this->get_database()->query("
SELECT
Expand Down
82 changes: 57 additions & 25 deletions views/database_role.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,40 +101,72 @@
</div>

<div class="tab-pane" id="rewire_role_tab">
<h6>Rewire hosts</h6>

<div class="pull-left">
Assign this role to other hosts; remove this role from other hosts
</div>

<div class="pull-right">
<form action="index.php" method="post" class="form-inline" name="rewire_database_role_form" id="rewire_database_role_form">
<input type="hidden" name="action" value="rewire_database_role">
<input type="hidden" name="database_role_id" value="<?php echo htmlspecialchars($database_role["database_role_id"]); ?>">
<select class="chosen-select" multiple="true" name="assigned_instance_ids[]">
<?php foreach($all_database_instances as $instance) { ?>
<tr>
<option value="<?php echo $instance["database_instance_id"] ?>"
<?php if(in_array($instance["database_instance_id"], $assigned_instance_ids)) {?>
selected="true"
<?php } ?>
><?php echo $instance["host"] ?>:<?php echo $instance["port"] ?></option>
</tr>
<?php } ?>
</select>
<input class="btn-primary btn-small" type="submit" value="Rewire" name="submit"/>
</form>
</div>
<div class="tab-pane-section">
<h6>Rewire hosts</h6>

<div class="pull-left">
Assign this role to other hosts; remove this role from other hosts
</div>

<div class="pull-right">
<form action="index.php" method="post" class="form-inline" name="rewire_database_role_form" id="rewire_database_role_form">
<input type="hidden" name="action" value="rewire_database_role">
<input type="hidden" name="database_role_id" value="<?php echo htmlspecialchars($database_role["database_role_id"]); ?>">
<select class="chosen-select chosen-select-instances" multiple="true" name="assigned_instance_ids[]">
<?php foreach($all_database_instances as $instance) { ?>
<tr>
<option value="<?php echo $instance["database_instance_id"] ?>"
<?php if(in_array($instance["database_instance_id"], $assigned_instance_ids)) {?>
selected="true"
<?php } ?>
><?php echo $instance["host"] ?>:<?php echo $instance["port"] ?></option>
</tr>
<?php } ?>
</select>
<input class="btn-primary btn-small" type="submit" value="Rewire" name="submit"/>
</form>
</div>
</div>
<div class="tab-pane-section">
<h6>Rewire known schemas</h6>

<div class="pull-left">
Associate schemas with this role
</div>

<div class="pull-right">
<form action="index.php" method="post" class="form-inline" name="rewire_database_role_schema_form" id="rewire_database_role_schema_form">
<input type="hidden" name="action" value="rewire_database_role_schemas">
<input type="hidden" name="database_role_id" value="<?php echo htmlspecialchars($database_role["database_role_id"]); ?>">
<select class="chosen-select chosen-select-schemas" multiple="true" name="assigned_schema_ids[]">
<?php foreach($all_known_schemas as $schema) { ?>
<tr>
<option value="<?php echo $schema["known_deploy_schema_id"] ?>"
<?php if(in_array($schema["known_deploy_schema_id"], $assigned_schemas_ids)) {?>
selected="true"
<?php } ?>
><?php echo $schema["schema_name"] ?></option>
</tr>
<?php } ?>
</select>
<input class="btn-primary btn-small" type="submit" value="Rewire" name="submit"/>
</form>
</div>
</div>
</div>
</div>
</div>
<?php } ?>

<script lang="JavaScript">
$(document).ready(function() {
$(".chosen-select").chosen({
$(".chosen-select.chosen-select-instances").chosen({
placeholder_text_multiple: "Choose instance",
width: "480px"
});
$(".chosen-select.chosen-select-schemas").chosen({
placeholder_text_multiple: "Choose schema",
width: "480px"
});
});
</script>

0 comments on commit 6639e13

Please sign in to comment.