NETRAVE/lib/utils/database_manager.rb
VetheonGames 335cd89ebe Detailed Refactoring of Database Interaction
1. **Refactoring of Database Interaction Methods**
   - Refactored the `store_services` method in the `DatabaseManager` class to handle an array of services instead of a hash. This change was made to simplify the interaction with the database and to avoid unnecessary complexity in the data structure.
   - The `store_services` method now iterates over an array of services and inserts each service into the database with a default status of true. This design choice was made to ensure that all services are active by default.

2. **Modification of Database Schema**
   - Modified the `create_services_table` method in the `DatabaseManager` class to create a table with only two columns: `id` and `services`. This change was made to align the database schema with the new data structure used in the `store_services` method.
   - The `status` column was removed from the `services` table because the status of all services is now assumed to be true by default.

3. **Error Handling and Debugging**
   - Encountered a `Sequel::DatabaseError` with the message "Operand should contain 1 column(s)" during the execution of the `store_services` method. This error was caused by an attempt to insert a hash into a single database column.
   - The error was resolved by refactoring the `store_services` method to handle an array of services instead of a hash.

4. **Unorthodox Design Choices**
   - The decision to use an array of services instead of a hash and to assume that the status of all services is true by default may seem unorthodox. However, these design choices were made to simplify the interaction with the database and to avoid unnecessary complexity in the data structure.
   - These design choices also helped to resolve the `Sequel::DatabaseError` that was encountered during the execution of the `store_services` method.

This commit represents a significant refactoring of the database interaction methods in the NETRAVE project. The changes made in this commit have simplified the interaction with the database and have resolved a `Sequel::DatabaseError` that was encountered during the execution of the `store_services` method.
2023-06-09 19:39:54 -06:00

57 lines
1.3 KiB
Ruby

# frozen_string_literal: true
require 'sequel'
require 'mysql2'
require_relative 'system_information_gather'
require_relative '../utils/utilities'
# database manager
class DatabaseManager
include Utilities
def initialize
@db = nil
end
def test_db_connection(db_details)
# Decrypt the password before using it
decrypted_password = decrypt_string_chacha20(db_details[:password], db_details[:key])
connection_string = "mysql2://#{db_details[:username]}:#{decrypted_password}@localhost/#{db_details[:database]}"
@db = Sequel.connect(connection_string)
# Try a simple query to test the connection
@db.run 'SELECT 1'
true
rescue Sequel::DatabaseConnectionError
false
ensure
@db&.disconnect
end
def create_system_info_table
@db.create_table? :system_info do
primary_key :id
Integer :uplink_speed
Integer :downlink_speed
Integer :total_bandwidth
end
end
def store_system_info(system_info)
@db[:system_info].insert(system_info)
end
def create_services_table
@db.create_table? :services do
primary_key :id
String :service_name
TrueClass :status, default: true
end
end
def store_services(services)
services.each do |service|
@db[:services].insert(service_name: service, status: true)
end
end
end