Skip to content

Commit

Permalink
Execution data
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Blat committed Jan 18, 2016
1 parent 98df186 commit 479dd22
Show file tree
Hide file tree
Showing 75 changed files with 28 additions and 100 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
21 changes: 0 additions & 21 deletions data/presupuestos_municipales/2014/archive/load_data.sh

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
21 changes: 0 additions & 21 deletions data/presupuestos_municipales/2015/archive/load_data.sh

This file was deleted.

86 changes: 28 additions & 58 deletions data/presupuestos_municipales/import.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
class PresupuestosMunicipales::DataImport

def import_in_database!
# years = [2010,2011,2012,2013,2014,2015]
def initialize(database, what)
@database = database
@what = what
end

def import_in_database
Dir.foreach(File.expand_path('../', __FILE__)) do |directory|
next if ['.', '..'].include?(directory)
year = directory
Expand All @@ -12,46 +16,24 @@ def import_in_database!
end
end

# create_yearable_tables
#
# TODO: we don't need this, because we read the data from each year table
# 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
# TODO: do we need to rename the tables?
# 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)
# Clean tables
db.execute(<<SQL)
-- drop table
SQL
end

private

def import_year_data(folder, year)
base_path = folder + '/archive/'
base_path = folder + "/#{@what}/"

Dir.foreach(base_path) do |file|
if relevant_file?(file)
import_file(base_path + file, year)
end
end

ActiveRecord::Base.connection.execute(<<SQL)
db.execute(<<SQL)
update "tb_cuentasEconomica_#{year}" set cdcta = LTRIM(RTRIM(cdcta));
update "tb_cuentasEconomica_#{year}" set nombre = LTRIM(RTRIM(nombre));
update "tb_cuentasProgramas_#{year}" set cdfgr = LTRIM(RTRIM(cdfgr));
Expand All @@ -70,40 +52,28 @@ def relevant_file?(file)

def import_file(file, year)
table_name = File.basename(file, '.sql.gz')
%x(gunzip < #{file} | psql #{ActiveRecord::Base.configurations[Rails.env]['database']})
%x(gunzip < #{file} | psql #{@database})

puts "Imported file #{file}"
puts "Imported file #{file} in database #{@database}"

ActiveRecord::Base.connection.execute(%Q{ALTER TABLE "#{table_name}" RENAME TO "#{table_name}_#{year}"})
db.execute(%Q{ALTER TABLE "#{table_name}" RENAME TO "#{table_name}_#{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)
def db
@db ||= begin
ActiveRecord::Base.establish_connection ActiveRecord::Base.configurations[Rails.env].merge('database' => @database)
ActiveRecord::Base.connection
end
end
end

PresupuestosMunicipales::DataImport.new.import_in_database!
database = ARGV[0].strip
what = ARGV[1].strip

raise 'Missing database name' if database.blank?
raise 'Invalid type option' unless %{ planned executed }.include?(what)

importer = PresupuestosMunicipales::DataImport.new(database, what)
importer.import_in_database

0 comments on commit 479dd22

Please sign in to comment.