|
| 1 | +<?php |
| 2 | + |
| 3 | +use CzProject\SqlGenerator\Drivers; |
| 4 | +use CzProject\SqlGenerator\SqlDocument; |
| 5 | +use CzProject\SqlGenerator\Statements\IndexDefinition; |
| 6 | +use Tester\Assert; |
| 7 | + |
| 8 | +require __DIR__ . '/../bootstrap.php'; |
| 9 | + |
| 10 | + |
| 11 | +test(function () { |
| 12 | + |
| 13 | + $sql = new SqlDocument; |
| 14 | + $driver = new Drivers\MysqlDriver; |
| 15 | + |
| 16 | + $contactTable = $sql->alterTable('contact'); |
| 17 | + $contactTable->setOption('ENGINE', 'InnoDB'); |
| 18 | + |
| 19 | + // columns |
| 20 | + $contactTable->addColumn('active', 'TINYINT', array(1), array('UNSIGNED' => NULL)) |
| 21 | + ->setDefaultValue(TRUE) |
| 22 | + ->setNullable() |
| 23 | + ->setComment('Contact status') |
| 24 | + ->moveAfterColumn('name'); |
| 25 | + |
| 26 | + $contactTable->addColumn('id', 'INT') |
| 27 | + ->setAutoIncrement() |
| 28 | + ->moveToFirstPosition(); |
| 29 | + |
| 30 | + $contactTable->modifyColumn('name', 'VARCHAR(200)') |
| 31 | + ->setDefaultValue('XYZ') |
| 32 | + ->setComment('Name of contact') |
| 33 | + ->setNullable() |
| 34 | + ->moveAfterColumn('id'); |
| 35 | + |
| 36 | + $contactTable->modifyColumn('id', 'INT') |
| 37 | + ->setAutoIncrement() |
| 38 | + ->moveToFirstPosition(); |
| 39 | + |
| 40 | + $contactTable->dropColumn('removed'); |
| 41 | + |
| 42 | + // indexes |
| 43 | + $contactTable->addIndex(NULL, IndexDefinition::TYPE_PRIMARY) |
| 44 | + ->addColumn('id'); |
| 45 | + |
| 46 | + $contactTable->dropIndex('name'); |
| 47 | + |
| 48 | + // foreign keys |
| 49 | + $contactTable->dropForeignKey('fk_creator'); |
| 50 | + $contactTable->addForeignKey('fk_creator', 'creator_id', 'user', 'id') |
| 51 | + ->setOnUpdateAction('NO ACTION') |
| 52 | + ->setOnDeleteAction('NO ACTION'); |
| 53 | + |
| 54 | + // comment |
| 55 | + $contactTable->setComment('Table of contacts.'); |
| 56 | + |
| 57 | + Assert::same(implode("\n", array( |
| 58 | + '', |
| 59 | + 'ALTER TABLE `contact`', |
| 60 | + "ADD COLUMN `active` TINYINT(1) UNSIGNED NULL DEFAULT 1 COMMENT 'Contact status' AFTER `name`,", |
| 61 | + "ADD COLUMN `id` INT NOT NULL AUTO_INCREMENT FIRST,", |
| 62 | + "MODIFY COLUMN `name` VARCHAR(200) NULL DEFAULT 'XYZ' COMMENT 'Name of contact' AFTER `id`,", |
| 63 | + "MODIFY COLUMN `id` INT NOT NULL AUTO_INCREMENT FIRST,", |
| 64 | + "DROP COLUMN `removed`,", |
| 65 | + "ADD PRIMARY KEY (`id`),", |
| 66 | + "DROP INDEX `name`,", |
| 67 | + "DROP FOREIGN KEY `fk_creator`,", |
| 68 | + "ADD CONSTRAINT `fk_creator` FOREIGN KEY (`creator_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,", |
| 69 | + 'COMMENT \'Table of contacts.\',', |
| 70 | + 'ENGINE=InnoDB;', |
| 71 | + '', |
| 72 | + )), $sql->toSql($driver)); |
| 73 | + |
| 74 | +}); |
| 75 | + |
| 76 | + |
| 77 | +test(function () { |
| 78 | + |
| 79 | + $sql = new SqlDocument; |
| 80 | + $driver = new Drivers\MysqlDriver; |
| 81 | + |
| 82 | + $contactTable = $sql->createTable('contact'); |
| 83 | + $contactTable->addColumn('name', 'VARCHAR(100)'); |
| 84 | + |
| 85 | + Assert::exception(function () use ($contactTable) { |
| 86 | + |
| 87 | + $contactTable->addColumn('name', 'VARCHAR(50)'); |
| 88 | + |
| 89 | + }, 'CzProject\SqlGenerator\DuplicateException', "Column 'name' already exists."); |
| 90 | + |
| 91 | +}); |
| 92 | + |
| 93 | + |
| 94 | +test(function () { |
| 95 | + |
| 96 | + $sql = new SqlDocument; |
| 97 | + |
| 98 | + $contactTable = $sql->createTable('contact'); |
| 99 | + $contactTable->addIndex('name', 'INDEX'); |
| 100 | + |
| 101 | + Assert::exception(function () use ($contactTable) { |
| 102 | + |
| 103 | + $contactTable->addIndex('name', 'INDEX'); |
| 104 | + |
| 105 | + }, 'CzProject\SqlGenerator\DuplicateException', "Index 'name' already exists."); |
| 106 | + |
| 107 | +}); |
| 108 | + |
| 109 | + |
| 110 | +test(function () { |
| 111 | + |
| 112 | + $sql = new SqlDocument; |
| 113 | + |
| 114 | + $contactTable = $sql->createTable('contact'); |
| 115 | + $contactTable->addForeignKey('fk_person', 'person_id', 'person', 'id'); |
| 116 | + |
| 117 | + Assert::exception(function () use ($contactTable) { |
| 118 | + |
| 119 | + $contactTable->addForeignKey('fk_person', 'person_id', 'person', 'id'); |
| 120 | + |
| 121 | + }, 'CzProject\SqlGenerator\DuplicateException', "Foreign key 'fk_person' already exists."); |
| 122 | + |
| 123 | +}); |
0 commit comments