<commit_before><commit_msg>Enforce that index names be unique<commit_after><?php
/**
 * DBSteward unit test for testing that indexes are caught if they are not unique
 *
 * @package DBSteward
 * @license http://www.opensource.org/licenses/bsd-license.php Simplified BSD License
 * @author <NAME> <<EMAIL>>
 */

require_once 'PHPUnit/Framework/TestCase.php';
require_once __DIR__ . '/../../lib/DBSteward/dbsteward.php';
require_once __DIR__ . '/../mock_output_file_segmenter.php';

class UniqueIndexTest extends PHPUnit_Framework_TestCase {

  private $xml_empty = <<<XML_EMPTY
<dbsteward>
  <database>
    <role>
      <application>app_runtime</application>
      <owner>app_owner</owner>
      <replication/>
      <readonly>app_audit_readonly</readonly>
    </role>
  </database>
  <schema name="schema1" owner="ROLE_OWNER">
  </schema>
</dbsteward>
XML_EMPTY;
  
  private $xml = <<<XML_NEW
<dbsteward>
  <database>
    <role>
      <application>app_runtime</application>
      <owner>app_owner</owner>
      <replication/>
      <readonly>app_audit_readonly</readonly>
    </role>
  </database>
  <schema name="schema1" owner="ROLE_OWNER">
    <table name="table_shable" owner="ROLE_OWNER" primaryKey="table_shable_id">
      <column name="table_shable_id" type="int"/>
      <column name="table_shable_value" type="char(10)"/>
      <index name="table_shable_idx" using="btree">
        <indexDimension name="position_1">table_shable_value</indexDimension>
      </index>
      <index name="table_shable_idx" using="gin">
        <indexDimension name="position_1">table_shable_value</indexDimension>
      </index>
    </table>
  </schema>
</dbsteward>
XML_NEW;

  /**
   * @exceptionException Exception
  */
  public function testExceptionIsThrownWithDupes() {
    dbsteward::set_sql_format('pgsql8');
    dbsteward::$quote_all_names = TRUE;
    dbsteward::$single_stage_upgrade = TRUE;

    $doc_empty = simplexml_load_string($this->xml_empty);
    $doc_empty = xml_parser::composite_doc(FALSE, $doc_empty);
    dbsteward::$old_database = $doc_empty;
    $doc = simplexml_load_string($this->xml);
    $doc = xml_parser::composite_doc(FALSE, $doc);
    dbsteward::$new_database = $doc;
    
    $table_dependency = xml_parser::table_dependency_order($doc);

    $schema = $doc->schema;
    $table = $schema->table;
    
    // make sure the type is named with quoting as part of a definition build
    $mofs = new mock_output_file_segmenter();
    try {
      pgsql8::build_schema($doc, $mofs, $table_dependency);
      $this->fail("build_schema did not detect duplicate index names");
    }
    catch (Exception $e) {
    }
  }
}

{'fraction_non_alphanumeric': 0.13841486537732273, 'fraction_numerical': 0.0030337504740235114, 'mean_word_length': 4.034351145038168, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 9}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7303670', 'n_tokens_mistral': 889, 'n_tokens_neox': 890, 'n_words': 162}