Skip to content
This repository was archived by the owner on Aug 27, 2025. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# wa-sqlite-build-env

Build environment for [livestorejs/wa-sqlite](https://github.com/livestorejs/wa-sqlite).
Build environment for [livestorejs/wa-sqlite](https://github.com/livestorejs/wa-sqlite) and SQLite utilities.

## Build
## Build wa-sqlite

```sh
cp -r wa-sqlite wa-sqlite-local
Expand All @@ -18,6 +18,29 @@ nix develop '.?submodules=1' --print-build-logs

NOTE: `.?submodules=1` is required since `wa-sqlite` is a submodule.

## Build sqldiff WASM

Build a WebAssembly version of SQLite's `sqldiff` utility with JavaScript bindings:

```sh
nix run .#build-sqldiff
```

This creates `sqldiff-wasm/dist/` with:
- `sqldiff.mjs` - ES6 module with WASM loader
- `sqldiff.wasm` - WebAssembly binary
- `README.md` - Usage instructions

### Testing sqldiff WASM

Run the comprehensive test suite:

```sh
node test-sqldiff-comprehensive.js
```

This tests all sqldiff features including basic diff, summary, schema-only, table-specific diffs, transaction wrapping, and help.

## Publish

```sh
Expand Down
25 changes: 25 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
waSQLiteSrc = "${self}/wa-sqlite";
};

# Build WASM version of sqldiff with JS bindings
sqldiff-wasm = pkgs.callPackage ./nix/sqldiff-wasm.nix {
inherit pkgsUnstable;
};

# wa-sqlite-livestore-esm = pkgs.callPackage ./packages/sqlite/nix/default.nix {
# wa-sqlite-livestore = self.packages.${system}.wa-sqlite-livestore;
# };
Expand Down Expand Up @@ -59,6 +64,25 @@
echo "✓ wa-sqlite build complete"
'');
};

build-sqldiff = {
type = "app";
program = toString (pkgs.writeShellScript "build-sqldiff" ''
set -euo pipefail

echo "Building sqldiff-wasm..."

pkg=$(nix build --no-link --print-out-paths .#sqldiff-wasm)

# Setup/update dist directory
mkdir -p sqldiff-wasm
rm -rf sqldiff-wasm/dist
echo "Copying built package from $pkg..."
cp -rf "$pkg/dist" sqldiff-wasm/dist
chmod -R u+w sqldiff-wasm/dist
echo "✓ sqldiff-wasm build complete"
'');
};
};

# Clean devShell without problematic interpolations
Expand All @@ -77,6 +101,7 @@
echo ""
echo "Available commands:"
echo " nix run .#build-wa-sqlite - Build wa-sqlite"
echo " nix run .#build-sqldiff - Build sqldiff-wasm"
'';

};
Expand Down
128 changes: 128 additions & 0 deletions nix/sqldiff-wasm.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{ lib, stdenv, fetchFromGitHub, fetchurl, pkgs, pkgsUnstable }:

stdenv.mkDerivation rec {
pname = "sqldiff-wasm";
version = "3.47.0";

src = fetchFromGitHub {
owner = "sqlite";
repo = "sqlite";
rev = "f5fb820c0f4781337faf02ed871be68d13a83d94";
sha256 = "sha256-35xrRPgoj92rji9EAyCHvhMP/NEz9hffOMJyhSKCCZ8=";
name = "sqlite-src";
};

# Disable the automatic update of GNU config scripts
dontUpdateAutotoolsGnuConfigScripts = true;

nativeBuildInputs = [
pkgs.which # needed for Make file
pkgs.tcl
pkgs.gcc
pkgs.wabt
pkgsUnstable.emscripten
pkgs.unzip
pkgs.openssl
pkgs.zip
];

configurePhase = ''
echo "Emscripten version:"
emcc --version

pwd
ls -la

# Configure SQLite with all features enabled
./configure --enable-all

# Make sqlite3.c amalgamation
make sqlite3.c

# Copy sqldiff.c from tool directory for building
cp tool/sqldiff.c .

# Copy required header files from ext/misc directory
cp ext/misc/sqlite3_stdio.h .
'';

buildPhase = ''
# Needed for `make`
export DESTDIR="$PWD"
export HOME="$PWD"

mkdir -p cache/emscripten
export EM_CACHE="$PWD/cache/emscripten"

# Ensure dist directory exists and has correct permissions
mkdir -p dist
chmod 755 dist

# Build WASM version of sqldiff
emcc -O2 \
-s WASM=1 \
-s ALLOW_MEMORY_GROWTH=1 \
-s ENVIRONMENT="web,worker,node" \
-s INVOKE_RUN=0 \
-s MODULARIZE=1 \
-s EXPORT_ES6=1 \
-s EXPORT_NAME="createSqlDiffModule" \
-s EXPORTED_FUNCTIONS='["_main", "_malloc", "_free"]' \
-s EXPORTED_RUNTIME_METHODS='["callMain", "FS", "PROXYFS", "MEMFS", "ccall", "cwrap"]' \
-s STACK_SIZE=512KB \
-s WASM_BIGINT=0 \
-DSQLITE_ENABLE_BYTECODE_VTAB \
-DSQLITE_ENABLE_SESSION \
-DSQLITE_ENABLE_PREUPDATE_HOOK \
sqldiff.c sqlite3.c \
-o dist/sqldiff.mjs

# Create a README with usage instructions
cat > dist/README.md << 'EOF'
# SqlDiff WASM

This package contains a WebAssembly build of SQLite's sqldiff utility.

## Files

- `sqldiff.mjs` - The main WASM module
- `sqldiff.wasm` - The WebAssembly binary

## Usage

```javascript
import createSqlDiffModule from './sqldiff.mjs';

async function main() {
const module = await createSqlDiffModule();

// Write test databases to virtual filesystem
module.FS.writeFile('/db1.sqlite', db1Bytes);
module.FS.writeFile('/db2.sqlite', db2Bytes);

// Run sqldiff
const result = module.callMain(['sqldiff', '/db1.sqlite', '/db2.sqlite']);

console.log('Exit code:', result);
}

main();
```

## Command Line Options

The WASM version supports the same options as the native sqldiff:

- `--changeset FILE` - Write binary changeset to FILE
- `--schema` - Show only schema differences
- `--summary` - Show summary of changes
- `--table TABLE` - Show differences for specific table
- `--transaction` - Wrap output in transaction
- `--vtab` - Handle virtual tables
EOF
'';

installPhase = ''
cp -r . $out
'';
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"private": true,
"version": "0.0.0",
"type": "module",
"packageManager": "[email protected]",
"devDependencies": {},
"scripts": {}
Expand Down
Loading