|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import sys |
| 4 | +import optuna |
3 | 5 | import os.path |
4 | 6 | from pathlib import Path |
5 | 7 | import re |
@@ -80,7 +82,35 @@ def guess_storage_from_url(storage_url: str) -> BaseStorage: |
80 | 82 |
|
81 | 83 |
|
82 | 84 | def get_rdb_storage(storage_url: str) -> RDBStorage: |
83 | | - return RDBStorage(storage_url, skip_compatibility_check=True, skip_table_creation=True) |
| 85 | + """Create an RDBStorage instance for the dashboard (read-only viewer). |
| 86 | +
|
| 87 | + Raises SystemExit with a clear message if the database schema has not been |
| 88 | + initialized (i.e., no Optuna study has ever been run against this storage). |
| 89 | + """ |
| 90 | + try: |
| 91 | + return RDBStorage( |
| 92 | + storage_url, |
| 93 | + skip_compatibility_check=True, |
| 94 | + skip_table_creation=True, |
| 95 | + ) |
| 96 | + except optuna.exceptions.StorageInternalError as e: |
| 97 | + # This typically happens when the database exists but has no Optuna schema |
| 98 | + # (e.g., a brand-new empty SQLite file). Provide a human-readable error |
| 99 | + # instead of a raw SQLAlchemy traceback. |
| 100 | + err_msg = str(e).lower() |
| 101 | + cause_msg = str(e.__cause__).lower() if e.__cause__ else "" |
| 102 | + if "no such table" in err_msg or "version_info" in err_msg or "no such table" in cause_msg or "version_info" in cause_msg: |
| 103 | + print( |
| 104 | + f"Error: The database at '{storage_url}' does not contain an Optuna schema.\n" |
| 105 | + "Please run an Optuna study first to initialize the database:\n\n" |
| 106 | + " import optuna\n" |
| 107 | + " study = optuna.create_study(storage='<your-storage-url>')\n" |
| 108 | + " study.optimize(objective, n_trials=1)\n\n" |
| 109 | + "Then re-run optuna-dashboard.", |
| 110 | + file=sys.stderr, |
| 111 | + ) |
| 112 | + sys.exit(1) |
| 113 | + raise # Re-raise unexpected StorageInternalError variants unchanged |
84 | 114 |
|
85 | 115 |
|
86 | 116 | def get_journal_file_storage(file_path: str) -> JournalStorage: |
|
0 commit comments