Class: PlantWatchdog::Model::Schema

Inherits:
ActiveRecord::Migration
  • Object
show all
Defined in:
lib/plantwatchdog/model.rb

Class Method Summary collapse

Class Method Details

.downObject

self.up



131
132
133
134
135
136
137
138
# File 'lib/plantwatchdog/model.rb', line 131

def self.down
  drop_table :syncs
  drop_table :devices
  drop_table :plants
  drop_table :users
  drop_table :measurement_chunks
  drop_table :metadata
end

.upObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
# File 'lib/plantwatchdog/model.rb', line 70

def self.up

  create_table :users do |t|
    t.column :id, :integer # prim key
    t.column :timezone, :string # for converting UTC to local time
    t.column :password, :string
    t.column :name, :string
    # *_year is a performance optimization: it is used from the UI to find the years
    # for which there is data avalailable.
    # it could always be restored by looking at the available measurement_chunks
    t.column :start_year, :integer
    t.column :end_year, :integer
  end

  create_table :plants do |t|
    t.column :id, :integer # prim key
    t.column :user_id, :integer # foreign key
    t.column :aggregationrule_id, :integer # foreign key
  end

  # single table inheritance: inverter and sunmeter are devices
  create_table :devices do |t|
    t.column :type, :text # single table inheritance
    t.column :plant_id, :integer # foreign key
    t.column :aggregationrule_id, :integer # foreign key
    t.column :metadata_id, :integer # foreign key
    t.column :name, :string
    # the unique id is needed for the synchronization in order to assign measurements to a device
    # an example for a unique would be the serial number of an inverter
    t.column :unique_id, :string
  end

  create_table :measurement_chunks do |t|
    t.column :type, :text # for single table inheritance
    t.column :device_id, :integer
    t.column :plant_id, :integer # a Measurement Aggregate can belong to a plant instead of a device

    # the day on which data was collected according to local time
    t.column :time_year, :integer
    t.column :time_day_of_year, :integer

    t.column :data, :text, :limit => 16000000 # raw csv, limit to 16MB
    t.column :metadata_id, :integer # description of the columns of data
  end

  # metadata, used for describing the content of measurements and for describing aggregation rules
  # description must not be altered. Instead, create new records
  # whenever the column description changes
  create_table :metadata do |t|
    t.column :description, :string # serialized representation (JSON)
  end

  # performance only, could be retrieved by scanning the measurements table
  create_table :syncs do |t|
    t.column :user_id, :integer # foreign key
    t.column :device_id, :integer
    t.column :last_time, :integer # utc, epoch secs
  end

end