1515from sqlmesh .core .console import configure_console , get_console
1616from sqlmesh .utils import Verbosity
1717from sqlmesh .core .config import load_configs
18- from sqlmesh .core .config .connection import CONNECTION_CONFIG_TO_TYPE , DISPLAY_NAME_TO_TYPE
18+ from sqlmesh .core .config .connection import INIT_DISPLAY_INFO_TO_TYPE
1919from sqlmesh .core .context import Context
2020from sqlmesh .utils .date import TimeLike
2121from sqlmesh .utils .errors import MissingDependencyError , SQLMeshError
4141)
4242SKIP_CONTEXT_COMMANDS = ("init" , "ui" )
4343
44- # These are ordered for user display - do not reorder
45- ENGINE_TYPE_DISPLAY_ORDER = [
46- "duckdb" ,
47- "snowflake" ,
48- "databricks" ,
49- "bigquery" ,
50- "motherduck" ,
51- "clickhouse" ,
52- "redshift" ,
53- "spark" ,
54- "trino" ,
55- "azuresql" ,
56- "mssql" ,
57- "postgres" ,
58- "gcp_postgres" ,
59- "mysql" ,
60- "athena" ,
61- "risingwave" ,
62- ]
63-
6444
6545def _sqlmesh_version () -> str :
6646 try :
@@ -195,23 +175,16 @@ def init(
195175 try :
196176 project_template = ProjectTemplate (template .lower ())
197177 except ValueError :
198- template_strings = (
199- "'" + "', '" .join ([template .value for template in ProjectTemplate ]) + "'"
200- )
178+ template_strings = "', '" .join ([template .value for template in ProjectTemplate ])
201179 raise click .ClickException (
202- f"Invalid project template '{ template } '. Please specify one of { template_strings } ."
180+ f"Invalid project template '{ template } '. Please specify one of ' { template_strings } ' ."
203181 )
204182
205- if project_template == ProjectTemplate .DBT and not Path (ctx .obj , "dbt_project.yml" ).exists ():
206- raise click .ClickException (
207- "Required dbt project file 'dbt_project.yml' not found in the current directory.\n \n Please add it or change directories before running `sqlmesh init` to set up your dbt project with SQLMesh."
208- )
209-
210183 if engine or project_template == ProjectTemplate .DBT :
211184 init_example_project (
212185 path = ctx .obj ,
213- engine_type = engine ,
214186 template = project_template or ProjectTemplate .DEFAULT ,
187+ engine_type = engine ,
215188 pipeline = dlt_pipeline ,
216189 dlt_path = dlt_path ,
217190 )
@@ -222,8 +195,6 @@ def init(
222195 console = srich .console
223196
224197 project_template , engine_type , cli_mode = _interactive_init (ctx .obj , console , project_template )
225- if project_template != ProjectTemplate .DBT :
226- _check_engine_installed (console , engine_type )
227198
228199 config_path = init_example_project (
229200 path = ctx .obj ,
@@ -1318,10 +1289,6 @@ def _interactive_init(
13181289 project_template = _init_template_prompt (console ) if not project_template else project_template
13191290
13201291 if project_template == ProjectTemplate .DBT :
1321- if not Path (path , "dbt_project.yml" ).exists ():
1322- raise SQLMeshError (
1323- "Required dbt project file 'dbt_project.yml' not found in the current directory.\n \n Please add it or change directories before running `sqlmesh init` to set up your dbt project with SQLMesh."
1324- )
13251292 return (project_template , None , None )
13261293
13271294 engine_type = _init_engine_prompt (console )
@@ -1333,24 +1300,26 @@ def _interactive_init(
13331300def _init_integer_prompt (
13341301 console : Console , err_msg_entity : str , num_options : int , retry_func : t .Callable [[t .Any ], t .Any ]
13351302) -> int :
1336- err_msg = "\n ERROR: '{option_str}' is not a valid {err_msg_entity} number - please enter a number between 1 and {num_options} or exit with control+c"
1337- option_str = Prompt .ask ("Enter a number" , console = console )
1338- try :
1339- option_num = int (option_str )
1340- if option_num < 1 or option_num > num_options :
1341- raise ValueError
1342- except ValueError :
1343- console .print (
1344- err_msg .format (
1345- option_str = option_str , err_msg_entity = err_msg_entity , num_options = num_options
1346- ),
1347- style = "red" ,
1348- )
1349- return retry_func (console )
1350- finally :
1351- console .print ("" )
1303+ err_msg = "\n ERROR: '{option_str}' is not a valid {err_msg_entity} number - please enter a number between 1 and {num_options} or exit with control+c\n "
1304+ while True :
1305+ option_str = Prompt .ask ("Enter a number" , console = console )
13521306
1353- return option_num
1307+ value_error = False
1308+ try :
1309+ option_num = int (option_str )
1310+ except ValueError :
1311+ value_error = True
1312+
1313+ if value_error or option_num < 1 or option_num > num_options :
1314+ console .print (
1315+ err_msg .format (
1316+ option_str = option_str , err_msg_entity = err_msg_entity , num_options = num_options
1317+ ),
1318+ style = "red" ,
1319+ )
1320+ continue
1321+ console .print ("" )
1322+ return option_num
13541323
13551324
13561325def _init_template_prompt (console : Console ) -> ProjectTemplate :
@@ -1381,17 +1350,22 @@ def _init_engine_prompt(console: Console) -> str:
13811350 console .print ("──────────────────────────────\n " )
13821351 console .print ("Choose your SQL engine:\n " )
13831352
1384- display_num_to_engine = {}
1385- for i , engine_type in enumerate (ENGINE_TYPE_DISPLAY_ORDER ):
1386- console .print (f" \\ [{ i + 1 } ] { ' ' if i < 9 else '' } { DISPLAY_NAME_TO_TYPE [engine_type ]} " )
1387- display_num_to_engine [i + 1 ] = engine_type
1353+ # INIT_DISPLAY_INFO_TO_TYPE is a dict of {engine_type: (display_order, display_name)}
1354+ ordered_engine_display_names = [
1355+ info [1 ] for info in sorted (INIT_DISPLAY_INFO_TO_TYPE .values (), key = lambda x : x [0 ])
1356+ ]
1357+ display_num_to_display_name = {}
1358+ for i , display_name in enumerate (ordered_engine_display_names ):
1359+ console .print (f" \\ [{ i + 1 } ] { ' ' if i < 9 else '' } { display_name } " )
1360+ display_num_to_display_name [i + 1 ] = display_name
13881361 console .print ("" )
13891362
13901363 engine_num = _init_integer_prompt (
1391- console , "engine" , len (ENGINE_TYPE_DISPLAY_ORDER ), _init_engine_prompt
1364+ console , "engine" , len (ordered_engine_display_names ), _init_engine_prompt
13921365 )
13931366
1394- return display_num_to_engine [engine_num ]
1367+ DISPLAY_NAME_TO_TYPE = {v [1 ]: k for k , v in INIT_DISPLAY_INFO_TO_TYPE .items ()}
1368+ return DISPLAY_NAME_TO_TYPE [display_num_to_display_name [engine_num ]]
13951369
13961370
13971371def _init_cli_mode_prompt (console : Console ) -> InitCliMode :
0 commit comments