Skip to content

Commit

Permalink
Add PGlite version to VFS file
Browse files Browse the repository at this point in the history
Related to electric-sql#369
  • Loading branch information
vs4vijay committed Oct 11, 2024
1 parent 0c7a627 commit 9e5ae26
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
13 changes: 13 additions & 0 deletions packages/pglite/src/pglite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ export class PGlite
for (const initFn of extensionInitFns) {
await initFn()
}

// Write the PGlite version to a file
await this.writePGliteVersionFile()
}

/**
Expand Down Expand Up @@ -763,4 +766,14 @@ export class PGlite
_runExclusiveTransaction<T>(fn: () => Promise<T>): Promise<T> {
return this.#transactionMutex.runExclusive(fn)
}

/**
* Write the PGlite version to a file
*/
async writePGliteVersionFile() {
const version = await this.query<{ version: string }>('SELECT version()')
const versionString = `Created by: ${version.rows[0].version}`
const filePath = `${PGDATA}/PGLITE_VERSION`
this.mod!.FS.writeFile(filePath, versionString)
}
}
17 changes: 16 additions & 1 deletion packages/pglite/src/postgresMod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,19 @@ type PostgresFactory<T extends PostgresMod = PostgresMod> = (
moduleOverrides?: Partial<T>,
) => Promise<T>

export default PostgresModFactory as PostgresFactory<PostgresMod>
const PGLITE_VERSION = 'v0.2.11'

const PostgresModFactoryWithVersion: PostgresFactory<PostgresMod> = async (
moduleOverrides = {},
) => {
const mod = await PostgresModFactory(moduleOverrides)
mod.preRun.push((mod) => {
mod.FS.writeFile(
'/confdefs.h',
`#define PG_VERSION_STR "PostgreSQL $PG_VERSION (PGlite ${PGLITE_VERSION}) on $host, compiled by $cc_string, \`expr $ac_cv_sizeof_void_p * 8\`-bit"`
)
})
return mod
}

export default PostgresModFactoryWithVersion
28 changes: 28 additions & 0 deletions packages/pglite/tests/version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { PGlite } from '../src/pglite';
import { describe, it, expect } from 'vitest';
import { promises as fs } from 'fs';

describe('PGlite version reporting', () => {
let pg: PGlite;

beforeAll(async () => {
pg = await PGlite.create({ dataDir: 'memory://test' });
});

afterAll(async () => {
await pg.close();
});

it('should create PGLITE_VERSION file with correct content', async () => {
const versionFilePath = '/tmp/pglite/base/PGLITE_VERSION';
const versionFileContent = await fs.readFile(versionFilePath, 'utf8');
const version = await pg.query<{ version: string }>('SELECT version()');
const expectedContent = `Created by: ${version.rows[0].version}`;
expect(versionFileContent).toBe(expectedContent);
});

it('should report correct version with SELECT VERSION()', async () => {
const result = await pg.query<{ version: string }>('SELECT version()');
expect(result.rows[0].version).toContain('PGlite');
});
});

0 comments on commit 9e5ae26

Please sign in to comment.