Sha256: e44950479abd68a07d981c8287631db121f52ccc751e7409d85e2c1ab6abc2e3

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

require "active_record"

class InitialSchema < ActiveRecord::Migration[7.0]
  def change
    create_table :records, if_not_exists: true do |t|
      t.string :hash, null: false, index: { unique: true }
      t.string :hostname, null: false
      t.json :headers, null: false
      t.text :filename, null: false
      t.string :downloaded_as, null: false
      t.integer :filesize, null: false
      t.string :mime_type, null: false
      t.text :url, null: false

      t.timestamps
    end
  end
end

class V11Schema < ActiveRecord::Migration[7.0]
  def change
    add_column :records, :source, :string, if_not_exists: true
  end
end

def adapter
  return "postgresql" if Miteru.configuration.database.start_with?("postgresql://", "postgres://")
  return "mysql2" if Miteru.configuration.database.start_with?("mysql2://")

  "sqlite3"
end

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

        # ActiveRecord::Base.logger = Logger.new STDOUT
        ActiveRecord::Migration.verbose = false

        InitialSchema.migrate(:up)
        V11Schema.migrate(:up)
      rescue StandardError => _e
        # Do nothing
      end

      def close
        ActiveRecord::Base.clear_active_connections!
        ActiveRecord::Base.connection.close
      end

      def destroy!
        return unless ActiveRecord::Base.connected?

        InitialSchema.migrate(:down)
        V11Schema.migrate(:down)
      end
    end
  end
end

Miteru::Database.connect

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
miteru-1.2.2 lib/miteru/database.rb
miteru-1.2.1 lib/miteru/database.rb
miteru-1.2.0 lib/miteru/database.rb