|
1 | 1 | <?php |
2 | | -use Opis\Database\Database; |
3 | | -use \Opis\Database\Connection; |
4 | | -use \Opis\Database\Schema\CreateTable; |
5 | 2 |
|
6 | 3 | trait SchemaManagementTrait { |
7 | 4 |
|
8 | 5 | public function doSetUp(\PDO $pdo): void { |
9 | 6 |
|
10 | | - // Now using opis/database to create tables so that it generates the |
11 | | - // appropriate SQL create statements for the current pdo driver. |
12 | | - // This makes the tests run with sqlite, mysql & postgres. |
13 | | - // Haven't tried sqlserver yet. |
14 | | - $connection = Connection::fromPDO($pdo); |
15 | | - $db = new Database($connection); |
16 | | - $schema = $db->schema(); |
| 7 | + $driver_name = strtolower(\LeanOrmCli\SchemaUtils::getPdoDriverName($pdo)); |
17 | 8 |
|
18 | | - $schema->create('authors', function(CreateTable $table) { |
19 | | - |
20 | | - //add table authors |
21 | | - $table->integer('author_id')->autoincrement(); |
22 | | - $table->primary('author_id', 'author_id'); |
23 | | - |
24 | | - $table->text('name'); |
25 | | - |
26 | | - $table->text('m_timestamp')->notNull(); |
27 | | - $table->text('date_created')->notNull(); |
28 | | - }); |
29 | | - |
30 | | - // Table creation method below is sqlite specific, |
31 | | - // won't work with mysql & the others |
32 | | -// $this->pdo->exec(" |
33 | | -// CREATE TABLE authors ( |
34 | | -// author_id INTEGER PRIMARY KEY, |
35 | | -// name TEXT, |
36 | | -// m_timestamp TEXT NOT NULL, |
37 | | -// date_created TEXT NOT NULL |
38 | | -// ) |
39 | | -// "); |
40 | | - |
41 | | - // The veiw creation sql below works on sqlite, mysql & postgres |
42 | | - // haven't tested with sqlserver though |
43 | | - $this->pdo->exec(" |
44 | | - CREATE VIEW v_authors |
45 | | - AS |
46 | | - SELECT |
47 | | - author_id, |
48 | | - name, |
49 | | - m_timestamp, |
50 | | - date_created |
51 | | - FROM authors |
52 | | - "); |
| 9 | + $create_queries = [ |
| 10 | + 'sqlite' => [ |
| 11 | + " |
| 12 | + CREATE TABLE authors ( |
| 13 | + author_id INTEGER PRIMARY KEY, |
| 14 | + name TEXT, |
| 15 | + m_timestamp TEXT NOT NULL, |
| 16 | + date_created TEXT NOT NULL |
| 17 | + ) |
| 18 | + ", |
| 19 | + " |
| 20 | + CREATE VIEW v_authors |
| 21 | + AS |
| 22 | + SELECT |
| 23 | + author_id, |
| 24 | + name, |
| 25 | + m_timestamp, |
| 26 | + date_created |
| 27 | + FROM |
| 28 | + authors |
| 29 | + ", |
| 30 | + " |
| 31 | + CREATE TABLE posts ( |
| 32 | + post_id INTEGER PRIMARY KEY, |
| 33 | + author_id INTEGER NOT NULL, |
| 34 | + datetime TEXT, |
| 35 | + title TEXT, |
| 36 | + body TEXT, |
| 37 | + m_timestamp TEXT NOT NULL, |
| 38 | + date_created TEXT NOT NULL, |
| 39 | + FOREIGN KEY(author_id) REFERENCES authors(author_id) |
| 40 | + ) |
| 41 | + ", |
| 42 | + ], |
| 43 | + 'mysql' => [ |
| 44 | + " |
| 45 | + CREATE TABLE `authors` ( |
| 46 | + `author_id` int unsigned NOT NULL AUTO_INCREMENT, |
| 47 | + `name` varchar(255) DEFAULT NULL, |
| 48 | + `m_timestamp` datetime NOT NULL, |
| 49 | + `date_created` datetime NOT NULL, |
| 50 | + PRIMARY KEY (`author_id`) |
| 51 | + ) |
| 52 | + ", |
| 53 | + " |
| 54 | + CREATE VIEW `v_authors` AS |
| 55 | + SELECT |
| 56 | + `authors`.`author_id` AS `author_id`, |
| 57 | + `authors`.`name` AS `name`, |
| 58 | + `authors`.`m_timestamp` AS `m_timestamp`, |
| 59 | + `authors`.`date_created` AS `date_created` |
| 60 | + FROM `authors` |
| 61 | + ", |
| 62 | + " |
| 63 | + CREATE TABLE `posts` ( |
| 64 | + `post_id` int unsigned NOT NULL AUTO_INCREMENT, |
| 65 | + `author_id` int unsigned NOT NULL, |
| 66 | + `datetime` datetime DEFAULT NULL, |
| 67 | + `title` varchar(255) DEFAULT NULL, |
| 68 | + `body` text, |
| 69 | + `m_timestamp` datetime NOT NULL, |
| 70 | + `date_created` datetime NOT NULL, |
| 71 | + PRIMARY KEY (`post_id`), |
| 72 | + KEY `fk_posts_belong_to_an_author` (`author_id`), |
| 73 | + CONSTRAINT `fk_posts_belong_to_an_author` FOREIGN KEY (`author_id`) REFERENCES `authors` (`author_id`) ON DELETE CASCADE ON UPDATE CASCADE |
| 74 | + ) |
| 75 | + ", |
| 76 | + ], |
| 77 | + 'pgsql' => [ |
| 78 | + " |
| 79 | + CREATE TABLE authors ( |
| 80 | + author_id SERIAL PRIMARY KEY, |
| 81 | + name varchar(255) DEFAULT NULL, |
| 82 | + m_timestamp TIMESTAMP NOT NULL, |
| 83 | + date_created TIMESTAMP NOT NULL |
| 84 | + ) |
| 85 | + ", |
| 86 | + " |
| 87 | + CREATE VIEW v_authors AS |
| 88 | + SELECT |
| 89 | + authors.author_id AS author_id, |
| 90 | + authors.name AS name, |
| 91 | + authors.m_timestamp AS m_timestamp, |
| 92 | + authors.date_created AS date_created |
| 93 | + FROM authors |
| 94 | + ", |
| 95 | + " |
| 96 | + CREATE TABLE posts ( |
| 97 | + post_id SERIAL PRIMARY KEY, |
| 98 | + author_id int NOT NULL, |
| 99 | + datetime TIMESTAMP DEFAULT NULL, |
| 100 | + title varchar(255) DEFAULT NULL, |
| 101 | + body text, |
| 102 | + m_timestamp TIMESTAMP NOT NULL, |
| 103 | + date_created TIMESTAMP NOT NULL |
| 104 | + ) |
| 105 | + ", |
| 106 | + ], |
| 107 | + ]; |
53 | 108 |
|
54 | | - // Table creation method below is sqlite specific, |
55 | | - // won't work with mysql & the others |
56 | | -// $this->pdo->exec(" |
57 | | -// CREATE TABLE posts ( |
58 | | -// post_id INTEGER PRIMARY KEY, |
59 | | -// author_id INTEGER NOT NULL, |
60 | | -// datetime TEXT, |
61 | | -// title TEXT, |
62 | | -// body TEXT, |
63 | | -// m_timestamp TEXT NOT NULL, |
64 | | -// date_created TEXT NOT NULL, |
65 | | -// FOREIGN KEY(author_id) REFERENCES authors(author_id) |
66 | | -// ) |
67 | | -// "); |
68 | | - |
69 | | - $schema->create('posts', function(CreateTable $table) { |
70 | | - |
71 | | - //add table authors |
72 | | - $table->integer('post_id')->autoincrement(); |
73 | | - $table->primary('post_id', 'post_id'); |
74 | | - |
75 | | - $table->integer('author_id')->notNull(); |
76 | | - |
77 | | - $table->text('datetime'); |
78 | | - $table->text('title'); |
79 | | - $table->text('body'); |
80 | | - |
81 | | - $table->text('m_timestamp')->notNull(); |
82 | | - $table->text('date_created')->notNull(); |
| 109 | + foreach ($create_queries[$driver_name] as $query) { |
83 | 110 |
|
84 | | - $table->foreign('author_id') |
85 | | - ->references('authors', 'author_id'); |
86 | | - }); |
| 111 | + $pdo->exec($query); |
| 112 | + } |
87 | 113 | } |
88 | 114 |
|
89 | 115 | protected function doTearDown(\PDO $pdo): void { |
|
0 commit comments