FAQ
I want to execute following shell command from go:
mysql -uroot -ppassword --database some < some.sql
I think I got implementation, but it doesn't work on large db sizes (6Mb
for example). When you have couple records in dump, works just fine.

Here is code:
https://github.com/romanoff/dk/blob/master/source/mysql.go#L56

Can anybody help me with this?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Tamás Gulácsi at May 10, 2014 at 4:12 am
    1. CHECK RETURNED ERRORS!!!
    2. Do not create a string and then split - what if a parameter have a space? (Filename password)
    3. Do not read the file into sqlCommands then create a pipe and write into a buffer and then into the pipe.... This may work but totally unneeded.
    Cmd.Stdin is an io.Reader, so provide one: bytes.NewReader(sqlCommands) or just the *os.File what os.Open returns.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Andrew Romanov at May 10, 2014 at 6:45 am
    Thanks for your response. It really helped. Have couple of questions though:
    1. Regarding errors - what do you mean by that? In my case I know that if
    file could not be found or mysqldump didn't work, then operation can't be
    completed. Error has to be shown and program interrupted. (I would agree
    that it was probably possible to provide more meaningful error messages)
    P.S. If you are referencing main.go line 18: config, _ = ReadConfig(), then
    this was made intentionally. Some commands do not require config to be
    parsed. So, if it errored while reading config, that's fine. There are
    additional checks (CheckConfig function). But probably it could be made
    differently (reading config only if you need it)
    2. I know about this issue. This was just quick and dirty way to make this
    work
    3. Thanks. This worked. Still not aware why my previous implementation
    didn't work though (actually it worked, but only on small sql dumps). Any
    thoughts on that?
    On Friday, May 9, 2014 9:11:58 PM UTC-7, Tamás Gulácsi wrote:

    1. CHECK RETURNED ERRORS!!!
    2. Do not create a string and then split - what if a parameter have a
    space? (Filename password)
    3. Do not read the file into sqlCommands then create a pipe and write into
    a buffer and then into the pipe.... This may work but totally unneeded.
    Cmd.Stdin is an io.Reader, so provide one: bytes.NewReader(sqlCommands) or
    just the *os.File what os.Open returns.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Gyepi SAM at May 10, 2014 at 8:36 am

    On Fri, May 09, 2014 at 11:45:54PM -0700, Andrew Romanov wrote:
    3. Thanks. This worked. Still not aware why my previous implementation
    didn't work though (actually it worked, but only on small sql dumps). Any
    thoughts on that?
    I can't see how the size of the dump should affect the program. I suspect the
    error is due to how you were constructing the dump command, which was error
    prone and suboptimal.

    Here's an improved and more efficient version:

         http://play.golang.org/p/Af2caw-C_z

    Note that I changed the parameter name to avoid a conflict with the path
    package name and to better reflect usage.

    -Gyepi

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Gyepi SAM at May 10, 2014 at 8:40 am

    On Sat, May 10, 2014 at 10:36:04AM +0200, Gyepi SAM wrote:
    On Fri, May 09, 2014 at 11:45:54PM -0700, Andrew Romanov wrote:
    3. Thanks. This worked. Still not aware why my previous implementation
    didn't work though (actually it worked, but only on small sql dumps). Any
    thoughts on that?
    I can't see how the size of the dump should affect the program. I suspect the
    error is due to how you were constructing the dump command, which was error
    prone and suboptimal.

    Here's an improved and more efficient version:

    http://play.golang.org/p/Af2caw-C_z
    Slightly improved, with a required parameter check.

         http://play.golang.org/p/yCg95jz3EN

    -Gyepi

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Tamás Gulácsi at May 10, 2014 at 9:06 am
    2014. május 10., szombat 8:45:54 UTC+2 időpontban Andrew Romanov a
    következőt írta:
    Thanks for your response. It really helped. Have couple of questions
    though:
    1. Regarding errors - what do you mean by that? In my case I know that if
    file could not be found or mysqldump didn't work, then operation can't be
    completed. Error has to be shown and program interrupted. (I would agree
    that it was probably possible to provide more meaningful error messages)
    P.S. If you are referencing main.go line 18: config, _ = ReadConfig(),
    then this was made intentionally. Some commands do not require config to be
    parsed. So, if it errored while reading config, that's fine. There are
    additional checks (CheckConfig function). But probably it could be made
    differently (reading config only if you need it)
    2. I know about this issue. This was just quick and dirty way to make this
    work
    3. Thanks. This worked. Still not aware why my previous implementation
    didn't work though (actually it worked, but only on small sql dumps). Any
    thoughts on that?
    1. using github.com/kisielk/errcheck:
    $ errcheck github.com/romanoff/dk github.com/romanoff/dk/source
    /home/tgulacsi/src/github.com/romanoff/dk/config.go:14:18
    ioutil.WriteFile(".dk", []byte{}, perm)
    /home/tgulacsi/src/github.com/romanoff/dk/dump.go:29:15 os.MkdirAll(path,
    perm)
    /home/tgulacsi/src/github.com/romanoff/dk/source/mysql.go:35:18
    ioutil.WriteFile(path+"/dump.sql", output, perm)

    But checking the yesterday's code:
    $ cd $GOPATH/src/github.com/romanoff/dk
    $ git checkout -b old 8a7036f2ee2733ebf80848bd42cf6ac44a7b4d07
    $ errcheck ./...
    config.go:14:18 ioutil.WriteFile(".dk", []byte{}, perm)
    dump.go:29:15 os.MkdirAll(path, perm)
    source/mysql.go:36:18 ioutil.WriteFile(path+"/dump.sql", output, perm)
    source/mysql.go:65:9 b.Write(sqlContent)
    source/mysql.go:66:11 b.WriteTo(stdin)

    See what I mean? (esp. mysql.go line 66)

    It's understood that your program cannot continue, but we have no
    exceptions (thank Go Authors!), so if you don't check the WriteTo(stdin)
    error, than you won't notice WHY you don't have all commands processed.

    3. Yes. Checkout that old version, add error checking, and log that errors
    to see what is happening.
    But I'd bet on that you use cmd.Run which waits for completion, and WriteTo
    just can write as much data as the kernel's pipe buffers.
    You should've used cmd.Start(); WriteTo(stdin); cmd.Wait(); in this order
    (and check all errors).

    On Friday, May 9, 2014 9:11:58 PM UTC-7, Tamás Gulácsi wrote:

    1. CHECK RETURNED ERRORS!!!
    2. Do not create a string and then split - what if a parameter have a
    space? (Filename password)
    3. Do not read the file into sqlCommands then create a pipe and write
    into a buffer and then into the pipe.... This may work but totally unneeded.
    Cmd.Stdin is an io.Reader, so provide one: bytes.NewReader(sqlCommands)
    or just the *os.File what os.Open returns.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 10, '14 at 3:54a
activeMay 10, '14 at 9:06a
posts6
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase