- Configuration Management:
- The system initializes a database connection using configuration loaded from a
config.yaml
file. Configuration management is handled using thegithub.com/spf13/viper
package. - It establishes a connection to an SQLite database using the database driver (
dbDriver
) and path (dbPath
) specified in the configuration file.
- The system initializes a database connection using configuration loaded from a
- DBStore Struct:
- The
DBStore
struct implements thedb.Store
interface, providing methods to interact with the database. - Key methods include:
AllMailboxes()
: Retrieves all mailboxes from the database and returns a channel (<-chan db.Mailbox
) that streams each mailbox as it's fetched.UsersForMailbox(mailboxID int)
: Retrieves users associated with a specific mailbox ID and returns a channel (<-chan db.User
) that streams each user record.
- The
- Functionality:
- The
Pipeline
function coordinates the process of retrieving mailboxes and their associated users. - It starts by fetching mailboxes using
store.AllMailboxes()
, which returns a channel ofMailbox
objects. - For each retrieved mailbox, it concurrently retrieves users using
store.UsersForMailbox(mb.ID)
and processes each user in a separate goroutine. - A
sync.WaitGroup
is used to ensure all user processing goroutines complete before the function finishes.
- The
-
Create Database Schema:
-
Use the provided SQL script to set up the database schema and sample data. Save the following script as
schema.sql
:-- Create mailboxes table CREATE TABLE mailboxes ( id INTEGER PRIMARY KEY, mpi_id VARCHAR(200), token VARCHAR(200), created_at TIMESTAMP ); -- Create users table CREATE TABLE users ( id INTEGER PRIMARY KEY, mailbox_id INTEGER, user_name VARCHAR(200), email_address VARCHAR(200), created_at TIMESTAMP, FOREIGN KEY (mailbox_id) REFERENCES mailboxes(id) ); -- Insert sample data into mailboxes table INSERT INTO mailboxes (id, mpi_id, token, created_at) VALUES (1, 'mpi123', 'token123', '2024-07-23 12:00:00'), (2, 'mpi456', 'token456', '2024-07-23 13:00:00'); -- Insert sample data into users table INSERT INTO users (id, mailbox_id, user_name, email_address, created_at) VALUES (101, 1, 'user1', 'user1@example.com', '2024-07-23 12:30:00'), (102, 1, 'user2', 'user2@example.com', '2024-07-23 12:45:00'), (201, 2, 'user3', 'user3@example.com', '2024-07-23 13:15:00');
-
-
Execute the Script:
- Run the script
bin/dbsetup
to setup the database and add test data:./bin/dbsetup
- Run the script
- Build the Application:
- Navigate to the project directory and execute
/bin/dev
to build and run the application:./bin/dev
- Navigate to the project directory and execute
-
Run Unit Tests:
- Ensure you have Go installed and the
go test
command available. - Run the tests using:
go test -v
- Ensure you have Go installed and the
-
Test Output:
- Review test results in the console output for verification of functionality and correctness.
-
Configuration File:
- Create a
config.yaml
file in the root directory with the following structure:database: driver: sqlite3 path: path_to_your_database.db
- Create a
-
Adjust the
path
according to your local database file location.
-
Dependencies:
- The project depends on the
github.com/spf13/viper
package for configuration management. Ensure it is included in yourgo.mod
file.
- The project depends on the
-
Logging:
- The application uses standard logging to provide runtime information and debugging output.