From eec4135d878a60695fffe74969d35b4e688054c8 Mon Sep 17 00:00:00 2001 From: Kirill Solomko Date: Mon, 1 Sep 2025 12:34:31 +0200 Subject: [PATCH] MT#63361 apply site_id retroactively * when ngcp-update-db-schema run and during the run the site_id column is added, the site_id is set for all scripts that were applied during the run before the 15867.up. It does not affect scripts there were applied previously and site_id remains as NULL for them, so they are considered as "already available globally". Change-Id: If62af77a8e0f2ffa9c8fe54c195d6dc97c27c261 --- ngcp-update-db-schema | 70 +++++++++++++++++++++++++++++++++++++++++-- schema/billing.json | 12 -------- schema/billing.sql | 1 - schema/ngcp.sql | 3 +- 4 files changed, 68 insertions(+), 18 deletions(-) diff --git a/ngcp-update-db-schema b/ngcp-update-db-schema index d0482e72..67905b70 100755 --- a/ngcp-update-db-schema +++ b/ngcp-update-db-schema @@ -110,6 +110,9 @@ class Config: _post_set_release_for_revisions (List[int]): set release in the db_schema table for all revisions that were applied when the `release` column was not available + _post_set_site_id_for_revisions (List[int]): set site_id in the + db_schema table for all revisions that were applied when + the `site_id` column was not available node_name (str): current node name @@ -185,6 +188,7 @@ class Config: _run_cmd_mysql_options: List[str] = [] _scripts_dir_order: List[str] = ['init', 'base', 'diff'] _post_set_release_for_revisions: List[int] = [] + _post_set_site_id_for_revisions: List[int] = [] # attributes that are initialised in __init__() node_name: str @@ -1107,8 +1111,12 @@ def apply_revisions(revisions: Revisions) -> None: config.db_conn.commit() config._script_was_applied = True + # order matters for next 2 lines + post_set_site_id_for_applied_revisions() post_set_release_for_applied_revisions() except RevisionApplyError: + # order matters for next 2 lines + post_set_site_id_for_applied_revisions() post_set_release_for_applied_revisions() shutdown(1) @@ -1233,8 +1241,12 @@ def apply_up_script(script_name: str, revision: int) -> int: config._temp_sql_file.write( str(f'\n; {sql}; COMMIT;\n').encode() ) + config._post_set_release_for_revisions.append(revision) + if is_db_role and not check_column_exists('site_id'): + config._post_set_site_id_for_revisions.append(revision) + if revision not in db_schema: db_schema[revision] = {} if site_id not in db_schema[revision]: @@ -1419,15 +1431,67 @@ def post_set_release_for_applied_revisions() -> None: if not check_column_exists('release'): return + node_name = config.node_name release = getattr(config, 'set_release', config.ngcp_version) cursor: pymysql.cursors.Cursor = config.db_conn.cursor() + if check_column_exists('site_id'): + site_id = int(config.node_roles.get('ngcp_site_id') or 1) + post_set_revisions = config._post_set_release_for_revisions + cursor.execute(f""" + UPDATE {config.db_schema_table} + SET `release` = '{release}', + applied_at = applied_at + WHERE site_id = {site_id} + AND node = '{node_name}' + AND revision IN ({ + ','.join(str(x) for x in post_set_revisions) + }) + """) + else: + cursor.execute(f""" + UPDATE {config.db_schema_table} + SET `release` = '{release}', + applied_at = applied_at + WHERE node = '{node_name}' + AND revision IN ({ + ','.join(str(x) for x in post_set_revisions) + }) + """) + + cursor.close() + config.db_conn.commit() + + +def post_set_site_id_for_applied_revisions() -> None: + """Sets site_id column for applied revisions. + + Sets site_id column for applied revisions + + Args: + None + + Returns + None + + """ + if config.mode != 'up': + return + if not config._post_set_site_id_for_revisions: + return + if not check_column_exists('site_id'): + return + + site_id = int(config.node_roles.get('ngcp_site_id') or 1) + cursor: pymysql.cursors.Cursor = config.db_conn.cursor() + cursor.execute(f""" UPDATE {config.db_schema_table} - SET `release` = '{release}', + SET site_id = '{site_id}', applied_at = applied_at - WHERE revision IN ({ - ','.join(str(x) for x in config._post_set_release_for_revisions) + WHERE site_id IS NULL + AND revision IN ({ + ','.join(str(x) for x in config._post_set_site_id_for_revisions) }) """) diff --git a/schema/billing.json b/schema/billing.json index bdca0085..ad5e6551 100644 --- a/schema/billing.json +++ b/schema/billing.json @@ -4356,18 +4356,6 @@ "TABLE_NAME" : "ncos_levels", "key_col" : "ncos_levels/time_set_id" }, - "ncos_levels/time_set_invert" : { - "CHARACTER_SET_NAME" : null, - "COLLATION_NAME" : null, - "COLUMN_DEFAULT" : "0", - "COLUMN_NAME" : "time_set_invert", - "COLUMN_TYPE" : "tinyint(1)", - "EXTRA" : "", - "IS_NULLABLE" : "NO", - "ORDINAL_POSITION" : 10, - "TABLE_NAME" : "ncos_levels", - "key_col" : "ncos_levels/time_set_invert" - }, "ncos_lnp_list/description" : { "CHARACTER_SET_NAME" : "utf8mb3", "COLLATION_NAME" : "utf8mb3_general_ci", diff --git a/schema/billing.sql b/schema/billing.sql index 59f91b49..fd83e849 100644 --- a/schema/billing.sql +++ b/schema/billing.sql @@ -738,7 +738,6 @@ CREATE TABLE `ncos_levels` ( `description` text DEFAULT NULL, `time_set_id` int(11) unsigned DEFAULT NULL, `expose_to_customer` tinyint(1) NOT NULL DEFAULT 0, - `time_set_invert` tinyint(1) NOT NULL DEFAULT 0, PRIMARY KEY (`id`), UNIQUE KEY `reslev_idx` (`reseller_id`,`level`), KEY `nl_time_set_id_idx` (`time_set_id`), diff --git a/schema/ngcp.sql b/schema/ngcp.sql index af94836d..b88a2a9d 100644 --- a/schema/ngcp.sql +++ b/schema/ngcp.sql @@ -25,7 +25,7 @@ CREATE TABLE `db_schema` ( PRIMARY KEY (`id`), UNIQUE KEY `rev_idx` (`revision`,`node`,`site_id`), KEY `release_idx` (`release`) -) ENGINE=InnoDB AUTO_INCREMENT=915 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci; +) ENGINE=InnoDB AUTO_INCREMENT=914 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci; /*!40101 SET character_set_client = @saved_cs_client */; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8mb4 */; @@ -30965,7 +30965,6 @@ INSERT INTO `db_schema` VALUES (910,15865,'spce','1970-01-01 00:00:01','trunk',N INSERT INTO `db_schema` VALUES (911,15866,'spce','1970-01-01 00:00:01','trunk',NULL); INSERT INTO `db_schema` VALUES (912,15867,'spce','1970-01-01 00:00:01','trunk',1); INSERT INTO `db_schema` VALUES (913,15868,'spce','1970-01-01 00:00:01','trunk',1); -INSERT INTO `db_schema` VALUES (914,15869,'spce','1970-01-01 00:00:01','trunk',1); commit; set autocommit=0; INSERT INTO `timezone` VALUES ('1','localtime','1970-01-01 00:00:01','1970-01-01 00:00:01',NULL);