Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exec method doesn't support multiple statements (separated by semi-colon) #22

Open
JayJamieson opened this issue Apr 2, 2024 · 6 comments

Comments

@JayJamieson
Copy link

Exec method appears to not support multi line statement queries, it executes up until first ; then completes.

Minimal code required to reproduce.

db, err := sql.Open("libsql", url)
if err != nil {
  return nil, err
}
defer db.Close()

db.Exec(`create table foo (id integer primary key);
create table bar (id integer primary key);`)

My expectation would be the same behavior as other sqlite3 drivers mattn sqlite3 and modernc where multi statement queries are executed fully when using Exec.

My current work around is at the template level to inject a separator symbol that I can split on and run Exec in a loop. This however isn't great for larger SQL scripts.

@imajacket
Copy link

imajacket commented Jul 4, 2024

For me, multiple statements work for embedded replicas & syncing up to Turso, but do not work for a regular file:.db

@zedutch
Copy link

zedutch commented Sep 16, 2024

I have the same issue when running migrations through goose with go-libsql as the driver. Only the first statement from each migration file is actually ran, without any errors indicating something went wrong

@codingpop
Copy link

I have the same issue when running migrations through goose with go-libsql as the driver. Only the first statement from each migration file is actually ran, without any errors indicating something went wrong

I ran into this with Goose. But I fixed it by removing the -- +goose StatementBegin and -- +goose StatementEnd comments from the migration file. You can find further information at the No transaction section of this page: http://pressly.github.io/goose/documentation/annotations/#example

@Sillyvan
Copy link

Yea we just ran into this and this is kind of a big deal because there is no batch methods either

@tizu69
Copy link

tizu69 commented Jan 17, 2025

if your statement is simple enough, you can simply do something akin to

for _, stmt := range strings.Split(schema, ";") {
	db.MustExec(stmt)
}

Note that this won't work in some edge cases, like when youre creating tables with a col with default text that contains ;. easiest one-liner workaround in that case would be to use a separator like <;>, and then replacing all real ; with <;> in the schema.

@tizu69
Copy link

tizu69 commented Jan 17, 2025

I have also noted that mustexec doesn't actually seem to complain if it finds a ; denoting a new statement (and thus not actually executing the full thing).

If this is a real issue, can someone create an issue for it?
If this issue would be fixed alongside exec not supporting multiple statements, I'd say it's fine if it stays in this comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants