sqlsrv.schema.test 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. /**
  3. * @file
  4. * Support tests for SQL Server.
  5. */
  6. class SqlServerSchemaTest extends DrupalWebTestCase {
  7. public static function getInfo() {
  8. return array(
  9. 'name' => 'Schema tests',
  10. 'description' => 'Generic tests for SQL Server Schema.',
  11. 'group' => 'Database (SQL Server)'
  12. );
  13. }
  14. /**
  15. * Test adding / removing / readding a primary key to a table.
  16. */
  17. public function testPrimaryKeyHandling() {
  18. $table_spec = array(
  19. 'fields' => array(
  20. 'id' => array(
  21. 'type' => 'int',
  22. 'not null' => TRUE,
  23. ),
  24. ),
  25. );
  26. db_create_table('test_table', $table_spec);
  27. $this->assertTrue(db_table_exists('test_table'), t('Creating a table without a primary key works.'));
  28. db_add_primary_key('test_table', array('id'));
  29. $this->pass(t('Adding a primary key should work when the table has no data.'));
  30. // Try adding a row.
  31. db_insert('test_table')->fields(array('id' => 1))->execute();
  32. // The second row with the same value should conflict.
  33. try {
  34. db_insert('test_table')->fields(array('id' => 1))->execute();
  35. $this->fail(t('Duplicate values in the table should not be allowed when the primary key is there.'));
  36. }
  37. catch (Exception $e) {
  38. }
  39. // Drop the primary key and retry.
  40. db_drop_primary_key('test_table');
  41. $this->pass(t('Removing a primary key should work.'));
  42. db_insert('test_table')->fields(array('id' => 1))->execute();
  43. $this->pass(t('Adding a duplicate row should work without the primary key.'));
  44. try {
  45. db_add_primary_key('test_table', array('id'));
  46. $this->fail(t('Trying to add a primary key should fail with duplicate rows in the table.'));
  47. }
  48. catch (Exception $e) {
  49. }
  50. }
  51. /**
  52. * Test altering a primary key.
  53. */
  54. public function testPrimaryKeyAlter() {
  55. $table_spec = array(
  56. 'fields' => array(
  57. 'id' => array(
  58. 'type' => 'int',
  59. 'not null' => TRUE,
  60. ),
  61. ),
  62. 'primary key' => array('id'),
  63. );
  64. db_create_table('test_table', $table_spec);
  65. // Add a default value.
  66. db_change_field('test_table', 'id', 'id', array(
  67. 'type' => 'int',
  68. 'not null' => TRUE,
  69. 'default' => 1,
  70. ));
  71. }
  72. /**
  73. * Test adding / modifying an unsigned column.
  74. */
  75. public function testUnsignedField() {
  76. $table_spec = array(
  77. 'fields' => array(
  78. 'id' => array(
  79. 'type' => 'int',
  80. 'not null' => TRUE,
  81. 'unsigned' => TRUE,
  82. ),
  83. ),
  84. );
  85. db_create_table('test_table', $table_spec);
  86. try {
  87. db_insert('test_table')->fields(array('id' => -1))->execute();
  88. $failed = FALSE;
  89. }
  90. catch (Exception $e) {
  91. $failed = TRUE;
  92. }
  93. $this->assertTrue($failed, t('Inserting a negative value in an unsigned field failed.'));
  94. $this->assertUnsignedField('test_table', 'id');
  95. try {
  96. db_insert('test_table')->fields(array('id' => 1))->execute();
  97. $failed = FALSE;
  98. }
  99. catch (Exception $e) {
  100. $failed = TRUE;
  101. }
  102. $this->assertFalse($failed, t('Inserting a positive value in an unsigned field succeeded.'));
  103. // Change the field to signed.
  104. db_change_field('test_table', 'id', 'id', array(
  105. 'type' => 'int',
  106. 'not null' => TRUE,
  107. ));
  108. $this->assertSignedField('test_table', 'id');
  109. // Change the field back to unsigned.
  110. db_change_field('test_table', 'id', 'id', array(
  111. 'type' => 'int',
  112. 'not null' => TRUE,
  113. 'unsigned' => TRUE,
  114. ));
  115. $this->assertUnsignedField('test_table', 'id');
  116. }
  117. /**
  118. * Test insert data in unsigned field.
  119. */
  120. protected function assertUnsignedField($table, $field_name) {
  121. try {
  122. db_insert('test_table')->fields(array('id' => -1))->execute();
  123. $success = TRUE;
  124. }
  125. catch (Exception $e) {
  126. $success = FALSE;
  127. }
  128. $this->assertFalse($success, t('Inserting a negative value in an unsigned field failed.'));
  129. try {
  130. db_insert('test_table')->fields(array('id' => 1))->execute();
  131. $success = TRUE;
  132. }
  133. catch (Exception $e) {
  134. $success = FALSE;
  135. }
  136. $this->assertTrue($success, t('Inserting a positive value in an unsigned field succeeded.'));
  137. db_delete('test_table')->execute();
  138. }
  139. /**
  140. * Test inserting data in signed field.
  141. */
  142. protected function assertSignedField($table, $field_name) {
  143. try {
  144. db_insert('test_table')->fields(array('id' => -1))->execute();
  145. $success = TRUE;
  146. }
  147. catch (Exception $e) {
  148. $success = FALSE;
  149. }
  150. $this->assertTrue($success, t('Inserting a negative value in a signed field succeeded.'));
  151. try {
  152. db_insert('test_table')->fields(array('id' => 1))->execute();
  153. $success = TRUE;
  154. }
  155. catch (Exception $e) {
  156. $success = FALSE;
  157. }
  158. $this->assertTrue($success, t('Inserting a positive value in a signed field succeeded.'));
  159. db_delete('test_table')->execute();
  160. }
  161. /**
  162. * Test db_add_field() and db_change_field() with indexes.
  163. */
  164. public function testAddChangeWithIndex() {
  165. $table_spec = array(
  166. 'fields' => array(
  167. 'id' => array(
  168. 'type' => 'int',
  169. 'not null' => TRUE,
  170. ),
  171. ),
  172. 'primary key' => array('id'),
  173. );
  174. db_create_table('test_table', $table_spec);
  175. // Add a default value.
  176. db_add_field('test_table', 'test', array(
  177. 'type' => 'int',
  178. 'not null' => TRUE,
  179. 'default' => 1,
  180. ), array(
  181. 'indexes' => array(
  182. 'id_test' => array('id, test'),
  183. ),
  184. ));
  185. $this->assertTrue(db_index_exists('test_table', 'id_test'), t('The index has been created by db_add_field().'));
  186. // Change the definition, we have by contract to remove the indexes before.
  187. db_drop_index('test_table', 'id_test');
  188. $this->assertFalse(db_index_exists('test_table', 'id_test'), t('The index has been dropped.'));
  189. db_change_field('test_table', 'test', 'test', array(
  190. 'type' => 'int',
  191. 'not null' => TRUE,
  192. 'default' => 1,
  193. ), array(
  194. 'indexes' => array(
  195. 'id_test' => array('id, test'),
  196. ),
  197. ));
  198. $this->assertTrue(db_index_exists('test_table', 'id_test'), t('The index has been recreated by db_change_field().'));
  199. }
  200. /**
  201. * Test db_add_field() and db_change_field() with binary spec.
  202. */
  203. public function testAddChangeWithBinary() {
  204. $table_spec = array(
  205. 'fields' => array(
  206. 'id' => array(
  207. 'type' => 'serial',
  208. 'not null' => TRUE,
  209. ),
  210. 'name' => array(
  211. 'type' => 'varchar',
  212. 'length' => 255,
  213. 'binary' => false
  214. ),
  215. ),
  216. 'primary key' => array('id'),
  217. );
  218. db_create_table('test_table_binary', $table_spec);
  219. // Insert a value in name
  220. db_insert('test_table_binary')
  221. ->fields(array(
  222. 'name' => 'Sandra',
  223. ))->execute();
  224. // Insert a value in name
  225. db_insert('test_table_binary')
  226. ->fields(array(
  227. 'name' => 'sandra',
  228. ))->execute();
  229. // By default, datase collation
  230. // should be case insensitive, returning both rows.
  231. $result = db_query('SELECT COUNT(*) FROM {test_table_binary} WHERE name = :name', array(':name' => 'SANDRA'))->fetchField();
  232. $this->assertEqual($result, 2, 'Returned the correct number of total rows.');
  233. // Now let's change the field
  234. // to case sensistive
  235. db_change_field('test_table_binary', 'name', 'name', array(
  236. 'type' => 'varchar',
  237. 'length' => 255,
  238. 'binary' => true
  239. ));
  240. // With case sensitivity, no results.
  241. $result = db_query('SELECT COUNT(*) FROM {test_table_binary} WHERE name = :name', array(':name' => 'SANDRA'))->fetchField();
  242. $this->assertEqual($result, 0, 'Returned the correct number of total rows.');
  243. // Now one result.
  244. $result = db_query('SELECT COUNT(*) FROM {test_table_binary} WHERE name = :name', array(':name' => 'sandra'))->fetchField();
  245. $this->assertEqual($result, 1, 'Returned the correct number of total rows.');
  246. }
  247. /**
  248. * Test numeric field precision.
  249. */
  250. public function testNumericFieldPrecision() {
  251. $table_spec = array(
  252. 'fields' => array(
  253. 'id' => array(
  254. 'type' => 'serial',
  255. 'not null' => TRUE,
  256. ),
  257. 'name' => array(
  258. 'type' => 'numeric',
  259. 'precision' => 400,
  260. 'scale' => 2
  261. ),
  262. ),
  263. 'primary key' => array('id'),
  264. );
  265. $success = FALSE;
  266. try {
  267. db_create_table('test_table_binary', $table_spec);
  268. $success = TRUE;
  269. }
  270. catch (Exception $error) {
  271. $success = FALSE;
  272. }
  273. $this->assertTrue($success, t('Able to create a numeric field with an out of bounds precision.'));
  274. }
  275. /**
  276. * Test native XML storage.
  277. */
  278. public function textXMLStorage() {
  279. $table_spec = array(
  280. 'fields' => array(
  281. 'id' => array(
  282. 'type' => 'serial',
  283. 'not null' => TRUE,
  284. ),
  285. 'extend' => array(
  286. 'type' => 'text',
  287. 'sqlsrv_type' => 'xml',
  288. ),
  289. ),
  290. 'primary key' => array('id'),
  291. );
  292. $success = FALSE;
  293. try {
  294. db_create_table('test_table_xml', $table_spec);
  295. $success = TRUE;
  296. }
  297. catch (Exception $error) {
  298. $success = FALSE;
  299. }
  300. $this->assertTrue($success, t('Able to create a table with an XML field.'));
  301. // Insert something and retrieve.
  302. $data = '<xml><a>data</a></xml>';
  303. try {
  304. db_insert('test_table_xml')->fields(array('extend' => $data))->execute();
  305. $success = TRUE;
  306. }
  307. catch (\Exception $e) {
  308. $success = FALSE;
  309. }
  310. $this->assertTrue($success, t('Able to insert XML data in an XML field.'));
  311. $query = db_select('test_table_xml', 't');
  312. $query->addField('t', 'extend');
  313. $retrieved = $query->execute()->fetchAssoc();
  314. $this->assertEqual($data, $retrieved['extend'], t('XML was retrieved as the original string.'));
  315. // From now on there are little to no asserts, at least not having
  316. // Exceptions thrown is a good indication.
  317. // Add new field and convert it into the primary key, but make sure it is bigger than 900 bytes.
  318. db_add_field('test_table_xml', 'newid',
  319. array(
  320. 'type' => 'text',
  321. 'sqlsrv_type' => 'nvarchar(4000)'
  322. ));
  323. db_drop_primary_key('test_table_xml');
  324. // The driver should detect this is > 900 bytes and create
  325. // a computed Primary Key + Indexes to compensate.
  326. // Usually you would not be able to PK on a nullable column
  327. // but because it's hashed this will work.
  328. db_add_primary_key('test_table_xml', array('newid'));
  329. // Now change the primary key column to a size that fits in 900 bytes
  330. // this should re-expand the PK to it's natural version. But drop PK
  331. // because newid has no values.
  332. db_drop_primary_key('test_table_xml');
  333. db_drop_field('test_table_xml', 'newid');
  334. db_add_field('test_table_xml', 'newid',
  335. array(
  336. 'type' => 'text',
  337. 'sqlsrv_type' => 'nvarchar(300)',
  338. 'not null' => TRUE,
  339. 'default' => 'default',
  340. ));
  341. db_add_primary_key('test_table_xml', array('id', 'newid'));
  342. // Now add an XML key, because the current key is > 128 bytes
  343. // the driver should automatically recompress the PK into a computed
  344. // column to make space for this key.
  345. db_add_index('test_table_xml', 'xml_main', array('extend'));
  346. // Make sure the SCHEMA helper function confirms this.
  347. $xml_index_name = Database::getConnection()->schema()->tableHasXmlIndex('test_table_xml');
  348. $this->assertEqual($xml_index_name, 'xml_main_idx', t('XML index creation confirmed.'));
  349. // Now drop the index, the PK should be re-expanded.
  350. db_drop_index('test_table_xml', 'xml_main');
  351. }
  352. }