NETRAVE/bin/NETRAVE
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

64 lines
1.9 KiB
Ruby

#!/usr/bin/env ruby
# frozen_string_literal: true
require 'dotenv'
require_relative '../lib/utils/system_information_gather'
require_relative '../lib/utils/database_manager'
require_relative '../lib/utils/first_run_init'
require_relative '../lib/utils/utilities'
include Utilities # rubocop:disable Style/MixinUsage
# Create .env file if it doesn't exist
File.new('.env', 'w') unless File.exist?('.env')
# Load environment variables from .env file
Dotenv.load
# Initialize DatabaseManager
db_manager = DatabaseManager.new
# Get database details from environment variables
db_details = {
username: ENV['DB_USERNAME'],
password: ENV['DB_PASSWORD'],
key: ENV['DB_SECRET_KEY'],
database: ENV['DB_DATABASE']
}
# If any of the necessary details are missing, run the first run setup
if db_details.values.any?(&:nil?)
puts 'Missing or incomplete configuration. Running first run setup.'
first_run_init = FirstRunInit.new(db_manager)
first_run_init.run
# Reload environment variables after first run setup
Dotenv.load
db_details = {
username: ENV['DB_USERNAME'],
password: ENV['DB_PASSWORD'],
key: ENV['DB_SECRET_KEY'],
database: ENV['DB_DATABASE']
}
end
# Decrypt the password
db_details[:password] = decrypt_string_chacha20(db_details[:password], db_details[:key])
# Test connection
unless db_manager.test_db_connection(db_details)
puts 'Failed to connect to the database with existing configuration. Please re-enter your details.'
first_run_init = FirstRunInit.new(db_manager)
first_run_init.run
end
# Test connection again after potentially updating config
if db_manager.test_db_connection(db_details)
puts 'Successfully connected to the database.'
else
puts 'Failed to connect to the database. Please check your configuration.'
exit 1
end
puts 'Program successfully ran with no errors'
# TODO: Add the rest of your application logic here