11use std:: env;
22
3- use torrust_index:: databases:: database;
43use torrust_index:: web:: api:: Version ;
54
6- use super :: config:: { initialize_configuration, ENV_VAR_INDEX_SHARED } ;
5+ use super :: config:: { initialize_configuration, ENV_VAR_DB_CONNECT_URL , ENV_VAR_INDEX_SHARED } ;
76use crate :: common:: contexts:: settings:: Settings ;
87use crate :: environments:: { isolated, shared} ;
98
@@ -98,9 +97,10 @@ impl TestEnv {
9897
9998 /// Provides a database connect URL to connect to the database. For example:
10099 ///
101- /// `sqlite://storage/database/torrust_index_e2e_testing.db?mode=rwc`.
100+ /// - `sqlite://storage/database/torrust_index_e2e_testing.db?mode=rwc`.
101+ /// - `mysql://root:root_secret_password@localhost:3306/torrust_index_e2e_testing`.
102102 ///
103- /// It's used to run SQL queries against the database needed for some tests.
103+ /// It's used to run SQL queries against the E2E database needed for some tests.
104104 pub fn database_connect_url ( & self ) -> Option < String > {
105105 let internal_connect_url = self
106106 . starting_settings
@@ -109,62 +109,19 @@ impl TestEnv {
109109
110110 match self . state ( ) {
111111 State :: RunningShared => {
112- if let Some ( db_path) = internal_connect_url {
113- let maybe_db_driver = database:: get_driver ( & db_path) ;
114-
115- return match maybe_db_driver {
116- Ok ( db_driver) => match db_driver {
117- database:: Driver :: Sqlite3 => Some ( Self :: overwrite_sqlite_path ( & db_path, "./storage/index/lib" ) ) ,
118- database:: Driver :: Mysql => Some ( Self :: overwrite_mysql_host ( & db_path, "localhost" ) ) ,
119- } ,
120- Err ( _) => None ,
121- } ;
112+ let connect_url_env_var = ENV_VAR_DB_CONNECT_URL ;
113+
114+ if let Ok ( connect_url) = env:: var ( connect_url_env_var) {
115+ Some ( connect_url)
116+ } else {
117+ None
122118 }
123- None
124119 }
125120 State :: RunningIsolated => internal_connect_url,
126121 State :: Stopped => None ,
127122 }
128123 }
129124
130- /// It overrides the `SQLite` file path in a `SQLx` database connection URL.
131- /// For example:
132- ///
133- /// For:
134- ///
135- /// `sqlite:///var/lib/torrust/index/database/e2e_testing_sqlite3.db?mode=rwc`.
136- ///
137- /// It changes the `mysql` host name to `localhost`:
138- ///
139- /// `sqlite://./storage/index/lib/database/e2e_testing_sqlite3.db?mode=rwc`.
140- ///
141- /// For E2E tests, we use docker compose. Inside the container, the
142- /// `SQLite` file path is not the same as the host path.
143- fn overwrite_sqlite_path ( db_path : & str , host_path : & str ) -> String {
144- // todo: inject value with env var
145- db_path. replace ( "/var/lib/torrust/index" , host_path)
146- }
147-
148- /// It overrides the "Host" in a `SQLx` database connection URL.
149- /// For example:
150- ///
151- /// For:
152- ///
153- /// `mysql://root:root_secret_password@mysql:3306/torrust_index_e2e_testing`.
154- ///
155- /// It changes the `mysql` host name to `localhost`:
156- ///
157- /// `mysql://root:root_secret_password@localhost:3306/torrust_index_e2e_testing`.
158- ///
159- /// For E2E tests, we use docker compose, internally the index connects to
160- /// the `MySQL` database using the "mysql" host, which is the docker compose
161- /// service name, but tests connects directly to the localhost since the
162- /// `MySQL` is exposed to the host.
163- fn overwrite_mysql_host ( db_path : & str , new_host : & str ) -> String {
164- // todo: inject value with env var
165- db_path. replace ( "@mysql:" , & format ! ( "@{new_host}:" ) )
166- }
167-
168125 fn state ( & self ) -> State {
169126 if self . is_shared ( ) {
170127 return State :: RunningShared ;
0 commit comments