# Path: federal_spending/usaspending/scripts/usaspending/config.py
# INDEX_COLS_BY_TABLE = {
#     'usaspending_contract': [
#         'using gin (to_tsvector(\'federal_spending\'::regconfig, agency_name::text))',
#         'statecode, congressionaldistrict',
#         'using gin (to_tsvector(\'federal_spending\'::regconfig, contracting_agency_name::text))',
#         '(fiscal_year DESC, obligatedamount DESC)',
#         'dunsnumber',
#         'obligatedamount',
#         'piid',
#         'using gin (to_tsvector(\'federal_spending\'::regconfig, requesting_agency_name::text))',
#         'signeddate',
#         'using gin (to_tsvector(\'federal_spending\'::regconfig, city::text))',
#         'using gin (to_tsvector(\'federal_spending\'::regconfig, vendorname::text))',
#         'fiscal_year',
#         'unique_transaction_id',
#         'id'
#     ],
#     'usaspending_grant': [
#         'using gin (to_tsvector(\'federal_spending\'::regconfig, agency_name::text)) ',
#         'using gin (to_tsvector(\'federal_spending\'::regconfig, recipient_name::text))',
#         'total_funding_amount',
#         'unique_transaction_id',
#         'fiscal_year',
#         'id'
#     ],
# }


from django.core.management.base import CommandError, BaseCommand
from django.db import connections, transaction
from django.conf import settings
from federal_spending.usaspending.scripts.usaspending.config import INDEX_COLS_BY_TABLE

class Command(BaseCommand):

    @transaction.commit_on_success
    def handle(self, *args, **kwargs):
        """
        Takes a relation name and a fiscal year and creates a partition for it.
        Current relation names for spending data are:
            usaspending_contract
            usaspending_grant
        """
        grants_base = 'usaspending_grant'
        contracts_base = 'usaspending_contract'
        c = connections['default'].cursor()

        for fy in settings.FISCAL_YEARS:
            self.create_partition_indexes(c, contracts_base, "{0}_{1}".format(contracts_base, fy))
            self.create_partition_indexes(c, grants_base, "{0}_{1}".format(grants_base, fy))

        print "creating overall contract indexes"
        for statement in contracts_idx:
            print "executing '{0}'".format(statement)
            c.execute(statement)
        print "creating overall grant indexes"
        for statement in grants_idx:
            print "executing '{0}'".format(statement)
            c.execute(statement)

    def create_partition_indexes(self, c, base_table, partition_name):
        

# Path: federal_spending/usaspending/scripts/usaspending/config.py
# INDEX_COLS_BY_TABLE = {
#     'usaspending_contract': [
#         'using gin (to_tsvector(\'federal_spending\'::regconfig, agency_name::text))',
#         'statecode, congressionaldistrict',
#         'using gin (to_tsvector(\'federal_spending\'::regconfig, contracting_agency_name::text))',
#         '(fiscal_year DESC, obligatedamount DESC)',
#         'dunsnumber',
#         'obligatedamount',
#         'piid',
#         'using gin (to_tsvector(\'federal_spending\'::regconfig, requesting_agency_name::text))',
#         'signeddate',
#         'using gin (to_tsvector(\'federal_spending\'::regconfig, city::text))',
#         'using gin (to_tsvector(\'federal_spending\'::regconfig, vendorname::text))',
#         'fiscal_year',
#         'unique_transaction_id',
#         'id'
#     ],
#     'usaspending_grant': [
#         'using gin (to_tsvector(\'federal_spending\'::regconfig, agency_name::text)) ',
#         'using gin (to_tsvector(\'federal_spending\'::regconfig, recipient_name::text))',
#         'total_funding_amount',
#         'unique_transaction_id',
#         'fiscal_year',
#         'id'
#     ],
# }


# Path: federal_spending/usaspending/management/commands/create_indexes.py
from django.core.management.base import CommandError, BaseCommand
from django.db import connections, transaction
from django.conf import settings
from federal_spending.usaspending.scripts.usaspending.config import INDEX_COLS_BY_TABLE


class Command(BaseCommand):

    @transaction.commit_on_success
    def handle(self, *args, **kwargs):
        """
        Takes a relation name and a fiscal year and creates a partition for it.
        Current relation names for spending data are:
            usaspending_contract
            usaspending_grant
        """
        grants_base = 'usaspending_grant'
        contracts_base = 'usaspending_contract'
        c = connections['default'].cursor()

        for fy in settings.FISCAL_YEARS:
            self.create_partition_indexes(c, contracts_base, "{0}_{1}".format(contracts_base, fy))
            self.create_partition_indexes(c, grants_base, "{0}_{1}".format(grants_base, fy))

        print "creating overall contract indexes"
        for statement in contracts_idx:
            print "executing '{0}'".format(statement)
            c.execute(statement)
        print "creating overall grant indexes"
        for statement in grants_idx:
            print "executing '{0}'".format(statement)
            c.execute(statement)

    def create_partition_indexes(self, c, base_table, partition_name):
        

        for i, colname in enumerate(INDEX_COLS_BY_TABLE[base_table]):