From 5e232a88319651cd73adb50e708f03689d0cf353 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Victor=20Frans=20Pondaco=20Winandy?=
 <jvfpw18@inf.ufpr.br>
Date: Mon, 22 Apr 2019 09:32:31 -0300
Subject: [PATCH] Added confirmation prompt to remap

---
 database/actions.py        |  4 +--
 database/database_table.py | 50 ++++++++++++++++++++++++++------------
 manage.py                  |  4 +--
 3 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/database/actions.py b/database/actions.py
index 26733f1..522decb 100644
--- a/database/actions.py
+++ b/database/actions.py
@@ -87,12 +87,12 @@ def drop(table):
 
     table.drop()
 
-def remap(table):
+def remap(table, auto_confirmation=True):
     '''Applies change made in mapping protocols to database'''
     table = gen_data_table(table, META)
     table.map_from_database()
 
-    table.remap()
+    table.remap(auto_confirmation)
 
 def csv_from_tabbed(table_name, input_file, output_file, year, sep=';'):
     table = gen_data_table(table_name, META)
diff --git a/database/database_table.py b/database/database_table.py
index ab81092..8771c75 100644
--- a/database/database_table.py
+++ b/database/database_table.py
@@ -599,7 +599,7 @@ class DatabaseTable(Table):
         results = self.metadata.bind.execute(query).fetchall()
         db_target_list = [t[1] for t in results]
 
-        new_columns = [c for c in protocol_target_list if c not in db_target_list]
+        new_columns = [c for c in protocol_target_list if c not in db_target_list and c != '']
         to_drop_columns = [c for c in db_target_list if c not in protocol_target_list]
 
         update_columns = []
@@ -627,7 +627,7 @@ class DatabaseTable(Table):
 
         return new_columns, to_drop_columns, update_columns
 
-    def remap(self):
+    def remap(self, auto_confirmation=True):
         '''
         Checks mapping protocol for differences in table structure - then
         attempts to apply differences according to what is recorded in the
@@ -651,27 +651,47 @@ class DatabaseTable(Table):
 
         new_columns, to_drop_columns, update_columns = self.compare_mapping()
 
+        accept_new_columns, accept_drop_columns, accept_update_columns = [True for _ in range(3)]
+        if not auto_confirmation:
+            if new_columns:
+                print('The following columns will be CREATED:', ', '.join(new_columns))
+                prompt = input('Is it right (yes or no)? ')
+                accept_new_columns = prompt == 'yes' or prompt == 'y' or prompt == 1
+            if to_drop_columns:
+                print('The following columns will be DROPPED:', ', '.join(to_drop_columns))
+                prompt = input('Is it right (yes or no)? ')
+                accept_drop_columns = prompt == 'yes' or prompt == 'y' or prompt == 1
+            if update_columns:
+                update_list = [update_dict['name'] + ' -new name: ' + update_dict['new_name']
+                               + ' -new type: ' + update_dict['new_type'] for update_dict in update_columns]
+                print('The following columns will be UPDATED:', ', '.join(update_list))
+                prompt = input('Is it right (yes or no)? ')
+                accept_update_columns = prompt == 'yes' or prompt == 'y' or prompt == 1
+
         with self.metadata.bind.connect() as connection:
             # Create new columns
-            for column in new_columns:
-                try:
-                    dbcolumn = self._protocol.dbcolumn_from_target(column)
-                except InvalidTargetError:
-                    continue
+            if accept_new_columns:
+                for column in new_columns:
+                    try:
+                        dbcolumn = self._protocol.dbcolumn_from_target(column)
+                    except InvalidTargetError:
+                        continue
 
-                self.add_column(dbcolumn[0], dbcolumn[1], column, bind=connection)
+                    self.add_column(dbcolumn[0], dbcolumn[1], column, bind=connection)
 
             # Drop columns
-            for column in to_drop_columns:
-                column_name = select([mtable.c.name]).where(mtable.c.target_name == column)
-                column_name = connection.execute(column_name).fetchone()[0]
-                if not column_name:
-                    continue
+            if accept_drop_columns:
+                for column in to_drop_columns:
+                    column_name = select([mtable.c.name]).where(mtable.c.target_name == column)
+                    column_name = connection.execute(column_name).fetchone()[0]
+                    if not column_name:
+                        continue
 
-                self.drop_column(column_name, column, bind=connection)
+                    self.drop_column(column_name, column, bind=connection)
 
             # Update existing columns
-            self.transfer_data(connection, update_columns)
+            if accept_update_columns:
+                self.transfer_data(connection, update_columns)
 
     def _get_variable_target(self, original, year):
         '''
diff --git a/manage.py b/manage.py
index 236e6a5..cb87c7c 100755
--- a/manage.py
+++ b/manage.py
@@ -45,9 +45,9 @@ def drop(table):
     database.actions.drop(table)
 
 @manager.command
-def remap(table):
+def remap(table, auto_confirmation=False):
     '''Restructures a table to match the mapping protocol.'''
-    database.actions.remap(table)
+    database.actions.remap(table, auto_confirmation)
 
 @manager.command
 def update_from_file(csv_file, table, year, columns=None, target_list=None, offset=2, sep=';',
-- 
GitLab