Class: Goz::Database::Migrations

Inherits:
Object
  • Object
show all
Defined in:
lib/goz/database/migrations.rb

Overview

Goz::Database::Migrations - Database Migrations

See Also

Goz::Database

Author

blair christensen. <[email protected]>

Homepage

github.com/blairc/goz/

Class Method Summary collapse

Class Method Details

.migrate!Object

Perform database migrations.



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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/goz/database/migrations.rb', line 26

def self.migrate!
  Sequel.extension :migration

  # TODO Split!
  Sequel.migration do
    change do
      DB.create_table(:groups) do
        primary_key :id
        String      :display_name,   :null => false
        String      :identifier,     :null => false
        String      :klass,          :null => false
        String      :name,           :null => false
        timestamp   :created_at,     :null => false
        timestamp   :modified_at
        timestamp   :synchronized_at

        index :display_name,  :unique => true
        index :identifier,    :unique => true
        index :login,         :unique => true
      end
    end
  end

  Sequel.migration do
    change do
      DB.create_table(:services) do
        primary_key :id
        String      :name,           :null => false
        String      :klass,          :null => false
        timestamp   :created_at,     :null => false
        timestamp   :modified_at
        timestamp   :synchronized_at

        index :name, :unique => true
      end
    end
  end

  Sequel.migration do
    change do
      DB.create_table(:users) do
        primary_key :id
        String      :email,           :null => false
        String      :identifier,      :null => false
        String      :login,           :null => false
        String      :name,            :null => false
        String      :klass,           :null => false
        timestamp   :created_at,      :null => false
        timestamp   :modified_at
        timestamp   :synchronized_at

        index :email,       :unique => true
        index :identifier,  :unique => true
        index :login,       :unique => true
      end
    end
  end

  Sequel.migration do
    change do
      DB.create_table(:admins) do
        foreign_key :group_id,    :groups
        foreign_key :user_id,     :users

        index [ :group_id, :user_id ], :unique => true
      end
    end
  end

  Sequel.migration do
    change do
      DB.create_table(:members) do
        foreign_key :group_id,    :groups
        foreign_key :user_id,     :users

        index [ :group_id, :user_id ], :unique => true
      end
    end
  end

  Sequel.migration do
    change do
      DB.create_table(:group_services) do
        primary_key :id
        foreign_key :group_id,        :groups
        foreign_key :service_id,      :services
        timestamp   :synchronized_at

        index [ :group_id, :service_id ], :unique => true
      end
    end
  end

  Sequel.migration do
    change do
      DB.create_table(:event_types) do
        primary_key :id
        String      :name
        timestamp   :created_at,  :null => false

        index :name, :unique => true
      end
    end
  end

  # TODO Consider removing?
  Sequel.migration do
    change do
      event_types = DB[:event_types]
      # TODO More to add
      %w( admin_add         admin_remove
          group_add         group_remove          group_sync
          group_service_add group_service_remove  group_service_sync
          member_add        member_remove
          service_add       service_remove        service_sync
          user_add          user_remove           user_sync
      ).each do |type|
        event_types.insert( :name => type, :created_at => Time.now )
      end
    end
  end

  Sequel.migration do
    change do
      DB.create_table(:events) do
        primary_key :id
        String  :event_type,      :null => false
        String  :group,           :null => true
        Integer :group_id,        :null => true
        String  :service,         :null => true
        Integer :service_id,      :null => true
        String  :user,            :null => true
        Integer :user_id,         :null => true
        # TODO Some sort of boolean or enum completion status?
        timestamp   :created_at,  :null => false
        # TODO Some sort of completed_at timestamp?

        index :event_type
        index :group_id
        index :service_id
        index :user_id
      end
    end
  end

end