Add migration tool to project
This commit is contained in:
parent
0608cbaa09
commit
c6e73dd360
3 changed files with 82 additions and 8 deletions
6
internal/migrations/001_create_products_table.sql
Normal file
6
internal/migrations/001_create_products_table.sql
Normal 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
48
internal/migrator/db.go
Normal 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
36
main.go
|
@ -2,12 +2,11 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
// "fmt"
|
"log"
|
||||||
// "regexp"
|
|
||||||
// "strings"
|
|
||||||
// "t0xa/pdf_to_txt/internal/parser"
|
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
|
|
||||||
|
"t0xa/pdf_to_txt/internal/migrator"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CheckEntry struct {
|
type CheckEntry struct {
|
||||||
|
@ -19,8 +18,7 @@ func main() {
|
||||||
// TODO: Redo format of file passing to function
|
// TODO: Redo format of file passing to function
|
||||||
// text := parser.ParseX5Check("./internal/parser/testfiles/test3.pdf")
|
// text := parser.ParseX5Check("./internal/parser/testfiles/test3.pdf")
|
||||||
//
|
//
|
||||||
// split := strings.Split(text, "\n")
|
// split := strings.Split(text, "\n") stringsToFilter := "https://mail|Gmail - Электронный чек|Сумма|НДС"
|
||||||
// stringsToFilter := "https://mail|Gmail - Электронный чек|Сумма|НДС"
|
|
||||||
// re := regexp.MustCompile(stringsToFilter)
|
// re := regexp.MustCompile(stringsToFilter)
|
||||||
// filtered := []string{}
|
// filtered := []string{}
|
||||||
// for _, element := range split {
|
// 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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer conn.Close(ctx)
|
defer conn.Close(ctx)
|
||||||
|
|
||||||
|
if err := migrator.Migrate(ctx, conn); err != nil {
|
||||||
|
log.Fatalf("Migration failed: %s", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue