36663-vm/db/setup.php
Flatlogic Bot d2cdcf4dcb 1
2025-12-04 19:40:19 +00:00

72 lines
2.5 KiB
PHP

<?php
require_once 'config.php';
try {
$pdo = db();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Create responsables table
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS `responsables` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nombre` varchar(255) NOT NULL,
`telefono` varchar(20) DEFAULT NULL,
`correo_electronico` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL;
$pdo->exec($sql);
echo "Table 'responsables' created successfully.<br>";
// Create actividades table
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS `actividades` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`descripcion` text NOT NULL,
`fecha_inicio` date NOT NULL,
`hora_inicio` time NOT NULL,
`fecha_termino` date NOT NULL,
`hora_termino` time NOT NULL,
`responsable_id` int(11) DEFAULT NULL,
`estado` enum('en proceso','terminada','retrasada') NOT NULL DEFAULT 'en proceso',
`observaciones` text DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `responsable_id` (`responsable_id`),
CONSTRAINT `actividades_ibfk_1` FOREIGN KEY (`responsable_id`) REFERENCES `responsables` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL;
$pdo->exec($sql);
echo "Table 'actividades' created successfully.<br>";
// Create users table
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password_hash` varchar(255) NOT NULL,
`role` enum('administrador','capturista') NOT NULL DEFAULT 'capturista',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL;
$pdo->exec($sql);
echo "Table 'users' created successfully.<br>";
// Insert admin user if not exists
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = 'admin'");
$stmt->execute();
if ($stmt->fetch()) {
echo "User 'admin' already exists.<br>";
} else {
$adminPassword = password_hash('admin', PASSWORD_DEFAULT);
$sql = "INSERT INTO `users` (`username`, `password_hash`, `role`) VALUES ('admin', :password, 'administrador')";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':password', $adminPassword);
$stmt->execute();
echo "User 'admin' created successfully.<br>";
}
} catch (PDOException $e) {
die("DB ERROR: " . $e->getMessage());
}