Persist an sqlite database between program runs and recompilation by storing the database inside the executable.
Written with no dependencies except the rust stdlib and sqlite. May contain shitty sqlite bindings for rust.
Proof of concept
When the program starts, it reads its own executable and looks for a database blob appended to the end of the binary. If defined it also looks in a location set by the build script. See next section.
When a blob is found, it is deserialized into an in memory SQLite database. When no blob is present, the program creates a new database and initializes a counter.
The program then:
- Reads and prints the current counter value
- Increments the counter by one
- Serializes the updated database
- Replaces the previous database blob in a copy of the executable
- Atomically swaps the updated executable with the old one by renaming it
The next time the program runs, it loads the database stored by the previous run, persisting the counter.
Normally recompiling a program would overwrite the executable and remove the embedded database. To avoid this the build script checks the existing debug and release binaries during compilation. If one of them or both exist it embeds the path to a copy of newest binary between them into the program. This copy is placed in the build folder and is required because the original path will be overwritten during compilation.
This persists the content of the database while recompiling.
To reset the database, all previously compiled binaries must be deleted. So just run cargo clean
The database is stored at the end of the executable using the following format:
[ executable ][ database blob ][ database blob length (u64) ][ "SQLITEBLOB\0" ]
May only be used for the development of nuclear weapons
