redhillonrails_core 1.0.9.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +7 -0
- data/README.rdoc +31 -47
- data/lib/redhillonrails_core/active_record/base.rb +63 -0
- data/lib/redhillonrails_core/active_record/connection_adapters/abstract_adapter.rb +75 -0
- data/lib/redhillonrails_core/active_record/connection_adapters/column.rb +25 -0
- data/lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb +30 -0
- data/lib/redhillonrails_core/active_record/connection_adapters/index_definition.rb +17 -0
- data/lib/redhillonrails_core/active_record/connection_adapters/mysql_adapter.rb +78 -0
- data/lib/redhillonrails_core/active_record/connection_adapters/mysql_column.rb +12 -0
- data/lib/redhillonrails_core/active_record/connection_adapters/postgresql_adapter.rb +160 -0
- data/lib/redhillonrails_core/active_record/connection_adapters/sqlite3_adapter.rb +111 -0
- data/lib/redhillonrails_core/active_record/connection_adapters/table_definition.rb +31 -0
- data/lib/redhillonrails_core/active_record/schema.rb +27 -0
- data/lib/redhillonrails_core/active_record/schema_dumper.rb +70 -0
- data/lib/redhillonrails_core.rb +20 -26
- data/redhillonrails_core.gemspec +34 -40
- data/spec/connections/mysql/connection.rb +18 -0
- data/spec/connections/mysql2/connection.rb +18 -0
- data/spec/connections/postgresql/connection.rb +15 -0
- data/spec/connections/sqlite3/connection.rb +14 -0
- data/spec/foreign_key_spec.rb +100 -0
- data/spec/index_definition_spec.rb +145 -0
- data/spec/index_spec.rb +67 -0
- data/spec/models/comment.rb +5 -0
- data/spec/models/post.rb +6 -0
- data/spec/models/user.rb +5 -0
- data/spec/schema/schema.rb +21 -0
- data/spec/schema_dumper_spec.rb +117 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/support/reference.rb +66 -0
- metadata +32 -57
- data/.document +0 -5
- data/.gitignore +0 -23
- data/.rvmrc +0 -1
- data/Gemfile +0 -20
- data/Gemfile.lock +0 -50
- data/Rakefile +0 -76
- data/VERSION +0 -1
- data/examples/example_helper.rb +0 -44
- data/examples/postgresql_index_parser_example.rb +0 -198
- data/lib/red_hill_consulting/core/active_record/base.rb +0 -61
- data/lib/red_hill_consulting/core/active_record/connection_adapters/abstract_adapter.rb +0 -71
- data/lib/red_hill_consulting/core/active_record/connection_adapters/column.rb +0 -21
- data/lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb +0 -26
- data/lib/red_hill_consulting/core/active_record/connection_adapters/index_definition.rb +0 -13
- data/lib/red_hill_consulting/core/active_record/connection_adapters/mysql4_adapter.rb +0 -37
- data/lib/red_hill_consulting/core/active_record/connection_adapters/mysql5_adapter.rb +0 -40
- data/lib/red_hill_consulting/core/active_record/connection_adapters/mysql_adapter.rb +0 -103
- data/lib/red_hill_consulting/core/active_record/connection_adapters/mysql_column.rb +0 -8
- data/lib/red_hill_consulting/core/active_record/connection_adapters/postgresql_adapter.rb +0 -178
- data/lib/red_hill_consulting/core/active_record/connection_adapters/schema_statements.rb +0 -23
- data/lib/red_hill_consulting/core/active_record/connection_adapters/sqlite3_adapter.rb +0 -109
- data/lib/red_hill_consulting/core/active_record/connection_adapters/table_definition.rb +0 -27
- data/lib/red_hill_consulting/core/active_record/schema.rb +0 -25
- data/lib/red_hill_consulting/core/active_record/schema_dumper.rb +0 -66
- data/lib/red_hill_consulting/core/active_record.rb +0 -4
- data/lib/red_hill_consulting/core.rb +0 -4
- data/tasks/db/comments.rake +0 -9
| @@ -0,0 +1,145 @@ | |
| 1 | 
            +
            require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'models/user'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            describe "Index definition" do
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              let(:migration) { ::ActiveRecord::Migration }
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              context "when index is multicolumn" do
         | 
| 10 | 
            +
                before(:each) do
         | 
| 11 | 
            +
                  migration.suppress_messages do
         | 
| 12 | 
            +
                    migration.execute "CREATE INDEX users_login_index ON users (login, deleted_at)"
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
                  User.reset_column_information
         | 
| 15 | 
            +
                  @index = index_definition(%w[login deleted_at])
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                after(:each) do
         | 
| 19 | 
            +
                  migration.suppress_messages do
         | 
| 20 | 
            +
                    migration.remove_index :users, :name => 'users_login_index'
         | 
| 21 | 
            +
                  end
         | 
| 22 | 
            +
                end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                it "is included in User.indexes" do
         | 
| 25 | 
            +
                  User.indexes.select { |index| index.columns == %w[login deleted_at] }.should have(1).item
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              if ::ActiveRecord::Base.connection.class.include?(RedhillonrailsCore::ActiveRecord::ConnectionAdapters::PostgresqlAdapter)
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                context "when case insensitive is added" do
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                  before(:each) do
         | 
| 35 | 
            +
                    migration.suppress_messages do
         | 
| 36 | 
            +
                      migration.execute "CREATE INDEX users_login_index ON users(LOWER(login))"
         | 
| 37 | 
            +
                    end
         | 
| 38 | 
            +
                    User.reset_column_information
         | 
| 39 | 
            +
                    @index = User.indexes.detect { |i| i.expression =~ /lower\(\(login\)::text\)/i }
         | 
| 40 | 
            +
                  end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                  after(:each) do
         | 
| 43 | 
            +
                    migration.suppress_messages do
         | 
| 44 | 
            +
                      migration.remove_index :users, :name => 'users_login_index'
         | 
| 45 | 
            +
                    end
         | 
| 46 | 
            +
                  end
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                  it "is included in User.indexes" do
         | 
| 49 | 
            +
                    @index.should_not be_nil
         | 
| 50 | 
            +
                  end
         | 
| 51 | 
            +
             | 
| 52 | 
            +
                  it "is not case_sensitive" do
         | 
| 53 | 
            +
                    @index.should_not be_case_sensitive
         | 
| 54 | 
            +
                  end
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                  it "defines expression" do
         | 
| 57 | 
            +
                    @index.expression.should == "lower((login)::text)"
         | 
| 58 | 
            +
                  end
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                  it "doesn't define conditions" do
         | 
| 61 | 
            +
                    @index.conditions.should be_nil
         | 
| 62 | 
            +
                  end
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
             | 
| 67 | 
            +
                context "when index is partial and column is not downcased" do
         | 
| 68 | 
            +
                  before(:each) do
         | 
| 69 | 
            +
                    migration.suppress_messages do
         | 
| 70 | 
            +
                      migration.execute "CREATE INDEX users_login_index ON users(login) WHERE deleted_at IS NULL"
         | 
| 71 | 
            +
                    end
         | 
| 72 | 
            +
                    User.reset_column_information
         | 
| 73 | 
            +
                    @index = index_definition("login")
         | 
| 74 | 
            +
                  end
         | 
| 75 | 
            +
             | 
| 76 | 
            +
                  after(:each) do
         | 
| 77 | 
            +
                    migration.suppress_messages do
         | 
| 78 | 
            +
                      migration.remove_index :users, :name => 'users_login_index'
         | 
| 79 | 
            +
                    end
         | 
| 80 | 
            +
                  end
         | 
| 81 | 
            +
             | 
| 82 | 
            +
                  it "is included in User.indexes" do
         | 
| 83 | 
            +
                    User.indexes.select { |index| index.columns == ["login"] }.should have(1).item
         | 
| 84 | 
            +
                  end
         | 
| 85 | 
            +
             | 
| 86 | 
            +
                  it "is case_sensitive" do
         | 
| 87 | 
            +
                    @index.should be_case_sensitive
         | 
| 88 | 
            +
                  end
         | 
| 89 | 
            +
             | 
| 90 | 
            +
                  it "doesn't define expression" do
         | 
| 91 | 
            +
                    @index.expression.should be_nil
         | 
| 92 | 
            +
                  end
         | 
| 93 | 
            +
             | 
| 94 | 
            +
                  it "defines conditions" do
         | 
| 95 | 
            +
                    @index.conditions.should == "(deleted_at IS NULL)"
         | 
| 96 | 
            +
                  end
         | 
| 97 | 
            +
             | 
| 98 | 
            +
                end
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                context "when index contains expression" do
         | 
| 101 | 
            +
                  before(:each) do
         | 
| 102 | 
            +
                    migration.suppress_messages do
         | 
| 103 | 
            +
                      migration.execute "CREATE INDEX users_login_index ON users (extract(EPOCH from deleted_at)) WHERE deleted_at IS NULL"
         | 
| 104 | 
            +
                    end
         | 
| 105 | 
            +
                    User.reset_column_information
         | 
| 106 | 
            +
                    @index = User.indexes.detect { |i| i.expression.present? }
         | 
| 107 | 
            +
                  end
         | 
| 108 | 
            +
             | 
| 109 | 
            +
                  after(:each) do
         | 
| 110 | 
            +
                    migration.suppress_messages do
         | 
| 111 | 
            +
                      migration.remove_index :users, :name => 'users_login_index'
         | 
| 112 | 
            +
                    end
         | 
| 113 | 
            +
                  end
         | 
| 114 | 
            +
             | 
| 115 | 
            +
                  it "exists" do
         | 
| 116 | 
            +
                    @index.should_not be_nil
         | 
| 117 | 
            +
                  end
         | 
| 118 | 
            +
             | 
| 119 | 
            +
                  it "doesnt have columns defined" do
         | 
| 120 | 
            +
                    @index.columns.should be_empty
         | 
| 121 | 
            +
                  end
         | 
| 122 | 
            +
             | 
| 123 | 
            +
                  it "is case_sensitive" do
         | 
| 124 | 
            +
                    @index.should be_case_sensitive
         | 
| 125 | 
            +
                  end
         | 
| 126 | 
            +
             | 
| 127 | 
            +
                  it "defines expression" do
         | 
| 128 | 
            +
                    @index.expression.should == "date_part('epoch'::text, deleted_at)"
         | 
| 129 | 
            +
                  end
         | 
| 130 | 
            +
             | 
| 131 | 
            +
                  it "defines conditions" do
         | 
| 132 | 
            +
                    @index.conditions.should == "(deleted_at IS NULL)"
         | 
| 133 | 
            +
                  end
         | 
| 134 | 
            +
             | 
| 135 | 
            +
                end
         | 
| 136 | 
            +
             | 
| 137 | 
            +
              end # of postgresql specific examples
         | 
| 138 | 
            +
             | 
| 139 | 
            +
              protected
         | 
| 140 | 
            +
              def index_definition(column_names)
         | 
| 141 | 
            +
                User.indexes.detect { |index| index.columns == Array(column_names) }
         | 
| 142 | 
            +
              end
         | 
| 143 | 
            +
             | 
| 144 | 
            +
             | 
| 145 | 
            +
            end
         | 
    
        data/spec/index_spec.rb
    ADDED
    
    | @@ -0,0 +1,67 @@ | |
| 1 | 
            +
            require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'models/user'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            describe "add_index" do
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              let(:migration) { ::ActiveRecord::Migration }
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              after(:each) do
         | 
| 10 | 
            +
                migration.suppress_messages do
         | 
| 11 | 
            +
                  migration.remove_index(:users, :name => @index.name) if @index
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              it "should create index when called without additional options" do
         | 
| 16 | 
            +
                add_index(:users, :login)
         | 
| 17 | 
            +
                index_for(:login).should_not be_nil
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
              it "should create unique index" do
         | 
| 21 | 
            +
                add_index(:users, :login, :unique => true)
         | 
| 22 | 
            +
                index_for(:login).unique.should == true
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
              it "should assign given name" do
         | 
| 26 | 
            +
                add_index(:users, :login, :name => 'users_login_index')
         | 
| 27 | 
            +
                index_for(:login).name.should == 'users_login_index'
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              if ::ActiveRecord::Base.connection.class.include?(RedhillonrailsCore::ActiveRecord::ConnectionAdapters::PostgresqlAdapter)
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                it "should assign conditions" do
         | 
| 33 | 
            +
                  add_index(:users, :login, :conditions => 'deleted_at IS NULL')
         | 
| 34 | 
            +
                  index_for(:login).conditions.should == '(deleted_at IS NULL)'
         | 
| 35 | 
            +
                end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                it "should assign expression" do
         | 
| 38 | 
            +
                  add_index(:users, :expression => "USING hash (upper(login)) WHERE deleted_at IS NULL", :name => 'users_login_index')
         | 
| 39 | 
            +
                  @index = User.indexes.detect { |i| i.expression.present? }
         | 
| 40 | 
            +
                  @index.expression.should == "upper((login)::text)"
         | 
| 41 | 
            +
                  @index.conditions.should == "(deleted_at IS NULL)"
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                it "should raise if no column given and expression is missing" do
         | 
| 45 | 
            +
                  expect { add_index(:users, :name => 'users_login_index') }.should raise_error(ArgumentError)
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                it "should raise if expression without name is given" do
         | 
| 49 | 
            +
                  expect { add_index(:users, :expression => "USING btree (login)") }.should raise_error(ArgumentError)
         | 
| 50 | 
            +
                end
         | 
| 51 | 
            +
             | 
| 52 | 
            +
              end # of postgresql specific examples
         | 
| 53 | 
            +
             | 
| 54 | 
            +
              protected
         | 
| 55 | 
            +
              def add_index(*args)
         | 
| 56 | 
            +
                migration.suppress_messages do
         | 
| 57 | 
            +
                  migration.add_index(*args)
         | 
| 58 | 
            +
                end
         | 
| 59 | 
            +
                User.reset_column_information
         | 
| 60 | 
            +
              end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
              def index_for(column_names)
         | 
| 63 | 
            +
                @index = User.indexes.detect { |i| i.columns == Array(column_names).collect(&:to_s) }
         | 
| 64 | 
            +
              end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
             | 
| 67 | 
            +
            end
         | 
    
        data/spec/models/post.rb
    ADDED
    
    
    
        data/spec/models/user.rb
    ADDED
    
    
| @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            ActiveRecord::Schema.define do
         | 
| 2 | 
            +
             | 
| 3 | 
            +
              create_table :users, :force => true do |t|
         | 
| 4 | 
            +
                t.string :login
         | 
| 5 | 
            +
                t.datetime :deleted_at
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              create_table :posts, :force => true do |t|
         | 
| 9 | 
            +
                t.text :body
         | 
| 10 | 
            +
                t.integer :user_id
         | 
| 11 | 
            +
                t.integer :author_id
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              create_table :comments, :force => true do |t|
         | 
| 15 | 
            +
                t.text :body
         | 
| 16 | 
            +
                t.integer :post_id
         | 
| 17 | 
            +
              end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
              add_foreign_key :comments, :post_id, :posts, :id
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            end
         | 
| @@ -0,0 +1,117 @@ | |
| 1 | 
            +
            require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
         | 
| 2 | 
            +
            require 'stringio'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            require 'models/post'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            describe "Schema dump" do
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              let(:dump) do
         | 
| 9 | 
            +
                stream = StringIO.new
         | 
| 10 | 
            +
                ActiveRecord::SchemaDumper.ignore_tables = %w[users comments]
         | 
| 11 | 
            +
                ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
         | 
| 12 | 
            +
                stream.string
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              it "should include foreign_key definition" do
         | 
| 16 | 
            +
                with_foreign_key Post, :user_id, :users, :id do
         | 
| 17 | 
            +
                  dump.should match(to_regexp(%q{add_foreign_key "posts", ["user_id"], "users", ["id"]}))
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
             | 
| 22 | 
            +
              unless ::ActiveRecord::Base.connection.class.include?(RedhillonrailsCore::ActiveRecord::ConnectionAdapters::Sqlite3Adapter)
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                it "should include foreign_key options" do
         | 
| 25 | 
            +
                  with_foreign_key Post, :user_id, :users, :id, :on_update => :cascade, :on_delete => :set_null do
         | 
| 26 | 
            +
                    dump.should match(to_regexp(%q{add_foreign_key "posts", ["user_id"], "users", ["id"], :on_update => :cascade, :on_delete => :set_null}))
         | 
| 27 | 
            +
                  end
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
              it "should include index definition" do
         | 
| 33 | 
            +
                with_index Post, :user_id do
         | 
| 34 | 
            +
                  dump.should match(to_regexp(%q{add_index "posts", ["user_id"]}))
         | 
| 35 | 
            +
                end
         | 
| 36 | 
            +
              end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
              it "should include index name" do
         | 
| 39 | 
            +
                with_index Post, :user_id, :name => "posts_user_id_index" do
         | 
| 40 | 
            +
                  dump.should match(to_regexp(%q{add_index "posts", ["user_id"], :name => "posts_user_id_index"}))
         | 
| 41 | 
            +
                end
         | 
| 42 | 
            +
              end
         | 
| 43 | 
            +
              
         | 
| 44 | 
            +
              it "should define unique index" do
         | 
| 45 | 
            +
                with_index Post, :user_id, :name => "posts_user_id_index", :unique => true do
         | 
| 46 | 
            +
                  dump.should match(to_regexp(%q{add_index "posts", ["user_id"], :name => "posts_user_id_index", :unique => true}))
         | 
| 47 | 
            +
                end
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
              
         | 
| 50 | 
            +
              if ::ActiveRecord::Base.connection.class.include?(RedhillonrailsCore::ActiveRecord::ConnectionAdapters::PostgresqlAdapter)
         | 
| 51 | 
            +
             | 
| 52 | 
            +
              it "should define case insensitive index" do
         | 
| 53 | 
            +
                with_index Post, :name => "posts_user_body_index", :expression => "USING btree (LOWER(body))" do
         | 
| 54 | 
            +
                  dump.should match(to_regexp(%q{add_index "posts", ["body"], :name => "posts_user_body_index", :case_sensitive => false}))
         | 
| 55 | 
            +
                end
         | 
| 56 | 
            +
              end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
              it "should define conditions" do
         | 
| 59 | 
            +
                with_index Post, :user_id, :name => "posts_user_id_index", :conditions => "user_id IS NOT NULL" do
         | 
| 60 | 
            +
                  dump.should match(to_regexp(%q{add_index "posts", ["user_id"], :name => "posts_user_id_index", :conditions => "(user_id IS NOT NULL)"}))
         | 
| 61 | 
            +
                end
         | 
| 62 | 
            +
              end
         | 
| 63 | 
            +
             | 
| 64 | 
            +
              it "should define expression" do
         | 
| 65 | 
            +
                with_index Post, :name => "posts_freaky_index", :expression => "USING hash (least(id, user_id))" do
         | 
| 66 | 
            +
                  dump.should match(to_regexp(%q{add_index "posts", :name => "posts_freaky_index", :kind => "hash", :expression => "LEAST(id, user_id)"}))
         | 
| 67 | 
            +
                end
         | 
| 68 | 
            +
              end
         | 
| 69 | 
            +
              
         | 
| 70 | 
            +
              it "should define kind" do
         | 
| 71 | 
            +
                with_index Post, :name => "posts_body_index", :expression => "USING hash (body)" do
         | 
| 72 | 
            +
                  dump.should match(to_regexp(%q{add_index "posts", ["body"], :name => "posts_body_index", :kind => "hash"}))
         | 
| 73 | 
            +
                end
         | 
| 74 | 
            +
              end
         | 
| 75 | 
            +
             | 
| 76 | 
            +
              end # of postgresql specific examples
         | 
| 77 | 
            +
             | 
| 78 | 
            +
              protected
         | 
| 79 | 
            +
              def to_regexp(string)
         | 
| 80 | 
            +
                Regexp.new(Regexp.escape(string))
         | 
| 81 | 
            +
              end
         | 
| 82 | 
            +
             | 
| 83 | 
            +
              def with_foreign_key(model, columns, referenced_table_name, referenced_columns, options = {})
         | 
| 84 | 
            +
                ActiveRecord::Migration.suppress_messages do
         | 
| 85 | 
            +
                  ActiveRecord::Migration.add_foreign_key(model.table_name, columns, referenced_table_name, referenced_columns, options)
         | 
| 86 | 
            +
                end
         | 
| 87 | 
            +
                model.reset_column_information
         | 
| 88 | 
            +
                yield
         | 
| 89 | 
            +
                ActiveRecord::Migration.suppress_messages do
         | 
| 90 | 
            +
                  ActiveRecord::Migration.remove_foreign_key(model.table_name, determine_foreign_key_name(model, columns, options))
         | 
| 91 | 
            +
                end
         | 
| 92 | 
            +
              end
         | 
| 93 | 
            +
              
         | 
| 94 | 
            +
              def with_index(model, columns, options = {})
         | 
| 95 | 
            +
                ActiveRecord::Migration.suppress_messages do
         | 
| 96 | 
            +
                  ActiveRecord::Migration.add_index(model.table_name, columns, options)
         | 
| 97 | 
            +
                end
         | 
| 98 | 
            +
                model.reset_column_information
         | 
| 99 | 
            +
                yield
         | 
| 100 | 
            +
                ActiveRecord::Migration.suppress_messages do
         | 
| 101 | 
            +
                  ActiveRecord::Migration.remove_index(model.table_name, :name => determine_index_name(model, columns, options))
         | 
| 102 | 
            +
                end
         | 
| 103 | 
            +
              end
         | 
| 104 | 
            +
             | 
| 105 | 
            +
              def determine_index_name(model, columns, options)
         | 
| 106 | 
            +
                name = columns[:name] if columns.is_a?(Hash)
         | 
| 107 | 
            +
                name ||= options[:name]
         | 
| 108 | 
            +
                name ||= model.indexes.detect { |index| index.table == model.table_name.to_s && index.columns == Array(columns).collect(&:to_s) }.name
         | 
| 109 | 
            +
                name
         | 
| 110 | 
            +
              end
         | 
| 111 | 
            +
             | 
| 112 | 
            +
              def determine_foreign_key_name(model, columns, options)
         | 
| 113 | 
            +
                name = options[:name] 
         | 
| 114 | 
            +
                name ||= model.foreign_keys.detect { |fk| fk.table_name == model.table_name.to_s && fk.column_names == Array(columns).collect(&:to_s) }.name
         | 
| 115 | 
            +
              end
         | 
| 116 | 
            +
             | 
| 117 | 
            +
            end
         | 
    
        data/spec/spec_helper.rb
    ADDED
    
    | @@ -0,0 +1,16 @@ | |
| 1 | 
            +
            $LOAD_PATH.unshift(File.dirname(__FILE__))
         | 
| 2 | 
            +
            $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
         | 
| 3 | 
            +
            require 'rubygems'
         | 
| 4 | 
            +
            require 'active_record'
         | 
| 5 | 
            +
            require 'redhillonrails_core'
         | 
| 6 | 
            +
            require 'connection'
         | 
| 7 | 
            +
            Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            Spec::Runner.configure do |config|
         | 
| 10 | 
            +
              config.include(RedhillonrailsCoreMatchers)
         | 
| 11 | 
            +
              # load schema
         | 
| 12 | 
            +
              ActiveRecord::Migration.suppress_messages do
         | 
| 13 | 
            +
                eval(File.read(File.join(File.dirname(__FILE__), 'schema', 'schema.rb')))
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
            end
         | 
| 16 | 
            +
             | 
| @@ -0,0 +1,66 @@ | |
| 1 | 
            +
            module RedhillonrailsCoreMatchers
         | 
| 2 | 
            +
             | 
| 3 | 
            +
              class Reference
         | 
| 4 | 
            +
                def initialize(expected)
         | 
| 5 | 
            +
                  @column_names = nil
         | 
| 6 | 
            +
                  unless expected.empty?
         | 
| 7 | 
            +
                    @references_column_names = Array(expected).collect(&:to_s)
         | 
| 8 | 
            +
                    @references_table_name = @references_column_names.shift 
         | 
| 9 | 
            +
                  end
         | 
| 10 | 
            +
                end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                def matches?(model)
         | 
| 13 | 
            +
                  @model = model
         | 
| 14 | 
            +
                  if @references_table_name
         | 
| 15 | 
            +
                    @result = @model.foreign_keys.select do |fk|
         | 
| 16 | 
            +
                      fk.references_table_name == @references_table_name && 
         | 
| 17 | 
            +
                        fk.references_column_names == @references_column_names
         | 
| 18 | 
            +
                    end
         | 
| 19 | 
            +
                  else
         | 
| 20 | 
            +
                    @result = @model.foreign_keys
         | 
| 21 | 
            +
                  end
         | 
| 22 | 
            +
                  if @column_names 
         | 
| 23 | 
            +
                    @result.any? do |fk| 
         | 
| 24 | 
            +
                      fk.column_names == @column_names &&
         | 
| 25 | 
            +
                        (@on_update ? fk.on_update == @on_update : true) &&
         | 
| 26 | 
            +
                        (@on_delete ? fk.on_delete == @on_delete : true)
         | 
| 27 | 
            +
                    end
         | 
| 28 | 
            +
                  else
         | 
| 29 | 
            +
                    !!@result
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                def failure_message_for_should(should_not = false)
         | 
| 34 | 
            +
                  target_column_names = @column_names.present? ? "(#{@column_names.join(', ')})" : "" 
         | 
| 35 | 
            +
                  destinantion_column_names = @references_table_name ? "#{@references_table_name}(#{@references_column_names.join(', ')})" : "anything"
         | 
| 36 | 
            +
                  invert = should_not ? 'not' : ''
         | 
| 37 | 
            +
                  "Expected #{@model.table_name}#{target_column_names} #{invert} to reference #{destinantion_column_names}"
         | 
| 38 | 
            +
                end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
                def failure_message_for_should_not
         | 
| 41 | 
            +
                  failure_message_for_should(true)
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
              
         | 
| 44 | 
            +
                def on(*column_names)
         | 
| 45 | 
            +
                  @column_names = column_names.collect(&:to_s)
         | 
| 46 | 
            +
                  self
         | 
| 47 | 
            +
                end
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                def on_update(action)
         | 
| 50 | 
            +
                  @on_update = action
         | 
| 51 | 
            +
                  self
         | 
| 52 | 
            +
                end
         | 
| 53 | 
            +
             | 
| 54 | 
            +
                def on_delete(action)
         | 
| 55 | 
            +
                  @on_delete = action
         | 
| 56 | 
            +
                  self
         | 
| 57 | 
            +
                end
         | 
| 58 | 
            +
             | 
| 59 | 
            +
              end
         | 
| 60 | 
            +
             | 
| 61 | 
            +
              def reference(*expect)
         | 
| 62 | 
            +
                Reference.new(expect)
         | 
| 63 | 
            +
              end
         | 
| 64 | 
            +
             | 
| 65 | 
            +
            end
         | 
| 66 | 
            +
             | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,12 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: redhillonrails_core
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            -
              hash: 121
         | 
| 5 4 | 
             
              prerelease: false
         | 
| 6 5 | 
             
              segments: 
         | 
| 7 6 | 
             
              - 1
         | 
| 8 | 
            -
              - 0
         | 
| 9 | 
            -
              - 9
         | 
| 10 7 | 
             
              - 1
         | 
| 11 | 
            -
               | 
| 8 | 
            +
              - 0
         | 
| 9 | 
            +
              version: 1.1.0
         | 
| 12 10 | 
             
            platform: ruby
         | 
| 13 11 | 
             
            authors: 
         | 
| 14 12 | 
             
            - "Micha\xC5\x82 \xC5\x81omnicki"
         | 
| @@ -16,38 +14,22 @@ autorequire: | |
| 16 14 | 
             
            bindir: bin
         | 
| 17 15 | 
             
            cert_chain: []
         | 
| 18 16 |  | 
| 19 | 
            -
            date:  | 
| 17 | 
            +
            date: 2011-01-09 00:00:00 +01:00
         | 
| 20 18 | 
             
            default_executable: 
         | 
| 21 19 | 
             
            dependencies: 
         | 
| 22 20 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| 23 21 | 
             
              name: activerecord
         | 
| 24 22 | 
             
              prerelease: false
         | 
| 25 23 | 
             
              requirement: &id001 !ruby/object:Gem::Requirement 
         | 
| 26 | 
            -
                none: false
         | 
| 27 24 | 
             
                requirements: 
         | 
| 28 25 | 
             
                - - ">="
         | 
| 29 26 | 
             
                  - !ruby/object:Gem::Version 
         | 
| 30 | 
            -
                    hash: 3
         | 
| 31 27 | 
             
                    segments: 
         | 
| 32 28 | 
             
                    - 0
         | 
| 33 29 | 
             
                    version: "0"
         | 
| 34 30 | 
             
              type: :runtime
         | 
| 35 31 | 
             
              version_requirements: *id001
         | 
| 36 | 
            -
            -  | 
| 37 | 
            -
              name: micronaut
         | 
| 38 | 
            -
              prerelease: false
         | 
| 39 | 
            -
              requirement: &id002 !ruby/object:Gem::Requirement 
         | 
| 40 | 
            -
                none: false
         | 
| 41 | 
            -
                requirements: 
         | 
| 42 | 
            -
                - - ">="
         | 
| 43 | 
            -
                  - !ruby/object:Gem::Version 
         | 
| 44 | 
            -
                    hash: 3
         | 
| 45 | 
            -
                    segments: 
         | 
| 46 | 
            -
                    - 0
         | 
| 47 | 
            -
                    version: "0"
         | 
| 48 | 
            -
              type: :development
         | 
| 49 | 
            -
              version_requirements: *id002
         | 
| 50 | 
            -
            description: RedHill on Rails Core is a plugin that features to support other RedHill on Rails plugins. It creates and drops views and foreign-keys or obtains indexes directly from a model class.
         | 
| 32 | 
            +
            description: Adds support in ActiveRecord for foreign_keys, complex indexes and other database-related stuff. Easily create foreign_keys, complex indexes and views.
         | 
| 51 33 | 
             
            email: michal.lomnicki@gmail.com
         | 
| 52 34 | 
             
            executables: []
         | 
| 53 35 |  | 
| @@ -56,39 +38,24 @@ extensions: [] | |
| 56 38 | 
             
            extra_rdoc_files: 
         | 
| 57 39 | 
             
            - README.rdoc
         | 
| 58 40 | 
             
            files: 
         | 
| 59 | 
            -
            - .document
         | 
| 60 | 
            -
            - .gitignore
         | 
| 61 | 
            -
            - .rvmrc
         | 
| 62 41 | 
             
            - CHANGELOG
         | 
| 63 | 
            -
            - Gemfile
         | 
| 64 | 
            -
            - Gemfile.lock
         | 
| 65 42 | 
             
            - MIT-LICENSE
         | 
| 66 43 | 
             
            - README.rdoc
         | 
| 67 | 
            -
            - Rakefile
         | 
| 68 | 
            -
            - VERSION
         | 
| 69 | 
            -
            - examples/example_helper.rb
         | 
| 70 | 
            -
            - examples/postgresql_index_parser_example.rb
         | 
| 71 44 | 
             
            - init.rb
         | 
| 72 | 
            -
            - lib/red_hill_consulting/core.rb
         | 
| 73 | 
            -
            - lib/red_hill_consulting/core/active_record.rb
         | 
| 74 | 
            -
            - lib/red_hill_consulting/core/active_record/base.rb
         | 
| 75 | 
            -
            - lib/red_hill_consulting/core/active_record/connection_adapters/abstract_adapter.rb
         | 
| 76 | 
            -
            - lib/red_hill_consulting/core/active_record/connection_adapters/column.rb
         | 
| 77 | 
            -
            - lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb
         | 
| 78 | 
            -
            - lib/red_hill_consulting/core/active_record/connection_adapters/index_definition.rb
         | 
| 79 | 
            -
            - lib/red_hill_consulting/core/active_record/connection_adapters/mysql4_adapter.rb
         | 
| 80 | 
            -
            - lib/red_hill_consulting/core/active_record/connection_adapters/mysql5_adapter.rb
         | 
| 81 | 
            -
            - lib/red_hill_consulting/core/active_record/connection_adapters/mysql_adapter.rb
         | 
| 82 | 
            -
            - lib/red_hill_consulting/core/active_record/connection_adapters/mysql_column.rb
         | 
| 83 | 
            -
            - lib/red_hill_consulting/core/active_record/connection_adapters/postgresql_adapter.rb
         | 
| 84 | 
            -
            - lib/red_hill_consulting/core/active_record/connection_adapters/schema_statements.rb
         | 
| 85 | 
            -
            - lib/red_hill_consulting/core/active_record/connection_adapters/sqlite3_adapter.rb
         | 
| 86 | 
            -
            - lib/red_hill_consulting/core/active_record/connection_adapters/table_definition.rb
         | 
| 87 | 
            -
            - lib/red_hill_consulting/core/active_record/schema.rb
         | 
| 88 | 
            -
            - lib/red_hill_consulting/core/active_record/schema_dumper.rb
         | 
| 89 45 | 
             
            - lib/redhillonrails_core.rb
         | 
| 46 | 
            +
            - lib/redhillonrails_core/active_record/base.rb
         | 
| 47 | 
            +
            - lib/redhillonrails_core/active_record/connection_adapters/abstract_adapter.rb
         | 
| 48 | 
            +
            - lib/redhillonrails_core/active_record/connection_adapters/column.rb
         | 
| 49 | 
            +
            - lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb
         | 
| 50 | 
            +
            - lib/redhillonrails_core/active_record/connection_adapters/index_definition.rb
         | 
| 51 | 
            +
            - lib/redhillonrails_core/active_record/connection_adapters/mysql_adapter.rb
         | 
| 52 | 
            +
            - lib/redhillonrails_core/active_record/connection_adapters/mysql_column.rb
         | 
| 53 | 
            +
            - lib/redhillonrails_core/active_record/connection_adapters/postgresql_adapter.rb
         | 
| 54 | 
            +
            - lib/redhillonrails_core/active_record/connection_adapters/sqlite3_adapter.rb
         | 
| 55 | 
            +
            - lib/redhillonrails_core/active_record/connection_adapters/table_definition.rb
         | 
| 56 | 
            +
            - lib/redhillonrails_core/active_record/schema.rb
         | 
| 57 | 
            +
            - lib/redhillonrails_core/active_record/schema_dumper.rb
         | 
| 90 58 | 
             
            - redhillonrails_core.gemspec
         | 
| 91 | 
            -
            - tasks/db/comments.rake
         | 
| 92 59 | 
             
            has_rdoc: true
         | 
| 93 60 | 
             
            homepage: http://github.com/mlomnicki/redhillonrails_core
         | 
| 94 61 | 
             
            licenses: []
         | 
| @@ -99,30 +66,38 @@ rdoc_options: | |
| 99 66 | 
             
            require_paths: 
         | 
| 100 67 | 
             
            - lib
         | 
| 101 68 | 
             
            required_ruby_version: !ruby/object:Gem::Requirement 
         | 
| 102 | 
            -
              none: false
         | 
| 103 69 | 
             
              requirements: 
         | 
| 104 70 | 
             
              - - ">="
         | 
| 105 71 | 
             
                - !ruby/object:Gem::Version 
         | 
| 106 | 
            -
                  hash: 3
         | 
| 107 72 | 
             
                  segments: 
         | 
| 108 73 | 
             
                  - 0
         | 
| 109 74 | 
             
                  version: "0"
         | 
| 110 75 | 
             
            required_rubygems_version: !ruby/object:Gem::Requirement 
         | 
| 111 | 
            -
              none: false
         | 
| 112 76 | 
             
              requirements: 
         | 
| 113 77 | 
             
              - - ">="
         | 
| 114 78 | 
             
                - !ruby/object:Gem::Version 
         | 
| 115 | 
            -
                  hash: 3
         | 
| 116 79 | 
             
                  segments: 
         | 
| 117 80 | 
             
                  - 0
         | 
| 118 81 | 
             
                  version: "0"
         | 
| 119 82 | 
             
            requirements: []
         | 
| 120 83 |  | 
| 121 84 | 
             
            rubyforge_project: 
         | 
| 122 | 
            -
            rubygems_version: 1.3. | 
| 85 | 
            +
            rubygems_version: 1.3.6
         | 
| 123 86 | 
             
            signing_key: 
         | 
| 124 87 | 
             
            specification_version: 3
         | 
| 125 | 
            -
            summary:  | 
| 88 | 
            +
            summary: Adds support in ActiveRecord for foreign_keys, complex indexes and other database-related stuff
         | 
| 126 89 | 
             
            test_files: 
         | 
| 127 | 
            -
            -  | 
| 128 | 
            -
            -  | 
| 90 | 
            +
            - spec/schema/schema.rb
         | 
| 91 | 
            +
            - spec/foreign_key_spec.rb
         | 
| 92 | 
            +
            - spec/schema_dumper_spec.rb
         | 
| 93 | 
            +
            - spec/connections/mysql2/connection.rb
         | 
| 94 | 
            +
            - spec/connections/postgresql/connection.rb
         | 
| 95 | 
            +
            - spec/connections/sqlite3/connection.rb
         | 
| 96 | 
            +
            - spec/connections/mysql/connection.rb
         | 
| 97 | 
            +
            - spec/support/reference.rb
         | 
| 98 | 
            +
            - spec/index_definition_spec.rb
         | 
| 99 | 
            +
            - spec/spec_helper.rb
         | 
| 100 | 
            +
            - spec/index_spec.rb
         | 
| 101 | 
            +
            - spec/models/user.rb
         | 
| 102 | 
            +
            - spec/models/post.rb
         | 
| 103 | 
            +
            - spec/models/comment.rb
         | 
    
        data/.document
    DELETED
    
    
    
        data/.gitignore
    DELETED
    
    
    
        data/.rvmrc
    DELETED
    
    | @@ -1 +0,0 @@ | |
| 1 | 
            -
            rvm 1.8.7@redhillonrails_core
         | 
    
        data/Gemfile
    DELETED
    
    | @@ -1,20 +0,0 @@ | |
| 1 | 
            -
            source :rubygems
         | 
| 2 | 
            -
             | 
| 3 | 
            -
            gem "pg"
         | 
| 4 | 
            -
            gem "activerecord"
         | 
| 5 | 
            -
            gem "rake"
         | 
| 6 | 
            -
             | 
| 7 | 
            -
            group :development, :test do
         | 
| 8 | 
            -
              gem "jeweler"
         | 
| 9 | 
            -
              gem "micronaut"
         | 
| 10 | 
            -
             | 
| 11 | 
            -
              platforms :ruby_18 do
         | 
| 12 | 
            -
                gem "ruby-debug"
         | 
| 13 | 
            -
              end
         | 
| 14 | 
            -
             | 
| 15 | 
            -
              platforms :ruby_19 do
         | 
| 16 | 
            -
                gem "ruby-debug19"
         | 
| 17 | 
            -
              end
         | 
| 18 | 
            -
            end
         | 
| 19 | 
            -
             | 
| 20 | 
            -
            # vim: ft=ruby
         |