ridgepole 0.7.3.beta → 0.7.3.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +38 -0
  3. data/Appraisals +2 -2
  4. data/README.md +3 -9
  5. data/Rakefile +7 -1
  6. data/bin/ridgepole +97 -108
  7. data/gemfiles/activerecord_5.2.gemfile +1 -1
  8. data/lib/ridgepole/cli/config.rb +43 -43
  9. data/lib/ridgepole/client.rb +65 -63
  10. data/lib/ridgepole/default_limit.rb +26 -24
  11. data/lib/ridgepole/delta.rb +367 -369
  12. data/lib/ridgepole/diff.rb +366 -377
  13. data/lib/ridgepole/dsl_parser.rb +22 -20
  14. data/lib/ridgepole/dsl_parser/context.rb +95 -93
  15. data/lib/ridgepole/dsl_parser/table_definition.rb +125 -123
  16. data/lib/ridgepole/dumper.rb +73 -71
  17. data/lib/ridgepole/execute_expander.rb +55 -55
  18. data/lib/ridgepole/ext/abstract_mysql_adapter/dump_auto_increment.rb +3 -3
  19. data/lib/ridgepole/ext/abstract_mysql_adapter/use_alter_index.rb +1 -1
  20. data/lib/ridgepole/ext/pp_sort_hash.rb +14 -14
  21. data/lib/ridgepole/ext/schema_dumper.rb +1 -1
  22. data/lib/ridgepole/external_sql_executer.rb +20 -20
  23. data/lib/ridgepole/logger.rb +17 -15
  24. data/lib/ridgepole/migration_ext.rb +6 -8
  25. data/lib/ridgepole/schema_statements_ext.rb +3 -3
  26. data/lib/ridgepole/version.rb +1 -1
  27. data/ridgepole.gemspec +11 -10
  28. data/spec/cli_helper.rb +2 -2
  29. data/spec/erb_helper.rb +6 -2
  30. data/spec/hide_pending_formatter.rb +5 -4
  31. data/spec/mysql/_migrate/migrate_change_table_option_spec.rb +8 -8
  32. data/spec/mysql/bigint_pk/bigint_pk_spec.rb +8 -8
  33. data/spec/mysql/bigint_pk/int_pk_spec.rb +13 -13
  34. data/spec/mysql/cli/config_spec.rb +52 -53
  35. data/spec/mysql/cli/ridgepole_spec.rb +111 -111
  36. data/spec/mysql/collation/collation_spec.rb +39 -39
  37. data/spec/mysql/comment/comment_spec.rb +36 -36
  38. data/spec/mysql/default_lambda/default_lambda_spec.rb +30 -30
  39. data/spec/mysql/diff/diff2_spec.rb +18 -18
  40. data/spec/mysql/diff/diff_spec.rb +11 -11
  41. data/spec/mysql/dump/dump_class_method_spec.rb +2 -2
  42. data/spec/mysql/dump/dump_some_tables_spec.rb +14 -14
  43. data/spec/mysql/dump/dump_spec.rb +2 -2
  44. data/spec/mysql/dump/dump_unknown_column_type_spec.rb +3 -3
  45. data/spec/mysql/dump/dump_without_table_options_spec.rb +8 -8
  46. data/spec/mysql/fk/migrate_change_fk_spec.rb +40 -40
  47. data/spec/mysql/fk/migrate_create_fk_spec.rb +40 -40
  48. data/spec/mysql/fk/migrate_drop_fk_spec.rb +60 -60
  49. data/spec/mysql/migrate/check_orphan_index_spec.rb +6 -6
  50. data/spec/mysql/migrate/migrate_add_column2_spec.rb +43 -43
  51. data/spec/mysql/migrate/migrate_add_column_order_spec.rb +50 -50
  52. data/spec/mysql/migrate/migrate_add_column_spec.rb +11 -11
  53. data/spec/mysql/migrate/migrate_add_column_with_alter_extra_spec.rb +8 -8
  54. data/spec/mysql/migrate/migrate_add_column_with_script_spec.rb +11 -11
  55. data/spec/mysql/migrate/migrate_change_column2_spec.rb +20 -20
  56. data/spec/mysql/migrate/migrate_change_column3_spec.rb +60 -60
  57. data/spec/mysql/migrate/migrate_change_column4_spec.rb +16 -16
  58. data/spec/mysql/migrate/migrate_change_column5_spec.rb +24 -24
  59. data/spec/mysql/migrate/migrate_change_column6_spec.rb +40 -40
  60. data/spec/mysql/migrate/migrate_change_column7_spec.rb +4 -4
  61. data/spec/mysql/migrate/migrate_change_column8_spec.rb +9 -9
  62. data/spec/mysql/migrate/migrate_change_column_default_spec.rb +48 -48
  63. data/spec/mysql/migrate/migrate_change_column_float_spec.rb +16 -16
  64. data/spec/mysql/migrate/migrate_change_column_spec.rb +11 -11
  65. data/spec/mysql/migrate/migrate_change_index2_spec.rb +20 -20
  66. data/spec/mysql/migrate/migrate_change_index3_spec.rb +43 -43
  67. data/spec/mysql/migrate/migrate_change_index4_spec.rb +24 -24
  68. data/spec/mysql/migrate/migrate_change_index5_spec.rb +16 -16
  69. data/spec/mysql/migrate/migrate_change_index6_spec.rb +12 -12
  70. data/spec/mysql/migrate/migrate_change_index7_spec.rb +20 -20
  71. data/spec/mysql/migrate/migrate_change_index8_spec.rb +20 -20
  72. data/spec/mysql/migrate/migrate_change_index_spec.rb +17 -17
  73. data/spec/mysql/migrate/migrate_change_table_comment_spec.rb +10 -10
  74. data/spec/mysql/migrate/migrate_change_table_option_spec.rb +18 -18
  75. data/spec/mysql/migrate/migrate_check_relation_column_type_spec.rb +37 -37
  76. data/spec/mysql/migrate/migrate_create_index2_spec.rb +12 -12
  77. data/spec/mysql/migrate/migrate_create_index_spec.rb +11 -11
  78. data/spec/mysql/migrate/migrate_create_table_spec.rb +8 -8
  79. data/spec/mysql/migrate/migrate_create_table_with_index_spec.rb +6 -6
  80. data/spec/mysql/migrate/migrate_create_table_with_options_spec.rb +12 -12
  81. data/spec/mysql/migrate/migrate_create_table_with_script_spec.rb +11 -11
  82. data/spec/mysql/migrate/migrate_drop_column_and_index2_spec.rb +9 -9
  83. data/spec/mysql/migrate/migrate_drop_column_and_index_spec.rb +12 -12
  84. data/spec/mysql/migrate/migrate_drop_column_and_unique_index_spec.rb +10 -10
  85. data/spec/mysql/migrate/migrate_drop_column_spec.rb +11 -11
  86. data/spec/mysql/migrate/migrate_drop_index_spec.rb +11 -11
  87. data/spec/mysql/migrate/migrate_drop_table_spec.rb +8 -8
  88. data/spec/mysql/migrate/migrate_duplicate_index_spec.rb +6 -6
  89. data/spec/mysql/migrate/migrate_duplicate_table_spec.rb +6 -6
  90. data/spec/mysql/migrate/migrate_empty_spec.rb +4 -4
  91. data/spec/mysql/migrate/migrate_execute_spec.rb +55 -55
  92. data/spec/mysql/migrate/migrate_log_file_spec.rb +9 -9
  93. data/spec/mysql/migrate/migrate_merge_mode_spec.rb +9 -9
  94. data/spec/mysql/migrate/migrate_noop_spec.rb +19 -19
  95. data/spec/mysql/migrate/migrate_primary_key_spec.rb +18 -18
  96. data/spec/mysql/migrate/migrate_rename_column_spec.rb +17 -17
  97. data/spec/mysql/migrate/migrate_rename_table_spec.rb +26 -26
  98. data/spec/mysql/migrate/migrate_same_default_null_spec.rb +16 -16
  99. data/spec/mysql/migrate/migrate_same_spec.rb +4 -4
  100. data/spec/mysql/migrate/migrate_script_error_spec.rb +8 -8
  101. data/spec/mysql/migrate/migrate_skip_column_comment_change_spec.rb +8 -8
  102. data/spec/mysql/migrate/migrate_skip_drop_table_spec.rb +9 -9
  103. data/spec/mysql/migrate/migrate_skip_rename_column_spec.rb +8 -8
  104. data/spec/mysql/migrate/migrate_skip_rename_table_spec.rb +9 -9
  105. data/spec/mysql/migrate/migrate_with_ignore_tables_spec.rb +64 -64
  106. data/spec/mysql/migrate/migrate_with_pre_post_query_spec.rb +12 -12
  107. data/spec/mysql/migrate/migrate_with_tables_spec.rb +29 -29
  108. data/spec/mysql/migrate/migrate_with_verbose_log_spec.rb +18 -18
  109. data/spec/mysql/migrate_/migrate_create_index_with_alter_spec.rb +8 -8
  110. data/spec/mysql/migrate_/migrate_drop_index_with_alter_spec.rb +8 -8
  111. data/spec/mysql/text_blob_types/text_blob_types_spec.rb +8 -8
  112. data/spec/mysql/~default_name_fk/migrate_change_fk_spec.rb +12 -12
  113. data/spec/mysql/~default_name_fk/migrate_create_fk_spec.rb +30 -30
  114. data/spec/mysql/~default_name_fk/migrate_drop_fk_spec.rb +22 -22
  115. data/spec/mysql/~dump_auto_increment/migrate_create_table_with_index_spec.rb +7 -7
  116. data/spec/mysql57/json/add_json_column_spec.rb +8 -8
  117. data/spec/mysql57/json/change_json_column_spec.rb +24 -24
  118. data/spec/mysql57/json/drop_json_column_spec.rb +8 -8
  119. data/spec/mysql57/virtual/add_virtual_column_spec.rb +9 -9
  120. data/spec/mysql57/virtual/change_virtual_column_spec.rb +9 -9
  121. data/spec/mysql57/virtual/drop_virtual_column_spec.rb +9 -9
  122. data/spec/postgresql/diff/diff_spec.rb +18 -18
  123. data/spec/postgresql/dump/dump_spec.rb +2 -2
  124. data/spec/postgresql/fk/migrate_change_fk_spec.rb +24 -24
  125. data/spec/postgresql/fk/migrate_create_fk_spec.rb +40 -40
  126. data/spec/postgresql/fk/migrate_drop_fk_spec.rb +44 -44
  127. data/spec/postgresql/migrate/migrate_add_column_spec.rb +21 -21
  128. data/spec/postgresql/migrate/migrate_add_expression_index_spec.rb +2 -2
  129. data/spec/postgresql/migrate/migrate_bigint_spec.rb +12 -12
  130. data/spec/postgresql/migrate/migrate_change_column_default_spec.rb +8 -8
  131. data/spec/postgresql/migrate/migrate_change_column_spec.rb +19 -19
  132. data/spec/postgresql/migrate/migrate_change_index_spec.rb +13 -13
  133. data/spec/postgresql/migrate/migrate_check_relation_column_type_spec.rb +19 -19
  134. data/spec/postgresql/migrate/migrate_create_table_spec.rb +8 -8
  135. data/spec/postgresql/migrate/migrate_create_table_with_default_proc_spec.rb +18 -18
  136. data/spec/postgresql/migrate/migrate_drop_column_spec.rb +11 -11
  137. data/spec/postgresql/migrate/migrate_drop_column_with_index_spec.rb +12 -12
  138. data/spec/postgresql/migrate/migrate_drop_expression_index_spec.rb +8 -8
  139. data/spec/postgresql/migrate/migrate_drop_index_spec.rb +11 -11
  140. data/spec/postgresql/migrate/migrate_drop_table_spec.rb +8 -8
  141. data/spec/postgresql/migrate/migrate_ext_cols_spec.rb +8 -8
  142. data/spec/postgresql/migrate/migrate_primary_key_spec.rb +97 -0
  143. data/spec/postgresql/migrate/migrate_references_spec.rb +24 -24
  144. data/spec/postgresql/migrate/migrate_rename_column_spec.rb +11 -11
  145. data/spec/postgresql/migrate/migrate_rename_table_spec.rb +16 -16
  146. data/spec/postgresql/migrate/migrate_same_spec.rb +4 -4
  147. data/spec/postgresql/~default_name_fk/migrate_change_fk_spec.rb +12 -12
  148. data/spec/postgresql/~default_name_fk/migrate_create_fk_spec.rb +30 -30
  149. data/spec/postgresql/~default_name_fk/migrate_drop_fk_spec.rb +22 -22
  150. data/spec/processing_for_travis.rb +1 -1
  151. data/spec/spec_condition.rb +2 -2
  152. data/spec/spec_const.rb +9 -9
  153. data/spec/spec_helper.rb +21 -23
  154. metadata +54 -37
@@ -1,29 +1,31 @@
1
- class Ridgepole::DSLParser
2
- def initialize(options = {})
3
- @options = options
4
- end
1
+ module Ridgepole
2
+ class DSLParser
3
+ def initialize(options = {})
4
+ @options = options
5
+ end
5
6
 
6
- def parse(dsl, opts = {})
7
- definition, execute = Context.eval(dsl, opts)
8
- check_orphan_index(definition)
9
- check_orphan_foreign_key(definition)
10
- [definition, execute]
11
- end
7
+ def parse(dsl, opts = {})
8
+ definition, execute = Context.eval(dsl, opts)
9
+ check_orphan_index(definition)
10
+ check_orphan_foreign_key(definition)
11
+ [definition, execute]
12
+ end
12
13
 
13
- private
14
+ private
14
15
 
15
- def check_orphan_index(definition)
16
- definition.each do |table_name, attrs|
17
- if attrs[:indices] and not attrs[:definition]
18
- raise "Table `#{table_name}` to create the index is not defined: #{attrs[:indices].keys.join(',')}"
16
+ def check_orphan_index(definition)
17
+ definition.each do |table_name, attrs|
18
+ if attrs[:indices] && !(attrs[:definition])
19
+ raise "Table `#{table_name}` to create the index is not defined: #{attrs[:indices].keys.join(',')}"
20
+ end
19
21
  end
20
22
  end
21
- end
22
23
 
23
- def check_orphan_foreign_key(definition)
24
- definition.each do |table_name, attrs|
25
- if attrs[:foreign_keys] and not attrs[:definition]
26
- raise "Table `#{table_name}` to create the foreign key is not defined: #{attrs[:foreign_keys].keys.join(',')}"
24
+ def check_orphan_foreign_key(definition)
25
+ definition.each do |table_name, attrs|
26
+ if attrs[:foreign_keys] && !(attrs[:definition])
27
+ raise "Table `#{table_name}` to create the foreign key is not defined: #{attrs[:foreign_keys].keys.join(',')}"
28
+ end
27
29
  end
28
30
  end
29
31
  end
@@ -1,121 +1,123 @@
1
- class Ridgepole::DSLParser
2
- class Context
3
- attr_reader :__definition
4
- attr_reader :__execute
5
-
6
- def initialize(opts = {})
7
- @__working_dir = File.expand_path(opts[:path] ? File.dirname(opts[:path]) : Dir.pwd)
8
- @__definition = {}
9
- @__execute = []
10
- end
1
+ module Ridgepole
2
+ class DSLParser
3
+ class Context
4
+ attr_reader :__definition
5
+ attr_reader :__execute
6
+
7
+ def initialize(opts = {})
8
+ @__working_dir = File.expand_path(opts[:path] ? File.dirname(opts[:path]) : Dir.pwd)
9
+ @__definition = {}
10
+ @__execute = []
11
+ end
12
+
13
+ def self.eval(dsl, opts = {})
14
+ ctx = new(opts)
11
15
 
12
- def self.eval(dsl, opts = {})
13
- ctx = self.new(opts)
16
+ if opts[:path]
17
+ ctx.instance_eval(dsl, opts[:path])
18
+ else
19
+ ctx.instance_eval(dsl)
20
+ end
14
21
 
15
- if opts[:path]
16
- ctx.instance_eval(dsl, opts[:path])
17
- else
18
- ctx.instance_eval(dsl)
22
+ [ctx.__definition, ctx.__execute]
19
23
  end
20
24
 
21
- [ctx.__definition, ctx.__execute]
22
- end
25
+ def create_table(table_name, options = {})
26
+ table_name = table_name.to_s
27
+ table_definition = TableDefinition.new(table_name, self)
23
28
 
24
- def create_table(table_name, options = {})
25
- table_name = table_name.to_s
26
- table_definition = TableDefinition.new(table_name, self)
29
+ if options[:primary_key] && options[:primary_key].is_a?(Symbol)
30
+ options[:primary_key] = options[:primary_key].to_s
31
+ end
32
+ if options[:id] && TableDefinition::ALIAS_TYPES.key?(options[:id])
33
+ type, type_default_opts = TableDefinition::ALIAS_TYPES[options[:id]]
34
+ options[:id] = type
35
+ options = type_default_opts.merge(options)
36
+ end
27
37
 
28
- if options[:primary_key] and options[:primary_key].is_a?(Symbol)
29
- options[:primary_key] = options[:primary_key].to_s
30
- end
31
- if options[:id] and TableDefinition::ALIAS_TYPES.has_key?(options[:id])
32
- type, type_default_opts = TableDefinition::ALIAS_TYPES[options[:id]]
33
- options[:id] = type
34
- options = type_default_opts.merge(options)
35
- end
38
+ yield(table_definition)
39
+ @__definition[table_name] ||= {}
36
40
 
37
- yield(table_definition)
38
- @__definition[table_name] ||= {}
41
+ if @__definition[table_name][:definition]
42
+ raise "Table `#{table_name}` already defined"
43
+ end
39
44
 
40
- if @__definition[table_name][:definition]
41
- raise "Table `#{table_name}` already defined"
45
+ @__definition[table_name][:definition] = table_definition.__definition
46
+ options.delete(:force)
47
+ @__definition[table_name][:options] = options
42
48
  end
43
49
 
44
- @__definition[table_name][:definition] = table_definition.__definition
45
- options.delete(:force)
46
- @__definition[table_name][:options] = options
47
- end
50
+ def add_index(table_name, column_name, options = {})
51
+ table_name = table_name.to_s
52
+ # Keep column_name for expression index support
53
+ # https://github.com/rails/rails/pull/23393
54
+ unless column_name.is_a?(String) && /\W/ === column_name # rubocop:disable Style/CaseEquality
55
+ column_name = [column_name].flatten.map(&:to_s)
56
+ end
57
+ options[:name] = options[:name].to_s if options[:name]
58
+ @__definition[table_name] ||= {}
59
+ @__definition[table_name][:indices] ||= {}
60
+ idx = options[:name] || column_name
48
61
 
49
- def add_index(table_name, column_name, options = {})
50
- table_name = table_name.to_s
51
- # Keep column_name for expression index support
52
- # https://github.com/rails/rails/pull/23393
53
- unless column_name.is_a?(String) && /\W/ === column_name
54
- column_name = [column_name].flatten.map {|i| i.to_s }
55
- end
56
- options[:name] = options[:name].to_s if options[:name]
57
- @__definition[table_name] ||= {}
58
- @__definition[table_name][:indices] ||= {}
59
- idx = options[:name] || column_name
62
+ if @__definition[table_name][:indices][idx]
63
+ raise "Index `#{table_name}(#{idx})` already defined"
64
+ end
60
65
 
61
- if @__definition[table_name][:indices][idx]
62
- raise "Index `#{table_name}(#{idx})` already defined"
63
- end
66
+ if options[:length].is_a?(Numeric)
67
+ index_length = options[:length]
68
+ options[:length] = {}
64
69
 
65
- if options[:length].is_a?(Numeric)
66
- index_length = options[:length]
67
- options[:length] = {}
70
+ column_name.each do |col|
71
+ options[:length][col] = index_length
72
+ end
73
+ end
68
74
 
69
- column_name.each do |col|
70
- options[:length][col] = index_length
75
+ if options[:length]
76
+ options[:length] = options[:length].compact.symbolize_keys
71
77
  end
72
- end
73
78
 
74
- if options[:length]
75
- options[:length] = options[:length].compact.symbolize_keys
79
+ @__definition[table_name][:indices][idx] = {
80
+ column_name: column_name,
81
+ options: options
82
+ }
76
83
  end
77
84
 
78
- @__definition[table_name][:indices][idx] = {
79
- :column_name => column_name,
80
- :options => options,
81
- }
82
- end
85
+ def add_foreign_key(from_table, to_table, options = {})
86
+ from_table = from_table.to_s
87
+ to_table = to_table.to_s
88
+ options[:name] = options[:name].to_s if options[:name]
89
+ @__definition[from_table] ||= {}
90
+ @__definition[from_table][:foreign_keys] ||= {}
91
+ idx = options[:name] || [from_table, to_table]
83
92
 
84
- def add_foreign_key(from_table, to_table, options = {})
85
- from_table = from_table.to_s
86
- to_table = to_table.to_s
87
- options[:name] = options[:name].to_s if options[:name]
88
- @__definition[from_table] ||= {}
89
- @__definition[from_table][:foreign_keys] ||= {}
90
- idx = options[:name] || [from_table, to_table]
93
+ if @__definition[from_table][:foreign_keys][idx]
94
+ raise "Foreign Key `#{from_table}(#{idx})` already defined"
95
+ end
91
96
 
92
- if @__definition[from_table][:foreign_keys][idx]
93
- raise "Foreign Key `#{from_table}(#{idx})` already defined"
97
+ @__definition[from_table][:foreign_keys][idx] = {
98
+ to_table: to_table,
99
+ options: options
100
+ }
94
101
  end
95
102
 
96
- @__definition[from_table][:foreign_keys][idx] = {
97
- :to_table => to_table,
98
- :options => options,
99
- }
100
- end
101
-
102
- def require(file)
103
- schemafile = (file =~ %r|\A/|) ? file : File.join(@__working_dir, file)
103
+ def require(file)
104
+ schemafile = file =~ %r{\A/} ? file : File.join(@__working_dir, file)
104
105
 
105
- if File.exist?(schemafile)
106
- instance_eval(File.read(schemafile), schemafile)
107
- elsif File.exist?(schemafile + '.rb')
108
- instance_eval(File.read(schemafile + '.rb'), schemafile + '.rb')
109
- else
110
- Kernel.require(file)
106
+ if File.exist?(schemafile)
107
+ instance_eval(File.read(schemafile), schemafile)
108
+ elsif File.exist?(schemafile + '.rb')
109
+ instance_eval(File.read(schemafile + '.rb'), schemafile + '.rb')
110
+ else
111
+ Kernel.require(file)
112
+ end
111
113
  end
112
- end
113
114
 
114
- def execute(sql, name = nil, &cond)
115
- @__execute << {
116
- :sql => sql,
117
- :condition => cond,
118
- }
115
+ def execute(sql, _name = nil, &cond)
116
+ @__execute << {
117
+ sql: sql,
118
+ condition: cond
119
+ }
120
+ end
119
121
  end
120
122
  end
121
123
  end
@@ -1,140 +1,142 @@
1
- class Ridgepole::DSLParser
2
- class TableDefinition
3
- attr_reader :__definition
4
-
5
- def initialize(table_name, base)
6
- @__definition = {}
7
- @table_name = table_name
8
- @base = base
9
- end
1
+ module Ridgepole
2
+ class DSLParser
3
+ class TableDefinition
4
+ attr_reader :__definition
5
+
6
+ def initialize(table_name, base)
7
+ @__definition = {}
8
+ @table_name = table_name
9
+ @base = base
10
+ end
10
11
 
11
- def column(name, type, options = {})
12
- name = name.to_s
12
+ def column(name, type, options = {})
13
+ name = name.to_s
13
14
 
14
- @__definition[name] = {
15
- :type => type,
16
- :options => options,
17
- }
18
- end
19
-
20
- DEFAULT_PRIMARY_KEY_TYPE = Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.1') ? :bigint : :integer
21
-
22
- TYPES = [
23
- # https://github.com/rails/rails/blob/v4.2.1/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L274
24
- :string,
25
- :text,
26
- :integer,
27
- :bigint,
28
- :float,
29
- :decimal,
30
- :datetime,
31
- :timestamp,
32
- :time,
33
- :date,
34
- :binary,
35
- :boolean,
36
-
37
- # https://github.com/rails/rails/blob/v4.2.1/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L79
38
- :daterange,
39
- :numrange,
40
- :tsrange,
41
- :tstzrange,
42
- :int4range,
43
- :int8range,
44
- :binary,
45
- :boolean,
46
- :bigint,
47
- :xml,
48
- :tsvector,
49
- :hstore,
50
- :inet,
51
- :cidr,
52
- :macaddr,
53
- :uuid,
54
- :json,
55
- :jsonb,
56
- :ltree,
57
- :citext,
58
- :point,
59
- :bit,
60
- :bit_varying,
61
- :money,
62
-
63
- # https://github.com/rails/rails/blob/v5.1.1/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L184
64
- :virtual,
65
-
66
- # https://github.com/rails/rails/blob/v5.0.4/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L53
67
- :json,
68
- ].uniq
69
-
70
- TYPES.each do |column_type|
71
- define_method column_type do |*args|
72
- options = args.extract_options!
73
- column_names = args
74
- column_names.each {|name| column(name, column_type, options) }
15
+ @__definition[name] = {
16
+ type: type,
17
+ options: options
18
+ }
75
19
  end
76
- end
77
20
 
78
- ALIAS_TYPES = {
79
- # https://github.com/rails/rails/blob/v5.0.0.rc1/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb
80
- tinyblob: [:blob, {limit: 255}],
81
- mediumblob: [:binary, {limit: 16777215}],
82
- longblob: [:binary, {limit: 4294967295}],
83
- tinytext: [:text, {limit: 255}],
84
- mediumtext: [:text, {limit: 16777215}],
85
- longtext: [:text, {limit: 4294967295}],
86
- unsigned_integer: [:integer, {unsigned: true}],
87
- unsigned_bigint: [:bigint, {unsigned: true}],
88
- unsigned_float: [:float, {limit: 24, unsigned: true}],
89
- unsigned_decimal: [:decimal, {precision: 10, unsigned: true}],
90
- }
91
-
92
- # XXX:
93
- def blob(*args)
94
- options = args.extract_options!
95
- options = {limit: 65535}.merge(options)
96
- column_names = args
97
-
98
- column_names.each do |name|
99
- column_type = (0..0xff).include?(options[:limit]) ? :blob : :binary
100
- column(name, column_type, options)
21
+ DEFAULT_PRIMARY_KEY_TYPE = Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.1') ? :bigint : :integer
22
+
23
+ TYPES = [
24
+ # https://github.com/rails/rails/blob/v4.2.1/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L274
25
+ :string,
26
+ :text,
27
+ :integer,
28
+ :bigint,
29
+ :float,
30
+ :decimal,
31
+ :datetime,
32
+ :timestamp,
33
+ :time,
34
+ :date,
35
+ :binary,
36
+ :boolean,
37
+
38
+ # https://github.com/rails/rails/blob/v4.2.1/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L79
39
+ :daterange,
40
+ :numrange,
41
+ :tsrange,
42
+ :tstzrange,
43
+ :int4range,
44
+ :int8range,
45
+ :binary,
46
+ :boolean,
47
+ :bigint,
48
+ :xml,
49
+ :tsvector,
50
+ :hstore,
51
+ :inet,
52
+ :cidr,
53
+ :macaddr,
54
+ :uuid,
55
+ :json,
56
+ :jsonb,
57
+ :ltree,
58
+ :citext,
59
+ :point,
60
+ :bit,
61
+ :bit_varying,
62
+ :money,
63
+
64
+ # https://github.com/rails/rails/blob/v5.1.1/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L184
65
+ :virtual,
66
+
67
+ # https://github.com/rails/rails/blob/v5.0.4/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L53
68
+ :json
69
+ ].uniq
70
+
71
+ TYPES.each do |column_type|
72
+ define_method column_type do |*args|
73
+ options = args.extract_options!
74
+ column_names = args
75
+ column_names.each { |name| column(name, column_type, options) }
76
+ end
101
77
  end
102
- end
103
78
 
104
- ALIAS_TYPES.each do |alias_type, (column_type, default_options)|
105
- define_method alias_type do |*args|
79
+ ALIAS_TYPES = {
80
+ # https://github.com/rails/rails/blob/v5.0.0.rc1/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb
81
+ tinyblob: [:blob, { limit: 255 }],
82
+ mediumblob: [:binary, { limit: 16_777_215 }],
83
+ longblob: [:binary, { limit: 4_294_967_295 }],
84
+ tinytext: [:text, { limit: 255 }],
85
+ mediumtext: [:text, { limit: 16_777_215 }],
86
+ longtext: [:text, { limit: 4_294_967_295 }],
87
+ unsigned_integer: [:integer, { unsigned: true }],
88
+ unsigned_bigint: [:bigint, { unsigned: true }],
89
+ unsigned_float: [:float, { limit: 24, unsigned: true }],
90
+ unsigned_decimal: [:decimal, { precision: 10, unsigned: true }]
91
+ }.freeze
92
+
93
+ # XXX:
94
+ def blob(*args)
106
95
  options = args.extract_options!
107
- options = default_options.merge(options)
96
+ options = { limit: 65_535 }.merge(options)
108
97
  column_names = args
109
- column_names.each {|name| column(name, column_type, options) }
98
+
99
+ column_names.each do |name|
100
+ column_type = (0..0xff).cover?(options[:limit]) ? :blob : :binary
101
+ column(name, column_type, options)
102
+ end
110
103
  end
111
- end
112
104
 
113
- def index(name, options = {})
114
- @base.add_index(@table_name, name, options)
115
- end
105
+ ALIAS_TYPES.each do |alias_type, (column_type, default_options)|
106
+ define_method alias_type do |*args|
107
+ options = args.extract_options!
108
+ options = default_options.merge(options)
109
+ column_names = args
110
+ column_names.each { |name| column(name, column_type, options) }
111
+ end
112
+ end
116
113
 
117
- def timestamps(*args)
118
- options = {:null => false}.merge(args.extract_options!)
119
- column(:created_at, :datetime, options)
120
- column(:updated_at, :datetime, options)
121
- end
114
+ def index(name, options = {})
115
+ @base.add_index(@table_name, name, options)
116
+ end
122
117
 
123
- def references(*args)
124
- options = args.extract_options!
125
- polymorphic = options.delete(:polymorphic)
126
- index_options = options.has_key?(:index) ? options.delete(:index) : true
127
- type = options.delete(:type) || DEFAULT_PRIMARY_KEY_TYPE
128
-
129
- args.each do |col|
130
- column("#{col}_id", type, options)
131
- column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
132
- if index_options
133
- columns = polymorphic ? ["#{col}_type", "#{col}_id"] : ["#{col}_id"]
134
- index(columns, index_options.is_a?(Hash) ? index_options : {})
118
+ def timestamps(*args)
119
+ options = { null: false }.merge(args.extract_options!)
120
+ column(:created_at, :datetime, options)
121
+ column(:updated_at, :datetime, options)
122
+ end
123
+
124
+ def references(*args)
125
+ options = args.extract_options!
126
+ polymorphic = options.delete(:polymorphic)
127
+ index_options = options.key?(:index) ? options.delete(:index) : true
128
+ type = options.delete(:type) || DEFAULT_PRIMARY_KEY_TYPE
129
+
130
+ args.each do |col|
131
+ column("#{col}_id", type, options)
132
+ column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
133
+ if index_options
134
+ columns = polymorphic ? ["#{col}_type", "#{col}_id"] : ["#{col}_id"]
135
+ index(columns, index_options.is_a?(Hash) ? index_options : {})
136
+ end
135
137
  end
136
138
  end
139
+ alias belongs_to references
137
140
  end
138
- alias :belongs_to :references
139
141
  end
140
142
  end