Search code examples
bashsqlitetestingbats-core

using savepoints in sqlite from command line


I am using a bash script (specifically, bats) to test some code I wrote.

Because I want to avoid interference in the tests, I am using setup and teardown functions (which bats provides) like this:

setup() {
  sqlite3 test.db "SAVEPOINT pre_test"
}

teardown() {
  sqlite3 test.db "ROLLBACK TO SAVEPOINT pre_test"
}

Unfortunately, when rolling back, I get:

Error: no such savepoint: pre_test

If, however, I start an interactive session in SQLite, I can use savepoints as I would expect. I guess that this means all savepoints are forgotten about after the command-line program exits.

Is there any way of working around this?


Solution

  • Given that the documentation refers to savepoints as similar to transactions my impression is that a savepoint does not persist beyond a single sqlite session.

    The documentation makes no note of this in particular, but the exhaustive explanation of how transactions are handled implies throughout the this ought all be contained in single session.

    Given that an inactive database can be trivially copied cp precious.db pre_test.db it seems that such would be the obvious way to effect the setup. Teardown is merely rm pretest.db.