This commit is contained in:
Yanislav Igonin 2021-11-14 12:14:10 +02:00
parent 39b93514a8
commit f812166fc8
2 changed files with 30 additions and 6 deletions

View File

@ -3,8 +3,11 @@ package db
import ( import (
"context" "context"
"log" "log"
"path/filepath"
"strings"
Config "micrach/config" Config "micrach/config"
Files "micrach/files"
"github.com/jackc/pgx/v4/pgxpool" "github.com/jackc/pgx/v4/pgxpool"
) )
@ -23,5 +26,14 @@ func Init() {
} }
func Migrate() { 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") log.Println("database migrations - online")
} }

View File

@ -4,25 +4,37 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "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() currentPath, err := os.Getwd()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
files, err := ioutil.ReadDir(filepath.Join(currentPath + "/migrations")) fullFolderPath := path.Join(currentPath, folder)
files, err := ioutil.ReadDir(fullFolderPath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
var filesNames []string var paths []string
for _, file := range files { 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)
} }