<commit_before>import configparser

from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy import create_engine
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

def create_engine_str():
    config = configparser.ConfigParser()
    config.read('config/database.ini')

    config_module = 'database'
    db_engine = config.get(config_module, 'engine')
    login = config.get(config_module, 'user')
    passwd = config.get(config_module, 'passwd')
    host = config.get(config_module, 'host')
    port = config.get(config_module, 'port')
    db_name = config.get(config_module, 'db_name')

    return '{}://{}:{}@{}:{}/{}'.format(db_engine, login, passwd, host, port, db_name)

engine = create_engine(create_engine_str(), echo=True)

Session = sessionmaker(bind=engine)

class EntityType(Base):
    __tablename__ = 'Entity_Types'

    id = Column(Integer, primary_key=True)
    name = Column(String(255))


class TagAttribute(Base):
    __tablename__ = 'Tag_Attributes'

    id = Column(Integer, primary_key=True)
    name = Column(String(255), nullable=False)
    entity_type_id_fk = Column(Integer, ForeignKey('Entity_Types.id', ondelete='CASCADE'), nullable=False)

    entity_type = relationship('EntityType', backref='tags', primaryjoin='EntityType.id == TagAttribute.entity_type_id_fk')


class SeriesAttribute(Base):
    __tablename__ = 'Series_Attributes'

    id = Column(Integer, primary_key=True)
    name = Column(String(255), nullable=False)
    entity_type_id_fk = Column(Integer, ForeignKey('Entity_Types.id', ondelete='CASCADE'), nullable=False)

    entity_type = relationship('EntityType', backref='series', primaryjoin='EntityType.id == SeriesAttribute.entity_type_id_fk')


class MetaAttribute(Base):
    __tablename__ = 'Meta_Attributes'

    id = Column(Integer, primary_key=True)
    name = Column(String(255), nullable=False)
    entity_type_id_fk = Column(Integer, ForeignKey('Entity_Types.id', ondelete='CASCADE'), nullable=False)

    entity_type = relationship('EntityType', backref='meta', primaryjoin='EntityType.id == MetaAttribute.entity_type_id_fk')


class Entity(Base):
    __tablename__ = 'Entities'

    id = Column(Integer, primary_key=True)
    entity_type_id_fk = Column(Integer, ForeignKey('Entity_Types.id', ondelete='CASCADE'), nullable=False)
    parent_id_fk = Column(Integer, ForeignKey('Entities.id', ondelete='CASCADE'), nullable=True)

    entity_type = relationship('EntityType', primaryjoin='Entity.entity_type_id_fk == EntityType.id')
    parent = relationship('Entity', primaryjoin='Entity.parent_id_fk == Entity.id')


class EntityTag(Base):
    __tablename__ = 'Entity_Tags'

    entity_id_fk = Column(Integer, ForeignKey('Entities.id', ondelete='CASCADE'), primary_key=True)
    tag_id_fk = Column(Integer, ForeignKey('Tag_Attributes.id', ondelete='CASCADE'), primary_key=True)
    value = Column(String(255), nullable=False)

    name = relationship('TagAttribute', primaryjoin='EntityTag.tag_id_fk == TagAttribute.id')
    entity = relationship('Entity', backref='tags', primaryjoin='Entity.id == EntityTag.entity_id_fk')

class EntityMeta(Base):
    __tablename__ = 'Entity_Meta'

    entity_id_fk = Column(Integer, ForeignKey('Entities.id', ondelete='CASCADE'), primary_key=True)
    meta_id_fk = Column(Integer, ForeignKey('Meta_Attributes.id', ondelete='CASCADE'), primary_key=True)
    value = Column(String(255), nullable=False)

    name = relationship('MetaAttribute', primaryjoin='EntityMeta.meta_id_fk == MetaAttribute.id')
    entity = relationship('Entity', backref='meta', primaryjoin='Entity.id == EntityMeta.entity_id_fk')

<commit_msg>(19) Change cascade deletes to delete_ts<commit_after>import configparser

from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy import create_engine
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.orm.query import Query
from sqlalchemy.ext.declarative import declarative_base

def create_engine_str():
    config = configparser.ConfigParser()
    config.read('config/database.ini')

    config_module = 'database'
    db_engine = config.get(config_module, 'engine')
    login = config.get(config_module, 'user')
    passwd = config.get(config_module, 'passwd')
    host = config.get(config_module, 'host')
    port = config.get(config_module, 'port')
    db_name = config.get(config_module, 'db_name')

    return '{}://{}:{}@{}:{}/{}'.format(db_engine, login, passwd, host, port, db_name)


Base = declarative_base()

class FilterQuery(Query):
    def get(self, ident):
        return super(self, FilterQuery).get(self.populate_existing(), ident)

    def __iter__(self):
        return super(self, FilterQuery).__iter__(self.filter())

    def from_self(self, *ent):
        return Query.from_self(self.filter(), *ent)

    def filter(self):
        mapper = self._mapper_zero()
        if mapper is not None:
            crit = mapper.class_.delete_ts == None
            return self.enable_assertions(False).filter(crit)
        else:
            return self

engine = create_engine(create_engine_str(), echo=True)

Session = sessionmaker(bind=engine, query_cls=FilterQuery)

class EntityType(Base):
    __tablename__ = 'Entity_Types'

    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    delete_ts = Column(Integer, nullable=True, default=None)


class TagAttribute(Base):
    __tablename__ = 'Tag_Attributes'

    id = Column(Integer, primary_key=True)
    name = Column(String(255), nullable=False)
    entity_type_id_fk = Column(Integer, ForeignKey('Entity_Types.id'), nullable=False)
    delete_ts = Column(Integer, nullable=True, default=None)

    entity_type = relationship('EntityType', backref='tags', primaryjoin='EntityType.id == TagAttribute.entity_type_id_fk')


class SeriesAttribute(Base):
    __tablename__ = 'Series_Attributes'

    id = Column(Integer, primary_key=True)
    name = Column(String(255), nullable=False)
    entity_type_id_fk = Column(Integer, ForeignKey('Entity_Types.id'), nullable=False)
    delete_ts = Column(Integer, nullable=True, default=None)

    entity_type = relationship('EntityType', backref='series', primaryjoin='EntityType.id == SeriesAttribute.entity_type_id_fk')


class MetaAttribute(Base):
    __tablename__ = 'Meta_Attributes'

    id = Column(Integer, primary_key=True)
    name = Column(String(255), nullable=False)
    entity_type_id_fk = Column(Integer, ForeignKey('Entity_Types.id'), nullable=False)
    delete_ts = Column(Integer, nullable=True, default=None)

    entity_type = relationship('EntityType', backref='meta', primaryjoin='EntityType.id == MetaAttribute.entity_type_id_fk')


class Entity(Base):
    __tablename__ = 'Entities'

    id = Column(Integer, primary_key=True)
    entity_type_id_fk = Column(Integer, ForeignKey('Entity_Types.id'), nullable=False)
    parent_id_fk = Column(Integer, ForeignKey('Entities.id'), nullable=True)
    delete_ts = Column(Integer, nullable=True, default=None)

    entity_type = relationship('EntityType', primaryjoin='Entity.entity_type_id_fk == EntityType.id')
    parent = relationship('Entity', primaryjoin='Entity.parent_id_fk == Entity.id')


class EntityTag(Base):
    __tablename__ = 'Entity_Tags'

    entity_id_fk = Column(Integer, ForeignKey('Entities.id'), primary_key=True)
    tag_id_fk = Column(Integer, ForeignKey('Tag_Attributes.id'), primary_key=True)
    value = Column(String(255), nullable=False)
    delete_ts = Column(Integer, nullable=True, default=None)

    name = relationship('TagAttribute', primaryjoin='EntityTag.tag_id_fk == TagAttribute.id')
    entity = relationship('Entity', backref='tags', primaryjoin='Entity.id == EntityTag.entity_id_fk')

class EntityMeta(Base):
    __tablename__ = 'Entity_Meta'

    entity_id_fk = Column(Integer, ForeignKey('Entities.id'), primary_key=True)
    meta_id_fk = Column(Integer, ForeignKey('Meta_Attributes.id'), primary_key=True)
    value = Column(String(255), nullable=False)
    delete_ts = Column(Integer, nullable=True, default=None)

    name = relationship('MetaAttribute', primaryjoin='EntityMeta.meta_id_fk == MetaAttribute.id')
    entity = relationship('Entity', backref='meta', primaryjoin='Entity.id == EntityMeta.entity_id_fk')


{'fraction_non_alphanumeric': 0.12788739172281038, 'fraction_numerical': 0.004571703561116458, 'mean_word_length': 5.540519276160503, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4355614', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2585, 'n_words': 480}