Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move total_value_year fields to new table #249

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/change_stat.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class ChangeStat < ApplicationRecord
belongs_to :habitat, -> { where(name: 'mangroves') }
belongs_to :geo_entity
has_many :change_stats_total_values
validates :habitat_id, uniqueness: { scope: :geo_entity_id }
end
3 changes: 3 additions & 0 deletions app/models/change_stats_total_value.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ChangeStatsTotalValue < ApplicationRecord
belongs_to :change_stat
end
11 changes: 11 additions & 0 deletions db/migrate/20230310154459_create_change_stats_total_values.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateChangeStatsTotalValues < ActiveRecord::Migration[5.1]
def change
create_table :change_stats_total_values do |t|
t.integer :change_stat_id
t.decimal :total_value, default: 0
t.integer :year

t.timestamps
end
end
end
10 changes: 9 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20210812135252) do
ActiveRecord::Schema.define(version: 20230310154459) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -34,6 +34,14 @@
t.index ["habitat_id"], name: "index_change_stats_on_habitat_id"
end

create_table "change_stats_total_values", force: :cascade do |t|
t.integer "change_stat_id"
t.decimal "total_value", default: "0.0"
t.integer "year"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "coastal_stats", force: :cascade do |t|
t.bigint "geo_entity_id"
t.decimal "total_coast_length", default: "0.0"
Expand Down
17 changes: 17 additions & 0 deletions lib/tasks/migrate_change_stats_total_values.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace :migrate do
desc 'Migrate total values from change stats table to new table'
task :change_stats_total_values => [:environment] do

YEARS = [1996,2007,2008,2009,2010,2015,2016]

ch = ChangeStat.all
ch.each do |c|
YEARS.each do |year|
total_value = c.send("total_value_#{year}")
c.change_stats_total_values.create(total_value: total_value, year: year)
end
end

puts "Data migration complete"
end
end