diff --git a/sdk/python/tests/test_pg0.py b/sdk/python/tests/test_pg0.py index bf4ad40..2c12779 100644 --- a/sdk/python/tests/test_pg0.py +++ b/sdk/python/tests/test_pg0.py @@ -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.", diff --git a/src/main.rs b/src/main.rs index 2a85de3..64b7a58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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" {