Class: LedgerWeb::Database
- Inherits:
-
Object
- Object
- LedgerWeb::Database
- Defined in:
- lib/ledger_web/db.rb
Class Method Summary collapse
- .close ⇒ Object
- .connect ⇒ Object
- .dump_ledger_to_csv ⇒ Object
- .handle ⇒ Object
- .load_database(file) ⇒ Object
- .load_prices ⇒ Object
- .parse_cost(cost) ⇒ Object
- .run_migrations ⇒ Object
Class Method Details
.close ⇒ Object
14 15 16 |
# File 'lib/ledger_web/db.rb', line 14 def self.close @@db.disconnect end |
.connect ⇒ Object
9 10 11 12 |
# File 'lib/ledger_web/db.rb', line 9 def self.connect @@db = Sequel.connect(LedgerWeb::Config.instance.get(:database_url)) self.run_migrations() end |
.dump_ledger_to_csv ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ledger_web/db.rb', line 31 def self.dump_ledger_to_csv ledger_bin_path = LedgerWeb::Config.instance.get :ledger_bin_path ledger_file = LedgerWeb::Config.instance.get :ledger_file ledger_format = LedgerWeb::Config.instance.get :ledger_format puts "Dumping ledger to file..." file = Tempfile.new('ledger') system "#{ledger_bin_path} -f #{ledger_file} --format='#{ledger_format}' reg > #{file.path}" replaced_file = Tempfile.new('ledger') replaced_file.write(file.read.gsub('\"', '""')) replaced_file.flush puts "Dump finished" return replaced_file end |
.handle ⇒ Object
18 19 20 |
# File 'lib/ledger_web/db.rb', line 18 def self.handle @@db end |
.load_database(file) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ledger_web/db.rb', line 47 def self.load_database(file) counter = 0 @@db.transaction do LedgerWeb::Config.instance.run_hooks(:before_load, @@db) puts "Clearing ledger table...." @@db["DELETE FROM ledger"].delete puts "Done clearing ledger table" puts "Loading into database...." CSV.foreach(file.path) do |row| counter += 1 row = Hash[*[ :xtn_id, :xtn_date, :note, :account, :commodity, :amount, :cleared, :virtual, :tags, :cost ].zip(row).flatten] xtn_date = Date.strptime(row[:xtn_date], '%Y/%m/%d') row[:xtn_month] = xtn_date.strftime('%Y/%m/01') row[:xtn_year] = xtn_date.strftime('%Y/01/01') row[:cost] = parse_cost(row[:cost]) row = LedgerWeb::Config.instance.run_hooks(:before_insert_row, row) @@db[:ledger].insert(row) LedgerWeb::Config.instance.run_hooks(:after_insert_row, row) end puts "Running after_load hooks" LedgerWeb::Config.instance.run_hooks(:after_load, @@db) end puts "Analyzing ledger table" @@db.fetch('VACUUM ANALYZE ledger').all puts "Done!" counter end |
.load_prices ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/ledger_web/db.rb', line 104 def self.load_prices query = <<HERE select commodity, min_date, case when amount = 0 then max_date else now()::date end as max_date from ( select commodity, min(xtn_date) as min_date, max(xtn_date) as max_date, sum(amount) as amount from ledger group by commodity ) x HERE puts " Deleting prices" @@db["DELETE FROM prices"].delete rows = @@db.fetch(query) proc = LedgerWeb::Config.instance.get :price_function skip = LedgerWeb::Config.instance.get :price_lookup_skip_symbols puts "Loading prices" rows.each do |row| if skip.include?(row[:commodity]) next end prices = proc.call(row[:commodity], row[:min_date], row[:max_date]) prices.each do |price| @@db[:prices].insert(:commodity => row[:commodity], :price_date => price[0], :price => price[1]) end end @@db.fetch("analyze prices").all puts "Done loading prices" end |
.parse_cost(cost) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/ledger_web/db.rb', line 94 def self.parse_cost(cost) match = cost.match(/([\d\.-]+) (.+) {(.+)} \[(.+)\]/) if match amount = match[1].to_f price = match[3].gsub(/[^\d\.-]/, '').to_f return price * amount end cost.gsub(/[^\d\.-]/, '') end |
.run_migrations ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/ledger_web/db.rb', line 22 def self.run_migrations Sequel::Migrator.apply(@@db, File.join(File.dirname(__FILE__), "db/migrate")) user_migrations = LedgerWeb::Config.instance.get :user_migrate_dir if not user_migrations.nil? Sequel::Migrator.run(@@db, user_migrations, :table => "user_schema_changes") end end |