Database Seeding in Go
Database seeding is the process of populating a database with initial data. This is crucial for development, testing, and sometimes even production environments, allowing you to quickly set up a database with known data for consistent results. This challenge asks you to implement a Go program that seeds a SQLite database with sample data.
Problem Description
You are tasked with creating a Go program that seeds a SQLite database named mydatabase.db with data for a simple users table. The users table has the following schema:
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
age INTEGER
);
Your program should connect to the database, create the table if it doesn't exist, and then insert three sample users with the following data:
- User 1: username = "john_doe", email = "john.doe@example.com", age = 30
- User 2: username = "jane_smith", email = "jane.smith@example.com", age = 25
- User 3: username = "peter_jones", email = "peter.jones@example.com", age = 40
The program should handle potential errors gracefully, such as database connection errors or duplicate email entries. If the table already exists, it should not attempt to recreate it. The program should print informative messages to the console indicating the success or failure of each step.
Examples
Example 1:
Input: No input required. The program creates and populates a database file.
Output:
"Connecting to database: mydatabase.db\n"
"Table 'users' does not exist. Creating table...\n"
"Inserting user: john_doe\n"
"Inserting user: jane_smith\n"
"Inserting user: peter_jones\n"
"Seeding complete. 3 users inserted.\n"
Explanation: The program connects to the database, creates the users table, and inserts the three sample users.
Example 2:
Input: The database file `mydatabase.db` already exists and contains the `users` table.
Output:
"Connecting to database: mydatabase.db\n"
"Table 'users' already exists.\n"
"Inserting user: john_doe\n"
"Inserting user: jane_smith\n"
"Inserting user: peter_jones\n"
"Seeding complete. 3 users inserted.\n"
Explanation: The program connects to the database, detects that the users table already exists, and inserts the three sample users.
Example 3: (Edge Case - Duplicate Email)
Input: The database file `mydatabase.db` exists, and contains a user with email "john.doe@example.com".
Output:
"Connecting to database: mydatabase.db\n"
"Table 'users' already exists.\n"
"Inserting user: john_doe\n"
"Error inserting john_doe: UNIQUE constraint failed: users.email\n"
"Inserting user: jane_smith\n"
"Inserting user: peter_jones\n"
"Seeding complete. 2 users inserted.\n"
Explanation: The program attempts to insert "john_doe" but fails due to a unique constraint violation. It continues to insert the other users.
Constraints
- The database file should be named
mydatabase.db. - The program must use the
database/sqlandgithub.com/mattn/go-sqlite3packages. - Error handling is required. The program should not panic on errors and should provide informative error messages.
- The program should be able to handle the case where the database file does not exist.
- The program should be able to handle the case where the
userstable already exists. - The program should handle unique constraint violations gracefully.
Notes
- Consider using prepared statements to prevent SQL injection vulnerabilities.
- You can use the
ospackage to check if the database file exists. - The
github.com/mattn/go-sqlite3package needs to be installed:go get github.com/mattn/go-sqlite3 - Focus on clear and concise code with proper error handling. The goal is to demonstrate a robust and reliable database seeding process.
- The program should not delete existing data in the table. It should only insert new records.