Skip to content

Instantly share code, notes, and snippets.

@benoittgt
Created July 31, 2024 11:15
Show Gist options
  • Save benoittgt/26ab2fe0dc48d60879c58fab7b6005d0 to your computer and use it in GitHub Desktop.
Save benoittgt/26ab2fe0dc48d60879c58fab7b6005d0 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rails"
gem "pg"
end
require "active_record"
require "minitest/autorun"
require "logger"
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "migrations_test")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :events, force: true do |t|
t.boolean :processed, default: false, null: false
t.boolean :processed_with_success
t.timestamp :created_at, null: false
end
add_index :events, [:created_at, :processed], where: "processed_with_success IS false", name: "index_events_on_created_at_and_processed"
add_index :events, [:created_at, :processed], where: "processed IS false", name: "index_events_not_processed_on_created_at"
end
# List indexes
puts "Indexes on the events table:"
indexes = ActiveRecord::Base.connection.indexes('events')
indexes.each do |index|
puts "Name: #{index.name}"
puts " Columns: #{index.columns.join(', ')}"
puts " Unique: #{index.unique}"
puts " Where: #{index.where}"
puts ""
end
# Output schema similar to schema.rb
puts "Schema:"
stream = StringIO.new
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
puts stream.string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment