diff --git a/data/presupuestos_municipales/import.rb b/data/presupuestos_municipales/import.rb index e8805ae..3d39ed0 100644 --- a/data/presupuestos_municipales/import.rb +++ b/data/presupuestos_municipales/import.rb @@ -1,6 +1,7 @@ class PresupuestosMunicipales::DataImport def import_in_database! + years = [2010,2011,2012,2013,2014,2015] Dir.foreach(File.expand_path('../', __FILE__)) do |directory| next if ['.', '..'].include?(directory) year = directory @@ -10,6 +11,32 @@ def import_in_database! import_year_data(directory, year) end end + + create_yearable_tables + + years.each do |year| + sql = <<-SQL +insert into tb_economica (id, idente, tipreig, cdcta, importe, year) +select id, idente, tipreig, cdcta, importe, #{year} as year +FROM tb_economica_#{year} +SQL + + ActiveRecord::Base.connection.execute(sql) + + sql = <<-SQL +insert into tb_funcional (id, idente, cdcta, cdfgr, importe, year) +select id, idente, cdcta, cdfgr, importe, #{year} as year +FROM tb_funcional_#{year} +SQL + + ActiveRecord::Base.connection.execute(sql) + end + + ActiveRecord::Base.connection.execute(%Q{ALTER TABLE "tb_cuentasEconomica_2015" RENAME TO "tb_cuentasEconomica"}) + ActiveRecord::Base.connection.execute(%Q{ALTER TABLE "tb_cuentasProgramas_2015" RENAME TO "tb_cuentasProgramas"}) + ActiveRecord::Base.connection.execute(%Q{ALTER TABLE tb_inventario_2015 RENAME TO tb_inventario}) + + # TODO: remove the tables that are not used anymore (the tables with a year in the suffix) end private @@ -49,6 +76,32 @@ def import_file(file, year) puts "Renamed table to #{table_name}_#{year}" puts end + + def create_yearable_tables +sql = <<-SQL +DROP TABLE IF EXISTS tb_funcional; +CREATE TABLE "tb_funcional" ( + "id" NUMERIC(15,2), + "idente" NUMERIC(15,2), + "cdcta" VARCHAR(6), + "cdfgr" VARCHAR(6), + "importe" NUMERIC(15,2), + "year" smallint +); + +DROP TABLE IF EXISTS tb_economica; +CREATE TABLE "tb_economica" ( + "id" NUMERIC(15,2), + "idente" NUMERIC(15,2), + "tipreig" VARCHAR(1), + "cdcta" VARCHAR(6), + "importe" NUMERIC(15,2), + "year" smallint +); +SQL + + ActiveRecord::Base.connection.execute(sql) + end end