ajout de setup_server_debian.sh
This commit is contained in:
91
local/share/ytdll/lib/Config.php
Normal file
91
local/share/ytdll/lib/Config.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
// Auteur : Cédrix pour a5l.fr
|
||||
// Projet : scripts-bash/ytdll
|
||||
|
||||
// Version 2024.07.07-14.31
|
||||
// Version initiale
|
||||
|
||||
|
||||
class Config {
|
||||
private $pdo;
|
||||
private $database;
|
||||
|
||||
public function __construct(Database $database) {
|
||||
$this->database = $database;
|
||||
$this->pdo = $this->database->getPDO();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set or update the version in the config table.
|
||||
*
|
||||
* @param string $version The version to set.
|
||||
* @return bool True on success, false on failure.
|
||||
*/
|
||||
public function setVersionDefinitionsTables($version) {
|
||||
try {
|
||||
$stmt = $this->pdo->prepare("
|
||||
INSERT INTO config (key, value)
|
||||
VALUES ('versionDefinitionsTables', :version)
|
||||
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value
|
||||
");
|
||||
$stmt->execute(['version' => $version]);
|
||||
} catch (PDOException $e) {
|
||||
// Log the error or handle it appropriately
|
||||
error_log('Error setting version: ' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current version from the config table.
|
||||
*
|
||||
* @return string|null The current version or null if not set.
|
||||
*/
|
||||
public function getVersionDefinitionsTables() {
|
||||
try {
|
||||
|
||||
$stmt = $this->pdo->prepare("SELECT value FROM config WHERE key = 'versionDefinitionsTables'");
|
||||
$stmt->execute();
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
return $row ? $row['value'] : null;
|
||||
} catch (PDOException $e) {
|
||||
error_log("Database error: " . $e->getMessage());
|
||||
// Return a default value or null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the current path Definitions Tables from the config table.
|
||||
*
|
||||
* @return string The current version or default path if not set.
|
||||
*/
|
||||
public function getPathDefinitionsTablesFile() {
|
||||
$defaultPath = getenv('HOME') . "/.local/share/ytdll/definitionsTables.json";
|
||||
return $defaultPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify database privileges by attempting to create and drop a test table.
|
||||
*
|
||||
* @return bool True if privileges are sufficient, false otherwise.
|
||||
*/
|
||||
public function verifyPrivileges() {
|
||||
try {
|
||||
$tableManager = new TableManager($this->database);
|
||||
$tableManager->createTable('test_table', [
|
||||
['name' => 'id', 'type' => 'SERIAL PRIMARY KEY'],
|
||||
['name' => 'name', 'type' => 'VARCHAR(255)']
|
||||
]);
|
||||
$tableManager->dropTable('test_table');
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
error_log("Erreur sur les privilèges d'accès à la base de données : " . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
26
local/share/ytdll/lib/Database.php
Normal file
26
local/share/ytdll/lib/Database.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
class Database {
|
||||
private $pdo;
|
||||
|
||||
public function __construct($config) {
|
||||
$dsn = "pgsql:host={$config['host']};dbname={$config['dbname']}";
|
||||
try {
|
||||
$this->pdo = new PDO($dsn, $config['user'], $config['password'], [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
|
||||
]);
|
||||
} catch (PDOException $e) {
|
||||
die('Connection failed: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getDefaultSchema() {
|
||||
$query = "SELECT current_schema()";
|
||||
$stmt = $this->pdo->query($query);
|
||||
return $stmt->fetchColumn();
|
||||
}
|
||||
|
||||
public function getPDO() {
|
||||
return $this->pdo;
|
||||
}
|
||||
}
|
||||
116
local/share/ytdll/lib/TableManager.php
Normal file
116
local/share/ytdll/lib/TableManager.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
// Auteur : Cédrix pour a5l.fr
|
||||
// Projet : scripts-bash/ytdll
|
||||
|
||||
// Version 2024.07.07-14.31
|
||||
// Version initiale
|
||||
|
||||
|
||||
class TableManager {
|
||||
private $pdo;
|
||||
private $database;
|
||||
|
||||
// SGBD PostGreSQL 16
|
||||
|
||||
public function __construct(Database $database) {
|
||||
$this->database = $database;
|
||||
$this->pdo = $this->database->getPDO();
|
||||
}
|
||||
|
||||
public function getTableColumns($tableName) {
|
||||
if (!$this->tableExists($tableName)) {
|
||||
return []; // Retourne un tableau vide si la table n'existe pas
|
||||
}
|
||||
|
||||
$query = "SELECT column_name, data_type FROM information_schema.columns WHERE table_name = :table_name AND table_schema = :table_schema";
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
$stmt->execute([
|
||||
':table_name' => $tableName
|
||||
]);
|
||||
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
private function tableExists($tableName) {
|
||||
$schema = $this->database->getDefaultSchema();
|
||||
|
||||
$query = "SELECT COUNT(*) FROM information_schema.tables WHERE table_name = :table_name AND table_schema = :table_schema";
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
echo "Vérification de l'existence de la table : $tableName\n";
|
||||
$stmt->execute([
|
||||
':table_name' => $tableName,
|
||||
':table_schema' => $schema
|
||||
]);
|
||||
return $stmt->fetchColumn() > 0;
|
||||
}
|
||||
|
||||
public function processTables($tables) {
|
||||
foreach ($tables as $table) {
|
||||
$this->processTable($table);
|
||||
}
|
||||
}
|
||||
|
||||
private function processTable($table) {
|
||||
$tableName = $table['name'];
|
||||
$columns = $table['columns'];
|
||||
$existingColumns = $this->getTableColumns($tableName);
|
||||
|
||||
if (empty($existingColumns) && !$this->tableExists($tableName)) {
|
||||
$this->createTable($tableName, $columns);
|
||||
return;
|
||||
}
|
||||
|
||||
$existingColumnsMap = [];
|
||||
foreach ($existingColumns as $column) {
|
||||
$existingColumnsMap[$column['column_name']] = $column['data_type'];
|
||||
}
|
||||
|
||||
foreach ($columns as $column) {
|
||||
$columnName = $column['name'];
|
||||
$columnType = $column['type'];
|
||||
|
||||
if (isset($existingColumnsMap[$columnName])) {
|
||||
if ($existingColumnsMap[$columnName] !== $columnType) {
|
||||
$this->alterColumnType($tableName, $columnName, $columnType);
|
||||
}
|
||||
unset($existingColumnsMap[$columnName]);
|
||||
} else {
|
||||
$this->addColumn($tableName, $columnName, $columnType);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($existingColumnsMap as $columnName => $columnType) {
|
||||
$this->dropColumn($tableName, $columnName);
|
||||
}
|
||||
}
|
||||
|
||||
public function createTable($tableName, $columns) {
|
||||
$columnsSql = [];
|
||||
foreach ($columns as $column) {
|
||||
$columnsSql[] = "{$column['name']} {$column['type']}";
|
||||
}
|
||||
$columnsSql = implode(", ", $columnsSql);
|
||||
$sql = "CREATE TABLE $tableName ($columnsSql)";
|
||||
$this->pdo->exec($sql);
|
||||
}
|
||||
|
||||
public function dropTable($tableName) {
|
||||
$sql = "DROP TABLE IF EXISTS $tableName";
|
||||
$this->pdo->exec($sql);
|
||||
}
|
||||
|
||||
private function alterColumnType($tableName, $columnName, $columnType) {
|
||||
$sql = "ALTER TABLE $tableName ALTER COLUMN $columnName TYPE $columnType USING $columnName::$columnType";
|
||||
$this->pdo->exec($sql);
|
||||
}
|
||||
|
||||
private function addColumn($tableName, $columnName, $columnType) {
|
||||
$sql = "ALTER TABLE $tableName ADD COLUMN $columnName $columnType";
|
||||
$this->pdo->exec($sql);
|
||||
}
|
||||
|
||||
private function dropColumn($tableName, $columnName) {
|
||||
$sql = "ALTER TABLE $tableName DROP COLUMN $columnName";
|
||||
$this->pdo->exec($sql);
|
||||
}
|
||||
}
|
||||
48
local/share/ytdll/lib/UpdateManager.php
Normal file
48
local/share/ytdll/lib/UpdateManager.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?PHP
|
||||
|
||||
// Auteur : Cédrix pour a5l.fr
|
||||
// Projet : scripts-bash/ytdll
|
||||
|
||||
// Version 2024.07.07-14.31
|
||||
// Version initiale
|
||||
|
||||
class UpdateManager {
|
||||
private $pdo;
|
||||
private $database;
|
||||
|
||||
public function __construct($database) {
|
||||
$this->database = $database;
|
||||
$this->pdo = $this->database->getPDO();
|
||||
|
||||
}
|
||||
|
||||
public function manageVersionTables() {
|
||||
$configManager = new Config($this->database);
|
||||
// Obtenir le numéro de version dans le fichier de définitions de table
|
||||
$definitionsTablesFile = json_decode(file_get_contents($configManager->getPathDefinitionsTablesFile()), true);
|
||||
$newDefinitionNumber = $definitionsTablesFile['definition_number'];
|
||||
|
||||
// Obtenir le numéro de version des Tables en base
|
||||
$currentDefinitionNumber = $configManager->getVersionDefinitionsTables();
|
||||
|
||||
if ($currentDefinitionNumber !== null) {
|
||||
echo 'Version not set or an error occurred.';
|
||||
$currentDefinitionNumber = 0;
|
||||
}
|
||||
|
||||
$tables = $definitionsTablesFile['tables'];
|
||||
|
||||
if ($newDefinitionNumber > $currentDefinitionNumber) {
|
||||
$tableManager = new TableManager($this->database);
|
||||
$tableManager->processTables($tables);
|
||||
$configManager->setVersionDefinitionsTables($newDefinitionNumber);
|
||||
if ($currentDefinitionNumber == 0) {
|
||||
echo "Tables created successfully.";
|
||||
} else {
|
||||
echo "Tables updated successfully from $currentDefinitionNumber to $newDefinitionNumber.";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user