def add_credential(self, credtype, username, password, key=None):
        """
        Check if this credential has already been added to the database, if not add it in.
        """
        credentials = []

        # a user can have multiple keys, all with passphrases, and a separate login password
        if key is not None:
            q = (
                select(self.CredentialsTable)
                .join(self.KeysTable)
                .filter(
                    func.lower(self.CredentialsTable.c.username) == func.lower(username),
                    func.lower(self.CredentialsTable.c.credtype) == func.lower(credtype),
                    self.KeysTable.c.data == key,
                )
            )
            results = self.sess.execute(q).all()
        else:
            q = select(self.CredentialsTable).filter(
                func.lower(self.CredentialsTable.c.username) == func.lower(username),
                func.lower(self.CredentialsTable.c.credtype) == func.lower(credtype),
            )
            results = self.sess.execute(q).all()

        # add new credential
        if not results:
            new_cred = {
                "credtype": credtype,
                "username": username,
                "password": password,
            }
            credentials = [new_cred]
        # update existing cred data
        else:
            for creds in results:
                # this will include the id, so we don't touch it
                cred_data = creds._asdict()
                # only update column if it is being passed in
                if credtype is not None:
                    cred_data["credtype"] = credtype
                if username is not None:
                    cred_data["username"] = username
                if password is not None:
                    cred_data["password"] = password
                # only add cred to be updated if it has changed
                if cred_data not in credentials:
                    credentials.append(cred_data)

        # TODO: find a way to abstract this away to a single Upsert call
        q_users = Insert(self.CredentialsTable)  # .returning(self.CredentialsTable.c.id)
        update_columns_users = {col.name: col for col in q_users.excluded if col.name not in "id"}
        q_users = q_users.on_conflict_do_update(index_elements=self.CredentialsTable.primary_key, set_=update_columns_users)
        cme_logger.debug(f"Adding credentials: {credentials}")

        self.sess.execute(q_users, credentials)  # .scalar()
        # return cred_ids

        # hacky way to get cred_id since we can't use returning() yet
        if len(credentials) == 1:
            cred_id = self.get_credential(credtype, username, password)
            if key is not None:
                self.add_key(cred_id, key)
            return cred_id
        else:
            return credentials

        Check if this credential has already been added to the database, if not add it in.
        
cme/protocols/ssh/database.py