Skip to content

Bun SQLite multi statement execution #5930

@brandonstubbs

Description

@brandonstubbs

What version of Effect is running?

[email protected]

What steps can reproduce the bug?

import { FileSystem } from "@effect/platform"
import { BunContext } from "@effect/platform-bun"
import { SqlClient } from "@effect/sql"
import { PgClient } from "@effect/sql-pg"
import { SqliteClient } from "@effect/sql-sqlite-bun"
import { Config, Effect, Layer } from "effect"

const program = Effect.gen(function*() {
  const sql = yield* SqlClient.SqlClient
  yield* sql`
    CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, name TEXT);
    CREATE TABLE IF NOT EXISTS test2 (id INTEGER PRIMARY KEY, name TEXT);
  `

  const tables = yield* sql.onDialectOrElse({
    sqlite: () => sql`SELECT * FROM sqlite_master`,
    pg: () => sql`SELECT * FROM pg_catalog.pg_tables WHERE schemaname NOT IN ('pg_catalog', 'information_schema')`,
    orElse: () => Effect.succeed(undefined)
  })
  console.log(tables)
})

const PGTest = Layer.unwrapEffect(Effect.gen(function*() {
  return PgClient.layer({
    host: yield* Config.string("DB_HOST"),
    database: yield* Config.string("DB_NAME"),
    username: yield* Config.string("DB_USER"),
    password: yield* Config.redacted("DB_PASSWORD")
  })
}))

const SqliteTest = Layer.unwrapEffect(Effect.gen(function*() {
  const fs = yield* FileSystem.FileSystem
  const dir = yield* fs.makeTempDirectoryScoped()
  return SqliteClient.layer(
    {
      filename: dir + "/test.db"
    }
  )
})).pipe(
  Layer.provideMerge(BunContext.layer)
)

Effect.runPromise(program.pipe(
  // Effect.provide(SqliteTest), // does not work (only creates first table)
  Effect.provide(PGTest), // works
  Effect.scoped
))

What is the expected behavior?

When you run this sample program, two tables (test, test2) should be created.
sql-sqlite-bun should ideally also support multi statements.

What do you see instead?

sql-sqlite-bun silently fails. It runs the first statement, ignoring the rest.

Additional information

  • Run the program with Effect.provide(PGTest) - you will see on the postgres implementation multi statements work and two tables are created.
  • Run the program with Effect.provide(SqliteTest) - the program completes successfully, however only a single table is created.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions