libsql-client-ts icon indicating copy to clipboard operation
libsql-client-ts copied to clipboard

Add option to wait for schema changes to `batch`

Open giovannibenussi opened this issue 3 months ago • 0 comments

This PR adds an option to batch to allow to wait until all schema changes are done:

await schemaClient.batch(["ALTER TABLE users ADD COLUMN test_column text"], {
  wait: true,
});

Currently, batch supports a transaction mode parameter, so I keep support for it but also added a new object parameter that supports a transactionMode property. The code below is equivalent:

// Previous API, still supported
await schemaClient.batch(["ALTER TABLE users ADD COLUMN test_column_2 number"], " write")

// New API
await schemaClient.batch(["ALTER TABLE users ADD COLUMN test_column_2 number"], {
  transactionMode: "write"
});

// You can mix transaction mode and the new wait option
await schemaClient.batch(["ALTER TABLE users ADD COLUMN test_column_2 number"], {
  transactionMode: "write",
  wait: true,
});

You can test these changes locally with this command:

cd packages/libsql-core && npm run build && cd - && cd packages/libsql-client && npm run build && cd - && node test.ts

It builds and run a test using the new wait parameter while I add automatic tests. Please note that the API returns frequent 500 errors which are being investigated internally.

If everything works as expected, you should see an output similar to the one below.

Waiting for migration jobs
json: {
  schema_version: 4,
  migrations: [
    { job_id: 2, status: 'RunSuccess' },
    { job_id: 1, status: 'RunSuccess' }
  ]
}
lastMigrationJob: { job_id: 2, status: 'RunSuccess' }
Finished waiting for migration jobs

giovannibenussi avatar May 15 '24 11:05 giovannibenussi