code: def rebuild_encrypted_properties(old_key, model, properties):
      
      inspector = reflection.Inspector.from_engine(db.engine)
      primary_key_names = inspector.get_primary_keys(model.__tablename__)
  
      new_secret_key = current_app.secret_key
      db.session.expunge_all()
      try:
          with db.session.begin_nested():
              current_app.secret_key = old_key
              db_columns = []
              for primary_key in primary_key_names:
                  db_columns.append(getattr(model, primary_key))
              for prop in properties:
                  db_columns.append(getattr(model, prop))
              old_rows = db.session.query(*db_columns).all()
      except Exception as e:
          current_app.logger.error(
              'Exception occurred while reading encrypted properties. '
              'Try again before starting the server with the new secret key.')
          raise e
      finally:
          current_app.secret_key = new_secret_key
          db.session.expunge_all()
  
      for old_row in old_rows:
          primary_keys, old_entries = old_row[:len(primary_key_names)], \
                                      old_row[len(primary_key_names):]
          primary_key_fields = dict(zip(primary_key_names, primary_keys))
          update_values = dict(zip(properties, old_entries))
          model.query.filter_by(**primary_key_fields).\
              update(update_values)
      db.session.commit()
docs: Rebuild a model's EncryptedType properties when the SECRET_KEY is changed.
  
      :param old_key: old SECRET_KEY.
      :param model: the affected db model.
      :param properties: list of properties to rebuild.