#Is anyone able to help me with running a set of commands in a .sql file within a Go SDK please?

1 messages · Page 1 of 1 (latest)

naive harness
#

I have the following code:

func main() {
    ctx := context.Background()
    client, err := dagger.Connect(ctx)
    if err != nil {
        panic(err)
    }
    defer client.Close()

    fn := "commands.sql"

    // Create the "commands.sql" file.
    f, err := os.Create(fn)
    if err != nil {
        panic(err)
    }

    _, err = f.WriteString("SELECT name FROM sys.databases")
    if err != nil {
        panic(err)
    }

    err = f.Close()
    if err != nil {
        panic(err)
    }

    output, err := client.Pipeline("test").
        Container().
        From("fabiang/sqlcmd").
        WithExec([]string{"-S", "ip_address", "-U", "sa", "-P", "***", "-C", "-N", "-i", "" + fn + ""}).
        Stdout(ctx)

    if err != nil {
        panic(err)
    }
    fmt.Println(output[:300])

...and when I run it, I'm getting the following error:

Stderr:
Sqlcmd: 'commands.sql': Invalid filename.
limber osprey
#

this is SQL server, correct?

jade vector
#

Are you mounting the commands.sql to the container somewhere? In the code above, it looks like its created on the host but then not put into the container

naive harness
limber osprey
#

@naive harness this works:

package main

import (
    "context"
    "fmt"
    "os"
    "time"

    "dagger.io/dagger"
)

func main() {
    ctx := context.Background()
    client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
    if err != nil {
        panic(err)
    }
    defer client.Close()

    output, err := client.Pipeline("test").
        Container().
        WithEnvVariable("CACHE", time.Now().String()).
        From("fabiang/sqlcmd").
        WithNewFile("/commands.sql", dagger.ContainerWithNewFileOpts{
            Contents: "select * from sys.databases",
        }).
        WithExec([]string{"-S", "172.18.0.1", "-U", "sa", "-P", "Passw0rd!", "-C", "-N", "-i", "/commands.sql"}).
        Stdout(ctx)
    if err != nil {
        panic(err)
    }
    fmt.Println(output[:300])
}
jade vector
#

And to mount a file from host instead:

...From...
.WithMountedFile("/commands.sql", client.Host().Directory(".").File("commands.sql")
...WithExec...
naive harness
#

What does WithEnvVariable("CACHE", time.Now().String()). do?

jade vector
#

It sets a new environment variable each time you run, meaning dagger will never see the run as cached and always execute the queries.

naive harness
#

....and, How would I be able to adjust the code to add multiple sql commands into the command.sql file?

limber osprey
naive harness
#

ah, I see with `` thanks

limber osprey