|
11 | 11 | #
|
12 | 12 | # SPDX-License-Identifier: MIT
|
13 | 13 | ###############################################################################
|
| 14 | +from datetime import datetime, timedelta |
14 | 15 | from os import environ
|
15 | 16 |
|
16 | 17 | import gdaltest
|
@@ -48,8 +49,11 @@ def setup_driver():
|
48 | 49 | conn,
|
49 | 50 | "SELECT REPLACE(CURRENT_UTCDATE, '-', '') || '_' || BINTOHEX(SYSUUID) FROM DUMMY;",
|
50 | 51 | )
|
51 |
| - gdaltest.hana_schema_name = "{}_{}".format("gdal_test", uid) |
| 52 | + schema_prefix = "gdal_test" |
52 | 53 |
|
| 54 | + drop_old_test_schemas(conn, schema_prefix) |
| 55 | + |
| 56 | + gdaltest.hana_schema_name = f"{schema_prefix}_{uid}" |
53 | 57 | execute_sql(conn, f'CREATE SCHEMA "{gdaltest.hana_schema_name}"')
|
54 | 58 |
|
55 | 59 | ds = open_datasource(1)
|
@@ -1375,6 +1379,24 @@ def execute_sql_scalar(conn, sql):
|
1375 | 1379 | return res
|
1376 | 1380 |
|
1377 | 1381 |
|
| 1382 | +def drop_old_test_schemas(conn, schema_prefix): |
| 1383 | + try: |
| 1384 | + assert conn |
| 1385 | + cursor = conn.cursor() |
| 1386 | + assert cursor |
| 1387 | + sql = f"""SELECT SCHEMA_NAME FROM SYS.SCHEMAS WHERE SCHEMA_NAME |
| 1388 | + LIKE '{schema_prefix.replace('_', '__')}__%' ESCAPE '_' AND |
| 1389 | + LOCALTOUTC(CREATE_TIME) < ?""" |
| 1390 | + cursor.execute(sql, datetime.now() - timedelta(days=1)) |
| 1391 | + rows = cursor.fetchall() |
| 1392 | + cursor.close() |
| 1393 | + for row in rows: |
| 1394 | + execute_sql(conn, f'DROP SCHEMA "{row["SCHEMA_NAME"]}" CASCADE') |
| 1395 | + except Exception as ex: |
| 1396 | + print(f"Unable to drop old test schemas. Error: {ex}") |
| 1397 | + pass |
| 1398 | + |
| 1399 | + |
1378 | 1400 | def open_datasource(update=0, open_opts=None):
|
1379 | 1401 | conn_str = "HANA:" + get_connection_str() + ";SCHEMA=" + gdaltest.hana_schema_name
|
1380 | 1402 | if open_opts is None:
|
|
0 commit comments