# Path: odpw/core/model.py
# class Portal(Base):
# class PortalSnapshot(Base):
# class Serializable(object):
# class PortalSnapshotDynamicity(Base,Serializable):
# class PortalSnapshotQuality(Base):
# class Dataset(Base):
# class DatasetData(Base):
# class DatasetQuality(Base):
# class MetaResource(Base):
# class ResourceInfo(Base):
# class ResourceCrawlLog(Base):
# class ResourceHistory(Base):
# class ResourceFreshness(Base):
# class FormatDist(Base):
#     def snapshot_count(self):
#     def snapshot_count(cls):
#     def first_snapshot(self):
#     def first_snapshot(cls):
#     def last_snapshot(self):
#     def last_snapshot(cls):
#     def datasetcount(self):
#     def datasetcount(cls):
#     def resourcecount(self):
#     def resourcecount(cls):
#     def __repr__(self):
#     def fetchtime(self):
#     def __repr__(self):
#     def to_dict(self):
#     def dyratio(self):
#     def adddelratio(self):
#     def addRatio(self):
#     def delRatio(self):
#     def updatedRatio(self):
#     def staticRatio(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def info(cls):
#     def __repr__(self):


from collections import defaultdict
from sqlalchemy import create_engine, inspect
from sqlalchemy import event
from sqlalchemy import exc
from sqlalchemy.engine import reflection
from sqlalchemy.orm import sessionmaker
from sqlalchemy.schema import (
    MetaData,
    Table,
    DropTable,
    ForeignKeyConstraint,
    DDL
)
from sqlalchemy.sql.ddl import DropConstraint
from odpw.core.model import Dataset, Base, tab_datasets, tab_resourcesinfo, ResourceInfo, tab_resourcescrawllog
import os
import structlog
import time
                      IF NOT FOUND THEN
                        EXECUTE
                          'CREATE TABLE '
                          || quote_ident(_table_name)
                          || ' (CHECK ("snapshot" = '
                          || _snapshot::smallint
                          || ')) INHERITS (""" + tab_resourcesinfo + """)';

                        -- Indexes are defined per child, so we assign a default index that uses the partition columns
                        EXECUTE 'ALTER TABLE '  || quote_ident(_table_name)|| ' ADD CONSTRAINT ' || quote_ident(_table_name||'_pkey') || ' PRIMARY KEY (uri, snapshot)';
                        EXECUTE 'CREATE INDEX ' || quote_ident(_table_name||'_status') || ' ON '||quote_ident(_table_name) || ' (status)';
                        EXECUTE 'CREATE INDEX ' || quote_ident(_table_name||'_uri') || ' ON '||quote_ident(_table_name) || ' (uri)';
                      END IF;

                      EXECUTE
                        'INSERT INTO '
                        || quote_ident(_table_name)
                        || ' VALUES ($1.*)'
                      USING NEW;



                      RETURN NULL;
                END;

                $$ LANGUAGE plpgsql;
                """)
        self.resourcesinfo_insert_trigger = DDL(
            """
            CREATE TRIGGER resourcesinfo_insert_trigger

# Path: odpw/core/model.py
# class Portal(Base):
# class PortalSnapshot(Base):
# class Serializable(object):
# class PortalSnapshotDynamicity(Base,Serializable):
# class PortalSnapshotQuality(Base):
# class Dataset(Base):
# class DatasetData(Base):
# class DatasetQuality(Base):
# class MetaResource(Base):
# class ResourceInfo(Base):
# class ResourceCrawlLog(Base):
# class ResourceHistory(Base):
# class ResourceFreshness(Base):
# class FormatDist(Base):
#     def snapshot_count(self):
#     def snapshot_count(cls):
#     def first_snapshot(self):
#     def first_snapshot(cls):
#     def last_snapshot(self):
#     def last_snapshot(cls):
#     def datasetcount(self):
#     def datasetcount(cls):
#     def resourcecount(self):
#     def resourcecount(cls):
#     def __repr__(self):
#     def fetchtime(self):
#     def __repr__(self):
#     def to_dict(self):
#     def dyratio(self):
#     def adddelratio(self):
#     def addRatio(self):
#     def delRatio(self):
#     def updatedRatio(self):
#     def staticRatio(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def info(cls):
#     def __repr__(self):


# Path: odpw/core/db.py
from collections import defaultdict
from sqlalchemy import create_engine, inspect
from sqlalchemy import event
from sqlalchemy import exc
from sqlalchemy.engine import reflection
from sqlalchemy.orm import sessionmaker
from sqlalchemy.schema import (
    MetaData,
    Table,
    DropTable,
    ForeignKeyConstraint,
    DDL
)
from sqlalchemy.sql.ddl import DropConstraint
from odpw.core.model import Dataset, Base, tab_datasets, tab_resourcesinfo, ResourceInfo, tab_resourcescrawllog
import os
import structlog
import time

                      IF NOT FOUND THEN
                        EXECUTE
                          'CREATE TABLE '
                          || quote_ident(_table_name)
                          || ' (CHECK ("snapshot" = '
                          || _snapshot::smallint
                          || ')) INHERITS (""" + tab_resourcesinfo + """)';

                        -- Indexes are defined per child, so we assign a default index that uses the partition columns
                        EXECUTE 'ALTER TABLE '  || quote_ident(_table_name)|| ' ADD CONSTRAINT ' || quote_ident(_table_name||'_pkey') || ' PRIMARY KEY (uri, snapshot)';
                        EXECUTE 'CREATE INDEX ' || quote_ident(_table_name||'_status') || ' ON '||quote_ident(_table_name) || ' (status)';
                        EXECUTE 'CREATE INDEX ' || quote_ident(_table_name||'_uri') || ' ON '||quote_ident(_table_name) || ' (uri)';
                      END IF;

                      EXECUTE
                        'INSERT INTO '
                        || quote_ident(_table_name)
                        || ' VALUES ($1.*)'
                      USING NEW;



                      RETURN NULL;
                END;

                $$ LANGUAGE plpgsql;
                """)
        self.resourcesinfo_insert_trigger = DDL(
            """
            CREATE TRIGGER resourcesinfo_insert_trigger

            BEFORE INSERT ON """ + tab_resourcesinfo + """