ajout de setup_server_debian.sh

This commit is contained in:
2025-03-03 08:47:52 +01:00
parent 2ceb4be24f
commit 2629dd832c
13 changed files with 699 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
{
"database": {
"host": "changeme",
"dbname": "changeme",
"user": "changeme",
"password": "changeme"
}
}

View File

@@ -0,0 +1,38 @@
{
"definition_number": 1,
"versions": [
{
"version" : "2024.07.07-14.31",
"comment" : "Version initiale"
}
],
"author": "Cédrix pour a5l.fr",
"project": "scripts-bash/ytdll",
"tables": [
{
"name": "config",
"columns": [
{"name": "id", "type": "SERIAL PRIMARY KEY"},
{"name": "key", "type": "VARCHAR(255)"},
{"name": "value", "type": "VARCHAR(255)"}
]
},
{
"name": "feed_metadata",
"columns": [
{"name": "channel_id", "type": "VARCHAR(255) PRIMARY KEY"},
{"name": "author_name", "type": "VARCHAR(255)"},
{"name": "author_uri", "type": "VARCHAR(255)"}
]
},
{
"name": "entry_metadata",
"columns": [
{"name": "video_id", "type": "VARCHAR(255) PRIMARY KEY"},
{"name": "channel_id", "type": "VARCHAR(255)"},
{"name": "title", "type": "VARCHAR(255)"},
{"name": "author_name", "type": "VARCHAR(255)"}
]
}
]
}

View 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;
}
}
}

View 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;
}
}

View 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);
}
}

View 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.";
}
}
}
}