|
1 | 1 | import os |
2 | | -import click |
3 | 2 | from .table_operations import get_table_list, choose_tables, generate_yaml_for_table |
4 | 3 | from .yaml_generator import generate_definitions_and_relationships |
5 | 4 | from ..utils.print import print_header, print_info, print_success, print_warning, print_prompt |
|
9 | 8 | @handle_database_errors |
10 | 9 | def init_context(): |
11 | 10 | print_header("Setting up the context(semantic layer) for your database..") |
12 | | - |
13 | 11 | print_warning( |
14 | 12 | "This will create a folder context in the current directory. And override if there is an existing file") |
15 | | - |
16 | 13 | print_prompt("You can edit it anytime...") |
17 | | - |
18 | 14 | print_warning("Please choose a set of 10 or lesser tables..\n") |
19 | | - |
20 | 15 | print_info("🗄️ Fetching tables from the database") |
| 16 | + |
21 | 17 | db_type, all_tables = get_table_list() |
| 18 | + |
22 | 19 | chosen_tables = choose_tables(all_tables) |
23 | 20 |
|
24 | 21 | os.makedirs('context/tables', exist_ok=True) |
25 | 22 |
|
26 | | - for table in chosen_tables: |
27 | | - yaml_content = generate_yaml_for_table(table) |
28 | | - with open(f'context/tables/{table}.yaml', 'w') as f: |
| 23 | + for table_info in chosen_tables: |
| 24 | + if isinstance(table_info, dict): |
| 25 | + schema = table_info['schema'] |
| 26 | + table = table_info['table'] |
| 27 | + else: |
| 28 | + schema = 'main' # default schema |
| 29 | + table = table_info |
| 30 | + |
| 31 | + yaml_content = generate_yaml_for_table(schema, table) |
| 32 | + with open(f'context/tables/{schema}___{table}.yaml', 'w') as f: |
29 | 33 | f.write(yaml_content) |
30 | 34 | print("\n") |
31 | | - print_success(f"Generated YAML for table: {table}") |
| 35 | + print_success(f"Generated YAML for table: {schema}.{table}") |
32 | 36 |
|
33 | 37 | print_info("Generating definitions and relationships...") |
34 | | - definitions_yaml, relationships_yaml = generate_definitions_and_relationships( |
35 | | - chosen_tables, db_type) |
| 38 | + try: |
| 39 | + definitions_yaml, relationships_yaml = generate_definitions_and_relationships( |
| 40 | + chosen_tables, db_type) |
36 | 41 |
|
37 | | - with open('context/definitions.yaml', 'w') as f: |
38 | | - f.write(definitions_yaml) |
39 | | - print_success("Generated definitions.yaml") |
| 42 | + with open('context/definitions.yaml', 'w') as f: |
| 43 | + f.write(definitions_yaml) |
| 44 | + print_success("Generated definitions.yaml") |
40 | 45 |
|
41 | | - with open('context/relationships.yaml', 'w') as f: |
42 | | - f.write(relationships_yaml) |
43 | | - print_success("Generated relationships.yaml") |
| 46 | + with open('context/relationships.yaml', 'w') as f: |
| 47 | + f.write(relationships_yaml) |
| 48 | + print_success("Generated relationships.yaml") |
44 | 49 |
|
45 | | - print_success("Initialization complete!") |
| 50 | + print_success("Initialization complete!") |
| 51 | + except Exception as e: |
| 52 | + print_warning(f"An error occurred: {str(e)}") |
46 | 53 |
|
47 | 54 |
|
48 | 55 | if __name__ == '__main__': |
|
0 commit comments