from sqlalchemy import Column, Integer, String, func
from app import db

class CnesEstablishment(db.Model):
    __tablename__                   = 'cnes_establishment'
    year                            = Column(Integer, primary_key=True)
    region                          = Column(String(1), primary_key=True)
    mesoregion                      = Column(String(4), primary_key=True)
    microregion                     = Column(String(5), primary_key=True)
    state                           = Column(String(2), primary_key=True)
    municipality                    = Column(String(7), primary_key=True)
    cnes                            = Column(String(7), primary_key=True)
    establishment_type              = Column(String(2), primary_key=True)
    sus_bond                        = Column(String(1), primary_key=True)
    provider_type                   = Column(String(2), primary_key=True)
    ambulatory_attention            = Column(String(1), primary_key=True)
    hospital_attention              = Column(String(1), primary_key=True)
    emergency_facilities            = Column(String(1), primary_key=True)
    ambulatory_care_facilities      = Column(String(1), primary_key=True)
    surgery_center_facilities       = Column(Integer, primary_key=True)
    obstetrical_center_facilities   = Column(Integer, primary_key=True)
    neonatal_unit_facilities        = Column(String(1), primary_key=True)
    hospital_care                   = Column(String(1), primary_key=True)
    selective_waste_collection      = Column(String(1), primary_key=True)
    dependency_level                = Column(String(1), primary_key=True)
    health_region                   = Column(String(5), primary_key=True)
    administrative_sphere           = Column(String(2), primary_key=True)
    tax_withholding                 = Column(String(2), primary_key=True)
    hierarchy_level                 = Column(String(2), primary_key=True)
    unit_type                       = Column(String(2), primary_key=True)


    @classmethod    
    def dimensions(cls):
        return [
            'year',
            'region',
            'mesoregion',
            'microregion',
            'state',
            'municipality',
        ]

    @classmethod
    def aggregate(cls, value):
        return {
            'establishments': func.count(cls.cnes)
        }[value]

    @classmethod
    def values(cls):
        return ['establishments']

@@ -684,26 +684,26 @@
     
-establishment_type
+unit_type         
     
@@ -1231,39 +1231,41 @@
        = Column(
-Integer
+String(1)
 , primary_key=Tr
@@ -1305,39 +1305,41 @@
 ties   = Column(
-Integer
+String(1)
 , primary_key=Tr
@@ -1938,82 +1938,8 @@
 ue)%0A
-    unit_type                       = Column(String(2), primary_key=True)%0A
 %0A%0A  
@@ -2115,16 +2115,16 @@
 state',%0A
-
         
@@ -2143,16 +2143,619 @@
 ality',%0A
+            'cnes',%0A            'unit_type',%0A            'sus_bond',%0A            'provider_type',%0A            'ambulatory_attention',%0A            'hospital_attention',%0A            'emergency_facilities',%0A            'ambulatory_care_facilities',%0A            'surgery_center_facilities',%0A            'obstetrical_center_facilities',%0A            'neonatal_unit_facilities',%0A            'hospital_care',%0A            'selective_waste_collection',%0A            'dependency_level',%0A            'health_region',%0A            'administrative_sphere',%0A            'tax_withholding',%0A            'hierarchy_level',%0A
         

Update cnes_establishment