Grinnz / Mojo-SQLite

Mojo::SQLite - A tiny Mojolicious wrapper for SQLite
https://metacpan.org/pod/Mojo::SQLite
Other
27 stars 12 forks source link

Better errors for failed migrations #10

Closed slobo closed 7 years ago

slobo commented 7 years ago

Currently, if there is an SQL error in a migration step, we get the error from DBD, but we can't point where it is in the migration file, eg:

schema.sql:

-- 1 up
CREATE TABLE blah (
  yada TEXT; -- woops
  );
-- 1 down
DROP TABLE blah;

app.pl:

# ...
my $sql = Mojo::SQLite->new('db.sqlite');
$sql->migrations->from_file('schema.sql')->migrate; # line 13

Error:

DBD::SQLite::db do failed: near ";": syntax error at ./app.pl line 13.
 at ./app.pl line 13.

Using MOJO_MIGRATIONS_DEBUG does show which SQL is being executed, but it would be useful to pinpoint the source of trouble, having something like this in the error:

schema.sql near line 3: near ";": syntax error

I don't know if DBD can even give us this info; I do know that sqlite3 -init schema.sql mydb will show line numbers, so hopefully this is available to the driver when sending queries too.

Is this functionality something you would be interested in? Looking at from_file (and from_string) it seems we would require a bit of surgery to save some metadata as migration file and strings are parsed in order to reconstruct exact source of trouble, but I don't mind giving it a go if it stands a chance of being accepted.

Thanks

Grinnz commented 7 years ago

When running migrations, the migration queries are passed to DBI as a single string, and any error message that is returned is already being passed on. I'm afraid I don't know of any way to get more detailed information in this instance. Storing string and file metadata does not sound worth the effort and cost, even if it was possible.