Untitled

 avatar
unknown
plain_text
a year ago
2.7 kB
13
Indexable
  def clear_postgres_tables(self):
        """Clear all PostgreSQL tables before migration"""
        cursor = self.postgres_conn.cursor()
        
        try:
            logger.info("Clearing PostgreSQL tables...")
            
            tables_to_clear = [
                'task_general',
                'task_source', 
                'task_member',
                'task_reception',
                'task_comment',
                'task_log',
                'task_file',
                'task_progress',
                'task_notification',
                'task_pin',
                'task_log_detail',
                'task_checklist',
                'eoffice_van_ban_den_task'
            ]
            
            for table in tables_to_clear:
                try:
                    cursor.execute(f"DELETE FROM {table}")
                    logger.info(f"Cleared table: {table}")
                except Exception as e:
                    logger.warning(f"Normal delete failed for {table}: {e}")
                    try:
                        cursor.execute(f"TRUNCATE TABLE {table} CASCADE")
                        logger.info(f"Truncated table with CASCADE: {table}")
                    except Exception as e2:
                        logger.error(f"Failed to clear table {table}: {e2}")
                        raise
            
                sequence_map = {
                    'task_general': 'task_general_id_seq',
                    'task_source': 'task_source_id_seq',
                    'task_member': 'task_member_id_seq',
                    'task_checklist': 'task_check_list_id_seq',
                    'task_log_detail': 'task_log_detail_id_seq',
                    'task_notification': 'task_notification_id_seq',
                    'task_progress': 'task_progress_id_seq',
                    'task_log': 'task_log_id_seq',
                    'task_comment': 'task_comment_id_seq',
                    'eoffice_van_ban_den_task': 'eoffice_van_ban_den_task_id_seq',
                }
                for table, seq in sequence_map.items():
                    try:
                        cursor.execute(f"ALTER SEQUENCE {seq} RESTART WITH 1")
                        logger.info(f"Reset sequence: {seq} for table: {table}")
                    except Exception as e:
                        logger.warning(f"Failed to reset sequence {seq} for table {table}: {e}")
            self.postgres_conn.commit()
            logger.info("All PostgreSQL tables cleared successfully")
            
        except Exception as e:
            self.postgres_conn.rollback()
            logger.error(f"Failed to clear PostgreSQL tables: {e}")
            raise
        finally:
            cursor.close()
Editor is loading...
Leave a Comment