diff --git a/db/db.go b/db/db.go index e0b9ee0..23a7504 100644 --- a/db/db.go +++ b/db/db.go @@ -3,8 +3,11 @@ package db import ( "context" "log" + "path/filepath" + "strings" Config "micrach/config" + Files "micrach/files" "github.com/jackc/pgx/v4/pgxpool" ) @@ -23,5 +26,14 @@ func Init() { } func Migrate() { + migrations := Files.GetFullFilePathsInFolder("migrations") + log.Println(migrations) + for _, m := range migrations { + filename := filepath.Base(m) + splitted := strings.Split(filename, "-") + id, name := splitted[0], splitted[1] + log.Println(id, name) + log.Println(Files.ReadFileText(m)) + } log.Println("database migrations - online") } diff --git a/files/get_files_in_folder.go b/files/get_files_in_folder.go index 9321ee4..449f31e 100644 --- a/files/get_files_in_folder.go +++ b/files/get_files_in_folder.go @@ -4,25 +4,37 @@ import ( "io/ioutil" "log" "os" - "path/filepath" + "path" ) -func GetFilesInFolder(folder string) []string { +// Reads folder and returns full file paths slice +func GetFullFilePathsInFolder(folder string) []string { currentPath, err := os.Getwd() if err != nil { log.Fatal(err) } - files, err := ioutil.ReadDir(filepath.Join(currentPath + "/migrations")) + fullFolderPath := path.Join(currentPath, folder) + files, err := ioutil.ReadDir(fullFolderPath) if err != nil { log.Fatal(err) } - var filesNames []string + var paths []string for _, file := range files { - filesNames = append(filesNames, file.Name()) + paths = append(paths, path.Join(fullFolderPath, file.Name())) } - return filesNames + return paths +} + +// Reads file contents by full path and returns string +func ReadFileText(fullFilePath string) string { + file, err := ioutil.ReadFile(fullFilePath) + if err != nil { + log.Fatal(err) + } + + return string(file) }