Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support changing config after initdb, or editing from user call #415

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/pglite/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export interface PGliteOptions<TExtensions extends Extensions = Extensions> {
fsBundle?: Blob | File
parsers?: ParserOptions
serializers?: SerializerOptions
postgresqlConf?: Record<string, string | string[]>
}

export type PGliteInterface<T extends Extensions = Extensions> =
Expand Down
50 changes: 50 additions & 0 deletions packages/pglite/src/pglite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ export class PGlite
'INITDB created a new datadir, but an alternative db/user was requested',
)
}
// time to mark extensions that need special treatment on startup.
if (options.postgresqlConf) this.updateConf(options.postgresqlConf)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, this runs after initdb. I think I need to add some tests to check is PG correctly reads and process it.

}
}

Expand Down Expand Up @@ -777,4 +779,52 @@ export class PGlite
_runExclusiveTransaction<T>(fn: () => Promise<T>): Promise<T> {
return this.#transactionMutex.runExclusive(fn)
}

/**
* Update the PostgreSQL configuration file with the provided key-value pairs.
* @param config Key-value pairs to update in the configuration.
*/
async updateConf(config: Record<string, string | string[]>) {
const codec = new TextDecoder()
const configPath = `${PGDATA}/postgresql.conf`
const lines = codec.decode(this.mod!.FS.readFile(configPath)).split('\n')

const updatedLines = new Set<string>()
const newLines: string[] = []

for (const [key, value] of Object.entries(config)) {
const existingLineIndex = lines.findIndex((line) => line.startsWith(key))
// TODO: In future we may want to extract the array from the existing line
// and merge it with the new value.
const newValue = Array.isArray(value) ? value.join(',') : value.toString()
const newLine = `${key} = ${newValue}`

if (existingLineIndex !== -1) {
const oldLine = lines[existingLineIndex]
if (oldLine !== newLine) {
this.#log('postgresql.conf edited line:', oldLine, '->', newLine)
lines[existingLineIndex] = newLine
} else {
this.#log('postgresql.conf unchanged line:', newLine)
}
updatedLines.add(newLine)
} else {
this.#log('postgresql.conf new line:', newLine)
newLines.push(newLine)
}
}

if (newLines.length > 0 || updatedLines.size > 0) {
const finalLines = lines
.filter((line) => !updatedLines.has(line))
.concat(newLines)
this.mod!.FS.writeFile(configPath, finalLines.join('\n'))
this.mod!.FS.syncfs(false, (error) => {
if (error) {
this.#log('updateConf error:', error)
throw error
}
})
}
}
}