Load SQLite3 database from virtual filesystem (afero)

341 Views Asked by At

Setting:

1) SQLite3 database file gets uploaded to a fileserver

2) Files get stored into virtual filesystem

3) I want to execute sql queries on that database

Currently I am using go-sqlite3 with a database file located in afero.


Question:

Is there are possbility to load a database file into go-sqlite3 with another source than a file from the os filesystem.


Current approach:

My current solution is to copy the file from afero into the os.TempDir() directory, but that fails with our ci and is no actual solution as it does not use the dedicated filesystem anymore.

1

There are 1 best solutions below

4
On

After a little source diving I see this in sqlite3.go

name := C.CString(dsn)
defer C.free(unsafe.Pointer(name))
rv := C._sqlite3_open_v2(name, &db,
    mutex|C.SQLITE_OPEN_READWRITE|C.SQLITE_OPEN_CREATE,
     nil)

So this answers your question, the actual opening of the database happens "outside" Go and "inside" the sqlite3 library. If we check the function sqlite3_open_v2 we can see that it requires a file name:

int sqlite3_open_v2(
  const char *filename,   /* Database filename (UTF-8) */
  sqlite3 **ppDb,         /* OUT: SQLite db handle */
  int flags,              /* Flags */
  const char *zVfs        /* Name of VFS module to use */
);

I think the only way here is to implement your own Sqlite VFS that somehow interacts with the afero abstractions.