Guides
Switching databases
Move a backend from SQLite to Postgres or MySQL, and bring its data along.
Switching databases means changing which database server your contexts open, rewriting their migrations in that database’s SQL, and copying the data across. newt does the first part for you when you create a project; for an existing one you do it by hand, with a freshly generated project as your model.
What newt does and does not do
Section titled “What newt does and does not do”| Step | Who does it |
|---|---|
Write src/stores.ts for the new database |
You, copied from a new project |
| Rewrite the migrations in the new SQL | You |
| Create the new databases | You |
| Create the tables on the new databases | newt, when the backend starts |
| Copy the rows | You, with a script |
There is no newt command that switches a backend or moves data.
1. Generate a model project
Section titled “1. Generate a model project”In a scratch folder, make an empty project with the database you want and the same contexts:
newt init newthub-model --db postgrescd newthub-modelnewt add context tasksUse --db mysql for MySQL. You copy files from this project in the next steps, then delete it.
2. Change src/stores.ts
Section titled “2. Change src/stores.ts”Replace openDatabase and its imports with the model’s. From SQLite to Postgres:
import { MemoryDatabase } from '@newt-js/core/database/memory'import { PostgresDatabase } from '@newt-js/core/database/postgres'// --------------------------------------------------------import { requiredEnv } from './env'
export async function openDatabase( context: string, migrations: string, stores: Stores,): Promise<Database> { if (stores === 'memory') { return new MemoryDatabase() } return PostgresDatabase.open({ url: requiredEnv(`${envOf(context)}_DATABASE_URL`), migrations, })}Remove the SqliteDatabase import. Keep openStorage as it is: files do not change. If your project has no src/env.ts (where requiredEnv lives), copy it from the model too.
3. Change the environment
Section titled “3. Change the environment”Each context’s variable changes from a file path to a URL:
| Before (SQLite) | After (Postgres) |
|---|---|
TASKS_DATABASE=data/databases/tasks/tasks.sqlite |
TASKS_DATABASE_URL=postgres://... |
Update .env, .env.example and your production settings. Then create one database per context, as Postgres or MySQL explains.
4. Rewrite the migrations
Section titled “4. Rewrite the migrations”The new databases start empty, so you can rewrite each migration file in place, in the new SQL. Keep the file names and numbers.
The task table, side by side:
| Column | SQLite | Postgres | MySQL |
|---|---|---|---|
id |
TEXT |
TEXT COLLATE "C" |
VARCHAR(64) COLLATE utf8mb4_0900_bin |
version |
INTEGER |
INTEGER |
INTEGER |
name |
TEXT |
TEXT COLLATE "C" |
VARCHAR(255) COLLATE utf8mb4_0900_bin |
Other differences to watch for:
- Text: on Postgres, add
COLLATE "C"to every text column. On MySQL, useVARCHAR(n) COLLATE utf8mb4_0900_binand end the table withCHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin. newt refuses to start on text columns that would sort differently. - Yes/no: SQLite
BOOLEAN(stored as 0 or 1), Postgresboolean, MySQLBOOLEAN(TINYINT(1)). - JSON: SQLite a type starting with
JSON, Postgresjsonb, MySQLJSON. - Large whole numbers:
INTEGERholds them on SQLite; usebiginton Postgres andBIGINTon MySQL. - Quotes: SQLite and Postgres quote names with
"task", MySQL with backticks. ALTER TABLEdiffers a lot between the three. Check eachALTERagainst the new database’s manual.
5. Change the tests
Section titled “5. Change the tests”Module tests written on SQLite open SqliteDatabase with ':memory:', which would now run Postgres SQL on SQLite. Change them to the model’s shape:
-
Copy
src/test-database.tsfrom the model project. -
In each
<module>.module.test.ts, open the database withtestDatabaseand drop it after each test:src/contexts/tasks/modules/task/task.module.test.ts import { testDatabase } from '../../../../test-database'let dropDatabase: () => Promise<void>beforeEach(async () => {const { database, drop } = await testDatabase(join(import.meta.dir, '../../migrations'),)dropDatabase = dropcontext = testContext({name: 'tasks',cluster: new MemoryCluster(),database,})// ...})afterEach(async () => {await context.close()await dropDatabase()}) -
Run the tests against a real server, so the new migrations run:
Terminal window TEST_DATABASE_URL=postgres://postgres:postgres@localhost:5432/postgres \bun test
More in Testing.
6. Move the data
Section titled “6. Move the data”newt does not copy data between databases. A short Bun script does it, table by table.
-
Let the backend finish its pending jobs, then stop it. Nothing must write to the SQLite file while you copy.
-
Start the backend once on the new database and stop it. This creates the tables.
-
Copy your tables’ rows with a script like this one:
scripts/copy-tasks.ts import { Database } from 'bun:sqlite'import { SQL } from 'bun'const tables = ['project', 'task']const from = new Database('data/databases/tasks/tasks.sqlite', { readonly: true })const to = new SQL(Bun.env.TASKS_DATABASE_URL!)for (const table of tables) {const rows = from.query(`SELECT * FROM "${table}"`).all() as Record<string, unknown>[]for (const row of rows) {await to`INSERT INTO ${to(table)} ${to(row)}`}console.log(`${table}: ${rows.length} rows`)}from.close()await to.close()Terminal window bun scripts/copy-tasks.ts -
Repeat for each context, with its own file and URL.
-
Start the backend and check your data.
Notes on the script:
- Column names are the same on every database, so rows copy as they are.
- SQLite returns yes/no columns as
0or1. If a table has them, turn them intotrueorfalsebefore you insert into Postgres. - List tables in an order that respects
REFERENCES: a project before the tasks that point to it. - Do not copy newt’s own tables (
newt_migrations,newt_framework_migrations,newt_job,newt_job_queue,newt_schedule_tick,newt_idempotency_key). The new database has its own, and the backend fills them again.
Going the other way
Section titled “Going the other way”Moving from Postgres or MySQL to SQLite is the same in reverse: SqliteDatabase in src/stores.ts, <CONTEXT>_DATABASE file paths in .env, migrations in SQLite’s SQL, and module tests on SqliteDatabase.open({ path: ':memory:', migrations }) as SQLite shows. Delete src/test-database.ts.