Skip to content

Commit 3f6a9a4

Browse files
Automatic Migration (dev)
1 parent 1a6d401 commit 3f6a9a4

File tree

4 files changed

+120
-60
lines changed

4 files changed

+120
-60
lines changed

.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ APP_ENV=development
1212

1313
# Enable or disable automatic database migrations on startup
1414
# Set to true for development, false for production
15-
AUTO_MIGRATE=false
15+
AUTO_MIGRATE=true
1616

1717
# Enable or disable debug mode (true = detailed errors, false = generic errors)
1818
# Use true only in development environments to avoid exposing sensitive information
@@ -45,7 +45,7 @@ REDIS_DB=0
4545
# Ensure the database server is running and accessible
4646
DB_HOST=db
4747
DB_PORT=3306
48-
DB_NAME=data_base_name
48+
DB_NAME=db_echoapi
4949

5050
# Database user credentials
5151
# Ensure the user has the necessary permissions on the database

bootstrap/app.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
// Carrega o autoload do Composer
1919
require_once __DIR__ . '/../vendor/autoload.php';
2020

21+
// ==========================
22+
// LOGGER (antes da migração!)
23+
// ==========================
24+
$logger = LoggerFactory::create();
25+
2126
// ==========================
2227
// AMBIENTE E CONFIGURAÇÃO
2328
// ==========================
@@ -37,27 +42,11 @@
3742
// AUTOMATIC MIGRATION (dev)
3843
// ==========================
3944

40-
$shouldMigrate = isset($_ENV['AUTO_MIGRATE']) && $_ENV['AUTO_MIGRATE'] === 'true';
41-
$tableExists = $database->query("SHOW TABLES LIKE 'users'")->fetch();
42-
43-
if ($shouldMigrate && !$tableExists) {
44-
$migrationFile = __DIR__ . '/../core/migrations/auth-migrations.sql';
45-
46-
if (file_exists($migrationFile)) {
47-
try {
48-
$sql = file_get_contents($migrationFile);
49-
$database->pdo->exec($sql);
50-
error_log("[EchoAPI] Database initialized using 'auth-migrations.sql'.");
51-
} catch (PDOException $e) {
52-
error_log("[EchoAPI] Failed to execute migration: " . $e->getMessage());
53-
}
54-
} else {
55-
error_log("[EchoAPI] Database initialized using 'auth-migrations.sql'.");
56-
echo "[EchoAPI] Database initialized using 'auth-migrations.sql'.\n";
57-
}
45+
if (file_exists(__DIR__ . '/../core/Migration/auto_migrate.php')) {
46+
require_once __DIR__ . '/../core/Migration/auto_migrate.php';
47+
runMigrationIfNeeded($database, $logger);
5848
}
5949

60-
6150
// ==========================
6251
// LOGGER
6352
// ==========================

core/Migration/auth-migrations.sql

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
-- --------------------------------------------------------
2-
-- Servidor: 127.0.0.1
3-
-- Versão do servidor: 10.4.32-MariaDB - mariadb.org binary distribution
4-
-- OS do Servidor: Win64
5-
-- HeidiSQL Versão: 12.11.0.7065
2+
-- Exportação gerada por HeidiSQL
3+
-- Banco será definido pela conexão PDO no PHP
64
-- --------------------------------------------------------
75

86
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
@@ -14,70 +12,56 @@
1412
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
1513
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
1614

17-
18-
-- Copiando estrutura do banco de dados para data_base_name
19-
CREATE DATABASE IF NOT EXISTS `data_base_name` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci */;
20-
USE `data_base_name`;
21-
22-
-- Copiando estrutura para tabela data_base_name.password_resets
2315
CREATE TABLE IF NOT EXISTS `password_resets` (
24-
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
25-
`email` varchar(255) NOT NULL,
26-
`token` varchar(255) NOT NULL,
16+
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
17+
`email` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
18+
`token` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
2719
`expiration` datetime NOT NULL,
28-
`creation_date` timestamp NOT NULL DEFAULT current_timestamp(),
20+
`creation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
2921
PRIMARY KEY (`id`)
3022
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
3123

32-
-- Copiando dados para a tabela data_base_name.password_resets: ~0 rows (aproximadamente)
33-
34-
-- Copiando estrutura para tabela data_base_name.roles
3524
CREATE TABLE IF NOT EXISTS `roles` (
36-
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
37-
`name` varchar(50) NOT NULL,
25+
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
26+
`name` varchar(50) COLLATE utf8mb4_general_ci NOT NULL,
3827
PRIMARY KEY (`id`),
3928
UNIQUE KEY `unique_name` (`name`)
4029
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
4130

42-
-- Copiando dados para a tabela data_base_name.roles: ~4 rows (aproximadamente)
4331
INSERT INTO `roles` (`id`, `name`) VALUES
4432
(1, 'master'),
4533
(2, 'admin'),
4634
(3, 'manager'),
4735
(4, 'user');
4836

49-
-- Copiando estrutura para tabela data_base_name.users
5037
CREATE TABLE IF NOT EXISTS `users` (
51-
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
52-
`role_id` bigint(20) unsigned NOT NULL DEFAULT 3,
53-
`user` varchar(255) NOT NULL,
54-
`email` varchar(255) NOT NULL,
55-
`password` varchar(255) NOT NULL,
56-
`creation_date` timestamp NOT NULL DEFAULT current_timestamp(),
38+
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
39+
`role_id` bigint unsigned NOT NULL DEFAULT '3',
40+
`user` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
41+
`email` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
42+
`password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
43+
`activated` tinyint(1) NOT NULL DEFAULT (1),
44+
`creation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
5745
PRIMARY KEY (`id`),
5846
UNIQUE KEY `unique_email` (`email`),
5947
KEY `fk_users_roles` (`role_id`),
60-
CONSTRAINT `fk_users_roles` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
48+
CONSTRAINT `fk_users_roles` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`)
6149
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
6250

63-
-- Copiando dados para a tabela data_base_name.users: ~1 rows (aproximadamente)
64-
INSERT INTO `users` (`id`, `role_id`, `user`, `email`, `password`, `creation_date`) VALUES
65-
(1, 1, 'Master', '[email protected]', '$2y$10$kUpUMOLxlkcoZIlZuMb1zOuW7d3bW5kkpkFaDCV6Fg5/qzNGBZBMC', '2025-07-14 18:43:15');
51+
INSERT INTO `users` (`id`, `role_id`, `user`, `email`, `password`, `activated`, `creation_date`) VALUES
52+
(1, 1, 'Master', '[email protected]', '$2y$10$kUpUMOLxlkcoZIlZuMb1zOuW7d3bW5kkpkFaDCV6Fg5/qzNGBZBMC', 1, '2025-07-14 18:43:15');
6653

67-
-- Copiando estrutura para tabela data_base_name.user_tokens
6854
CREATE TABLE IF NOT EXISTS `user_tokens` (
69-
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
70-
`user_id` bigint(20) unsigned NOT NULL,
71-
`token` varchar(255) NOT NULL,
72-
`revoked` tinyint(1) NOT NULL DEFAULT 0,
73-
`creation_date` timestamp NOT NULL DEFAULT current_timestamp(),
55+
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
56+
`user_id` bigint unsigned NOT NULL,
57+
`token` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
58+
`revoked` tinyint(1) NOT NULL DEFAULT '0',
59+
`creation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
7460
PRIMARY KEY (`id`),
7561
KEY `fk_user_tokens_users` (`user_id`),
7662
CONSTRAINT `fk_user_tokens_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
7763
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
7864

79-
-- Copiando dados para a tabela data_base_name.user_tokens: ~0 rows (aproximadamente)
80-
8165
/*!40103 SET TIME_ZONE=IFNULL(@OLD_TIME_ZONE, 'system') */;
8266
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
8367
/*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */;

core/Migration/auto_migrate.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
use Medoo\Medoo;
4+
use Monolog\Logger;
5+
6+
/**
7+
* Verifica se a migração automática está habilitada via .env.
8+
*/
9+
function shouldAutoMigrate(): bool
10+
{
11+
return isset($_ENV['AUTO_MIGRATE']) && $_ENV['AUTO_MIGRATE'] === 'true';
12+
}
13+
14+
/**
15+
* Verifica se a tabela existe no banco de dados.
16+
*/
17+
function tableExists(PDO $pdo, string $table): bool
18+
{
19+
$stmt = $pdo->query("SHOW TABLES LIKE '$table'");
20+
return $stmt->fetch() !== false;
21+
}
22+
23+
/**
24+
* Desativa a flag AUTO_MIGRATE no arquivo .env após sucesso.
25+
*/
26+
function disableAutoMigrate(Logger $logger): void
27+
{
28+
$envFile = __DIR__ . '/../.env';
29+
30+
if (!file_exists($envFile)) {
31+
$logger->warning(".env file not found. Unable to disable AUTO_MIGRATE.");
32+
return;
33+
}
34+
35+
$content = file_get_contents($envFile);
36+
$updated = preg_replace('/^AUTO_MIGRATE\s*=\s*true$/mi', 'AUTO_MIGRATE=false', $content);
37+
38+
if ($updated !== null) {
39+
file_put_contents($envFile, $updated);
40+
$logger->info("AUTO_MIGRATE has been set to false.");
41+
} else {
42+
$logger->warning("Failed to update AUTO_MIGRATE in .env.");
43+
}
44+
}
45+
46+
/**
47+
* Executa a migração do banco de dados se necessário.
48+
*/
49+
function runMigrationIfNeeded(Medoo $database, Logger $logger): void
50+
{
51+
if (!shouldAutoMigrate()) {
52+
return;
53+
}
54+
55+
if (tableExists($database->pdo, 'users')) {
56+
return;
57+
}
58+
59+
$migrationFile = __DIR__ . '/auth-migrations.sql';
60+
61+
if (!file_exists($migrationFile)) {
62+
$logger->error("Migration file not found: auth-migrations.sql", [
63+
'file' => $migrationFile
64+
]);
65+
return;
66+
}
67+
68+
try {
69+
$sql = file_get_contents($migrationFile);
70+
$database->pdo->exec($sql);
71+
72+
// Verifica se a tabela foi realmente criada após a migração
73+
if (tableExists($database->pdo, 'users')) {
74+
disableAutoMigrate($logger);
75+
} else {
76+
$logger->error("Migration executed but 'users' table still missing.", [
77+
'file' => $migrationFile
78+
]);
79+
}
80+
81+
} catch (\PDOException $e) {
82+
$logger->error("Failed to execute migration.", [
83+
'exception' => $e,
84+
'file' => $migrationFile
85+
]);
86+
}
87+
}

0 commit comments

Comments
 (0)