dbschema.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class Test_DBSchema extends PHPUnit_Framework_TestCase {
  9. protected $schema_file = 'static://test_db_scheme';
  10. protected $schema_file2 = 'static://test_db_scheme2';
  11. protected $table1;
  12. protected $table2;
  13. public function setUp() {
  14. $dbfile = OC::$SERVERROOT.'/tests/data/db_structure.xml';
  15. $dbfile2 = OC::$SERVERROOT.'/tests/data/db_structure2.xml';
  16. $r = '_'.OC_Util::generateRandomBytes('4').'_';
  17. $content = file_get_contents( $dbfile );
  18. $content = str_replace( '*dbprefix*', '*dbprefix*'.$r, $content );
  19. file_put_contents( $this->schema_file, $content );
  20. $content = file_get_contents( $dbfile2 );
  21. $content = str_replace( '*dbprefix*', '*dbprefix*'.$r, $content );
  22. file_put_contents( $this->schema_file2, $content );
  23. $prefix = OC_Config::getValue( "dbtableprefix", "oc_" );
  24. $this->table1 = $prefix.$r.'cntcts_addrsbks';
  25. $this->table2 = $prefix.$r.'cntcts_cards';
  26. }
  27. public function tearDown() {
  28. unlink($this->schema_file);
  29. unlink($this->schema_file2);
  30. }
  31. // everything in one test, they depend on each other
  32. /**
  33. * @medium
  34. */
  35. public function testSchema() {
  36. $this->doTestSchemaCreating();
  37. $this->doTestSchemaChanging();
  38. $this->doTestSchemaDumping();
  39. $this->doTestSchemaRemoving();
  40. }
  41. public function doTestSchemaCreating() {
  42. OC_DB::createDbFromStructure($this->schema_file);
  43. $this->assertTableExist($this->table1);
  44. $this->assertTableExist($this->table2);
  45. }
  46. public function doTestSchemaChanging() {
  47. OC_DB::updateDbFromStructure($this->schema_file2);
  48. $this->assertTableExist($this->table2);
  49. }
  50. public function doTestSchemaDumping() {
  51. $outfile = 'static://db_out.xml';
  52. OC_DB::getDbStructure($outfile);
  53. $content = file_get_contents($outfile);
  54. $this->assertContains($this->table1, $content);
  55. $this->assertContains($this->table2, $content);
  56. }
  57. public function doTestSchemaRemoving() {
  58. OC_DB::removeDBStructure($this->schema_file);
  59. $this->assertTableNotExist($this->table1);
  60. $this->assertTableNotExist($this->table2);
  61. }
  62. public function tableExist($table) {
  63. switch (OC_Config::getValue( 'dbtype', 'sqlite' )) {
  64. case 'sqlite':
  65. case 'sqlite3':
  66. $sql = "SELECT name FROM sqlite_master "
  67. . "WHERE type = 'table' AND name = ? "
  68. . "UNION ALL SELECT name FROM sqlite_temp_master "
  69. . "WHERE type = 'table' AND name = ?";
  70. $result = \OC_DB::executeAudited($sql, array($table, $table));
  71. break;
  72. case 'mysql':
  73. $sql = 'SHOW TABLES LIKE ?';
  74. $result = \OC_DB::executeAudited($sql, array($table));
  75. break;
  76. case 'pgsql':
  77. $sql = 'SELECT tablename AS table_name, schemaname AS schema_name '
  78. . 'FROM pg_tables WHERE schemaname NOT LIKE \'pg_%\' '
  79. . 'AND schemaname != \'information_schema\' '
  80. . 'AND tablename = ?';
  81. $result = \OC_DB::executeAudited($sql, array($table));
  82. break;
  83. case 'oci':
  84. $sql = 'SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME = ?';
  85. $result = \OC_DB::executeAudited($sql, array($table));
  86. break;
  87. case 'mssql':
  88. $sql = 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ?';
  89. $result = \OC_DB::executeAudited($sql, array($table));
  90. break;
  91. }
  92. $name = $result->fetchOne(); //FIXME checking with '$result->numRows() === 1' does not seem to work?
  93. if ($name === $table) {
  94. return true;
  95. } else {
  96. return false;
  97. }
  98. }
  99. public function assertTableExist($table) {
  100. $this->assertTrue($this->tableExist($table), 'Table ' . $table . ' does not exist');
  101. }
  102. public function assertTableNotExist($table) {
  103. $type=OC_Config::getValue( "dbtype", "sqlite" );
  104. if( $type == 'sqlite' || $type == 'sqlite3' ) {
  105. // sqlite removes the tables after closing the DB
  106. } else {
  107. $this->assertFalse($this->tableExist($table), 'Table ' . $table . ' exists.');
  108. }
  109. }
  110. }