From patchwork Sun May 15 19:31:18 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: gerrit-no-reply@lists.osmocom.org X-Patchwork-Id: 622420 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.osmocom.org (lists.osmocom.org [144.76.43.76]) by ozlabs.org (Postfix) with ESMTP id 3r7DGx701Lz9snl for ; Mon, 16 May 2016 05:31:21 +1000 (AEST) Received: from lists.osmocom.org (lists.osmocom.org [144.76.43.76]) by lists.osmocom.org (Postfix) with ESMTP id 4F0D01C96D; Sun, 15 May 2016 19:31:19 +0000 (UTC) X-Original-To: openbsc@lists.osmocom.org Delivered-To: openbsc@lists.osmocom.org Received: from 127.0.1.12 (unknown [127.0.1.12]) by lists.osmocom.org (Postfix) with ESMTPA id 972341C967; Sun, 15 May 2016 19:31:18 +0000 (UTC) Date: Sun, 15 May 2016 19:31:18 +0000 From: gerrit-no-reply@lists.osmocom.org To: Vadim Yanitskiy X-Gerrit-MessageType: merged Subject: Change in openbsc[master]: db.c: implemented incremental migration X-Gerrit-Change-Id: Ia9c2aa86f96b88ad8a710d0a23879ce219bc82dc X-Gerrit-ChangeURL: X-Gerrit-Commit: a8d8e93086580904aded88ffb957a6e7e3660cbe In-Reply-To: References: MIME-Version: 1.0 Content-Disposition: inline User-Agent: Gerrit/2.12.2-31-gb331dbd-dirty X-BeenThere: openbsc@lists.osmocom.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "Development of OpenBSC, OsmoBSC, OsmoNITB, OsmoCSCN" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: holger@freyther.de Errors-To: openbsc-bounces@lists.osmocom.org Sender: "OpenBSC" Message-Id: <20160515193119.4F0D01C96D@lists.osmocom.org> From Holger Freyther : Holger Freyther has submitted this change and it was merged. Change subject: db.c: implemented incremental migration ...................................................................... db.c: implemented incremental migration In the past, normal migration was possible only if the actual schema version differed from the version used in DB by 1. For example, if DB uses an old version 3 and you need to use it with the code written for version 5, the check_db_revision() will convert it to 4 and DB will still use incompatible schema version during Osmo-NITB running time. After next run it will be converted to version 5. This patch replaces a set of 'else-if' checks by a 'switch' without 'break' statements between 'case' labels (waterfall). It makes you able to migrate from current version to the latest despite any difference between them. Change-Id: Ia9c2aa86f96b88ad8a710d0a23879ce219bc82dc Reviewed-on: https://gerrit.osmocom.org/62 Tested-by: Jenkins Builder Reviewed-by: Holger Freyther --- M openbsc/src/libmsc/db.c 1 file changed, 35 insertions(+), 18 deletions(-) Approvals: Jenkins Builder: Verified Holger Freyther: Looks good to me, approved diff --git a/openbsc/src/libmsc/db.c b/openbsc/src/libmsc/db.c index e5017ae..b3235bb 100644 --- a/openbsc/src/libmsc/db.c +++ b/openbsc/src/libmsc/db.c @@ -374,9 +374,13 @@ { dbi_result result; const char *rev_s; + int db_rev = 0; + /* Make a query */ result = dbi_conn_query(conn, - "SELECT value FROM Meta WHERE key='revision'"); + "SELECT value FROM Meta " + "WHERE key = 'revision'"); + if (!result) return -EINVAL; @@ -384,33 +388,46 @@ dbi_result_free(result); return -EINVAL; } + + /* Fetch the DB schema revision */ rev_s = dbi_result_get_string(result, "value"); if (!rev_s) { dbi_result_free(result); return -EINVAL; } - if (!strcmp(rev_s, "2")) { - if (update_db_revision_2()) { - LOGP(DDB, LOGL_FATAL, "Failed to update database from schema revision '%s'.\n", rev_s); - dbi_result_free(result); - return -EINVAL; - } - } else if (!strcmp(rev_s, "3")) { - if (update_db_revision_3()) { - LOGP(DDB, LOGL_FATAL, "Failed to update database from schema revision '%s'.\n", rev_s); - dbi_result_free(result); - return -EINVAL; - } - } else if (!strcmp(rev_s, SCHEMA_REVISION)) { - /* everything is fine */ - } else { - LOGP(DDB, LOGL_FATAL, "Invalid database schema revision '%s'.\n", rev_s); + + if (!strcmp(rev_s, SCHEMA_REVISION)) { + /* Everything is fine */ dbi_result_free(result); + return 0; + } + + db_rev = atoi(rev_s); + dbi_result_free(result); + + /* Incremental migration waterfall */ + switch (db_rev) { + case 2: + if (update_db_revision_2()) + goto error; + case 3: + if (update_db_revision_3()) + goto error; + + /* The end of waterfall */ + break; + default: + LOGP(DDB, LOGL_FATAL, + "Invalid database schema revision '%d'.\n", db_rev); return -EINVAL; } - dbi_result_free(result); return 0; + +error: + LOGP(DDB, LOGL_FATAL, "Failed to update database " + "from schema revision '%d'.\n", db_rev); + return -EINVAL; } static int db_configure(void)