11<?php
22
3+ require_once __DIR__ . '/migrate.php ' ;
4+
35use App \Database ;
46
57/**
911 */
1012function createTables (): void
1113{
12- /**
13- * Tables' structure
14- */
15- $ tablesStructures = [
16- "CREATE TABLE IF NOT EXISTS `users` (
17- `id` INT UNSIGNED NOT NULL,
14+ initializeMigrationsTable ();
15+
16+ applyMigration ('create_users_table ' , function () {
17+ $ query = "CREATE TABLE IF NOT EXISTS `users` (
18+ `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
1819 `email` TINYTEXT NOT NULL,
1920 `password` TINYTEXT NOT NULL,
2021 `secret` TINYTEXT NOT NULL,
2122 `user_token` TINYTEXT NOT NULL,
2223 `verified` TINYINT UNSIGNED NOT NULL DEFAULT 0,
2324 `tagline` TINYTEXT NOT NULL,
2425 `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
25- `updated_at` TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW()
26- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; " ,
26+ `updated_at` TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW(),
27+ PRIMARY KEY (`id`)
28+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; " ;
29+ Database::query ($ query );
30+ Database::execute ();
31+ });
2732
28- "CREATE TABLE IF NOT EXISTS `posts` (
29- `id` INT UNSIGNED NOT NULL,
33+ applyMigration ('create_posts_table ' , function () {
34+ $ query = "CREATE TABLE IF NOT EXISTS `posts` (
35+ `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
3036 `user_id` INT UNSIGNED NOT NULL,
3137 `category` TINYTEXT NOT NULL,
3238 `title` TINYTEXT NOT NULL,
@@ -35,49 +41,19 @@ function createTables(): void
3541 `body` MEDIUMTEXT NOT NULL,
3642 `position` TINYINT UNSIGNED NOT NULL,
3743 `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
38- `updated_at` TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW()
39- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; "
40- ];
41-
42- /**
43- * Indexes
44- */
45- $ tablesIndexes = [
46- "ALTER TABLE `users` ADD PRIMARY KEY (`id`); " ,
47- "ALTER TABLE `posts` ADD PRIMARY KEY (`id`); "
48- ];
49-
50- /**
51- * Auto increments
52- */
53- $ tablesAutoIncrements = [
54- "ALTER TABLE `users` MODIFY `id` INT UNSIGNED NOT NULL AUTO_INCREMENT; " ,
55- "ALTER TABLE `posts` MODIFY `id` INT UNSIGNED NOT NULL AUTO_INCREMENT; "
56- ];
57-
58- /**
59- * Foreign keys
60- */
61- $ tablesForeignKeys = [
62- "ALTER TABLE `posts` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`); "
63- ];
64-
65- foreach ($ tablesStructures as $ tablesStructure ) {
66- Database::query ($ tablesStructure );
44+ `updated_at` TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW(),
45+ PRIMARY KEY (`id`)
46+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; " ;
47+ Database::query ($ query );
6748 Database::execute ();
68- }
69- foreach ($ tablesIndexes as $ tablesIndex ) {
70- Database::query ($ tablesIndex );
71- Database::execute ();
72- }
73- foreach ($ tablesAutoIncrements as $ tablesAutoIncrement ) {
74- Database::query ($ tablesAutoIncrement );
75- Database::execute ();
76- }
77- foreach ($ tablesForeignKeys as $ tablesForeignKey ) {
78- Database::query ($ tablesForeignKey );
49+
50+ // You might also include the foreign key creation here.
51+ $ fkQuery = "ALTER TABLE `posts` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`); " ;
52+ Database::query ($ fkQuery );
7953 Database::execute ();
80- }
54+ });
55+
56+ // Add your new migrations here
8157
8258 /**
8359 * Prevent to create existed tables by commenting a command that call this function
0 commit comments