Skip to content
Merged
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
23 changes: 23 additions & 0 deletions sdk/python/tests/test_pg0.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,29 @@ def test_port_conflict_error(self, clean_instance):
pg2.stop()
pg0.drop(f"{TEST_NAME}-2")

def test_restart_with_custom_database(self, clean_instance):
"""Restarting an instance with a non-default database must be idempotent.

Regression test for https://github.com/vectorize-io/pg0/issues/13
Previously, the second start failed because pg0 only matched the English
substring "already exists" when CREATE DATABASE returned a duplicate
error, so restarts on PostgreSQL builds with a non-English lc_messages
(e.g. Chinese on Windows: `数据库 "x" 已经存在`) crashed startup.
"""
pg = Pg0(name=TEST_NAME, port=TEST_PORT, database="testdb")
pg.start()
pg.execute("CREATE TABLE restart_test (id int);")
pg.execute("INSERT INTO restart_test VALUES (42);")
pg.stop()

# Second start must succeed and preserve data
info = pg.start()
assert info.running is True
assert "testdb" in info.uri
result = pg.execute("SELECT id FROM restart_test;")
assert "42" in result
pg.stop()

@pytest.mark.skipif(
sys.platform == "win32",
reason="signal.SIGKILL does not exist on Windows; crash-recovery behavior is exercised by the Unix matrix.",
Expand Down
13 changes: 6 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,13 +893,12 @@ fn start(

// Create the database if it doesn't exist and it's not the default 'postgres'
if database != "postgres" {
println!("Creating database '{}'...", database);
if let Err(e) = postgresql.create_database(&database) {
// Ignore error if database already exists
let err_str = e.to_string();
if !err_str.contains("already exists") {
return Err(e.into());
}
// Pre-check existence rather than relying on the duplicate-database error
// string, which is localized by PostgreSQL's lc_messages (e.g. on Windows
// with a Chinese locale: `数据库 "x" 已经存在`). See vectorize-io/pg0#13.
if !postgresql.database_exists(&database)? {
println!("Creating database '{}'...", database);
postgresql.create_database(&database)?;
}
// Grant privileges to the user on the database
if username != "postgres" {
Expand Down
Loading