#Cant call a method ( function with interface ) from normal function

21 messages · Page 1 of 1 (latest)

glossy igloo
#

Im trying to make a simple functionality that will run migrations for me, but now i cant call a method from a normal function and thats giving me a lot of trouble, since the functions are being called all over the file if i add the interface to each one of the functions i would have to add the interface to like 10 function ( also doesnt seem reasonable ). Are any better way i could solve it?

type MigrationBridge struct {
    store Storage
}
func readMigrationFile(cmd string) {
    filename, err := getMigrationsLastFile()
    filename = "./gosql/migrations/" + filename
    if err != nil {
        fmt.Println(FmtRed("Error trying to GET the migration file"), err)
        return
    }

    data, err := os.ReadFile(filename)
    if err != nil {
        fmt.Println(FmtRed("Error trying to READ the migration file"), err)
        return
    }
    dat := string(data)
    fileByLines := getFileByLines(dat)
    if err := validateFileLines(fileByLines); err != nil {
        fmt.Println(err)
        return
    }
        <------------ PROBLEM -------------->
    runMigration(fileByLines, cmd)    
}
#
func (mg *MigrationBridge) runMigration(data []string, m string) {

    numLine, err := getCmdsLines(data)
    if err != nil {
        fmt.Println(err)
    }
    if len(numLine) > 2 {
        return
    }

    upMigration := strings.Join(data[numLine[0]+1:numLine[1]-1], "")
    downMigration := strings.Join(data[numLine[1]+1:], "")

    if m == "up" {
        ch := &MigrationBody{
            choice: m,
            query:  upMigration,
        }
        err := mg.store.RunMigration(ch)
        if err != nil {
            errors.New(FmtRed("Error trying to run up migration => ") + err.Error())
        }
    }

    if m == "down" {
        ch := &MigrationBody{
            choice: m,
            query:  downMigration,
        }
        err := mg.store.RunMigration(ch)
        if err != nil {
            errors.New(FmtRed("Error trying to run up migration => ") + err.Error())
        }
    }
}

---storage.go----
type Storage interface {
    RunMigration(*MigrationBody) error
}
func (s *PostgresStore) RunMigration(m *MigrationBody) error {
    if m.choice != "up" || m.choice != "down" {
        return errors.New(FmtRed("Not valid migration choice => ") + m.choice)
    }

    _, err := s.db.Exec(m.query)
    if err != nil {
        errors.New(FmtRed("Error trying to run query => ") + err.Error())
        return err
    }
    fmt.Println(FmtGreen("Migration done!"))
    return nil
}
glossy igloo
#

up

glossy igloo
#

Also this is my MigrationBody struct

type MigrationBody struct {
    choice string
    query  string
}
distant relic
#

@glossy igloo what is the problem ?

#

can you copy paste your error message ?

glossy igloo
#

it isnt finding my function runMigration, althought its in the same file and in the same package ( main )

glossy igloo
#

Yeah yeah

distant relic
#

you put (mg *MigrationBridge) so you need to run it on an MigrationBridge

glossy igloo
#

The problem is if i implement (mg *MigrationBridge) in my readMigrationFile i will have to implement it in other 10 more function. Any other way i could solve it?

distant relic
#

I don't understand what you want

glossy igloo
distant relic
glossy igloo
#

So this is where im at, some of my tries:

var st *PostgresStore
    mg := MigrationBridge{store: &PostgresStore{
        db: st.db,
    }}
    mg.runMigration(fileByLines, cmd)

and

var st *PostgresStore = &PostgresStore{}
    mg := MigrationBridge{store: &PostgresStore{
        db: st.db,
    }}
    mg.runMigration(fileByLines, cmd)

None of them seems to work, im getting

[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4e0a32]```
distant relic
#

your code suggest you want to connect to a real postgress database and run sql code on it which perform migrations

#

var st *PostgresStore creates nothing, it's just a nil variable and doesn't actually point to anything
&PostgresStore{} creates a postgres store, but it does not connect it to anything

#

Most likely you need to either try searching for an already instranciated postgres connection or create a new postgress connection

glossy igloo
#

the thing is, to get the value from it i will have to implement ( s *PostgresStore ) in my function readMigrationFile right? If i do that i will have trouble in a bunch of other functions
maybe i will have to do that