42 lines
1.6 KiB
PHP
42 lines
1.6 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
try {
|
|
// Connect to MySQL server without specifying a database
|
|
$pdo = new PDO('mysql:host=' . DB_HOST . ';charset=utf8mb4', DB_USER, DB_PASS, [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
]);
|
|
|
|
// Create the database if it doesn't exist
|
|
$pdo->exec("CREATE DATABASE IF NOT EXISTS `" . DB_NAME . "`");
|
|
|
|
// Reconnect to the specific database
|
|
$pdo = db();
|
|
|
|
$sql = file_get_contents(__DIR__ . '/schema.sql');
|
|
$pdo->exec($sql);
|
|
echo "Database schema created successfully.\n";
|
|
|
|
// Check if 'cost' column exists in 'products' table and add it if not
|
|
$stmt = $pdo->query("SHOW COLUMNS FROM `products` LIKE 'cost'");
|
|
$column_exists = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$column_exists) {
|
|
$pdo->exec("ALTER TABLE `products` ADD `cost` DECIMAL(10, 2) NOT NULL DEFAULT 0.00 AFTER `price`");
|
|
echo "Column 'cost' added to 'products' table successfully.\n";
|
|
}
|
|
|
|
// Check if 'subtotal' column exists in 'sales' table and add columns if not
|
|
$stmt = $pdo->query("SHOW COLUMNS FROM `sales` LIKE 'subtotal'");
|
|
$column_exists = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$column_exists) {
|
|
$pdo->exec("ALTER TABLE `sales` ADD `subtotal` DECIMAL(10, 2) NOT NULL AFTER `customer_id`");
|
|
$pdo->exec("ALTER TABLE `sales` ADD `tax_total` DECIMAL(10, 2) NOT NULL DEFAULT 0.00 AFTER `subtotal`");
|
|
echo "Columns 'subtotal' and 'tax_total' added to 'sales' table successfully.\n";
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
die("Database setup failed: " . $e->getMessage() . "\n");
|
|
} |