feat: add db management scripts
This commit is contained in:
40
scripts/reset-db/main.go
Normal file
40
scripts/reset-db/main.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.ksdemosapps.com/kylesoda/pgx-learning/internal/config"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func dropTables(db *pgxpool.Pool, ctx context.Context) error {
|
||||
dropTableSQL := `DROP TABLE IF EXISTS tasks`
|
||||
|
||||
_, err := db.Exec(ctx, dropTableSQL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error droping tasks table: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
|
||||
db, err := pgxpool.New(ctx, config.Db.Url)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to create connection pool: %v", err)
|
||||
}
|
||||
|
||||
if err := dropTables(db, ctx); err != nil {
|
||||
log.Fatalf("Unexpected error: %v", err)
|
||||
} else {
|
||||
fmt.Println("Database reset completed succesfully")
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
}
|
||||
73
scripts/seed-db/main.go
Normal file
73
scripts/seed-db/main.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"git.ksdemosapps.com/kylesoda/pgx-learning/internal/config"
|
||||
"git.ksdemosapps.com/kylesoda/pgx-learning/internal/models"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func generateTasks(count int) []models.Task {
|
||||
tasks := make([]models.Task, count)
|
||||
|
||||
for i := 1; i <= count; i++ {
|
||||
tasks[i-1] = models.Task{
|
||||
Text: fmt.Sprintf("random task Nº %v", i),
|
||||
Completed: rand.Float64() > 0.5,
|
||||
}
|
||||
}
|
||||
|
||||
return tasks
|
||||
}
|
||||
|
||||
func saveTasks(db *pgxpool.Pool, ctx context.Context, tasks []models.Task) error {
|
||||
start := time.Now()
|
||||
|
||||
rowsCopied, err := db.CopyFrom(
|
||||
ctx,
|
||||
pgx.Identifier{"tasks"},
|
||||
[]string{"text", "completed"},
|
||||
pgx.CopyFromSlice(len(tasks), func(i int) ([]any, error) {
|
||||
return []any{
|
||||
(tasks)[i].Text,
|
||||
(tasks)[i].Completed,
|
||||
}, nil
|
||||
}),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error al realizar el CopyFrom: %w", err)
|
||||
}
|
||||
|
||||
duration := time.Since(start)
|
||||
fmt.Printf("Se insertaron exitosamente %d registros en %v\n", rowsCopied, duration)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
|
||||
db, err := pgxpool.New(ctx, config.Db.Url)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to create connection pool: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
count := 100000
|
||||
fmt.Printf("Generando %d registros...\n", count)
|
||||
tasks := generateTasks(count)
|
||||
|
||||
if err := saveTasks(db, ctx, tasks); err != nil {
|
||||
log.Fatalf("Unexpected error: %v", err)
|
||||
} else {
|
||||
fmt.Println("Database seed completed succesfully")
|
||||
}
|
||||
}
|
||||
48
scripts/setup-db/main.go
Normal file
48
scripts/setup-db/main.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.ksdemosapps.com/kylesoda/pgx-learning/internal/config"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func createTasksTable(db *pgxpool.Pool, ctx context.Context) error {
|
||||
createTableSQL := `
|
||||
CREATE TABLE IF NOT EXISTS tasks (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
text TEXT NOT NULL,
|
||||
completed BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ
|
||||
);
|
||||
`
|
||||
|
||||
_, err := db.Exec(ctx, createTableSQL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating tasks table: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
|
||||
db, err := pgxpool.New(ctx, config.Db.Url)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to create connection pool: %v", err)
|
||||
}
|
||||
|
||||
if err := createTasksTable(db, ctx); err != nil {
|
||||
log.Fatalf("Unexpected error: %v", err)
|
||||
} else {
|
||||
fmt.Println("Database setup completed succesfully")
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user