Skip to content

Using alternative compilation of SQLite doesn't work #399

@fractaledmind

Description

@fractaledmind

I am trying to get a project-local, custom build of SQLite working with my Rails app. I have been studying and using the installation instructions, but I can't quite get things to work and I don't know what I'm doing wrong. I'm hoping someone here can help steer me in the right direction.

I have the following script which compiles a custom, optimized version of SQLite:

#!/usr/bin/env sh

cd ../vendor
mkdir -p sqlite/bin
mkdir -p sqlite/lib
mkdir -p sqlite/include

# ============================================================================================================
# Compile and install sqlite3 (for performance turning and build customizations)
# SEE: https://www.sqlite.org/compile.html
# NOTE: The sqlite3 Ruby gem will not work with the following compile time flags
#       * -DSQLITE_OMIT_DEPRECATED
# ============================================================================================================
curl --remote-name --remote-header-name https://www.sqlite.org/2023/sqlite-amalgamation-3430000.zip && unzip sqlite-amalgamation-3430000.zip

cd sqlite-amalgamation-3430000

PREPROCESSOR_FLAGS=(
  -DSQLITE_DEFAULT_MEMSTATUS=0
  -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1
  -DSQLITE_DQS=0
  -DSQLITE_ENABLE_FTS5
  -DSQLITE_LIKE_DOESNT_MATCH_BLOBS
  -DSQLITE_MAX_EXPR_DEPTH=0
  -DSQLITE_OMIT_PROGRESS_CALLBACK
  -DSQLITE_OMIT_SHARED_CACHE
  -DSQLITE_USE_ALLOCA
)

# compile the executable
gcc "${PREPROCESSOR_FLAGS[@]}" shell.c sqlite3.c -lpthread -ldl -lm -o ../sqlite/bin/sqlite3

# compile and setup shared library
gcc "${PREPROCESSOR_FLAGS[@]}" shell.c sqlite3.c -lpthread -ldl -lm -fPIC -shared -o ../sqlite/lib/libsqlite3.so && \
chmod 755 ../sqlite/lib/libsqlite3.so && \
ln -s ../sqlite/lib/libsqlite3.so ../sqlite/lib/libsqlite3.so.0

# copy header/include files
mkdir -p ../sqlite/include/sqlite3 && \
cp sqlite3.h ../sqlite/include/ && \
cp sqlite3ext.h ../sqlite/include/

# cleanup
PREPROCESSOR_FLAGS=""

This creates a vendor/sqlite directory in my Rails app with this structure:

sqlite
├── bin
│   └── sqlite3
├── include
│   ├── sqlite3.h
│   └── sqlite3ext.h
└── lib
    ├── libsqlite3.so
    └── libsqlite3.so.0 -> ../sqlite/lib/libsqlite3.so

4 directories, 5 files

I setup the Bundle config like so:

bundle config set build.sqlite3 --enable-system-libraries --with-opt-dir=./vendor/sqlite

which creates a .bundle/config file like so:

---
BUNDLE_BUILD__SQLITE3: "--enable-system-libraries --with-opt-dir=./vendor/sqlite"

In my Gemfile, I specify the dependency on SQLite like so:

gem "sqlite3", force_ruby_platform: true

However, whenever I use bin/rails c to start a console and run ActiveRecord::Base.connection.execute 'PRAGMA compile_options', I see the compile_options of my system SQLite installation at /usr/bin/sqlite3.

When I manually enter the compiled SQLite prompt via vendor/sqlite/bin/sqlite3 and run PRAGMA compile_options, I see the specific compile options that I set.


So, how can I get the SQLite installation that lives in vendor/sqlite to be the one that the gem uses in my Rails app?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions