Skip to content

Commit

Permalink
Merge all the tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Blat committed Nov 11, 2015
1 parent 429fd16 commit 2903802
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions data/presupuestos_municipales/import.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit 2903802

Please sign in to comment.