schema_plus 0.4.1 → 1.0.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/.gitignore +3 -1
- data/.travis.yml +6 -9
- data/Gemfile +0 -4
- data/README.rdoc +168 -70
- data/Rakefile +58 -47
- data/gemfiles/rails-3.2/Gemfile.base +4 -0
- data/gemfiles/rails-3.2/Gemfile.mysql +4 -0
- data/gemfiles/rails-3.2/Gemfile.mysql2 +4 -0
- data/gemfiles/rails-3.2/Gemfile.postgresql +4 -0
- data/gemfiles/rails-3.2/Gemfile.sqlite3 +4 -0
- data/lib/schema_plus.rb +2 -0
- data/lib/schema_plus/active_record/column_options_handler.rb +73 -32
- data/lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb +60 -31
- data/lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb +7 -2
- data/lib/schema_plus/active_record/connection_adapters/index_definition.rb +2 -1
- data/lib/schema_plus/active_record/connection_adapters/mysql_adapter.rb +19 -1
- data/lib/schema_plus/active_record/connection_adapters/postgresql_adapter.rb +68 -17
- data/lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb +28 -3
- data/lib/schema_plus/active_record/connection_adapters/table_definition.rb +27 -1
- data/lib/schema_plus/active_record/db_default.rb +19 -0
- data/lib/schema_plus/active_record/foreign_keys.rb +40 -32
- data/lib/schema_plus/active_record/schema_dumper.rb +7 -3
- data/lib/schema_plus/version.rb +1 -1
- data/runspecs +5 -8
- data/schema_plus.gemspec +2 -5
- data/spec/column_definition_spec.rb +18 -1
- data/spec/column_spec.rb +39 -2
- data/spec/connection_spec.rb +1 -1
- data/spec/connections/mysql/connection.rb +1 -1
- data/spec/connections/mysql2/connection.rb +1 -1
- data/spec/connections/postgresql/connection.rb +1 -1
- data/spec/foreign_key_definition_spec.rb +0 -4
- data/spec/foreign_key_spec.rb +37 -13
- data/spec/index_definition_spec.rb +54 -2
- data/spec/index_spec.rb +59 -15
- data/spec/migration_spec.rb +336 -85
- data/spec/multiple_schemas_spec.rb +127 -0
- data/spec/schema_dumper_spec.rb +65 -25
- data/spec/schema_spec.rb +16 -18
- data/spec/spec_helper.rb +19 -18
- data/spec/support/matchers/reference.rb +7 -1
- data/spec/views_spec.rb +5 -2
- metadata +43 -54
- data/gemfiles/Gemfile.rails-2.3 +0 -6
- data/gemfiles/Gemfile.rails-2.3.lock +0 -65
- data/gemfiles/Gemfile.rails-3.0 +0 -5
- data/gemfiles/Gemfile.rails-3.0.lock +0 -113
- data/gemfiles/Gemfile.rails-3.1 +0 -5
- data/gemfiles/Gemfile.rails-3.1.lock +0 -123
- data/gemfiles/Gemfile.rails-3.2 +0 -5
- data/gemfiles/Gemfile.rails-3.2.lock +0 -121
- data/spec/models/comment.rb +0 -2
- data/spec/models/post.rb +0 -2
- data/spec/models/user.rb +0 -2
- data/spec/rails3_migration_spec.rb +0 -144
| @@ -0,0 +1,127 @@ | |
| 1 | 
            +
            require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe "with multiple schemas" do
         | 
| 4 | 
            +
              let (:connection) { ActiveRecord::Base.connection }
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              before(:all) do
         | 
| 7 | 
            +
                newdb = case connection.adapter_name
         | 
| 8 | 
            +
                        when /^mysql/i then      "CREATE SCHEMA IF NOT EXISTS schema_plus_test2"
         | 
| 9 | 
            +
                        when /^postgresql/i then "CREATE SCHEMA schema_plus_test2"
         | 
| 10 | 
            +
                        when /^sqlite/i then     "ATTACH ':memory:' AS schema_plus_test2"
         | 
| 11 | 
            +
                        end
         | 
| 12 | 
            +
                begin
         | 
| 13 | 
            +
                  ActiveRecord::Base.connection.execute newdb
         | 
| 14 | 
            +
                rescue ActiveRecord::StatementInvalid => e
         | 
| 15 | 
            +
                  raise unless e.message =~ /already exists/
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                class User < ::ActiveRecord::Base ; end
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              before(:each) do
         | 
| 22 | 
            +
                define_schema(:auto_create => false) do
         | 
| 23 | 
            +
                  create_table :users, :force => true do |t|
         | 
| 24 | 
            +
                    t.string :login
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                connection.execute 'DROP TABLE IF EXISTS schema_plus_test2.users'
         | 
| 29 | 
            +
                connection.execute 'CREATE TABLE schema_plus_test2.users (id ' + case connection.adapter_name
         | 
| 30 | 
            +
                      when /^mysql/i then      "integer primary key auto_increment"
         | 
| 31 | 
            +
                      when /^postgresql/i then "serial primary key"
         | 
| 32 | 
            +
                      when /^sqlite/i then     "integer primary key autoincrement"
         | 
| 33 | 
            +
                      end + ", login varchar(255))"
         | 
| 34 | 
            +
              end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
              context "with indexes in each schema" do
         | 
| 37 | 
            +
                before(:each) do
         | 
| 38 | 
            +
                  connection.execute 'CREATE INDEX ' + case connection.adapter_name
         | 
| 39 | 
            +
                  when /^mysql/i then      "index_users_on_login ON schema_plus_test2.users"
         | 
| 40 | 
            +
                  when /^postgresql/i then "index_users_on_login ON schema_plus_test2.users"
         | 
| 41 | 
            +
                  when /^sqlite/i then     "schema_plus_test2.index_users_on_login ON users"
         | 
| 42 | 
            +
                  end + " (login)"
         | 
| 43 | 
            +
                end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                it "should not find indexes in other schema" do
         | 
| 46 | 
            +
                  User.reset_column_information
         | 
| 47 | 
            +
                  User.indexes.should be_empty
         | 
| 48 | 
            +
                end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                it "should find index in current schema" do
         | 
| 51 | 
            +
                  connection.execute 'CREATE INDEX index_users_on_login ON users (login)'
         | 
| 52 | 
            +
                  User.reset_column_information
         | 
| 53 | 
            +
                  User.indexes.map(&:name).should == ['index_users_on_login']
         | 
| 54 | 
            +
                end
         | 
| 55 | 
            +
              end
         | 
| 56 | 
            +
             | 
| 57 | 
            +
              context "with views in each schema" do
         | 
| 58 | 
            +
                around(:each) do  |example|
         | 
| 59 | 
            +
                  begin
         | 
| 60 | 
            +
                    example.run
         | 
| 61 | 
            +
                  ensure
         | 
| 62 | 
            +
                    connection.execute 'DROP VIEW schema_plus_test2.myview' rescue nil
         | 
| 63 | 
            +
                    connection.execute 'DROP VIEW myview' rescue nil
         | 
| 64 | 
            +
                  end
         | 
| 65 | 
            +
                end
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                before(:each) do
         | 
| 68 | 
            +
                  connection.views.each { |view| connection.drop_view view }
         | 
| 69 | 
            +
                  connection.execute 'CREATE VIEW schema_plus_test2.myview AS SELECT * FROM users'
         | 
| 70 | 
            +
                end
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                it "should not find views in other schema" do
         | 
| 73 | 
            +
                  connection.views.should be_empty
         | 
| 74 | 
            +
                end
         | 
| 75 | 
            +
             | 
| 76 | 
            +
                it "should find views in this schema" do
         | 
| 77 | 
            +
                  connection.execute 'CREATE VIEW myview AS SELECT * FROM users'
         | 
| 78 | 
            +
                  connection.views.should == ['myview']
         | 
| 79 | 
            +
                end
         | 
| 80 | 
            +
              end
         | 
| 81 | 
            +
             | 
| 82 | 
            +
              context "with foreign key in each schema" do
         | 
| 83 | 
            +
                before(:each) do
         | 
| 84 | 
            +
                  class Comment < ::ActiveRecord::Base ; end
         | 
| 85 | 
            +
                  connection.execute 'DROP TABLE IF EXISTS schema_plus_test2.comments'
         | 
| 86 | 
            +
                  connection.execute 'CREATE TABLE schema_plus_test2.comments (user_id integer,' + case connection.adapter_name
         | 
| 87 | 
            +
                        when /^mysql/i then      "foreign key (user_id) references schema_plus_test2.users (id))"
         | 
| 88 | 
            +
                        when /^postgresql/i then "foreign key (user_id) references schema_plus_test2.users (id))"
         | 
| 89 | 
            +
                        when /^sqlite/i then     "foreign key (user_id) references users (id))"
         | 
| 90 | 
            +
                        end
         | 
| 91 | 
            +
                end
         | 
| 92 | 
            +
             | 
| 93 | 
            +
                around(:each) do |example|
         | 
| 94 | 
            +
                  begin
         | 
| 95 | 
            +
                    example.run
         | 
| 96 | 
            +
                  ensure
         | 
| 97 | 
            +
                    connection.execute 'DROP TABLE IF EXISTS comments'
         | 
| 98 | 
            +
                    connection.execute 'DROP TABLE IF EXISTS schema_plus_test2.comments'
         | 
| 99 | 
            +
                  end
         | 
| 100 | 
            +
                end
         | 
| 101 | 
            +
             | 
| 102 | 
            +
                it "should not find foreign keys in other schema" do
         | 
| 103 | 
            +
                  connection.create_table :comments, :force => true do |t|
         | 
| 104 | 
            +
                    t.integer :user_id, :foreign_key => false
         | 
| 105 | 
            +
                  end
         | 
| 106 | 
            +
                  Comment.reset_column_information
         | 
| 107 | 
            +
                  Comment.foreign_keys.length.should == 0
         | 
| 108 | 
            +
                  User.reset_column_information
         | 
| 109 | 
            +
                  User.reverse_foreign_keys.length.should == 0
         | 
| 110 | 
            +
                end
         | 
| 111 | 
            +
             | 
| 112 | 
            +
                it "should find foreign keys in this schema" do
         | 
| 113 | 
            +
                  connection.create_table :comments, :force => true do |t|
         | 
| 114 | 
            +
                    t.integer :user_id, :foreign_key => true
         | 
| 115 | 
            +
                  end
         | 
| 116 | 
            +
                  Comment.reset_column_information
         | 
| 117 | 
            +
                  Comment.foreign_keys.map(&:column_names).flatten.should == ["user_id"]
         | 
| 118 | 
            +
                  User.reset_column_information
         | 
| 119 | 
            +
                  User.reverse_foreign_keys.map(&:column_names).flatten.should == ["user_id"]
         | 
| 120 | 
            +
                end
         | 
| 121 | 
            +
             | 
| 122 | 
            +
              end
         | 
| 123 | 
            +
             | 
| 124 | 
            +
            end
         | 
| 125 | 
            +
             | 
| 126 | 
            +
             | 
| 127 | 
            +
             | 
    
        data/spec/schema_dumper_spec.rb
    CHANGED
    
    | @@ -36,37 +36,44 @@ describe "Schema dump" do | |
| 36 36 | 
             
                class ::Comment < ActiveRecord::Base ; end
         | 
| 37 37 | 
             
              end
         | 
| 38 38 |  | 
| 39 | 
            -
               | 
| 40 | 
            -
                 | 
| 41 | 
            -
             | 
| 42 | 
            -
                 | 
| 43 | 
            -
                stream.string
         | 
| 39 | 
            +
              it "should include foreign_key definition" do
         | 
| 40 | 
            +
                with_foreign_key Post, :user_id, :users, :id do
         | 
| 41 | 
            +
                  dump_posts.should match(to_regexp(%q{t.foreign_key ["user_id"], "users", ["id"]}))
         | 
| 42 | 
            +
                end
         | 
| 44 43 | 
             
              end
         | 
| 45 44 |  | 
| 46 | 
            -
               | 
| 47 | 
            -
                 | 
| 48 | 
            -
             | 
| 49 | 
            -
                 | 
| 50 | 
            -
                stream.string
         | 
| 45 | 
            +
              it "should include foreign_key name" do
         | 
| 46 | 
            +
                with_foreign_key Post, :user_id, :users, :id, :name => "yippee" do
         | 
| 47 | 
            +
                  dump_posts.should match /foreign_key.*user_id.*users.*id.*:name => "yippee"/
         | 
| 48 | 
            +
                end
         | 
| 51 49 | 
             
              end
         | 
| 52 50 |  | 
| 53 | 
            -
              it "should  | 
| 54 | 
            -
                 | 
| 55 | 
            -
                   | 
| 51 | 
            +
              it "should sort foreign_key definitions" do
         | 
| 52 | 
            +
                with_foreign_keys Comment, [ [ :post_id, :posts, :id ], [ :commenter_id, :users, :id ]] do
         | 
| 53 | 
            +
                  dump_schema.should match(/foreign_key.+commenter_id.+foreign_key.+post_id/m)
         | 
| 56 54 | 
             
                end
         | 
| 57 55 | 
             
              end
         | 
| 58 56 |  | 
| 59 57 | 
             
              context "with constraint dependencies" do
         | 
| 60 58 | 
             
                it "should sort in Posts => Comments direction" do
         | 
| 61 59 | 
             
                  with_foreign_key Comment, :post_id, :posts, :id do
         | 
| 62 | 
            -
                     | 
| 60 | 
            +
                    dump_schema.should match(%r{create_table "posts".*create_table "comments"}m)
         | 
| 63 61 | 
             
                  end
         | 
| 64 62 | 
             
                end
         | 
| 65 63 | 
             
                it "should sort in Comments => Posts direction" do
         | 
| 66 64 | 
             
                  with_foreign_key Post, :first_comment_id, :comments, :id do
         | 
| 67 | 
            -
                     | 
| 65 | 
            +
                    dump_schema.should match(%r{create_table "comments".*create_table "posts"}m)
         | 
| 68 66 | 
             
                  end
         | 
| 69 67 | 
             
                end
         | 
| 68 | 
            +
             | 
| 69 | 
            +
                it "should handle regexp in ignore_tables" do
         | 
| 70 | 
            +
                  with_foreign_key Comment, :post_id, :posts, :id do
         | 
| 71 | 
            +
                    dump = dump_schema(:ignore => /post/)
         | 
| 72 | 
            +
                    dump.should match /create_table "comments"/
         | 
| 73 | 
            +
                    dump.should_not match /create_table "posts"/
         | 
| 74 | 
            +
                  end
         | 
| 75 | 
            +
                end
         | 
| 76 | 
            +
             | 
| 70 77 | 
             
              end
         | 
| 71 78 |  | 
| 72 79 | 
             
              context "with date default" do
         | 
| @@ -82,6 +89,12 @@ describe "Schema dump" do | |
| 82 89 | 
             
                      dump_posts.should match(%r{t.datetime "posted_at",\s*:default => '2001-09-28 00:00:00'})
         | 
| 83 90 | 
             
                    end
         | 
| 84 91 | 
             
                  end
         | 
| 92 | 
            +
             | 
| 93 | 
            +
                  it "can dump a complex default expression" do
         | 
| 94 | 
            +
                    with_additional_column Post, :name, :string, :default => {:expr => 'substring(random()::text from 3 for 6)'} do
         | 
| 95 | 
            +
                      dump_posts.should match(%r{t.string\s+"name", :default => { :expr => "\\"substring\\"\(\(random\(\)\)::text, 3, 6\)" }})
         | 
| 96 | 
            +
                    end
         | 
| 97 | 
            +
                  end
         | 
| 85 98 | 
             
                end
         | 
| 86 99 |  | 
| 87 100 | 
             
                if SchemaPlusHelpers.sqlite3?
         | 
| @@ -126,8 +139,8 @@ describe "Schema dump" do | |
| 126 139 | 
             
              end
         | 
| 127 140 |  | 
| 128 141 | 
             
              it "should include index name" do
         | 
| 129 | 
            -
                with_index Post, :user_id, :name => " | 
| 130 | 
            -
                  dump_posts.should match(to_regexp(%q{t.index ["user_id"], :name => " | 
| 142 | 
            +
                with_index Post, :user_id, :name => "custom_name" do
         | 
| 143 | 
            +
                  dump_posts.should match(to_regexp(%q{t.index ["user_id"], :name => "custom_name"}))
         | 
| 131 144 | 
             
                end
         | 
| 132 145 | 
             
              end
         | 
| 133 146 |  | 
| @@ -137,6 +150,16 @@ describe "Schema dump" do | |
| 137 150 | 
             
                end
         | 
| 138 151 | 
             
              end
         | 
| 139 152 |  | 
| 153 | 
            +
              unless SchemaPlusHelpers.mysql?
         | 
| 154 | 
            +
             | 
| 155 | 
            +
                it "should include index order" do
         | 
| 156 | 
            +
                  with_index Post, [:user_id, :first_comment_id], :order => { :user_id => :asc, :first_comment_id => :desc } do
         | 
| 157 | 
            +
                    dump_posts.should match(%r{t.index \["user_id", "first_comment_id"\],.*:order => {"user_id" => :asc, "first_comment_id" => :desc}})
         | 
| 158 | 
            +
                  end
         | 
| 159 | 
            +
                end
         | 
| 160 | 
            +
             | 
| 161 | 
            +
              end
         | 
| 162 | 
            +
             | 
| 140 163 | 
             
              if SchemaPlusHelpers.postgresql?
         | 
| 141 164 |  | 
| 142 165 | 
             
                it "should define case insensitive index" do
         | 
| @@ -176,15 +199,15 @@ describe "Schema dump" do | |
| 176 199 | 
             
                  end
         | 
| 177 200 |  | 
| 178 201 | 
             
                  it "should not raise an error" do
         | 
| 179 | 
            -
                    expect {  | 
| 202 | 
            +
                    expect { dump_schema }.to_not raise_error
         | 
| 180 203 | 
             
                  end
         | 
| 181 204 |  | 
| 182 205 | 
             
                  it "should dump constraints after the tables they reference" do
         | 
| 183 | 
            -
                     | 
| 184 | 
            -
                     | 
| 185 | 
            -
                     | 
| 186 | 
            -
                     | 
| 187 | 
            -
                     | 
| 206 | 
            +
                    dump_schema.should match(%r{create_table "comments".*foreign_key.*\["first_comment_id"\], "comments", \["id"\]}m)
         | 
| 207 | 
            +
                    dump_schema.should match(%r{create_table "posts".*foreign_key.*\["first_post_id"\], "posts", \["id"\]}m)
         | 
| 208 | 
            +
                    dump_schema.should match(%r{create_table "posts".*foreign_key.*\["post_id"\], "posts", \["id"\]}m)
         | 
| 209 | 
            +
                    dump_schema.should match(%r{create_table "users".*foreign_key.*\["commenter_id"\], "users", \["id"\]}m)
         | 
| 210 | 
            +
                    dump_schema.should match(%r{create_table "users".*foreign_key.*\["user_id"\], "users", \["id"\]}m)
         | 
| 188 211 | 
             
                  end
         | 
| 189 212 | 
             
                end
         | 
| 190 213 | 
             
              end
         | 
| @@ -207,14 +230,20 @@ describe "Schema dump" do | |
| 207 230 | 
             
                yield
         | 
| 208 231 | 
             
              end
         | 
| 209 232 |  | 
| 210 | 
            -
              def with_foreign_key(model, columns, referenced_table_name, referenced_columns, options = {})
         | 
| 233 | 
            +
              def with_foreign_key(model, columns, referenced_table_name, referenced_columns, options = {}, &block)
         | 
| 234 | 
            +
                with_foreign_keys(model, [[columns, referenced_table_name, referenced_columns, options]], &block)
         | 
| 235 | 
            +
              end
         | 
| 236 | 
            +
             | 
| 237 | 
            +
              def with_foreign_keys(model, columnsets)
         | 
| 211 238 | 
             
                table_columns = model.columns.reject{|column| column.name == 'id'}
         | 
| 212 239 | 
             
                ActiveRecord::Migration.suppress_messages do
         | 
| 213 240 | 
             
                  ActiveRecord::Migration.create_table model.table_name, :force => true do |t|
         | 
| 214 241 | 
             
                    table_columns.each do |column|
         | 
| 215 242 | 
             
                      t.column column.name, column.type
         | 
| 216 243 | 
             
                    end
         | 
| 217 | 
            -
                     | 
| 244 | 
            +
                    columnsets.each do |columns, referenced_table_name, referenced_columns, options|
         | 
| 245 | 
            +
                      t.foreign_key columns, referenced_table_name, referenced_columns, options || {}
         | 
| 246 | 
            +
                    end
         | 
| 218 247 | 
             
                  end
         | 
| 219 248 | 
             
                end
         | 
| 220 249 | 
             
                model.reset_column_information
         | 
| @@ -257,4 +286,15 @@ describe "Schema dump" do | |
| 257 286 | 
             
                name ||= model.foreign_keys.detect { |fk| fk.table_name == model.table_name.to_s && fk.column_names == Array(columns).collect(&:to_s) }.name
         | 
| 258 287 | 
             
              end
         | 
| 259 288 |  | 
| 289 | 
            +
              def dump_schema(opts={})
         | 
| 290 | 
            +
                stream = StringIO.new
         | 
| 291 | 
            +
                ActiveRecord::SchemaDumper.ignore_tables = Array.wrap(opts[:ignore]) || []
         | 
| 292 | 
            +
                ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
         | 
| 293 | 
            +
                stream.string
         | 
| 294 | 
            +
              end
         | 
| 295 | 
            +
             | 
| 296 | 
            +
              def dump_posts
         | 
| 297 | 
            +
                dump_schema(:ignore => %w[users comments])
         | 
| 298 | 
            +
              end
         | 
| 299 | 
            +
             | 
| 260 300 | 
             
            end
         | 
    
        data/spec/schema_spec.rb
    CHANGED
    
    | @@ -17,42 +17,40 @@ describe ActiveRecord::Schema do | |
| 17 17 | 
             
                end
         | 
| 18 18 |  | 
| 19 19 | 
             
                it "should pass" do
         | 
| 20 | 
            -
                  expect {  | 
| 20 | 
            +
                  expect { do_schema }.to_not raise_error
         | 
| 21 21 | 
             
                end
         | 
| 22 22 |  | 
| 23 23 | 
             
                it "should create only explicity added indexes" do
         | 
| 24 | 
            -
                   | 
| 24 | 
            +
                  do_schema
         | 
| 25 25 | 
             
                  expected = SchemaPlusHelpers.mysql? ? 2 : 1
         | 
| 26 26 | 
             
                  connection.tables.collect { |table| connection.indexes(table) }.flatten.should have(expected).items
         | 
| 27 27 | 
             
                end
         | 
| 28 28 |  | 
| 29 29 | 
             
                it "should create only explicity added foriegn keys" do
         | 
| 30 | 
            -
                   | 
| 30 | 
            +
                  do_schema
         | 
| 31 31 | 
             
                  connection.tables.collect { |table| connection.foreign_keys(table) }.flatten.should have(2).items
         | 
| 32 32 | 
             
                end
         | 
| 33 33 |  | 
| 34 34 | 
             
              end
         | 
| 35 35 |  | 
| 36 36 | 
             
              protected
         | 
| 37 | 
            -
              def  | 
| 38 | 
            -
                 | 
| 39 | 
            -
                   | 
| 40 | 
            -
                    connection.tables.each do |table| drop_table table end
         | 
| 37 | 
            +
              def do_schema
         | 
| 38 | 
            +
                define_schema do
         | 
| 39 | 
            +
                  connection.tables.each do |table| drop_table table end
         | 
| 41 40 |  | 
| 42 | 
            -
             | 
| 43 | 
            -
             | 
| 41 | 
            +
                  create_table :users, :force => true do
         | 
| 42 | 
            +
                  end
         | 
| 44 43 |  | 
| 45 | 
            -
             | 
| 46 | 
            -
             | 
| 44 | 
            +
                  create_table :colors, :force => true do
         | 
| 45 | 
            +
                  end
         | 
| 47 46 |  | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 47 | 
            +
                  create_table :shoes, :force => true do
         | 
| 48 | 
            +
                  end
         | 
| 50 49 |  | 
| 51 | 
            -
             | 
| 52 | 
            -
             | 
| 53 | 
            -
             | 
| 54 | 
            -
             | 
| 55 | 
            -
                    end
         | 
| 50 | 
            +
                  create_table :posts, :force => true do |t|
         | 
| 51 | 
            +
                    t.integer :user_id, :references => :users, :index => true
         | 
| 52 | 
            +
                    t.integer :shoe_id, :references => :shoes   # should not have an index (except mysql)
         | 
| 53 | 
            +
                    t.integer :color_id   # should not have a foreign key nor index
         | 
| 56 54 | 
             
                  end
         | 
| 57 55 | 
             
                end
         | 
| 58 56 | 
             
              end
         | 
    
        data/spec/spec_helper.rb
    CHANGED
    
    | @@ -19,30 +19,31 @@ RSpec.configure do |config| | |
| 19 19 | 
             
              config.include(SchemaPlusHelpers)
         | 
| 20 20 | 
             
            end
         | 
| 21 21 |  | 
| 22 | 
            -
            def  | 
| 23 | 
            -
               | 
| 24 | 
            -
             | 
| 22 | 
            +
            def with_fk_config(opts, &block)
         | 
| 23 | 
            +
              save = Hash[opts.keys.collect{|key| [key, SchemaPlus.config.foreign_keys.send(key)]}]
         | 
| 24 | 
            +
              begin
         | 
| 25 | 
            +
                SchemaPlus.config.foreign_keys.update_attributes(opts)
         | 
| 26 | 
            +
                yield
         | 
| 27 | 
            +
              ensure
         | 
| 28 | 
            +
                SchemaPlus.config.foreign_keys.update_attributes(save)
         | 
| 25 29 | 
             
              end
         | 
| 26 30 | 
             
            end
         | 
| 27 31 |  | 
| 28 | 
            -
            def  | 
| 29 | 
            -
               | 
| 30 | 
            -
                config.foreign_keys.auto_create = false;
         | 
| 31 | 
            -
              end
         | 
| 32 | 
            -
              load_schema('core_schema.rb')
         | 
| 33 | 
            -
              load 'models/user.rb'
         | 
| 34 | 
            -
              load 'models/post.rb'
         | 
| 35 | 
            -
              load 'models/comment.rb'
         | 
| 32 | 
            +
            def with_fk_auto_create(value = true, &block)
         | 
| 33 | 
            +
              with_fk_config(:auto_create => value, &block)
         | 
| 36 34 | 
             
            end
         | 
| 37 35 |  | 
| 38 | 
            -
            def  | 
| 39 | 
            -
               | 
| 40 | 
            -
                 | 
| 36 | 
            +
            def define_schema(config={}, &block)
         | 
| 37 | 
            +
              with_fk_config(config) do
         | 
| 38 | 
            +
                ActiveRecord::Migration.suppress_messages do
         | 
| 39 | 
            +
                  ActiveRecord::Schema.define do
         | 
| 40 | 
            +
                    connection.tables.each do |table|
         | 
| 41 | 
            +
                      drop_table table
         | 
| 42 | 
            +
                    end
         | 
| 43 | 
            +
                    instance_eval &block
         | 
| 44 | 
            +
                  end
         | 
| 45 | 
            +
                end
         | 
| 41 46 | 
             
              end
         | 
| 42 | 
            -
              load_schema('auto_schema.rb')
         | 
| 43 | 
            -
              load 'models/user.rb'
         | 
| 44 | 
            -
              load 'models/post.rb'
         | 
| 45 | 
            -
              load 'models/comment.rb'
         | 
| 46 47 | 
             
            end
         | 
| 47 48 |  | 
| 48 49 | 
             
            def remove_all_models
         | 
| @@ -23,7 +23,8 @@ module SchemaPlusMatchers | |
| 23 23 | 
             
                    @result.any? do |fk| 
         | 
| 24 24 | 
             
                      fk.column_names == @column_names &&
         | 
| 25 25 | 
             
                        (@on_update ? fk.on_update == @on_update : true) &&
         | 
| 26 | 
            -
                        (@on_delete ? fk.on_delete == @on_delete : true)
         | 
| 26 | 
            +
                        (@on_delete ? fk.on_delete == @on_delete : true) &&
         | 
| 27 | 
            +
                        (@name ? fk.name == @name : true)
         | 
| 27 28 | 
             
                    end
         | 
| 28 29 | 
             
                  else
         | 
| 29 30 | 
             
                    !@result.empty?
         | 
| @@ -56,6 +57,11 @@ module SchemaPlusMatchers | |
| 56 57 | 
             
                  self
         | 
| 57 58 | 
             
                end
         | 
| 58 59 |  | 
| 60 | 
            +
                def with_name(action)
         | 
| 61 | 
            +
                  @name = action
         | 
| 62 | 
            +
                  self
         | 
| 63 | 
            +
                end
         | 
| 64 | 
            +
             | 
| 59 65 | 
             
              end
         | 
| 60 66 |  | 
| 61 67 | 
             
              def reference(*expect)
         | 
    
        data/spec/views_spec.rb
    CHANGED
    
    | @@ -1,5 +1,8 @@ | |
| 1 1 | 
             
            require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
         | 
| 2 2 |  | 
| 3 | 
            +
            class Item < ActiveRecord::Base
         | 
| 4 | 
            +
            end
         | 
| 5 | 
            +
             | 
| 3 6 | 
             
            class AOnes < ActiveRecord::Base
         | 
| 4 7 | 
             
            end
         | 
| 5 8 |  | 
| @@ -81,7 +84,7 @@ describe ActiveRecord do | |
| 81 84 |  | 
| 82 85 |  | 
| 83 86 | 
             
                  it "should raise an error by default" do
         | 
| 84 | 
            -
                    expect {migration.create_view('dupe_me', 'SELECT * FROM items WHERE (a=2)')}. | 
| 87 | 
            +
                    expect {migration.create_view('dupe_me', 'SELECT * FROM items WHERE (a=2)')}.to raise_error ActiveRecord::StatementInvalid
         | 
| 85 88 | 
             
                  end
         | 
| 86 89 |  | 
| 87 90 | 
             
                  it "should override existing definition if :force true" do
         | 
| @@ -137,7 +140,7 @@ describe ActiveRecord do | |
| 137 140 | 
             
                      t.string  :s
         | 
| 138 141 | 
             
                    end
         | 
| 139 142 |  | 
| 140 | 
            -
                    create_view :a_ones,  | 
| 143 | 
            +
                    create_view :a_ones, Item.select('b, s').where(:a => 1)
         | 
| 141 144 | 
             
                    create_view :ab_ones, "select s from a_ones where b = 1"
         | 
| 142 145 | 
             
                  end
         | 
| 143 146 | 
             
                end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: schema_plus
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 1.0.0
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -10,22 +10,27 @@ authors: | |
| 10 10 | 
             
            autorequire: 
         | 
| 11 11 | 
             
            bindir: bin
         | 
| 12 12 | 
             
            cert_chain: []
         | 
| 13 | 
            -
            date: 2012- | 
| 13 | 
            +
            date: 2012-12-04 00:00:00.000000000 Z
         | 
| 14 14 | 
             
            dependencies:
         | 
| 15 15 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 16 16 | 
             
              name: rails
         | 
| 17 | 
            -
              requirement:  | 
| 17 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 18 18 | 
             
                none: false
         | 
| 19 19 | 
             
                requirements:
         | 
| 20 20 | 
             
                - - ! '>='
         | 
| 21 21 | 
             
                  - !ruby/object:Gem::Version
         | 
| 22 | 
            -
                    version: ' | 
| 22 | 
            +
                    version: '3.2'
         | 
| 23 23 | 
             
              type: :runtime
         | 
| 24 24 | 
             
              prerelease: false
         | 
| 25 | 
            -
              version_requirements:  | 
| 25 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 26 | 
            +
                none: false
         | 
| 27 | 
            +
                requirements:
         | 
| 28 | 
            +
                - - ! '>='
         | 
| 29 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 30 | 
            +
                    version: '3.2'
         | 
| 26 31 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 27 32 | 
             
              name: valuable
         | 
| 28 | 
            -
              requirement:  | 
| 33 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 29 34 | 
             
                none: false
         | 
| 30 35 | 
             
                requirements:
         | 
| 31 36 | 
             
                - - ! '>='
         | 
| @@ -33,21 +38,15 @@ dependencies: | |
| 33 38 | 
             
                    version: '0'
         | 
| 34 39 | 
             
              type: :runtime
         | 
| 35 40 | 
             
              prerelease: false
         | 
| 36 | 
            -
              version_requirements:  | 
| 37 | 
            -
            - !ruby/object:Gem::Dependency
         | 
| 38 | 
            -
              name: rake
         | 
| 39 | 
            -
              requirement: &70097168545600 !ruby/object:Gem::Requirement
         | 
| 41 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 40 42 | 
             
                none: false
         | 
| 41 43 | 
             
                requirements:
         | 
| 42 | 
            -
                - -  | 
| 44 | 
            +
                - - ! '>='
         | 
| 43 45 | 
             
                  - !ruby/object:Gem::Version
         | 
| 44 | 
            -
                    version: 0 | 
| 45 | 
            -
              type: :development
         | 
| 46 | 
            -
              prerelease: false
         | 
| 47 | 
            -
              version_requirements: *70097168545600
         | 
| 46 | 
            +
                    version: '0'
         | 
| 48 47 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 49 | 
            -
              name:  | 
| 50 | 
            -
              requirement:  | 
| 48 | 
            +
              name: rake
         | 
| 49 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 51 50 | 
             
                none: false
         | 
| 52 51 | 
             
                requirements:
         | 
| 53 52 | 
             
                - - ! '>='
         | 
| @@ -55,21 +54,15 @@ dependencies: | |
| 55 54 | 
             
                    version: '0'
         | 
| 56 55 | 
             
              type: :development
         | 
| 57 56 | 
             
              prerelease: false
         | 
| 58 | 
            -
              version_requirements:  | 
| 59 | 
            -
            - !ruby/object:Gem::Dependency
         | 
| 60 | 
            -
              name: pg
         | 
| 61 | 
            -
              requirement: &70097168069640 !ruby/object:Gem::Requirement
         | 
| 57 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 62 58 | 
             
                none: false
         | 
| 63 59 | 
             
                requirements:
         | 
| 64 60 | 
             
                - - ! '>='
         | 
| 65 61 | 
             
                  - !ruby/object:Gem::Version
         | 
| 66 62 | 
             
                    version: '0'
         | 
| 67 | 
            -
              type: :development
         | 
| 68 | 
            -
              prerelease: false
         | 
| 69 | 
            -
              version_requirements: *70097168069640
         | 
| 70 63 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 71 | 
            -
              name:  | 
| 72 | 
            -
              requirement:  | 
| 64 | 
            +
              name: rspec
         | 
| 65 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 73 66 | 
             
                none: false
         | 
| 74 67 | 
             
                requirements:
         | 
| 75 68 | 
             
                - - ! '>='
         | 
| @@ -77,21 +70,15 @@ dependencies: | |
| 77 70 | 
             
                    version: '0'
         | 
| 78 71 | 
             
              type: :development
         | 
| 79 72 | 
             
              prerelease: false
         | 
| 80 | 
            -
              version_requirements:  | 
| 81 | 
            -
            - !ruby/object:Gem::Dependency
         | 
| 82 | 
            -
              name: sqlite3
         | 
| 83 | 
            -
              requirement: &70097168067300 !ruby/object:Gem::Requirement
         | 
| 73 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 84 74 | 
             
                none: false
         | 
| 85 75 | 
             
                requirements:
         | 
| 86 76 | 
             
                - - ! '>='
         | 
| 87 77 | 
             
                  - !ruby/object:Gem::Version
         | 
| 88 78 | 
             
                    version: '0'
         | 
| 89 | 
            -
              type: :development
         | 
| 90 | 
            -
              prerelease: false
         | 
| 91 | 
            -
              version_requirements: *70097168067300
         | 
| 92 79 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 93 80 | 
             
              name: simplecov
         | 
| 94 | 
            -
              requirement:  | 
| 81 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 95 82 | 
             
                none: false
         | 
| 96 83 | 
             
                requirements:
         | 
| 97 84 | 
             
                - - ! '>='
         | 
| @@ -99,10 +86,15 @@ dependencies: | |
| 99 86 | 
             
                    version: '0'
         | 
| 100 87 | 
             
              type: :development
         | 
| 101 88 | 
             
              prerelease: false
         | 
| 102 | 
            -
              version_requirements:  | 
| 89 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 90 | 
            +
                none: false
         | 
| 91 | 
            +
                requirements:
         | 
| 92 | 
            +
                - - ! '>='
         | 
| 93 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 94 | 
            +
                    version: '0'
         | 
| 103 95 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 104 96 | 
             
              name: simplecov-gem-adapter
         | 
| 105 | 
            -
              requirement:  | 
| 97 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 106 98 | 
             
                none: false
         | 
| 107 99 | 
             
                requirements:
         | 
| 108 100 | 
             
                - - ! '>='
         | 
| @@ -110,7 +102,12 @@ dependencies: | |
| 110 102 | 
             
                    version: '0'
         | 
| 111 103 | 
             
              type: :development
         | 
| 112 104 | 
             
              prerelease: false
         | 
| 113 | 
            -
              version_requirements:  | 
| 105 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 106 | 
            +
                none: false
         | 
| 107 | 
            +
                requirements:
         | 
| 108 | 
            +
                - - ! '>='
         | 
| 109 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 110 | 
            +
                    version: '0'
         | 
| 114 111 | 
             
            description: ! 'SchemaPlus is an ActiveRecord extension that provides enhanced capabilities
         | 
| 115 112 | 
             
              for schema definition and querying, including: enhanced and more DRY index capabilities,
         | 
| 116 113 | 
             
              support and automation for foreign key constraints, and support for views.'
         | 
| @@ -127,14 +124,11 @@ files: | |
| 127 124 | 
             
            - MIT-LICENSE
         | 
| 128 125 | 
             
            - README.rdoc
         | 
| 129 126 | 
             
            - Rakefile
         | 
| 130 | 
            -
            - gemfiles/ | 
| 131 | 
            -
            - gemfiles/ | 
| 132 | 
            -
            - gemfiles/ | 
| 133 | 
            -
            - gemfiles/ | 
| 134 | 
            -
            - gemfiles/ | 
| 135 | 
            -
            - gemfiles/Gemfile.rails-3.1.lock
         | 
| 136 | 
            -
            - gemfiles/Gemfile.rails-3.2
         | 
| 137 | 
            -
            - gemfiles/Gemfile.rails-3.2.lock
         | 
| 127 | 
            +
            - gemfiles/rails-3.2/Gemfile.base
         | 
| 128 | 
            +
            - gemfiles/rails-3.2/Gemfile.mysql
         | 
| 129 | 
            +
            - gemfiles/rails-3.2/Gemfile.mysql2
         | 
| 130 | 
            +
            - gemfiles/rails-3.2/Gemfile.postgresql
         | 
| 131 | 
            +
            - gemfiles/rails-3.2/Gemfile.sqlite3
         | 
| 138 132 | 
             
            - init.rb
         | 
| 139 133 | 
             
            - lib/rails/tasks/database.rake
         | 
| 140 134 | 
             
            - lib/schema_plus.rb
         | 
| @@ -149,6 +143,7 @@ files: | |
| 149 143 | 
             
            - lib/schema_plus/active_record/connection_adapters/schema_statements.rb
         | 
| 150 144 | 
             
            - lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb
         | 
| 151 145 | 
             
            - lib/schema_plus/active_record/connection_adapters/table_definition.rb
         | 
| 146 | 
            +
            - lib/schema_plus/active_record/db_default.rb
         | 
| 152 147 | 
             
            - lib/schema_plus/active_record/foreign_keys.rb
         | 
| 153 148 | 
             
            - lib/schema_plus/active_record/schema.rb
         | 
| 154 149 | 
             
            - lib/schema_plus/active_record/schema_dumper.rb
         | 
| @@ -168,10 +163,7 @@ files: | |
| 168 163 | 
             
            - spec/index_definition_spec.rb
         | 
| 169 164 | 
             
            - spec/index_spec.rb
         | 
| 170 165 | 
             
            - spec/migration_spec.rb
         | 
| 171 | 
            -
            - spec/ | 
| 172 | 
            -
            - spec/models/post.rb
         | 
| 173 | 
            -
            - spec/models/user.rb
         | 
| 174 | 
            -
            - spec/rails3_migration_spec.rb
         | 
| 166 | 
            +
            - spec/multiple_schemas_spec.rb
         | 
| 175 167 | 
             
            - spec/schema/auto_schema.rb
         | 
| 176 168 | 
             
            - spec/schema/core_schema.rb
         | 
| 177 169 | 
             
            - spec/schema_dumper_spec.rb
         | 
| @@ -203,7 +195,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 203 195 | 
             
                  version: '0'
         | 
| 204 196 | 
             
            requirements: []
         | 
| 205 197 | 
             
            rubyforge_project: schema_plus
         | 
| 206 | 
            -
            rubygems_version: 1.8. | 
| 198 | 
            +
            rubygems_version: 1.8.24
         | 
| 207 199 | 
             
            signing_key: 
         | 
| 208 200 | 
             
            specification_version: 3
         | 
| 209 201 | 
             
            summary: Enhances ActiveRecord schema mechanism, including more DRY index creation
         | 
| @@ -221,10 +213,7 @@ test_files: | |
| 221 213 | 
             
            - spec/index_definition_spec.rb
         | 
| 222 214 | 
             
            - spec/index_spec.rb
         | 
| 223 215 | 
             
            - spec/migration_spec.rb
         | 
| 224 | 
            -
            - spec/ | 
| 225 | 
            -
            - spec/models/post.rb
         | 
| 226 | 
            -
            - spec/models/user.rb
         | 
| 227 | 
            -
            - spec/rails3_migration_spec.rb
         | 
| 216 | 
            +
            - spec/multiple_schemas_spec.rb
         | 
| 228 217 | 
             
            - spec/schema/auto_schema.rb
         | 
| 229 218 | 
             
            - spec/schema/core_schema.rb
         | 
| 230 219 | 
             
            - spec/schema_dumper_spec.rb
         |