From 3e0f98a75af4e7e38d646a37e7174b55cd5f59b3 Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Wed, 22 Nov 2023 17:23:01 +0100 Subject: [PATCH] sqldb: introduce replace for the BIGINT type alias --- sqldb/postgres.go | 3 ++- sqldb/sqlite.go | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/sqldb/postgres.go b/sqldb/postgres.go index 3cfa59efc..9aac2a489 100644 --- a/sqldb/postgres.go +++ b/sqldb/postgres.go @@ -89,7 +89,7 @@ func NewPostgresStore(cfg *PostgresConfig) (*PostgresStore, error) { // system. // // First, we'll need to open up a new migration instance for - // our current target database: sqlite. + // our current target database: Postgres. driver, err := postgres_migrate.WithInstance( rawDB, &postgres_migrate.Config{}, ) @@ -100,6 +100,7 @@ func NewPostgresStore(cfg *PostgresConfig) (*PostgresStore, error) { postgresFS := newReplacerFS(sqlSchemas, map[string]string{ "BLOB": "BYTEA", "INTEGER PRIMARY KEY": "SERIAL PRIMARY KEY", + "BIGINT PRIMARY KEY": "BIGSERIAL PRIMARY KEY", "TIMESTAMP": "TIMESTAMP WITHOUT TIME ZONE", }) diff --git a/sqldb/sqlite.go b/sqldb/sqlite.go index 8ecadd68d..4b9895999 100644 --- a/sqldb/sqlite.go +++ b/sqldb/sqlite.go @@ -133,8 +133,16 @@ func NewSqliteStore(cfg *SqliteConfig) (*SqliteStore, error) { return nil, err } + // We use INTEGER PRIMARY KEY for sqlite, because it acts as a + // ROWID alias which is 8 bytes big and also autoincrements. + // It's important to use the ROWID as a primary key because the + // key look ups are almost twice as fast + sqliteFS := newReplacerFS(sqlSchemas, map[string]string{ + "BIGINT PRIMARY KEY": "INTEGER PRIMARY KEY", + }) + err = applyMigrations( - sqlSchemas, driver, "sqlc/migrations", "sqlc", + sqliteFS, driver, "sqlc/migrations", "sqlc", ) if err != nil { return nil, err