Class: SnowmanIO::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/snowman-io/migration.rb

Overview

Helpfull class to seed data and update DB

Instance Method Summary collapse

Constructor Details

#initializeMigration

Returns a new instance of Migration.



4
5
6
7
8
# File 'lib/snowman-io/migration.rb', line 4

def initialize
  @db = Mongoid::Sessions.default
  @done_versions = @db[:snowman_io_schema].find.map { |c| c["version"] }
  @migration_versions = []
end

Instance Method Details

#migrateObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
# File 'lib/snowman-io/migration.rb', line 10

def migrate
  SnowmanIO.logger.info "Migration start"

  migration "Set boundary template for all checks" do
    @db[:snowman_io_checks].find().update_all("$set" => {"template" => "boundary"})
  end

  migration "boundary => last_value_limit" do
    @db[:snowman_io_checks].find({template: "boundary"}).update_all("$set" => {"template" => "last_value_limit"})
  end

  migration "daily_datapoints => prev_day_datapoints_limit" do
    @db[:snowman_io_checks].find({template: "daily_datapoints"}).update_all("$set" => {"template" => "prev_day_datapoints_limit"})
  end

  migration "set owner to each check" do
    if user = @db[:snowman_io_users].find.first
      @db[:snowman_io_checks].find.update_all("$set" => {"user_id" => user["_id"]})
    end
  end

  migration "users timestamps" do
    @db[:snowman_io_users].find.update_all("$set" => {"created_at" => Time.now})
    @db[:snowman_io_users].find.update_all("$set" => {"updated_at" => Time.now})
  end

  migration "users daily_report" do
    @db[:snowman_io_users].find.update_all("$set" => {"daily_report" => true})
  end

  migration "users invite_token" do
    @db[:snowman_io_users].find.update_all("$set" => {"invite_token" => ""})
  end

  migration "users invite_send_count" do
    @db[:snowman_io_users].find.update_all("$set" => {"invite_send_count" => 0})
  end

  migration "users status" do
    @db[:snowman_io_users].find.update_all("$set" => {"status" => "wait_invite"})
    @db[:snowman_io_users].where(invite_token: "").update_all("$set" => {"status" => "active"})
  end

  migration "users name" do
    @db[:snowman_io_users].where(status: "active").each do |record|
      @db[:snowman_io_users].where(_id: record["_id"]).update_all("$set" => {"name" => record["email"]})
    end
  end

  migration "remove next_report_date setting" do
    @db[:snowman_io_settings].where(name: "next_report_date").remove_all
  end

  migration "user daily_report" do
    @db[:snowman_io_users].find.update_all("$unset" => {"daily_report" => 1})
  end

  migration "add sys app" do
    @db[:snowman_io_apps].find.update_all("$set" => {"system" => false})
    @db[:snowman_io_apps].insert(name: "Snowman", system: true)
  end

  SnowmanIO.logger.info "Migration done"
end