Repository: mindsdb/mindsdb

Issue:
Streams are still exists if parent integration is deleted
**Describe the bug**
Streams are still exists if parent integration is deleted

**To Reproduce**
Steps to reproduce the behavior, for example:
1. Create a Stream Integration
2. Create a Stream which is based on this integration
3.Delete the integration
4. check db. `select * from stream;`
Note, that created stream is still exist while its parent integration is deleted.

**Expected behavior**
Need to delete all related streams in case of deleting a stream integration



Discussion:

Resolving patch:
diff --git a/mindsdb/integrations/base/integration.py b/mindsdb/integrations/base/integration.py
--- a/mindsdb/integrations/base/integration.py
+++ b/mindsdb/integrations/base/integration.py
@@ -37,6 +37,7 @@ def setup(self):
         Thread(target=StreamIntegration._loop, args=(self,)).start()
 
     def _loop(self):
+        log.info("INTEGRATION %s: starting", self.name)
         while not STOP_THREADS_EVENT.wait(1.0):
             if self._control_stream is not None:
                 # Create or delete streams based on messages from control_stream
@@ -104,8 +105,10 @@ def _loop(self):
             # Start new streams found in DB
             for s in stream_db_recs:
                 if s.name not in map(lambda x: x.name, self._streams):
+                    log.info("INTEGRATION %s: starting stream - %s", self.name, s.name)
                     self._streams.append(self._make_stream(s))
 
+        log.info("INTEGRATION %s: stopping", self.name)
         for s in self._streams:
             s.stop_event.set()
 
diff --git a/mindsdb/interfaces/storage/db.py b/mindsdb/interfaces/storage/db.py
--- a/mindsdb/interfaces/storage/db.py
+++ b/mindsdb/interfaces/storage/db.py
@@ -4,7 +4,7 @@
 
 import numpy as np
 from sqlalchemy import create_engine, orm, types, UniqueConstraint
-from sqlalchemy.orm import scoped_session, sessionmaker
+from sqlalchemy.orm import scoped_session, sessionmaker, relationship
 from sqlalchemy.ext.declarative import declarative_base
 from sqlalchemy import Column, Integer, String, DateTime, Boolean, Index
 from sqlalchemy.sql.expression import null
@@ -109,6 +109,7 @@ class Predictor(Base):
     code = Column(String, nullable=True)
     lightwood_version = Column(String, nullable=True)
     dtype_dict = Column(Json, nullable=True)
+    streams = relationship("Stream", cascade="all, delete")
 
 
 class AITable(Base):
@@ -145,6 +146,7 @@ class Integration(Base):
     name = Column(String, nullable=False, unique=True)
     data = Column(Json)
     company_id = Column(Integer)
+    streams = relationship("Stream", cascade="all, delete")
 
 
 class Stream(Base):
@@ -155,8 +157,8 @@ class Stream(Base):
     stream_out = Column(String, nullable=False)
     anomaly_stream = Column(String)
     learning_stream = Column(String)
-    integration = Column(String, ForeignKey('integration.name', ondelete='CASCADE'), nullable=True)
-    predictor = Column(String, ForeignKey('predictor.name', ondelete='CASCADE'), nullable=False)
+    integration = Column(String, ForeignKey(Integration.name))
+    predictor = Column(String, ForeignKey(Predictor.name), nullable=False)
     company_id = Column(Integer)
     updated_at = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)
     created_at = Column(DateTime, default=datetime.datetime.now)


Test patch: