summaryrefslogtreecommitdiffstats
path: root/.github/scripts/analytics/data_mart_delete_table.py
blob: 76121b3fc523ed519539aacabfa544c2bcf93628 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3

import argparse
import ydb
import configparser
import os

# Load configuration
dir = os.path.dirname(__file__)
config = configparser.ConfigParser()
config_file_path = f"{dir}/../../config/ydb_qa_db.ini"
config.read(config_file_path)

DATABASE_ENDPOINT = config["QA_DB"]["DATABASE_ENDPOINT"]
DATABASE_PATH = config["QA_DB"]["DATABASE_PATH"]

def parse_args():
    parser = argparse.ArgumentParser(description="Delete a YDB table")
    parser.add_argument("--table_path", required=True, help="Table path and name to delete")
    
    return parser.parse_args()

def check_table_exists(session, table_path):
    """Check if table exists"""
    try:
        session.describe_table(table_path)
        return True
    except ydb.SchemeError:
        return False

def delete_table(session, table_path):
    """Delete the specified table."""
    try:
        session.drop_table(table_path)
        print(f"Table '{table_path}' successfully deleted.")
        return True
    except ydb.Error as e:
        print(f"Error deleting table: {e}")
        return False

def main():
    args = parse_args()

    if "CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS" not in os.environ:
        print("Error: Env variable CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS is missing, skipping")
        return 1
    else:
        os.environ["YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS"] = os.environ[
            "CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS"
        ]

    table_path = args.table_path
    full_table_path = f'{DATABASE_PATH}/{table_path}'

    print(f"Connecting to YDB to delete table {full_table_path}")

    with ydb.Driver(
        endpoint=DATABASE_ENDPOINT,
        database=DATABASE_PATH,
        credentials=ydb.credentials_from_env_variables()
    ) as driver:
        # Wait until driver is ready
        driver.wait(timeout=10, fail_fast=True)
        
        with ydb.SessionPool(driver) as pool:
            # Проверяем существование таблицы
            def check_and_delete(session):
                exists = check_table_exists(session, full_table_path)
                if exists:
                    return delete_table(session, full_table_path)
                else:
                    print(f"Table '{full_table_path}' does not exist.")
                    return False
            
            result = pool.retry_operation_sync(check_and_delete)
            
            if result:
                print(f"Table {full_table_path} has been deleted successfully.")
                return 0
            else:
                print(f"No table was deleted.")
                return 1

if __name__ == "__main__":
    exit_code = main()
    exit(exit_code)