dbschema.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 UnitTestCase {
  9. protected static $schema_file = 'static://test_db_scheme';
  10. protected static $schema_file2 = 'static://test_db_scheme2';
  11. protected $test_prefix;
  12. protected $table1;
  13. protected $table2;
  14. public function setUp() {
  15. $dbfile = OC::$SERVERROOT.'/tests/data/db_structure.xml';
  16. $dbfile2 = OC::$SERVERROOT.'/tests/data/db_structure2.xml';
  17. $r = '_'.OC_Util::generate_random_bytes('4').'_';
  18. $content = file_get_contents( $dbfile );
  19. $content = str_replace( '*dbprefix*', '*dbprefix*'.$r, $content );
  20. file_put_contents( self::$schema_file, $content );
  21. $content = file_get_contents( $dbfile2 );
  22. $content = str_replace( '*dbprefix*', '*dbprefix*'.$r, $content );
  23. file_put_contents( self::$schema_file2, $content );
  24. $this->test_prefix = $r;
  25. $this->table1 = $this->test_prefix.'contacts_addressbooks';
  26. $this->table2 = $this->test_prefix.'contacts_cards';
  27. }
  28. public function tearDown() {
  29. unlink(self::$schema_file);
  30. unlink(self::$schema_file2);
  31. }
  32. // everything in one test, they depend on each other
  33. public function testSchema() {
  34. $this->doTestSchemaCreating();
  35. $this->doTestSchemaChanging();
  36. $this->doTestSchemaDumping();
  37. $this->doTestSchemaRemoving();
  38. }
  39. public function doTestSchemaCreating() {
  40. OC_DB::createDbFromStructure(self::$schema_file);
  41. $this->assertTableExist($this->table1);
  42. $this->assertTableExist($this->table2);
  43. }
  44. public function doTestSchemaChanging() {
  45. OC_DB::updateDbFromStructure(self::$schema_file2);
  46. $this->assertTableExist($this->table2);
  47. }
  48. public function doTestSchemaDumping() {
  49. $outfile = 'static://db_out.xml';
  50. OC_DB::getDbStructure($outfile);
  51. $content = file_get_contents($outfile);
  52. $this->assertContains($this->table1, $content);
  53. $this->assertContains($this->table2, $content);
  54. }
  55. public function doTestSchemaRemoving() {
  56. OC_DB::removeDBStructure(self::$schema_file);
  57. $this->assertTableNotExist($this->table1);
  58. $this->assertTableNotExist($this->table2);
  59. }
  60. public function tableExist($table) {
  61. $table = '*PREFIX*' . $table;
  62. switch (OC_Config::getValue( 'dbtype', 'sqlite' )) {
  63. case 'sqlite':
  64. case 'sqlite3':
  65. $sql = "SELECT name FROM sqlite_master "
  66. . "WHERE type = 'table' AND name != 'sqlite_sequence' "
  67. . "AND name != 'geometry_columns' AND name != 'spatial_ref_sys' "
  68. . "UNION ALL SELECT name FROM sqlite_temp_master "
  69. . "WHERE type = 'table' AND name = '".$table."'";
  70. $query = OC_DB::prepare($sql);
  71. $result = $query->execute(array());
  72. $exists = $result && $result->fetchOne();
  73. break;
  74. case 'mysql':
  75. $sql = 'SHOW TABLES LIKE "'.$table.'"';
  76. $query = OC_DB::prepare($sql);
  77. $result = $query->execute(array());
  78. $exists = $result && $result->fetchOne();
  79. break;
  80. case 'pgsql':
  81. $sql = "SELECT tablename AS table_name, schemaname AS schema_name "
  82. . "FROM pg_tables WHERE schemaname NOT LIKE 'pg_%' "
  83. . "AND schemaname != 'information_schema' "
  84. . "AND tablename = '".$table."'";
  85. $query = OC_DB::prepare($sql);
  86. $result = $query->execute(array());
  87. $exists = $result && $result->fetchOne();
  88. break;
  89. }
  90. return $exists;
  91. }
  92. public function assertTableExist($table) {
  93. $this->assertTrue($this->tableExist($table));
  94. }
  95. public function assertTableNotExist($table) {
  96. $type=OC_Config::getValue( "dbtype", "sqlite" );
  97. if( $type == 'sqlite' || $type == 'sqlite3' ) {
  98. // sqlite removes the tables after closing the DB
  99. }
  100. else {
  101. $this->assertFalse($this->tableExist($table));
  102. }
  103. }
  104. }