Skip to content

Navigation Menu

Sign in
Sign up

imapsql: SQLite to PostgreSQL migration #837

foxcpp started this conversation in Show and tell
Discussion options

Not adding this to maddy.email documentation since this is not really officially supported (yet). This involves some careful regex editing in the dump file.

  1. Dump SQLite DB to SQL.
sqlite3 /var/lib/maddy/imapsql.db
> .output dump.sql
> .dump
  1. Adjust schema types to match PostgreSQL.

INTEGER ... AUTOINCREMENT should be BIGSERIAL in PostgreSQL.

Open SQL dump with text editor.
2.1. Remove PRAGMA foreign_keys=OFF.
2.2. Update users.id definition:

CREATE TABLE users (
 id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
 username VARCHAR(255) NOT NULL UNIQUE,

should become

CREATE TABLE users (
 id BIGSERIAL NOT NULL PRIMARY KEY,
 username VARCHAR(255) NOT NULL UNIQUE,

2.3. Do the same for CREATE TABLE mboxes (mboxes.id need fixing)
2.4. LONGTEXT type should be BYTEA. Find&Replace will do just fine.

  1. Update blob format in dump to match PostgreSQL

SQLite writes hex-encoded blobs as X'data', PostgreSQL uses '\xdata'.

This works fine:

sed -i "s/,X'/,'\\\\x/g" ~/storage.sql
  1. Remove anything that mentions sqlite_schema, sqlite_stat1. These are SQLite internals and are not relevant.

  2. Convert sqlite_sequence into PostgreSQL sequences.

Somewhere near the end of dump, you will find this:

DELETE FROM sqlite_sequence;
INSERT INTO sqlite_sequence VALUES('users',4);
INSERT INTO sqlite_sequence VALUES('mboxes',24);

You should replace these with corresponding ALTER SEQUENCE commands, like this:

ALTER SEQUENCE mboxes_id_seq RESTART WITH 24;
ALTER SEQUENCE users_id_seq RESTART WITH 4;
  1. Load dump into PostgreSQL.
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /