chore: refactor project structure
This commit is contained in:
@@ -1,148 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func createTasksTable(db *pgxpool.Pool, ctx context.Context) error {
|
||||
createTableSQL := `
|
||||
CREATE TABLE IF NOT EXISTS tasks (
|
||||
id SERIAL PRIMARY KEY,
|
||||
text TEXT NOT NULL,
|
||||
completed BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
|
||||
);`
|
||||
|
||||
_, err := db.Exec(ctx, createTableSQL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating tasks table: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func truncateTasksTable(db *pgxpool.Pool, ctx context.Context) error {
|
||||
truncateSQL := `TRUNCATE TABLE tasks RESTART IDENTITY CASCADE;`
|
||||
_, err := db.Exec(ctx, truncateSQL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error truncating tasks table: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Task struct {
|
||||
Id int64
|
||||
Text string
|
||||
Completed bool
|
||||
Created_at time.Time
|
||||
Updated_at time.Time
|
||||
}
|
||||
|
||||
func generateTasks(count int) []Task {
|
||||
tasks := make([]Task, count)
|
||||
|
||||
for i := 1; i <= count; i++ {
|
||||
tasks[i-1] = Task{
|
||||
Text: fmt.Sprintf("random task Nº %v", i),
|
||||
Completed: rand.Float64() > 0.5,
|
||||
}
|
||||
}
|
||||
|
||||
return tasks
|
||||
}
|
||||
|
||||
func bulkInsertTasks(db *pgxpool.Pool, ctx context.Context, tasks []Task) error {
|
||||
if len(tasks) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
sb.WriteString(`INSERT INTO tasks (text, completed) VALUES `)
|
||||
|
||||
args := make([]any, 0, len(tasks)*2)
|
||||
|
||||
for i, task := range tasks {
|
||||
if i > 0 {
|
||||
sb.WriteString(`, `)
|
||||
}
|
||||
|
||||
fmt.Fprintf(&sb, `($%d, $%d)`, i*2+1, i*2+2)
|
||||
args = append(args, task.Text, task.Completed)
|
||||
}
|
||||
|
||||
sb.WriteString(`;`)
|
||||
|
||||
_, err := db.Exec(ctx, sb.String(), args...)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("error bulk inserting tasks: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Println("Warning: could not load .env file")
|
||||
}
|
||||
|
||||
dbURL := os.Getenv("PG_FROM_DB_URL")
|
||||
if dbURL == "" {
|
||||
log.Fatal("PG_FROM_DB_URL environment variable not set")
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
|
||||
defer cancel()
|
||||
|
||||
db, err := pgxpool.New(ctx, dbURL)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to create connection pool: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
fmt.Println("Creating tasks table...")
|
||||
err = createTasksTable(db, ctx)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create table: %v", err)
|
||||
}
|
||||
fmt.Println("✓ Tasks table created/already exists")
|
||||
|
||||
fmt.Println("Clearing existing data...")
|
||||
err = truncateTasksTable(db, ctx)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to truncate table: %v", err)
|
||||
}
|
||||
fmt.Println("✓ Table cleared")
|
||||
|
||||
fmt.Println("Generating 1000 tasks records...")
|
||||
tasks := generateTasks(1000)
|
||||
fmt.Println("✓ Tasks generated")
|
||||
|
||||
fmt.Println("Performing bulk insert...")
|
||||
err = bulkInsertTasks(db, ctx, tasks)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to bulk insert tasks: %v", err)
|
||||
}
|
||||
fmt.Println("✓ All 1000 task records inserted successfully")
|
||||
|
||||
var count int
|
||||
err = db.QueryRow(ctx, "SELECT COUNT(*) FROM tasks;").Scan(&count)
|
||||
if err != nil {
|
||||
log.Printf("Warning: could not verify record count: %v", err)
|
||||
} else {
|
||||
fmt.Printf("✓ Final task count: %d\n", count)
|
||||
}
|
||||
|
||||
fmt.Println("\nFixture creation completed successfully!")
|
||||
}
|
||||
Reference in New Issue
Block a user