@@ -18,7 +18,7 @@ use crossterm::{
1818use futures:: StreamExt ;
1919use portpicker:: pick_unused_port;
2020use shuttle_common:: {
21- database:: { AwsRdsEngine , SharedEngine } ,
21+ database:: { self , AwsRdsEngine , SharedEngine } ,
2222 ContainerRequest , ContainerResponse , Secret ,
2323} ;
2424use shuttle_proto:: provisioner:: {
@@ -157,20 +157,26 @@ impl LocalProvisioner {
157157 & self ,
158158 project_name : & str ,
159159 db_type : Type ,
160+ db_name : Option < String > ,
160161 ) -> Result < DatabaseResponse , Status > {
161162 trace ! ( "getting sql string for project '{project_name}'" ) ;
162163
164+ let database_name = match db_type {
165+ database:: Type :: AwsRds ( _) => db_name. unwrap_or_else ( || project_name. to_string ( ) ) ,
166+ database:: Type :: Shared ( SharedEngine :: MongoDb ) => "admin" . to_string ( ) ,
167+ _ => project_name. to_string ( ) ,
168+ } ;
169+
163170 let EngineConfig {
164171 r#type,
165172 image,
166173 engine,
167174 username,
168175 password,
169- database_name,
170176 port,
171177 env,
172178 is_ready_cmd,
173- } = db_type_to_config ( db_type) ;
179+ } = db_type_to_config ( db_type, & database_name ) ;
174180 let container_name = format ! ( "shuttle_{project_name}_{type}" ) ;
175181
176182 let container = self
@@ -320,12 +326,13 @@ impl Provisioner for LocalProvisioner {
320326 let DatabaseRequest {
321327 project_name,
322328 db_type,
329+ db_name,
323330 } = request. into_inner ( ) ;
324331
325332 let db_type: Option < Type > = db_type. unwrap ( ) . into ( ) ;
326333
327334 let res = self
328- . get_db_connection_string ( & project_name, db_type. unwrap ( ) )
335+ . get_db_connection_string ( & project_name, db_type. unwrap ( ) , db_name )
329336 . await ?;
330337
331338 Ok ( Response :: new ( res) )
@@ -387,23 +394,24 @@ struct EngineConfig {
387394 engine : String ,
388395 username : String ,
389396 password : Secret < String > ,
390- database_name : String ,
391397 port : String ,
392398 env : Option < Vec < String > > ,
393399 is_ready_cmd : Vec < String > ,
394400}
395401
396- fn db_type_to_config ( db_type : Type ) -> EngineConfig {
402+ fn db_type_to_config ( db_type : Type , database_name : & str ) -> EngineConfig {
397403 match db_type {
398404 Type :: Shared ( SharedEngine :: Postgres ) => EngineConfig {
399405 r#type : "shared_postgres" . to_string ( ) ,
400406 image : "docker.io/library/postgres:14" . to_string ( ) ,
401407 engine : "postgres" . to_string ( ) ,
402408 username : "postgres" . to_string ( ) ,
403409 password : "postgres" . to_string ( ) . into ( ) ,
404- database_name : "postgres" . to_string ( ) ,
405410 port : "5432/tcp" . to_string ( ) ,
406- env : Some ( vec ! [ "POSTGRES_PASSWORD=postgres" . to_string( ) ] ) ,
411+ env : Some ( vec ! [
412+ "POSTGRES_PASSWORD=postgres" . to_string( ) ,
413+ format!( "POSTGRES_DB={database_name}" ) ,
414+ ] ) ,
407415 is_ready_cmd : vec ! [
408416 "/bin/sh" . to_string( ) ,
409417 "-c" . to_string( ) ,
@@ -416,11 +424,11 @@ fn db_type_to_config(db_type: Type) -> EngineConfig {
416424 engine : "mongodb" . to_string ( ) ,
417425 username : "mongodb" . to_string ( ) ,
418426 password : "password" . to_string ( ) . into ( ) ,
419- database_name : "admin" . to_string ( ) ,
420427 port : "27017/tcp" . to_string ( ) ,
421428 env : Some ( vec ! [
422429 "MONGO_INITDB_ROOT_USERNAME=mongodb" . to_string( ) ,
423430 "MONGO_INITDB_ROOT_PASSWORD=password" . to_string( ) ,
431+ format!( "MONGO_INITDB_DATABASE={database_name}" ) ,
424432 ] ) ,
425433 is_ready_cmd : vec ! [
426434 "mongosh" . to_string( ) ,
@@ -435,9 +443,11 @@ fn db_type_to_config(db_type: Type) -> EngineConfig {
435443 engine : "postgres" . to_string ( ) ,
436444 username : "postgres" . to_string ( ) ,
437445 password : "postgres" . to_string ( ) . into ( ) ,
438- database_name : "postgres" . to_string ( ) ,
439446 port : "5432/tcp" . to_string ( ) ,
440- env : Some ( vec ! [ "POSTGRES_PASSWORD=postgres" . to_string( ) ] ) ,
447+ env : Some ( vec ! [
448+ "POSTGRES_PASSWORD=postgres" . to_string( ) ,
449+ format!( "POSTGRES_DB={database_name}" ) ,
450+ ] ) ,
441451 is_ready_cmd : vec ! [
442452 "/bin/sh" . to_string( ) ,
443453 "-c" . to_string( ) ,
@@ -450,9 +460,11 @@ fn db_type_to_config(db_type: Type) -> EngineConfig {
450460 engine : "mariadb" . to_string ( ) ,
451461 username : "root" . to_string ( ) ,
452462 password : "mariadb" . to_string ( ) . into ( ) ,
453- database_name : "mysql" . to_string ( ) ,
454463 port : "3306/tcp" . to_string ( ) ,
455- env : Some ( vec ! [ "MARIADB_ROOT_PASSWORD=mariadb" . to_string( ) ] ) ,
464+ env : Some ( vec ! [
465+ "MARIADB_ROOT_PASSWORD=mariadb" . to_string( ) ,
466+ format!( "MARIADB_DATABASE={database_name}" ) ,
467+ ] ) ,
456468 is_ready_cmd : vec ! [
457469 "mysql" . to_string( ) ,
458470 "-pmariadb" . to_string( ) ,
@@ -467,9 +479,11 @@ fn db_type_to_config(db_type: Type) -> EngineConfig {
467479 engine : "mysql" . to_string ( ) ,
468480 username : "root" . to_string ( ) ,
469481 password : "mysql" . to_string ( ) . into ( ) ,
470- database_name : "mysql" . to_string ( ) ,
471482 port : "3306/tcp" . to_string ( ) ,
472- env : Some ( vec ! [ "MYSQL_ROOT_PASSWORD=mysql" . to_string( ) ] ) ,
483+ env : Some ( vec ! [
484+ "MYSQL_ROOT_PASSWORD=mysql" . to_string( ) ,
485+ format!( "MYSQL_DATABASE={database_name}" ) ,
486+ ] ) ,
473487 is_ready_cmd : vec ! [
474488 "mysql" . to_string( ) ,
475489 "-pmysql" . to_string( ) ,
0 commit comments