Skip to content

Commit

Permalink
Merge dde6d5b into fea639e
Browse files Browse the repository at this point in the history
  • Loading branch information
nkiraly committed Jul 7, 2015
2 parents fea639e + dde6d5b commit bceaa09
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/DBSteward/dbsteward.php
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ public function xml_data_insert($def_file, $data_file) {
public static function get_logger() {
if (!self::$logger) {
self::$logger = new Monolog\Logger('dbsteward');
self::$logger->pushHandler($sh = new Monolog\Handler\StreamHandler(STDERR, static::$LOG_LEVEL));
self::$logger->pushHandler($sh = new Monolog\Handler\StreamHandler('php://stderr', static::$LOG_LEVEL));
$sh->setFormatter(new DBStewardConsoleLogFormatter);
}
return self::$logger;
Expand Down
116 changes: 94 additions & 22 deletions lib/DBSteward/sql_format/pgsql8/pgsql8.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,14 @@ public static function build($output_prefix, $db_doc) {
if ( dbsteward::$generate_slonik ) {
$replica_sets = static::get_slony_replica_sets($db_doc);
foreach($replica_sets AS $replica_set) {
pgsql8::build_slonik_preamble($db_doc, $replica_set, $output_prefix . "_slony_replica_set_" . $replica_set['id'] . "_preamble.slonik");
pgsql8::build_slonik_create_set($db_doc, $replica_set, $output_prefix . '_slony_replica_set_' . $replica_set['id'] . '_create.slonik');
pgsql8::build_slonik_paths($db_doc, $replica_set, $output_prefix . "_slony_replica_set_" . $replica_set['id'] . "_paths.slonik");
pgsql8::build_slonik_preamble($db_doc, $replica_set, $output_prefix . "_slony_replica_set_" . $replica_set['id'] . "_create_nodes.slonik");
pgsql8::build_slonik_store_nodes($db_doc, $replica_set, $output_prefix . "_slony_replica_set_" . $replica_set['id'] . "_create_nodes.slonik");
pgsql8::build_slonik_paths($db_doc, $replica_set, $output_prefix . "_slony_replica_set_" . $replica_set['id'] . "_create_nodes.slonik");

pgsql8::build_slonik_preamble($db_doc, $replica_set, $output_prefix . "_slony_replica_set_" . $replica_set['id'] . "_subscribe.slonik");
pgsql8::build_slonik_create_set($db_doc, $replica_set, $output_prefix . '_slony_replica_set_' . $replica_set['id'] . '_subscribe.slonik');
foreach($replica_set->slonyReplicaSetNode AS $replica_set_node) {
pgsql8::build_slonik_subscribe_set_node($db_doc, $replica_set, $output_prefix . '_slony_replica_set_' . $replica_set['id'] . '_subscribe_node_' . $replica_set_node['id'] . '.slonik', $replica_set_node);
pgsql8::build_slonik_subscribe_set_node($db_doc, $replica_set, $output_prefix . "_slony_replica_set_" . $replica_set['id'] . "_subscribe.slonik", $replica_set_node);
}

static::slony_ids_required_during_build($replica_set, $db_doc);
Expand Down Expand Up @@ -647,16 +650,16 @@ public static function build_data($db_doc, $ofs, $tables) {
* @throws exception
*/
public static function build_slonik_create_set($db_doc, $replica_set, $slonik_file) {
dbsteward::notice("Building slony replication set create file " . $slonik_file);
$slonik_fp = fopen($slonik_file, 'w');
dbsteward::notice("Building slonik CREATE SET for replica set ID " . $replica_set['id'] . " output file " . $slonik_file);
$slonik_fp = fopen($slonik_file, 'a');
if ($slonik_fp === FALSE) {
throw new exception("failed to open slonik file " . $slonik_file . ' for output');
}
$slonik_ofs = new output_file_segmenter($slonik_file, 1, $slonik_fp, $slonik_file);
$slonik_ofs->set_comment_line_prefix("#"); // keep slonik file comment lines consistent
$generation_date = date('r');
$slonik_ofs->write("# DBSteward slony replica set ID " . $replica_set['id'] . " " . $replica_set['comment'] . " create commands generated " . $generation_date . "\n\n");
$slonik_ofs->write("ECHO 'DBSteward slony replica set ID " . $replica_set['id'] . " " . $replica_set['comment'] . " create commands generated " . $generation_date . " starting';\n\n");
$slonik_ofs->write("ECHO 'DBSteward slony replica set ID " . $replica_set['id'] . " " . $replica_set['comment'] . " create commands generated " . $generation_date . "';\n\n");

$slonik_ofs->write("CREATE SET (ID = " . $replica_set['id'] . ", ORIGIN = " . $replica_set['originNodeId'] . ", COMMENT = '" . $replica_set['comment'] . "');\n\n");

Expand Down Expand Up @@ -713,16 +716,16 @@ public static function build_slonik_create_set($db_doc, $replica_set, $slonik_fi
* @throws exception
*/
public static function build_slonik_subscribe_set_node($db_doc, $replica_set, $slonik_file, $replica_set_node) {
dbsteward::notice("Building slony replication set " . $replica_set['id'] . " node " . $replica_set_node['id'] . " subscription file " . $slonik_file);
$slonik_fp = fopen($slonik_file, 'w');
dbsteward::notice("Building slonik replica set ID " . $replica_set['id'] . " node ID " . $replica_set_node['id'] . " subscription output file " . $slonik_file);
$slonik_fp = fopen($slonik_file, 'a');
if ($slonik_fp === FALSE) {
throw new exception("failed to open slonik file " . $slonik_file . ' for output');
}
$slonik_ofs = new output_file_segmenter($slonik_file, 1, $slonik_fp, $slonik_file);
$slonik_ofs->set_comment_line_prefix("#"); // keep slonik file comment lines consistent
$generation_date = date('r');
$slonik_ofs->write("# DBSteward slony replica set ID " . $replica_set['id'] . " node " . $replica_set_node['id'] . " subscription commands generated " . $generation_date . "\n\n");
$slonik_ofs->write("ECHO 'DBSteward slony replica set ID " . $replica_set['id'] . " node " . $replica_set_node['id'] . " subscription commands generated " . $generation_date . " starting';\n\n");
$slonik_ofs->write("ECHO 'DBSteward slony replica set ID " . $replica_set['id'] . " node " . $replica_set_node['id'] . " subscription commands generated " . $generation_date . "';\n\n");

// on the subscriber node
// wait for sync to come from primary node to provider node
Expand All @@ -737,15 +740,15 @@ public static function build_slonik_subscribe_set_node($db_doc, $replica_set, $s
WAIT ON = " . $replica_set_node['providerNodeId'] . ",
TIMEOUT = 0
);
SLEEP (SECONDS=60);
SLEEP (SECONDS=30);
SUBSCRIBE SET (
ID = " . $replica_set['id'] . ",
PROVIDER = " . $replica_set_node['providerNodeId'] . ",
RECEIVER = " . $replica_set_node['id'] . ",
FORWARD = YES
);
SLEEP (SECONDS=60);
SLEEP (SECONDS=30);
ECHO 'Waiting for replicaNode " . $replica_set_node['id'] . " subscription to providerNodeID " . $replica_set_node['providerNodeId'] . " replica set ID " . $replica_set['id'] . "';
SYNC (ID = " . $replica_set['originNodeId'] . ");
Expand All @@ -755,7 +758,7 @@ public static function build_slonik_subscribe_set_node($db_doc, $replica_set, $s
WAIT ON = " . $replica_set_node['id'] . ",
TIMEOUT = 0
);
SLEEP (SECONDS=60);
";
$slonik_ofs->write($subscription);
}
Expand Down Expand Up @@ -880,8 +883,12 @@ public static function build_upgrade($old_output_prefix, $old_composite_file, $o
}

public static function build_slonik_preamble($db_doc, $replica_set, $slony_preamble_file) {
dbsteward::notice("Building slonik pramble for replica set ID " . $replica_set['id'] . " output file " . $slony_preamble_file);
$timestamp = date('r');

// all the other slonik file writers use mode a
// have the preamble function to w to overwrite previous slonik command file sets
// as the preamble is always the first thing in a slonik file
$slony_preamble_fp = fopen($slony_preamble_file, 'w');
if ($slony_preamble_fp === FALSE) {
throw new exception("failed to open slony preamble output file " . $slony_preamble_file);
Expand Down Expand Up @@ -914,14 +921,60 @@ public static function build_slonik_preamble($db_doc, $replica_set, $slony_pream

$slony_preamble_ofs->write("\n");
$slony_preamble_ofs->write("# " . $slony_preamble_file . "\n");
$slony_preamble_ofs->write("# DBSteward slony preamble file generated " . $timestamp . "\n");
$slony_preamble_ofs->write("# DBSteward slony preamble generated " . $timestamp . "\n");
$slony_preamble_ofs->write("# Replica Set: " . $replica_set['id'] . "\n\n");
}

public static function build_slonik_store_nodes($db_doc, $replica_set, $slony_store_nodes_file) {
dbsteward::notice("Building slonik STORE NODEs for replica set ID " . $replica_set['id'] . " output file " . $slony_store_nodes_file);
$timestamp = date('r');

$slony_paths_fp = fopen($slony_store_nodes_file, 'a');
if ($slony_paths_fp === FALSE) {
throw new exception("failed to open slony paths output file " . $slony_store_nodes_file);
}
$slony_paths_ofs = new output_file_segmenter($slony_store_nodes_file, 1, $slony_paths_fp, $slony_store_nodes_file);
$slony_paths_ofs->set_comment_line_prefix("#"); // keep slonik file comment lines consistent
// don't write the fixed file header file name. slony preamble must start with the CLUSTER NAME directive
$slony_paths_ofs->disable_fixed_file_header();

$slony_paths_ofs->write("# " . $slony_store_nodes_file . "\n");
$slony_paths_ofs->write("# DBSteward slony store nodes generated " . $timestamp . "\n");
$slony_paths_ofs->write("# Replica Set: " . $replica_set['id'] . "\n\n");

if ( ! isset($db_doc->database->slony->slonyNode) ) {
$slony_paths_ofs->write("DBSTEWARD: NO SLONY NODES DEFINED\n");
return FALSE;
}

$origin_node = $replica_set['originNodeId'];
$slony_paths_ofs->write("# Initialize Cluster with origin node for Replica Set " . $replica_set['id'] . "\n");
$slony_paths_ofs->write(
"INIT CLUSTER ( ID = " . $origin_node
. ", COMMENT = '" . pgsql8::get_slony_replica_set_node_attribute($db_doc, $replica_set, $origin_node, 'comment') . "'"
. " );\n\n");

$slony_paths_ofs->write("# Store Cluster Nodes for Replica Set " . $replica_set['id'] . "\n");
$node_ids = pgsql8::get_slony_replica_set_node_ids($replica_set);
for($i = 0; $i < count($node_ids); $i++) {
$node_i = $node_ids[$i];
// don't STORE NODE the origin node, it was created during INIT CLUSTER
if ( $node_i != $origin_node ) {
$slony_paths_ofs->write(
"STORE NODE ( EVENT NODE = " . $origin_node . ", ID = " . $node_i
. ", COMMENT = '" . pgsql8::get_slony_replica_set_node_attribute($db_doc, $replica_set, $node_i, 'comment') . "'"
. " );\n");
}
}

$slony_paths_ofs->write("\n");
}

public static function build_slonik_paths($db_doc, $replica_set, $slony_paths_file) {
dbsteward::notice("Building slonik STORE PATHs for replica set ID " . $replica_set['id'] . " output file " . $slony_paths_file);
$timestamp = date('r');

$slony_paths_fp = fopen($slony_paths_file, 'w');
$slony_paths_fp = fopen($slony_paths_file, 'a');
if ($slony_paths_fp === FALSE) {
throw new exception("failed to open slony paths output file " . $slony_paths_file);
}
Expand All @@ -931,7 +984,7 @@ public static function build_slonik_paths($db_doc, $replica_set, $slony_paths_fi
$slony_paths_ofs->disable_fixed_file_header();

$slony_paths_ofs->write("# " . $slony_paths_file . "\n");
$slony_paths_ofs->write("# DBSteward slony paths file generated " . $timestamp . "\n");
$slony_paths_ofs->write("# DBSteward slony paths generated " . $timestamp . "\n");
$slony_paths_ofs->write("# Replica Set: " . $replica_set['id'] . "\n\n");

if ( ! isset($db_doc->database->slony->slonyNode) ) {
Expand Down Expand Up @@ -963,29 +1016,32 @@ public static function build_slonik_paths($db_doc, $replica_set, $slony_paths_fi
}

public static function build_upgrade_slonik_replica_set($old_db_doc, $new_db_doc, $old_replica_set, $new_replica_set, $slonik_file_prefix, $origin_header = '') {
dbsteward::notice("Building slonik upgrade replica set ID " . $new_replica_set['id']);
$timestamp = date('r');

$slony_stage1_file = $slonik_file_prefix . '_stage1.slonik';
$slony_stage1_fp = fopen($slony_stage1_file, 'w');
pgsql8::build_slonik_preamble($new_db_doc, $new_replica_set, $slony_stage1_file);
$slony_stage1_fp = fopen($slony_stage1_file, 'a');
if ($slony_stage1_fp === FALSE) {
throw new exception("failed to open upgrade slony stage 1 output file " . $slony_stage1_file);
}
$slony_stage1_ofs = new output_file_segmenter($slony_stage1_file, 1, $slony_stage1_fp, $slony_stage1_file);
$slony_stage1_ofs->set_comment_line_prefix("#"); // keep slonik file comment lines consistent
$slony_stage1_ofs->write("# DBSteward slony stage 1 upgrade file generated " . $timestamp . "\n");
$slony_stage1_ofs->write("# DBSteward slony stage 1 upgrade generated " . $timestamp . "\n");
$slony_stage1_ofs->write($origin_header . "\n");
$slony_stage1_ofs->write("ECHO 'DBSteward slony upgrade replica set " . $new_replica_set['id'] . " stage 1 file generated " . date('r') . " starting';\n\n");
$slony_stage1_ofs->write("ECHO 'DBSteward slony upgrade replica set " . $new_replica_set['id'] . " stage 1 generated " . date('r') . "';\n\n");

$slony_stage3_file = $slonik_file_prefix . '_stage3.slonik';
$slony_stage3_fp = fopen($slony_stage3_file, 'w');
pgsql8::build_slonik_preamble($new_db_doc, $new_replica_set, $slony_stage3_file);
$slony_stage3_fp = fopen($slony_stage3_file, 'a');
if ($slony_stage3_fp === FALSE) {
throw new exception("failed to open upgrade slony stage 3 output file " . $slony_stage3_file . ' for output');
}
$slony_stage3_ofs = new output_file_segmenter($slony_stage3_file, 1, $slony_stage3_fp, $slony_stage3_file);
$slony_stage3_ofs->set_comment_line_prefix("#"); // keep slonik file comment lines consistent
$slony_stage3_ofs->write("# DBSteward slony stage 3 upgrade file generated " . $timestamp . "\n");
$slony_stage3_ofs->write("# DBSteward slony stage 3 upgrade generated " . $timestamp . "\n");
$slony_stage3_ofs->write($origin_header . "\n");
$slony_stage3_ofs->write("ECHO 'DBSteward slony upgrade replica set " . $new_replica_set['id'] . " stage 3 file generated " . date('r') . " starting';\n\n");
$slony_stage3_ofs->write("ECHO 'DBSteward slony upgrade replica set " . $new_replica_set['id'] . " stage 3 generated " . date('r') . "';\n\n");

// slony replication configuration changes
// SLONY STAGE 1
Expand Down Expand Up @@ -1255,6 +1311,22 @@ public static function build_upgrade_slonik_replica_set($old_db_doc, $new_db_doc
$new_replica_set['originNodeId']
) . "\n\n");
}

// execute post-slony shaping SQL DDL / DCL commands at the end of stage 1 and 3 .slonik files
$sql_stage1_file = $slonik_file_prefix . '_stage1_schema1.sql';
// TODO: need to collect sql_stage1_file names from diff_doc() if there is
// more than one as a result of many changes between definition files
$slony_stage1_ofs->write("ECHO 'DBSteward upgrade replica set " . $new_replica_set['id'] . " stage 1 SQL EXECUTE SCRIPT';\n");
$slony_stage1_ofs->write("EXECUTE SCRIPT (
FILENAME = '" . $sql_stage1_file . "',
EVENT NODE = " . $new_replica_set['originNodeId'] . "
);\n\n");
$sql_stage3_file = $slonik_file_prefix . '_stage3_schema1.sql';
$slony_stage3_ofs->write("ECHO 'DBSteward upgrade replica set " . $new_replica_set['id'] . " stage 3 SQL EXECUTE SCRIPT';\n");
$slony_stage3_ofs->write("EXECUTE SCRIPT (
FILENAME = '" . $sql_stage3_file . "',
EVENT NODE = " . $new_replica_set['originNodeId'] . "
);\n\n");
}

protected static function create_slonik_upgrade_set($ofs, $doc, $replica_set) {
Expand Down
6 changes: 2 additions & 4 deletions lib/DBSteward/sql_format/pgsql8/pgsql8_diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public static function diff_doc($old_xml_file, $new_xml_file, $old_database, $ne
$old_set_new_set = "-- Old definition: " . $old_xml_file . "\n"
. "-- New definition: " . $new_xml_file . "\n"
. "-- Replica Set: " . $replica_set_id . "\n";

pgsql8::build_slonik_preamble($new_database, $replica_set, $upgrade_prefix . "_slony_replica_set_" . $replica_set_id . "_preamble.slonik");

$ofsm_stage1->set_replica_set_ofs(
$replica_set_id,
Expand Down Expand Up @@ -154,7 +152,7 @@ public static function diff_doc_work($stage1_ofs, $stage2_ofs, $stage3_ofs, $sta
$stage1_ofs->append_footer("\nCOMMIT;\n");
}
else {
$stage1_ofs->append_header("\n-- generateslonik specified: pgsql8 STAGE1 upgrade omitting BEGIN. slonik EXECUTE SCRIPT will wrap the submission in a transaction\n");
$stage1_ofs->append_header("\n-- generateslonik specified: pgsql8 STAGE1 upgrade omitting BEGIN. slonik EXECUTE SCRIPT will wrap stage 1 DDL and DCL in a transaction\n");
}
if ( ! dbsteward::$single_stage_upgrade ) {
$stage2_ofs->append_header("\nBEGIN;\n\n");
Expand All @@ -166,7 +164,7 @@ public static function diff_doc_work($stage1_ofs, $stage2_ofs, $stage3_ofs, $sta
$stage3_ofs->append_footer("\nCOMMIT;\n");
}
else {
$stage3_ofs->append_header("\n-- generateslonik specified: pgsql8 STAGE1 upgrade omitting BEGIN. slonik EXECUTE SCRIPT will wrap the submission in a transaction\n");
$stage3_ofs->append_header("\n-- generateslonik specified: pgsql8 STAGE1 upgrade omitting BEGIN. slonik EXECUTE SCRIPT will wrap stage 3 DDL and DCL in a transaction\n");
}
$stage4_ofs->append_header("\nBEGIN;\n\n");
$stage4_ofs->append_footer("\nCOMMIT;\n");
Expand Down
2 changes: 1 addition & 1 deletion lib/DBSteward/sql_format/sql99/sql99_diff_indexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function get_drop_indexes($old_schema, $old_table, $new_schema, $n

if (($new_table != null) && ($old_table != null)) {
foreach(format_index::get_table_indexes($old_schema, $old_table) as $old_index) {
$new_index = format_index::get_table_index($new_schema, $new_table, $old_index['name']);
$new_index = dbx::get_table_index($new_schema, $new_table, $old_index['name']);
if ( !format_table::contains_index($new_schema, $new_table, $old_index['name']) ) {
$list[] = $old_index;
}
Expand Down

0 comments on commit bceaa09

Please sign in to comment.