Skip to content

Commit

Permalink
Merge 7ac1077 into 86d1b3c
Browse files Browse the repository at this point in the history
  • Loading branch information
bstoots committed Sep 25, 2015
2 parents 86d1b3c + 7ac1077 commit 058559e
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/DBSteward/sql_format/pgsql8/pgsql8_xml_parser.php
Expand Up @@ -160,6 +160,14 @@ private static function create_partition_tables($orig_schema, &$schema, $orig_ta
$index->addChild('indexDimension', (string)$orig_dimension)
->addAttribute('name', ((string)$orig_dimension) . '_' . $odi++);
}
foreach ($orig_index->indexWhere AS $orig_where) {
$indexWhere = $index->addChild('indexWhere', (string)$orig_where);
$orig_where_attributes = $orig_where->attributes();
// Make sure sqlFormat and other indexWhere attributes get added
foreach ($orig_where_attributes AS $orig_where_key => $orig_where_value) {
$indexWhere->addAttribute($orig_where_key, $orig_where_value);
}
}
}
// Copy unique constraints
// Other types of constraints are inherited from the parent table,
Expand Down
12 changes: 12 additions & 0 deletions tests/data/pgsql8/partition/partition_001_in.xml
@@ -0,0 +1,12 @@
<dbsteward>
<schema name="test_schema" owner="ROLE_OWNER">
<table name="test_table" primaryKey="primary_id" owner="ROLE_OWNER">
<tablePartition type="MODULO">
<tablePartitionOption name="number" value="8" />
<tablePartitionOption name="column" value="partition_id" />
</tablePartition>
<column name="primary_id" type="serial" />
<column name="partition_id" type="bigint" null="false" />
</table>
</schema>
</dbsteward>
35 changes: 35 additions & 0 deletions tests/data/pgsql8/partition/partition_001_out.xml
@@ -0,0 +1,35 @@
<?xml version="1.0"?>
<dbsteward>
<schema name="test_schema" owner="ROLE_OWNER">
<table name="test_table" primaryKey="primary_id" owner="ROLE_OWNER">
<tablePartition type="MODULO">
<tablePartitionOption name="number" value="8"/>
<tablePartitionOption name="column" value="partition_id"/>
</tablePartition>
<column name="primary_id" type="serial"/>
<column name="partition_id" type="bigint" null="false"/>
</table>
<trigger name="test_table_part_trg" sqlFormat="pgsql8" event="INSERT" when="BEFORE" table="test_table" forEach="ROW" function="_p_test_schema_test_table.insert_trigger()"/></schema>
<schema name="_p_test_schema_test_table"><table name="partition_0" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_0_chk" definition="((&quot;partition_id&quot; % 8) = 0)"/></table><table name="partition_1" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_1_chk" definition="((&quot;partition_id&quot; % 8) = 1)"/></table><table name="partition_2" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_2_chk" definition="((&quot;partition_id&quot; % 8) = 2)"/></table><table name="partition_3" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_3_chk" definition="((&quot;partition_id&quot; % 8) = 3)"/></table><table name="partition_4" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_4_chk" definition="((&quot;partition_id&quot; % 8) = 4)"/></table><table name="partition_5" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_5_chk" definition="((&quot;partition_id&quot; % 8) = 5)"/></table><table name="partition_6" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_6_chk" definition="((&quot;partition_id&quot; % 8) = 6)"/></table><table name="partition_7" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_7_chk" definition="((&quot;partition_id&quot; % 8) = 7)"/></table><function name="insert_trigger" returns="TRIGGER" owner="ROLE_OWNER" description="DBSteward auto-generated for table partition"><functionDefinition language="PLPGSQL" sqlFormat="pgsql8">DECLARE
mod_result INT;
BEGIN
mod_result := NEW."partition_id" % 8;
IF (mod_result = 0) THEN
INSERT INTO "_p_test_schema_test_table"."partition_0" VALUES (NEW.*);
ELSEIF (mod_result = 1) THEN
INSERT INTO "_p_test_schema_test_table"."partition_1" VALUES (NEW.*);
ELSEIF (mod_result = 2) THEN
INSERT INTO "_p_test_schema_test_table"."partition_2" VALUES (NEW.*);
ELSEIF (mod_result = 3) THEN
INSERT INTO "_p_test_schema_test_table"."partition_3" VALUES (NEW.*);
ELSEIF (mod_result = 4) THEN
INSERT INTO "_p_test_schema_test_table"."partition_4" VALUES (NEW.*);
ELSEIF (mod_result = 5) THEN
INSERT INTO "_p_test_schema_test_table"."partition_5" VALUES (NEW.*);
ELSEIF (mod_result = 6) THEN
INSERT INTO "_p_test_schema_test_table"."partition_6" VALUES (NEW.*);
ELSEIF (mod_result = 7) THEN
INSERT INTO "_p_test_schema_test_table"."partition_7" VALUES (NEW.*);
END IF;
RETURN NULL;
END;</functionDefinition><grant operation="EXECUTE" role="ROLE_APPLICATION"/></function></schema></dbsteward>
15 changes: 15 additions & 0 deletions tests/data/pgsql8/partition/partition_002_in.xml
@@ -0,0 +1,15 @@
<dbsteward>
<schema name="test_schema" owner="ROLE_OWNER">
<table name="test_table" primaryKey="primary_id" owner="ROLE_OWNER">
<tablePartition type="MODULO">
<tablePartitionOption name="number" value="8" />
<tablePartitionOption name="column" value="partition_id" />
</tablePartition>
<column name="primary_id" type="serial" />
<column name="partition_id" type="bigint" null="false" />
<index name="primary_id_idx" using="btree">
<indexDimension name="primary_id">primary_id</indexDimension>
</index>
</table>
</schema>
</dbsteward>
36 changes: 36 additions & 0 deletions tests/data/pgsql8/partition/partition_002_out.xml
@@ -0,0 +1,36 @@
<?xml version="1.0"?>
<dbsteward>
<schema name="test_schema" owner="ROLE_OWNER">
<table name="test_table" primaryKey="primary_id" owner="ROLE_OWNER">
<tablePartition type="MODULO">
<tablePartitionOption name="number" value="8"/>
<tablePartitionOption name="column" value="partition_id"/>
</tablePartition>
<column name="primary_id" type="serial"/>
<column name="partition_id" type="bigint" null="false"/>

</table>
<trigger name="test_table_part_trg" sqlFormat="pgsql8" event="INSERT" when="BEFORE" table="test_table" forEach="ROW" function="_p_test_schema_test_table.insert_trigger()"/></schema>
<schema name="_p_test_schema_test_table"><table name="partition_0" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_0_chk" definition="((&quot;partition_id&quot; % 8) = 0)"/><index name="primary_id_idx_p0" using="btree"><indexDimension name="primary_id_1">primary_id</indexDimension></index></table><table name="partition_1" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_1_chk" definition="((&quot;partition_id&quot; % 8) = 1)"/><index name="primary_id_idx_p1" using="btree"><indexDimension name="primary_id_1">primary_id</indexDimension></index></table><table name="partition_2" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_2_chk" definition="((&quot;partition_id&quot; % 8) = 2)"/><index name="primary_id_idx_p2" using="btree"><indexDimension name="primary_id_1">primary_id</indexDimension></index></table><table name="partition_3" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_3_chk" definition="((&quot;partition_id&quot; % 8) = 3)"/><index name="primary_id_idx_p3" using="btree"><indexDimension name="primary_id_1">primary_id</indexDimension></index></table><table name="partition_4" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_4_chk" definition="((&quot;partition_id&quot; % 8) = 4)"/><index name="primary_id_idx_p4" using="btree"><indexDimension name="primary_id_1">primary_id</indexDimension></index></table><table name="partition_5" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_5_chk" definition="((&quot;partition_id&quot; % 8) = 5)"/><index name="primary_id_idx_p5" using="btree"><indexDimension name="primary_id_1">primary_id</indexDimension></index></table><table name="partition_6" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_6_chk" definition="((&quot;partition_id&quot; % 8) = 6)"/><index name="primary_id_idx_p6" using="btree"><indexDimension name="primary_id_1">primary_id</indexDimension></index></table><table name="partition_7" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_7_chk" definition="((&quot;partition_id&quot; % 8) = 7)"/><index name="primary_id_idx_p7" using="btree"><indexDimension name="primary_id_1">primary_id</indexDimension></index></table><function name="insert_trigger" returns="TRIGGER" owner="ROLE_OWNER" description="DBSteward auto-generated for table partition"><functionDefinition language="PLPGSQL" sqlFormat="pgsql8">DECLARE
mod_result INT;
BEGIN
mod_result := NEW."partition_id" % 8;
IF (mod_result = 0) THEN
INSERT INTO "_p_test_schema_test_table"."partition_0" VALUES (NEW.*);
ELSEIF (mod_result = 1) THEN
INSERT INTO "_p_test_schema_test_table"."partition_1" VALUES (NEW.*);
ELSEIF (mod_result = 2) THEN
INSERT INTO "_p_test_schema_test_table"."partition_2" VALUES (NEW.*);
ELSEIF (mod_result = 3) THEN
INSERT INTO "_p_test_schema_test_table"."partition_3" VALUES (NEW.*);
ELSEIF (mod_result = 4) THEN
INSERT INTO "_p_test_schema_test_table"."partition_4" VALUES (NEW.*);
ELSEIF (mod_result = 5) THEN
INSERT INTO "_p_test_schema_test_table"."partition_5" VALUES (NEW.*);
ELSEIF (mod_result = 6) THEN
INSERT INTO "_p_test_schema_test_table"."partition_6" VALUES (NEW.*);
ELSEIF (mod_result = 7) THEN
INSERT INTO "_p_test_schema_test_table"."partition_7" VALUES (NEW.*);
END IF;
RETURN NULL;
END;</functionDefinition><grant operation="EXECUTE" role="ROLE_APPLICATION"/></function></schema></dbsteward>
16 changes: 16 additions & 0 deletions tests/data/pgsql8/partition/partition_003_in.xml
@@ -0,0 +1,16 @@
<dbsteward>
<schema name="test_schema" owner="ROLE_OWNER">
<table name="test_table" primaryKey="primary_id" owner="ROLE_OWNER">
<tablePartition type="MODULO">
<tablePartitionOption name="number" value="8" />
<tablePartitionOption name="column" value="partition_id" />
</tablePartition>
<column name="primary_id" type="serial" />
<column name="partition_id" type="bigint" null="false" />
<index name="primary_id_idx" using="btree">
<indexDimension name="primary_id">primary_id</indexDimension>
<indexWhere sqlFormat="pgsql8">primary_id IS NOT NULL</indexWhere>
</index>
</table>
</schema>
</dbsteward>
36 changes: 36 additions & 0 deletions tests/data/pgsql8/partition/partition_003_out.xml
@@ -0,0 +1,36 @@
<?xml version="1.0"?>
<dbsteward>
<schema name="test_schema" owner="ROLE_OWNER">
<table name="test_table" primaryKey="primary_id" owner="ROLE_OWNER">
<tablePartition type="MODULO">
<tablePartitionOption name="number" value="8"/>
<tablePartitionOption name="column" value="partition_id"/>
</tablePartition>
<column name="primary_id" type="serial"/>
<column name="partition_id" type="bigint" null="false"/>

</table>
<trigger name="test_table_part_trg" sqlFormat="pgsql8" event="INSERT" when="BEFORE" table="test_table" forEach="ROW" function="_p_test_schema_test_table.insert_trigger()"/></schema>
<schema name="_p_test_schema_test_table"><table name="partition_0" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_0_chk" definition="((&quot;partition_id&quot; % 8) = 0)"/><index name="primary_id_idx_p0" using="btree"><indexDimension name="primary_id_1">primary_id</indexDimension><indexWhere sqlFormat="pgsql8">primary_id IS NOT NULL</indexWhere></index></table><table name="partition_1" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_1_chk" definition="((&quot;partition_id&quot; % 8) = 1)"/><index name="primary_id_idx_p1" using="btree"><indexDimension name="primary_id_1">primary_id</indexDimension><indexWhere sqlFormat="pgsql8">primary_id IS NOT NULL</indexWhere></index></table><table name="partition_2" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_2_chk" definition="((&quot;partition_id&quot; % 8) = 2)"/><index name="primary_id_idx_p2" using="btree"><indexDimension name="primary_id_1">primary_id</indexDimension><indexWhere sqlFormat="pgsql8">primary_id IS NOT NULL</indexWhere></index></table><table name="partition_3" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_3_chk" definition="((&quot;partition_id&quot; % 8) = 3)"/><index name="primary_id_idx_p3" using="btree"><indexDimension name="primary_id_1">primary_id</indexDimension><indexWhere sqlFormat="pgsql8">primary_id IS NOT NULL</indexWhere></index></table><table name="partition_4" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_4_chk" definition="((&quot;partition_id&quot; % 8) = 4)"/><index name="primary_id_idx_p4" using="btree"><indexDimension name="primary_id_1">primary_id</indexDimension><indexWhere sqlFormat="pgsql8">primary_id IS NOT NULL</indexWhere></index></table><table name="partition_5" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_5_chk" definition="((&quot;partition_id&quot; % 8) = 5)"/><index name="primary_id_idx_p5" using="btree"><indexDimension name="primary_id_1">primary_id</indexDimension><indexWhere sqlFormat="pgsql8">primary_id IS NOT NULL</indexWhere></index></table><table name="partition_6" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_6_chk" definition="((&quot;partition_id&quot; % 8) = 6)"/><index name="primary_id_idx_p6" using="btree"><indexDimension name="primary_id_1">primary_id</indexDimension><indexWhere sqlFormat="pgsql8">primary_id IS NOT NULL</indexWhere></index></table><table name="partition_7" owner="ROLE_OWNER" primaryKey="primary_id" inheritsTable="test_table" inheritsSchema="test_schema"><constraint type="CHECK" name="test_table_p_7_chk" definition="((&quot;partition_id&quot; % 8) = 7)"/><index name="primary_id_idx_p7" using="btree"><indexDimension name="primary_id_1">primary_id</indexDimension><indexWhere sqlFormat="pgsql8">primary_id IS NOT NULL</indexWhere></index></table><function name="insert_trigger" returns="TRIGGER" owner="ROLE_OWNER" description="DBSteward auto-generated for table partition"><functionDefinition language="PLPGSQL" sqlFormat="pgsql8">DECLARE
mod_result INT;
BEGIN
mod_result := NEW."partition_id" % 8;
IF (mod_result = 0) THEN
INSERT INTO "_p_test_schema_test_table"."partition_0" VALUES (NEW.*);
ELSEIF (mod_result = 1) THEN
INSERT INTO "_p_test_schema_test_table"."partition_1" VALUES (NEW.*);
ELSEIF (mod_result = 2) THEN
INSERT INTO "_p_test_schema_test_table"."partition_2" VALUES (NEW.*);
ELSEIF (mod_result = 3) THEN
INSERT INTO "_p_test_schema_test_table"."partition_3" VALUES (NEW.*);
ELSEIF (mod_result = 4) THEN
INSERT INTO "_p_test_schema_test_table"."partition_4" VALUES (NEW.*);
ELSEIF (mod_result = 5) THEN
INSERT INTO "_p_test_schema_test_table"."partition_5" VALUES (NEW.*);
ELSEIF (mod_result = 6) THEN
INSERT INTO "_p_test_schema_test_table"."partition_6" VALUES (NEW.*);
ELSEIF (mod_result = 7) THEN
INSERT INTO "_p_test_schema_test_table"."partition_7" VALUES (NEW.*);
END IF;
RETURN NULL;
END;</functionDefinition><grant operation="EXECUTE" role="ROLE_APPLICATION"/></function></schema></dbsteward>
18 changes: 18 additions & 0 deletions tests/dbstewardUnitTestBase.php
Expand Up @@ -23,6 +23,11 @@

class dbstewardUnitTestBase extends PHPUnit_Framework_TestCase {

// Directory containing data files used for testing
protected static $test_data_dir = NULL;
// LOL Right?
protected static $last_quote_all_names;

// test cases need to define $this->xml_content_a and $this->xml_content_b for their scenarios
protected $xml_content_a = "XML_CONTENT_A_UNDEFINED";
protected $xml_content_b = "XML_CONTENT_B_UNDEFINED";
Expand Down Expand Up @@ -244,6 +249,19 @@ protected function upgrade_db_mysql5() {
//@TODO: confirm tables defined in B are present
}

/**
* Get / cache the location of the testing data directory
*
* This is a good place to put XML files testing instead of loading a ton of
* XML data via PHPUnit data providers
*/
protected static function getTestDataDir() {
if (is_null(static::$test_data_dir)) {
static::$test_data_dir = __DIR__ . '/data';
}
return static::$test_data_dir;
}

}

?>
58 changes: 58 additions & 0 deletions tests/pgsql8/Pgsql8XmlParserTest.php
@@ -0,0 +1,58 @@
<?php
/**
* DBSteward unit tests for pgsql8_xml_parser
*
* @package DBSteward
* @license http://www.opensource.org/licenses/bsd-license.php Simplified BSD License
* @author Brian Stoots <bstoots@gmail.com>
*/

require_once __DIR__ . '/../dbstewardUnitTestBase.php';

/**
* @group pgsql8
*/
class Pgsql8XmlParserTest extends dbstewardUnitTestBase {

/**
*
*/
public function setUp() {
parent::setUp();
static::$last_quote_all_names = dbsteward::$quote_all_names;
dbsteward::$quote_all_names = TRUE;
}

/**
*
*/
public function tearDown() {
dbsteward::$quote_all_names = static::$last_quote_all_names;
parent::tearDown();
}

/**
* @dataProvider processProvider()
*/
public function testProcess($in_xml, $out_xml) {
$doc = new SimpleXMLElement(file_get_contents($in_xml));
pgsql8_xml_parser::process($doc);
// Uncomment this line to create the _out file that will be asserted against.
// Make sure to double check the contents and then commit to version control
//file_put_contents($out_xml, $doc->saveXML());
$this->assertEquals(new SimpleXMLElement(file_get_contents($out_xml)), $doc);
}

/**
*
*/
public static function processProvider() {
$dir = static::getTestDataDir();
return array(
array("{$dir}/pgsql8/partition/partition_001_in.xml", "{$dir}/pgsql8/partition/partition_001_out.xml"),
array("{$dir}/pgsql8/partition/partition_002_in.xml", "{$dir}/pgsql8/partition/partition_002_out.xml"),
array("{$dir}/pgsql8/partition/partition_003_in.xml", "{$dir}/pgsql8/partition/partition_003_out.xml"),
);
}

}

0 comments on commit 058559e

Please sign in to comment.