Add migration tool to project

This commit is contained in:
pro100ton 2024-11-07 21:22:20 +03:00
parent 0608cbaa09
commit c6e73dd360
3 changed files with 82 additions and 8 deletions

View file

@ -0,0 +1,6 @@
CREATE TABLE product (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
price float,
purchase_date timestamp
);

48
internal/migrator/db.go Normal file
View file

@ -0,0 +1,48 @@
package migrator
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"github.com/jackc/pgx/v5"
)
func runMigration(ctx context.Context, db *pgx.Conn, migrationFile string) error {
fmt.Printf("MF: %s\n", migrationFile)
content, err := os.ReadFile(migrationFile)
if err != nil {
return fmt.Errorf("Failed to read migration file: %s", migrationFile)
}
_, err = db.Exec(ctx, string(content))
if err != nil {
return fmt.Errorf("Failed to execute migtation code: %s", err)
}
return nil
}
func Migrate(ctx context.Context, db *pgx.Conn) error {
workingDirectoryPath, err := os.Getwd()
if err != nil {
return fmt.Errorf("Failed to get current working directory in migrate tool")
}
migrationsPath := workingDirectoryPath + "/internal/migrations/"
files, err := os.ReadDir(migrationsPath)
if err != nil {
return fmt.Errorf("Failed to read migrations directory: %s", err)
}
for _, file := range files {
if filepath.Ext(file.Name()) == ".sql" {
migrationPath := filepath.Join(migrationsPath, file.Name())
fmt.Printf("Migration path: %s\n", migrationPath)
if err := runMigration(ctx, db, migrationPath); err != nil {
return err
}
log.Printf("Migration %s applied\n", file.Name())
}
}
return nil
}

36
main.go
View file

@ -2,12 +2,11 @@ package main
import (
"context"
// "fmt"
// "regexp"
// "strings"
// "t0xa/pdf_to_txt/internal/parser"
"log"
"github.com/jackc/pgx/v5"
"t0xa/pdf_to_txt/internal/migrator"
)
type CheckEntry struct {
@ -19,8 +18,7 @@ func main() {
// TODO: Redo format of file passing to function
// text := parser.ParseX5Check("./internal/parser/testfiles/test3.pdf")
//
// split := strings.Split(text, "\n")
// stringsToFilter := "https://mail|Gmail - Электронный чек|Сумма|НДС"
// split := strings.Split(text, "\n") stringsToFilter := "https://mail|Gmail - Электронный чек|Сумма|НДС"
// re := regexp.MustCompile(stringsToFilter)
// filtered := []string{}
// for _, element := range split {
@ -41,11 +39,33 @@ func main() {
// }
// }
ctx := context.Background()
// ctx := context.Background()
//
// conn, err := pgx.Connect(ctx, "postgres://ruprod_user:ruprod_password@localhost:5433/ruprod")
// if err != nil {
// panic(err)
// }
// defer conn.Close(ctx)
//
// queries := ruprod.New(conn)
// insertedProduct, err := queries.CreateProduct(ctx, ruprod.CreateProductParams{
// Name: "Сырок",
// Price: pgtype.Float8{Float64: 12.9, Valid: true},
// PurchaseDate: pgtype.Timestamp{Time: time.Now()},
// })
// if err != nil {
// panic(err)
// }
// fmt.Println(insertedProduct)
conn, err := pgx.Connect(ctx, "postgres://ruprod_user:ruprod_password@localhost:5433")
ctx := context.Background()
conn, err := pgx.Connect(ctx, "postgres://ruprod_user:ruprod_password@localhost:5433/ruprod")
if err != nil {
panic(err)
}
defer conn.Close(ctx)
if err := migrator.Migrate(ctx, conn); err != nil {
log.Fatalf("Migration failed: %s", err)
}
}