From c6e73dd360894a212804e4b6a20bee3a45243c6e Mon Sep 17 00:00:00 2001 From: pro100ton Date: Thu, 7 Nov 2024 21:22:20 +0300 Subject: [PATCH] Add migration tool to project --- .../migrations/001_create_products_table.sql | 6 +++ internal/migrator/db.go | 48 +++++++++++++++++++ main.go | 36 ++++++++++---- 3 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 internal/migrations/001_create_products_table.sql create mode 100644 internal/migrator/db.go diff --git a/internal/migrations/001_create_products_table.sql b/internal/migrations/001_create_products_table.sql new file mode 100644 index 0000000..7b6da1c --- /dev/null +++ b/internal/migrations/001_create_products_table.sql @@ -0,0 +1,6 @@ +CREATE TABLE product ( + id BIGSERIAL PRIMARY KEY, + name text NOT NULL, + price float, + purchase_date timestamp +); diff --git a/internal/migrator/db.go b/internal/migrator/db.go new file mode 100644 index 0000000..a66a60e --- /dev/null +++ b/internal/migrator/db.go @@ -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 +} diff --git a/main.go b/main.go index d2bc17a..addefec 100644 --- a/main.go +++ b/main.go @@ -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) + } }