File: postgresql_schema.rb

package info (click to toggle)
ruby-activerecord-import 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 840 kB
  • sloc: ruby: 4,698; makefile: 7
file content (63 lines) | stat: -rw-r--r-- 1,774 bytes parent folder | download
1
2
3
4
5
6
7
8
9
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
# frozen_string_literal: true

ActiveRecord::Schema.define do
  execute('CREATE extension IF NOT EXISTS "hstore";')
  execute('CREATE extension IF NOT EXISTS "pgcrypto";')
  execute('CREATE extension IF NOT EXISTS "uuid-ossp";')

  # create ENUM if it does not exist yet
  begin
    execute('CREATE TYPE vendor_type AS ENUM (\'wholesaler\', \'retailer\');')
  rescue ActiveRecord::StatementInvalid => e
    # since PostgreSQL does not support IF NOT EXISTS when creating a TYPE,
    # rescue the error and check the error class
    raise unless e.cause.is_a? PG::DuplicateObject
    execute('ALTER TYPE vendor_type ADD VALUE IF NOT EXISTS \'wholesaler\';')
    execute('ALTER TYPE vendor_type ADD VALUE IF NOT EXISTS \'retailer\';')
  end

  create_table :vendors, id: :uuid, force: :cascade do |t|
    t.string :name, null: true
    t.text :hours
    t.text :preferences

    if t.respond_to?(:json)
      t.json :pure_json_data
      t.json :data
    else
      t.text :data
    end

    if t.respond_to?(:hstore)
      t.hstore :config
    else
      t.text :config
    end

    if t.respond_to?(:jsonb)
      t.jsonb :pure_jsonb_data
      t.jsonb :settings
      t.jsonb :json_data, null: false, default: {}
    else
      t.text :settings
      t.text :json_data
    end

    t.column :vendor_type, :vendor_type

    t.datetime :created_at
    t.datetime :updated_at
  end

  create_table :alarms, force: true do |t|
    t.column :device_id, :integer, null: false
    t.column :alarm_type, :integer, null: false
    t.column :status, :integer, null: false
    t.column :metadata, :text
    t.column :secret_key, :binary
    t.datetime :created_at
    t.datetime :updated_at
  end

  add_index :alarms, [:device_id, :alarm_type], unique: true, where: 'status <> 0'
end