schema_plus 2.0.0.pre3 → 2.0.0.pre5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +0 -1
  3. data/README.md +4 -28
  4. data/TODO.md +5 -2
  5. data/gemfiles/Gemfile.base +0 -4
  6. data/lib/schema_plus.rb +1 -1
  7. data/lib/schema_plus/version.rb +1 -1
  8. data/lib/schema_plus_db_default.rb +0 -6
  9. data/lib/schema_plus_db_default/active_record/attribute.rb +2 -5
  10. data/lib/schema_plus_db_default/db_default.rb +2 -0
  11. data/lib/schema_plus_db_default/middleware.rb +1 -1
  12. data/lib/schema_plus_default_expr.rb +1 -0
  13. data/lib/schema_plus_default_expr/active_record/connection_adapters/column.rb +14 -0
  14. data/lib/schema_plus_default_expr/active_record/connection_adapters/sqlite3_adapter.rb +0 -16
  15. data/lib/schema_plus_default_expr/middleware.rb +3 -4
  16. data/lib/schema_plus_enums/middleware.rb +2 -2
  17. data/lib/schema_plus_foreign_keys.rb +1 -5
  18. data/lib/schema_plus_foreign_keys/active_record/base.rb +0 -4
  19. data/lib/schema_plus_foreign_keys/active_record/connection_adapters/abstract_adapter.rb +13 -19
  20. data/lib/schema_plus_foreign_keys/active_record/connection_adapters/foreign_key_definition.rb +9 -15
  21. data/lib/schema_plus_foreign_keys/active_record/connection_adapters/mysql2_adapter.rb +4 -12
  22. data/lib/schema_plus_foreign_keys/active_record/connection_adapters/postgresql_adapter.rb +2 -8
  23. data/lib/schema_plus_foreign_keys/active_record/connection_adapters/sqlite3_adapter.rb +2 -8
  24. data/lib/schema_plus_foreign_keys/active_record/migration/command_recorder.rb +3 -9
  25. data/lib/schema_plus_foreign_keys/active_record/schema.rb +3 -12
  26. data/lib/schema_plus_foreign_keys/middleware/dumper.rb +1 -1
  27. data/lib/schema_plus_foreign_keys/middleware/sql.rb +11 -0
  28. data/schema_dev.yml +0 -1
  29. data/schema_plus.gemspec +29 -28
  30. data/spec/schema_plus_foreign_keys/foreign_key_spec.rb +0 -22
  31. metadata +38 -27
  32. data/lib/schema_plus_foreign_keys/active_record/connection_adapters/schema_statements.rb +0 -33
  33. data/lib/schema_plus_views.rb +0 -16
  34. data/lib/schema_plus_views/active_record/connection_adapters/abstract_adapter.rb +0 -41
  35. data/lib/schema_plus_views/active_record/connection_adapters/mysql2_adapter.rb +0 -30
  36. data/lib/schema_plus_views/active_record/connection_adapters/postgresql_adapter.rb +0 -31
  37. data/lib/schema_plus_views/active_record/connection_adapters/sqlite3_adapter.rb +0 -18
  38. data/lib/schema_plus_views/middleware.rb +0 -49
  39. data/spec/schema_plus_views/named_schemas_spec.rb +0 -97
  40. data/spec/schema_plus_views/views_spec.rb +0 -194
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a80742e9f65732312bb80cd4adeb736affb95cd4
4
- data.tar.gz: 89912fd50562d59cf9643e47b77a204b4444aa87
3
+ metadata.gz: 024ef569211e48b3ced76fe41a1cfca18fd22dc2
4
+ data.tar.gz: 1c72275c9cf049c750fc7304e9e3d597a08bd7cb
5
5
  SHA512:
6
- metadata.gz: 29653f610f9e22ade23744e9ff9178111f3cb22afbe46c28cf557c38339e8498339ba719b525456d930361e5af4ff69bb7ab4112b627fc27d449360d9408e07f
7
- data.tar.gz: 1ca7e5dfbc8be73af6535bc26c423541f8a3ab0d7ae49806d6d400bf1b4e0e84b05c63871c2c8b50c8b9e146d929cdc5e93a89e323ddc3ab47607c5ef28b9b3d
6
+ metadata.gz: 2819f118b3f9ef1254b62be21cf2052da8498ab74f235c8784a33e36455b096b6fcaa9e42a4d6e36573c3def094b16e52031ee83e2f856786e5d6bf0981e1bfe
7
+ data.tar.gz: 5b797d46390e0b26a9db544024ab029c4105ebc3230a6de0101c7294966a8996b9f4cede86be400598342c878b10b4ef63d81c101a85d0be39d2f80a8039673c
data/.travis.yml CHANGED
@@ -5,7 +5,6 @@
5
5
  ---
6
6
  sudo: false
7
7
  rvm:
8
- - 1.9.3
9
8
  - 2.1.5
10
9
  gemfile:
11
10
  - gemfiles/activerecord-4.2/Gemfile.mysql2
data/README.md CHANGED
@@ -15,6 +15,7 @@ Starting with version 2.0.0, schema_plus is a wrapper that pulls in a collection
15
15
 
16
16
  * [schema_plus_indexes](https://github.com/SchemaPlus/schema_plus_indexes) -- Convenience and consistency in defining and manipulating indexes
17
17
  * [schema_plus_pg_indexes](https://github.com/SchemaPlus/schema_plus_pg_indexes) -- Support for PostgreSQL index features: `case_insenstive`, `expression` and `operator_class`
18
+ * [schema_plus_views](https://github.com/SchemaPlus/schema_plus_views) -- Support for creating and dropping views in migrations, and querying views
18
19
 
19
20
  See detailed documentation in each feature gem's README. You can of course just use whichever of those gems you want individually, rather than this wrapper.
20
21
 
@@ -24,9 +25,10 @@ See detailed documentation in each feature gem's README. You can of course just
24
25
  > * schema_plus_db_default -- Supports `update_attributes!(my_attr: ActiveRecord::DB_DEFAULT)` to set a column back to the default in the database schema.
25
26
  > * schema_plus_default_expr -- Supports using SQL expressions for database default values
26
27
  > * schema_plus_enums -- Support for enum types
27
- > * schema_plus_foreign_keys -- Extends support for foreign keys, including automatic creation.
28
+ > * schema_plus_foreign_keys -- Extends support for foreign keys
28
29
  > * schema_plus_tables -- Convenience and consistency in defining and manipulating tables
29
- > * schema_plus_views -- Adds support for creating and manipulating views
30
+ > * schema_auto_foreign_keys -- Automatic foreign key creation
31
+
30
32
  >
31
33
  > The documentation for these features is at the end of this README
32
34
  >
@@ -68,7 +70,6 @@ SchemaPlus 2.x is tested against all combinations of:
68
70
 
69
71
  <!-- SCHEMA_DEV: MATRIX - begin -->
70
72
  <!-- These lines are auto-generated by schema_dev based on schema_dev.yml -->
71
- * ruby **1.9.3** with activerecord **4.2**, using **mysql2**, **sqlite3** or **postgresql**
72
73
  * ruby **2.1.5** with activerecord **4.2**, using **mysql2**, **sqlite3** or **postgresql**
73
74
 
74
75
  <!-- SCHEMA_DEV: MATRIX - end -->
@@ -200,31 +201,6 @@ with cascade-like behavior.
200
201
 
201
202
  SchemaPlus likewise extends `create_table ... force: true` to use `:cascade`
202
203
 
203
- ### Views
204
-
205
- SchemaPlus provides support for creating and dropping views. In a migration,
206
- a view can be created using a rails relation or literal sql:
207
-
208
- create_view :posts_commented_by_staff, Post.joins(comment: user).where(users: {role: 'staff'}).uniq
209
- create_view :uncommented_posts, "SELECT * FROM posts LEFT OUTER JOIN comments ON comments.post_id = posts.id WHERE comments.id IS NULL"
210
-
211
- And can be dropped:
212
-
213
- drop_view :posts_commented_by_staff
214
- drop_view :uncommented_posts, :if_exists => true
215
-
216
- ActiveRecord works with views the same as with ordinary tables. That is, for
217
- the above views you can define
218
-
219
- class PostCommentedByStaff < ActiveRecord::Base
220
- table_name = "posts_commented_by_staff"
221
- end
222
-
223
- class UncommentedPost < ActiveRecord::Base
224
- end
225
-
226
- Note: In PostgreSQL, all internal views (the ones with `pg_` prefix) will be skipped.
227
-
228
204
  ### Column Defaults: Expressions
229
205
 
230
206
  SchemaPlus allows defaults to be set using expressions or constant values:
data/TODO.md CHANGED
@@ -11,9 +11,13 @@
11
11
  ## schema_plus_index
12
12
  * remove index specs that are testing things now handled by AR. (then see if coverage is still 100%)
13
13
 
14
+ ## schema_plus_pg_index
15
+ * pull over recent 1.8 fix for expression
16
+
14
17
  ## schema_plus_foreign_keys
15
18
  * add specs to cover the deprecations
16
19
  * just do fk enhancements rather than replace AR's add_foreign_key and remove_foreign_key methods and foreign key lookups
20
+ * pull over knojoot's failing spec & solution for circular fk dumping problems from 1.x branch.
17
21
 
18
22
  ## schema_monkey
19
23
  * consider automating the autoloads
@@ -28,7 +32,6 @@
28
32
  * @dump should include the header for consistency
29
33
  * specs for schema_monkey
30
34
  * README to document it -- the conventions and internal API
31
- * pull out schema_monkey_rails into its own gem
32
35
 
33
36
  ## general
34
37
  * rename schema_pg_enum to just schema_enum?
@@ -37,7 +40,7 @@
37
40
 
38
41
  ## schema_dev things:
39
42
 
40
- * boilerplate in README regarding installation, especially if schema_monkey_rails gets pulled out
43
+ * figure out why Gemfile.local is being read twice?
41
44
  * don't bother changing ruby if it's the current version anyway.
42
45
  * have a .schema_dev file with current settings
43
46
  * make it obvious and easy to override
@@ -1,8 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
  gemspec :path => File.expand_path('..', __FILE__)
3
3
 
4
- platform :ruby do
5
- gem "byebug" if RUBY_VERSION > "2"
6
- end
7
-
8
4
  File.exist?(gemfile_local = File.expand_path('../Gemfile.local', __FILE__)) and eval File.read(gemfile_local), binding, gemfile_local
data/lib/schema_plus.rb CHANGED
@@ -7,7 +7,7 @@ require 'schema_plus_foreign_keys'
7
7
  require 'schema_plus_indexes'
8
8
  require 'schema_plus_pg_indexes'
9
9
  require 'schema_plus_tables'
10
- require 'schema_plus_views'
10
+ require 'schema_plus/views'
11
11
 
12
12
  require_relative 'schema_plus/version'
13
13
 
@@ -1,3 +1,3 @@
1
1
  module SchemaPlus
2
- VERSION = "2.0.0.pre3"
2
+ VERSION = "2.0.0.pre5"
3
3
  end
@@ -4,10 +4,4 @@ require_relative 'schema_plus_db_default/active_record/attribute'
4
4
  require_relative 'schema_plus_db_default/db_default'
5
5
  require_relative 'schema_plus_db_default/middleware'
6
6
 
7
- module SchemaPlusDbDefault
8
- def self.insert
9
- ::ActiveRecord.const_set(:DB_DEFAULT, SchemaPlusDbDefault::DB_DEFAULT)
10
- end
11
- end
12
-
13
7
  SchemaMonkey.register(SchemaPlusDbDefault)
@@ -1,16 +1,13 @@
1
1
  module SchemaPlusDbDefault
2
2
  module ActiveRecord
3
3
  module Attribute
4
- def self.included(base)
5
- base.alias_method_chain :original_value, :schema_plus_db_default
6
- end
7
4
 
8
- def original_value_with_schema_plus_db_default
5
+ def original_value
9
6
  # prevent attempts to cast DB_DEFAULT to the attributes type.
10
7
  # We want to keep it as DB_DEFAULT so that we can handle it when
11
8
  # generating the sql.
12
9
  return DB_DEFAULT if value_before_type_cast.equal? DB_DEFAULT
13
- original_value_without_schema_plus_db_default
10
+ super
14
11
  end
15
12
 
16
13
  end
@@ -15,3 +15,5 @@ module SchemaPlusDbDefault
15
15
  end
16
16
  DB_DEFAULT = DbDefault.instance
17
17
  end
18
+
19
+ ::ActiveRecord.const_set :DB_DEFAULT, SchemaPlusDbDefault::DB_DEFAULT
@@ -2,7 +2,7 @@ module SchemaPlusDbDefault
2
2
  module Middleware
3
3
 
4
4
  module Query
5
- module ExecCache
5
+ module Exec
6
6
 
7
7
  module Postgresql
8
8
 
@@ -1,6 +1,7 @@
1
1
  require 'schema_monkey'
2
2
 
3
3
  require_relative 'schema_plus_default_expr/middleware'
4
+ require_relative 'schema_plus_default_expr/active_record/connection_adapters/column'
4
5
 
5
6
  module SchemaPlusDefaultExpr
6
7
  module ActiveRecord
@@ -0,0 +1,14 @@
1
+ module SchemaPlusDefaultExpr
2
+ module ActiveRecord
3
+ module ConnectionAdapters
4
+ module Column
5
+ module Sqlite3
6
+ def default_function
7
+ @default_function ||= "(#{default})" if default =~ /DATETIME/
8
+ super
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,11 +1,7 @@
1
1
  module SchemaPlusDefaultExpr
2
2
  module ActiveRecord
3
3
  module ConnectionAdapters
4
-
5
4
  module Sqlite3Adapter
6
- def self.included(base)
7
- SchemaMonkey.include_once ::ActiveRecord::ConnectionAdapters::Column, SQLiteColumn
8
- end
9
5
 
10
6
  def default_expr_valid?(expr)
11
7
  true # arbitrary sql is okay
@@ -18,18 +14,6 @@ module SchemaPlusDefaultExpr
18
14
  end
19
15
  end
20
16
  end
21
-
22
- module SQLiteColumn
23
-
24
- def self.included(base)
25
- base.alias_method_chain :default_function, :sqlite3 if base.instance_methods.include? :default_function
26
- end
27
-
28
- def default_function_with_sqlite3
29
- @default_function ||= "(#{default})" if default =~ /DATETIME/
30
- default_function_without_sqlite3
31
- end
32
- end
33
17
  end
34
18
  end
35
19
  end
@@ -1,15 +1,14 @@
1
1
  module SchemaPlusDefaultExpr
2
2
  module Middleware
3
3
 
4
- module Migration
5
- module ColumnOptionsSql
4
+ module Sql
5
+ module ColumnOptions
6
6
 
7
7
  # Add options for default expressions
8
8
  def before(env)
9
9
  options = env.options
10
- return unless env.caller.options_include_default?(options)
11
10
 
12
- default = options[:default]
11
+ return unless (default = options[:default])
13
12
 
14
13
  if default.is_a? Hash and [[:expr], [:value]].include?(default.keys)
15
14
  value = default[:value]
@@ -2,7 +2,7 @@ module SchemaPlusEnums
2
2
  module Middleware
3
3
 
4
4
  module Dumper
5
- module Extensions
5
+ module Initial
6
6
 
7
7
  module Postgresql
8
8
 
@@ -12,7 +12,7 @@ module SchemaPlusEnums
12
12
  params << values.map(&:inspect).join(', ')
13
13
  params << ":schema => #{schema.inspect}" if schema != 'public'
14
14
 
15
- env.extensions << "create_enum #{params.join(', ')}"
15
+ env.initial << "create_enum #{params.join(', ')}"
16
16
  end
17
17
  end
18
18
  end
@@ -5,13 +5,13 @@ require 'schema_plus_foreign_keys/version'
5
5
  require 'schema_plus_foreign_keys/active_record/base'
6
6
  require 'schema_plus_foreign_keys/active_record/connection_adapters/abstract_adapter'
7
7
  require 'schema_plus_foreign_keys/active_record/connection_adapters/table_definition'
8
- require 'schema_plus_foreign_keys/active_record/connection_adapters/schema_statements'
9
8
  require 'schema_plus_foreign_keys/active_record/schema'
10
9
  require 'schema_plus_foreign_keys/active_record/connection_adapters/foreign_key_definition'
11
10
  require 'schema_plus_foreign_keys/active_record/migration/command_recorder'
12
11
  require 'schema_plus_foreign_keys/middleware/dumper'
13
12
  require 'schema_plus_foreign_keys/middleware/migration'
14
13
  require 'schema_plus_foreign_keys/middleware/model'
14
+ require 'schema_plus_foreign_keys/middleware/sql'
15
15
 
16
16
  module SchemaPlusForeignKeys
17
17
  module ActiveRecord
@@ -87,10 +87,6 @@ module SchemaPlusForeignKeys
87
87
  yield config
88
88
  end
89
89
 
90
- def self.insert #:nodoc:
91
- SchemaMonkey.include_once ::ActiveRecord::ConnectionAdapters::AbstractAdapter::SchemaCreation, SchemaPlusForeignKeys::ActiveRecord::ConnectionAdapters::AbstractAdapter::VisitTableDefinition
92
- end
93
-
94
90
  end
95
91
 
96
92
  SchemaMonkey.register(SchemaPlusForeignKeys)
@@ -5,10 +5,6 @@ module SchemaPlusForeignKeys
5
5
  # SchemaPlusForeignKeys adds several methods to ActiveRecord::Base
6
6
  #
7
7
  module Base
8
- def self.included(base) #:nodoc:
9
- base.extend(ClassMethods)
10
- end
11
-
12
8
  module ClassMethods #:nodoc:
13
9
 
14
10
  public
@@ -12,6 +12,19 @@ module SchemaPlusForeignKeys
12
12
  #
13
13
  module AbstractAdapter
14
14
 
15
+ def create_table(table, options = {})
16
+ options = options.dup
17
+ config_options = options.delete(:foreign_keys) || {}
18
+
19
+ # override rails' :force to cascade
20
+ drop_table(table, if_exists: true, cascade: true) if options.delete(:force)
21
+
22
+ super(table, options) do |table_definition|
23
+ table_definition.schema_plus_config = SchemaPlusForeignKeys.config.merge(config_options)
24
+ yield table_definition if block_given?
25
+ end
26
+ end
27
+
15
28
  # Define a foreign key constraint. Valid options are :on_update,
16
29
  # :on_delete, and :deferrable, with values as described at
17
30
  # ConnectionAdapters::ForeignKeyDefinition
@@ -147,25 +160,6 @@ module SchemaPlusForeignKeys
147
160
  end
148
161
  end
149
162
 
150
- module VisitTableDefinition
151
- def self.included(base) #:nodoc:
152
- base.alias_method_chain :visit_TableDefinition, :schema_plus_foreign_keys
153
- end
154
-
155
- def visit_TableDefinition_with_schema_plus_foreign_keys(o) #:nodoc:
156
- create_sql = visit_TableDefinition_without_schema_plus_foreign_keys(o)
157
- last_chunk = ") #{o.options}"
158
-
159
- unless create_sql.end_with?(last_chunk)
160
- raise "Internal Error: Can't find '#{last_chunk}' at end of '#{create_sql}' - Rails internals have changed!"
161
- end
162
-
163
- unless o.foreign_keys.empty?
164
- create_sql[create_sql.size - last_chunk.size, 0] = ', ' + o.foreign_keys.map(&:to_sql) * ', '
165
- end
166
- create_sql
167
- end
168
- end
169
163
 
170
164
  #####################################################################
171
165
  #
@@ -17,12 +17,6 @@ module SchemaPlusForeignKeys
17
17
  # :initially_deferred
18
18
  module ForeignKeyDefinition
19
19
 
20
- def self.included(base)
21
- base.class_eval do
22
- alias_method_chain :initialize, :schema_plus_foreign_keys
23
- end
24
- end
25
-
26
20
  def column_names
27
21
  ActiveSupport::Deprecation.warn "ForeignKeyDefinition#column_names is depcreated, use Array.wrap(column)"
28
22
  Array.wrap(column)
@@ -46,7 +40,7 @@ module SchemaPlusForeignKeys
46
40
  ACTIONS = { :cascade => "CASCADE", :restrict => "RESTRICT", :nullify => "SET NULL", :set_default => "SET DEFAULT", :no_action => "NO ACTION" }.freeze
47
41
  ACTION_LOOKUP = ACTIONS.invert.freeze
48
42
 
49
- def initialize_with_schema_plus_foreign_keys(from_table, to_table, options={})
43
+ def initialize(from_table, to_table, options={})
50
44
  [:on_update, :on_delete].each do |key|
51
45
  if options[key] == :set_null
52
46
  require 'byebug' ; byebug
@@ -55,7 +49,8 @@ module SchemaPlusForeignKeys
55
49
  end
56
50
  end
57
51
 
58
- initialize_without_schema_plus_foreign_keys(from_table, to_table, options)
52
+ super from_table, to_table, options
53
+
59
54
  if column.is_a?(Array) and column.length == 1
60
55
  options[:column] = column[0]
61
56
  end
@@ -77,24 +72,23 @@ module SchemaPlusForeignKeys
77
72
  end
78
73
 
79
74
  # Dumps a definition of foreign key.
80
- def to_dump(opts={})
81
- opts = opts.keyword_args(:column, :inline)
75
+ def to_dump(column: nil, inline: nil)
82
76
  dump = case
83
- when opts.column then "foreign_key: {references:"
84
- when opts.inline then "t.foreign_key"
77
+ when column then "foreign_key: {references:"
78
+ when inline then "t.foreign_key"
85
79
  else "add_foreign_key #{from_table.inspect},"
86
80
  end
87
81
  dump << " #{to_table.inspect}"
88
82
 
89
83
  val_or_array = -> val { val.is_a?(Array) ? "[#{val.map(&:inspect).join(', ')}]" : val.inspect }
90
84
 
91
- dump << ", column: #{val_or_array.call column}" unless opts.column
92
- dump << ", primary_key: #{val_or_array.call column}" if custom_primary_key?
85
+ dump << ", column: #{val_or_array.call self.column}" unless column
86
+ dump << ", primary_key: #{val_or_array.call self.column}" if custom_primary_key?
93
87
  dump << ", name: #{name.inspect}" if name
94
88
  dump << ", on_update: #{on_update.inspect}" if on_update
95
89
  dump << ", on_delete: #{on_delete.inspect}" if on_delete
96
90
  dump << ", deferrable: #{deferrable.inspect}" if deferrable
97
- dump << "}" if opts.column
91
+ dump << "}" if column
98
92
  dump
99
93
  end
100
94
 
@@ -7,23 +7,15 @@ module SchemaPlusForeignKeys
7
7
 
8
8
  #:enddoc:
9
9
 
10
- def self.included(base)
11
- base.class_eval do
12
- alias_method_chain :remove_column, :schema_plus_foreign_keys
13
- alias_method_chain :rename_table, :schema_plus_foreign_keys
14
- end
15
- ::ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::SchemaCreation.send(:include, SchemaPlusForeignKeys::ActiveRecord::ConnectionAdapters::AbstractAdapter::VisitTableDefinition)
16
- end
17
-
18
- def remove_column_with_schema_plus_foreign_keys(table_name, column_name, type=nil, options={})
10
+ def remove_column(table_name, column_name, type=nil, options={})
19
11
  foreign_keys(table_name).select { |foreign_key| Array.wrap(foreign_key.column).include?(column_name.to_s) }.each do |foreign_key|
20
12
  remove_foreign_key(table_name, name: foreign_key.name)
21
13
  end
22
- remove_column_without_schema_plus_foreign_keys(table_name, column_name, type, options)
14
+ super table_name, column_name, type, options
23
15
  end
24
16
 
25
- def rename_table_with_schema_plus_foreign_keys(oldname, newname)
26
- rename_table_without_schema_plus_foreign_keys(oldname, newname)
17
+ def rename_table(oldname, newname)
18
+ super
27
19
  rename_foreign_keys(oldname, newname)
28
20
  end
29
21