Rush has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/316487

Change subject: labsdb: maintain-views option to purge unspecified views
......................................................................

labsdb: maintain-views option to purge unspecified views

--clean now allows the script to compare active views with
views in the wild and drop stale ones.  This makes the list of
views declarative.

Change-Id: I260405c3767d1c2b081b4132a34c7cea90505314
---
M modules/role/files/labsdb/maintain-views.py
1 file changed, 43 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/puppet 
refs/changes/87/316487/1

diff --git a/modules/role/files/labsdb/maintain-views.py 
b/modules/role/files/labsdb/maintain-views.py
index b1cb9d0..d5f4af9 100644
--- a/modules/role/files/labsdb/maintain-views.py
+++ b/modules/role/files/labsdb/maintain-views.py
@@ -52,6 +52,28 @@
         if not self.dry_run:
             self.cursor.execute(query)
 
+    def drop_view(self, view):
+        """ Drop an obsolete view
+        :param view: str
+        """
+        self.write_execute("drop view {}.{}".format(self.db_p, view))
+        return not self.table_exists(view, self.db_p)
+
+    def tables(self, database):
+        """ Determine whether a table of the given name exists in the database 
of
+        the given name.
+        :param database: str
+        :returns: list
+        """
+        self.cursor.execute("""
+            SELECT `table_name`
+            FROM `information_schema`.`tables`
+            WHERE `table_schema`=%s;
+        """, args=(database))
+
+        dbtables = self.cursor.fetchall()
+        return [t[0] for t in dbtables]
+
     def user_exists(self, name):
         """ Check if a user exists
         :param name: str
@@ -70,12 +92,7 @@
         :param database: str
         :returns: bool
         """
-        self.cursor.execute("""
-            SELECT `table_name`
-            FROM `information_schema`.`tables`
-            WHERE `table_name`=%s AND `table_schema`=%s;
-        """, args=(table, database))
-        return bool(self.cursor.rowcount)
+        return table in self.tables(database=database)
 
     def database_exists(self, database):
         """ Verify if a DB exists
@@ -290,6 +307,12 @@
         action="store_true"
     )
     argparser.add_argument(
+        "--clean",
+        help=("Clean out views from _p varient that are no longer specified"
+              " make changes."),
+        action="store_true"
+    )
+    argparser.add_argument(
         "--replace-all",
         help=("Give this parameter if you don't want the script to prompt"
               " before replacing views."),
@@ -315,6 +338,11 @@
         except yaml.YAMLError as exc:
             logging.critical(exc)
             sys.exit(1)
+
+    all_tables = []
+    all_tables = all_tables + config['fullviews']
+    all_tables = all_tables + config['logging_whitelist']
+    all_tables = all_tables + list(config['customviews'].keys())
 
     dbs_metadata = config['metadata']
     sensitive_db_lists = config['sensitive_db_lists']
@@ -347,7 +375,7 @@
     all_dbs_file = "{}/dblists/all.dblist".format(args.mediawiki_config)
     with open(all_dbs_file) as f:
         all_available_dbs = f.read().splitlines()
-    all_available_dbs.extend(config['add_to_all_dbs'])
+    all_available_dbs = all_available_dbs + ['add_to_all_dbs']
 
     # argparse will ensure we are declaring explicitly
     dbs = all_available_dbs
@@ -385,8 +413,16 @@
                                    db,
                                    db_info.get('size', None),
                                    cursor)
+
             if not ops.user_exists(ops.definer):
                 logging.critical("Definer has not been created")
                 sys.exit(1)
 
             ops.execute(config["fullviews"], customviews)
+
+            if args.clean:
+                live_tables = ops.tables(ops.db_p)
+                dead_tables = [t for t in live_tables if t not in all_tables]
+                for dt in dead_tables:
+                    logging.info("Dropping view {}".format(dt))
+                    ops.drop_view(dt)

-- 
To view, visit https://gerrit.wikimedia.org/r/316487
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I260405c3767d1c2b081b4132a34c7cea90505314
Gerrit-PatchSet: 1
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Rush <r...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to