Skip to content

Commit d5295c5

Browse files
committed
Fix volumes; add database name
1 parent 205b407 commit d5295c5

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

singlestoredb/server/docker.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class SingleStoreDB:
4646
def __init__(
4747
self,
4848
name: Optional[str] = None,
49+
*,
4950
root_password: Optional[str] = None,
5051
license: Optional[str] = None,
5152
enable_kai: bool = False,
@@ -60,6 +61,7 @@ def __init__(
6061
global_vars: Optional[Dict[str, Any]] = None,
6162
init_sql: Optional[str] = None,
6263
image: str = DEFAULT_IMAGE,
64+
database: Optional[str] = None,
6365
):
6466
self.kai_enabled = enable_kai
6567
self.kai_port = None
@@ -118,13 +120,13 @@ def __init__(
118120
# Setup volumes
119121
volumes: Dict[str, Dict[str, str]] = {}
120122
if data_dir:
121-
{data_dir: {'bind': '/data', 'mode': 'rw'}}
123+
volumes[data_dir] = {'bind': '/data', 'mode': 'rw'}
122124
if logs_dir:
123-
{logs_dir: {'bind': '/logs', 'mode': 'ro'}}
125+
volumes[logs_dir] = {'bind': '/logs', 'mode': 'ro'}
124126
if server_dir:
125-
{server_dir: {'bind': '/server', 'mode': 'ro'}}
127+
volumes[server_dir] = {'bind': '/server', 'mode': 'ro'}
126128
if init_sql:
127-
{init_sql: {'bind': '/init.sql', 'mode': 'ro'}}
129+
volumes[os.path.abspath(init_sql)] = {'bind': '/init.sql', 'mode': 'ro'}
128130
if volumes:
129131
kwargs['volumes'] = volumes
130132

@@ -141,8 +143,15 @@ def __init__(
141143
if not self._wait_on_ready():
142144
raise RuntimeError('server did not come up properly')
143145

146+
self._database = database
144147
self._set_server_urls()
145148

149+
def __str__(self) -> str:
150+
return f"SingleStoreDB('{self.connection_url}')"
151+
152+
def __repr__(self) -> str:
153+
return str(self)
154+
146155
def _get_available_port(self) -> int:
147156
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
148157
s.bind(('', 0))
@@ -152,6 +161,8 @@ def _get_available_port(self) -> int:
152161
def _set_server_urls(self) -> None:
153162
self._saved_server_urls['DATABASE_URL'] = os.environ.get('DATABASE_URL')
154163
os.environ['DATABASE_URL'] = self.connection_url
164+
self._saved_server_urls['SINGLESTOREDB_URL'] = os.environ.get('SINGLESTOREDB_URL')
165+
os.environ['SINGLESTOREDB_URL'] = self.connection_url
155166

156167
def _restore_server_urls(self) -> None:
157168
for k, v in self._saved_server_urls.items():
@@ -173,15 +184,17 @@ def logs(self) -> List[str]:
173184

174185
@property
175186
def connection_url(self) -> str:
187+
dbname = f'/{self._database}' if self._database else ''
176188
root_password = urllib.parse.quote_plus(self.root_password)
177189
return f'singlestoredb://root:{root_password}@' + \
178-
f'localhost:{self.server_port}'
190+
f'localhost:{self.server_port}{dbname}'
179191

180192
@property
181193
def http_connection_url(self) -> str:
194+
dbname = f'/{self._database}' if self._database else ''
182195
root_password = urllib.parse.quote_plus(self.root_password)
183196
return f'singlestoredb+http://root:{root_password}@' + \
184-
f'localhost:{self.data_api_port}'
197+
f'localhost:{self.data_api_port}{dbname}'
185198

186199
def connect(
187200
self,
@@ -213,7 +226,6 @@ def studio_url(self) -> str:
213226

214227
def connect_studio(self) -> None:
215228
import webbrowser
216-
217229
if platform.platform().lower().startswith('macos'):
218230
chrome_path = r'open -a /Applications/Google\ Chrome.app %s'
219231
webbrowser.get(chrome_path).open(self.studio_url, new=2)
@@ -255,6 +267,7 @@ def start(
255267
global_vars: Optional[Dict[str, Any]] = None,
256268
init_sql: Optional[str] = None,
257269
image: str = DEFAULT_IMAGE,
270+
database: Optional[str] = None,
258271
) -> SingleStoreDB:
259272
"""Start a SingleStoreDB server using Docker."""
260273
return SingleStoreDB(
@@ -273,4 +286,5 @@ def start(
273286
global_vars=global_vars,
274287
init_sql=init_sql,
275288
image=image,
289+
database=database,
276290
)

0 commit comments

Comments
 (0)