Class: BehaviorLens::Database

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

Class Method Summary collapse

Class Method Details

.connect(database, username, password, host) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/behavior_lens/database.rb', line 5

def self.connect(database, username, password, host)
  ActiveRecord::Base.establish_connection(
    adapter: 'mysql2',
    host: host,
    database: database,
    username: username,
    password: password
  )
end

.setupObject



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
# File 'lib/behavior_lens/database.rb', line 15

def self.setup
  ActiveRecord::Schema.define do
    connection = ActiveRecord::Base.connection

    # Check and create 'clicks' table
    unless connection.data_source_exists?('clicks')
      create_table :clicks do |t|
        t.string :link, null: false
        t.integer :count, default: 1
        t.timestamps
      end
    end

    # Check and create 'sessions' table
    unless connection.data_source_exists?('sessions')
      create_table :sessions do |t|
        t.string :user_id, null: false
        t.datetime :start_time
        t.datetime :end_time
        t.timestamps
      end
    end

    # Check and create 'events' table
    unless connection.data_source_exists?('events')
      create_table :events do |t|
        t.string :name, null: false
        t.json :metadata # JSON is fully supported in MySQL with ActiveRecord 7+
        t.timestamps
      end
    end
  end
end