Sha256: 52628604a98978a18bab3cccae39ec1ee981d95ea8c5e7686b7bbe33d32a493d

Contents?: true

Size: 1.59 KB

Versions: 9

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

require "active_record"

class InitialSchema < ActiveRecord::Migration[6.0]
  def change
    create_table :tags, if_not_exists: true do |t|
      t.string :name, null: false
    end

    create_table :alerts, if_not_exists: true do |t|
      t.string :title, null: false
      t.string :description, null: true
      t.string :source, null: false
      t.timestamps
    end

    create_table :artifacts, if_not_exists: true do |t|
      t.string :data, null: false
      t.string :data_type, null: false
      t.belongs_to :alert, foreign_key: true
      t.timestamps
    end

    create_table :taggings, if_not_exists: true do |t|
      t.integer :tag_id
      t.integer :alert_id
    end

    add_index :taggings, :tag_id, if_not_exists: true
    add_index :taggings, [:tag_id, :alert_id], unique: true, if_not_exists: true
  end
end

def adapter
  return "postgresql" if Mihari.config.database.start_with?("postgresql://", "postgres://")

  "sqlite3"
end

module Mihari
  class Database
    class << self
      def connect
        case adapter
        when "postgresql"
          ActiveRecord::Base.establish_connection(Mihari.config.database)
        else
          ActiveRecord::Base.establish_connection(
            adapter: adapter,
            database: Mihari.config.database
          )
        end

        ActiveRecord::Migration.verbose = false
        InitialSchema.migrate(:up)
      rescue StandardError
        # Do nothing
      end

      def destroy!
        InitialSchema.migrate(:down) if ActiveRecord::Base.connected?
      end
    end
  end
end

Mihari::Database.connect

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mihari-1.4.1 lib/mihari/database.rb
mihari-1.4.0 lib/mihari/database.rb
mihari-1.3.2 lib/mihari/database.rb
mihari-1.3.1 lib/mihari/database.rb
mihari-1.3.0 lib/mihari/database.rb
mihari-1.2.1 lib/mihari/database.rb
mihari-1.2.0 lib/mihari/database.rb
mihari-1.1.1 lib/mihari/database.rb
mihari-1.1.0 lib/mihari/database.rb