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.
- checksums.yaml +4 -4
- data/.rubocop.yml +38 -0
- data/Appraisals +2 -2
- data/README.md +3 -9
- data/Rakefile +7 -1
- data/bin/ridgepole +97 -108
- data/gemfiles/activerecord_5.2.gemfile +1 -1
- data/lib/ridgepole/cli/config.rb +43 -43
- data/lib/ridgepole/client.rb +65 -63
- data/lib/ridgepole/default_limit.rb +26 -24
- data/lib/ridgepole/delta.rb +367 -369
- data/lib/ridgepole/diff.rb +366 -377
- data/lib/ridgepole/dsl_parser.rb +22 -20
- data/lib/ridgepole/dsl_parser/context.rb +95 -93
- data/lib/ridgepole/dsl_parser/table_definition.rb +125 -123
- data/lib/ridgepole/dumper.rb +73 -71
- data/lib/ridgepole/execute_expander.rb +55 -55
- data/lib/ridgepole/ext/abstract_mysql_adapter/dump_auto_increment.rb +3 -3
- data/lib/ridgepole/ext/abstract_mysql_adapter/use_alter_index.rb +1 -1
- data/lib/ridgepole/ext/pp_sort_hash.rb +14 -14
- data/lib/ridgepole/ext/schema_dumper.rb +1 -1
- data/lib/ridgepole/external_sql_executer.rb +20 -20
- data/lib/ridgepole/logger.rb +17 -15
- data/lib/ridgepole/migration_ext.rb +6 -8
- data/lib/ridgepole/schema_statements_ext.rb +3 -3
- data/lib/ridgepole/version.rb +1 -1
- data/ridgepole.gemspec +11 -10
- data/spec/cli_helper.rb +2 -2
- data/spec/erb_helper.rb +6 -2
- data/spec/hide_pending_formatter.rb +5 -4
- data/spec/mysql/_migrate/migrate_change_table_option_spec.rb +8 -8
- data/spec/mysql/bigint_pk/bigint_pk_spec.rb +8 -8
- data/spec/mysql/bigint_pk/int_pk_spec.rb +13 -13
- data/spec/mysql/cli/config_spec.rb +52 -53
- data/spec/mysql/cli/ridgepole_spec.rb +111 -111
- data/spec/mysql/collation/collation_spec.rb +39 -39
- data/spec/mysql/comment/comment_spec.rb +36 -36
- data/spec/mysql/default_lambda/default_lambda_spec.rb +30 -30
- data/spec/mysql/diff/diff2_spec.rb +18 -18
- data/spec/mysql/diff/diff_spec.rb +11 -11
- data/spec/mysql/dump/dump_class_method_spec.rb +2 -2
- data/spec/mysql/dump/dump_some_tables_spec.rb +14 -14
- data/spec/mysql/dump/dump_spec.rb +2 -2
- data/spec/mysql/dump/dump_unknown_column_type_spec.rb +3 -3
- data/spec/mysql/dump/dump_without_table_options_spec.rb +8 -8
- data/spec/mysql/fk/migrate_change_fk_spec.rb +40 -40
- data/spec/mysql/fk/migrate_create_fk_spec.rb +40 -40
- data/spec/mysql/fk/migrate_drop_fk_spec.rb +60 -60
- data/spec/mysql/migrate/check_orphan_index_spec.rb +6 -6
- data/spec/mysql/migrate/migrate_add_column2_spec.rb +43 -43
- data/spec/mysql/migrate/migrate_add_column_order_spec.rb +50 -50
- data/spec/mysql/migrate/migrate_add_column_spec.rb +11 -11
- data/spec/mysql/migrate/migrate_add_column_with_alter_extra_spec.rb +8 -8
- data/spec/mysql/migrate/migrate_add_column_with_script_spec.rb +11 -11
- data/spec/mysql/migrate/migrate_change_column2_spec.rb +20 -20
- data/spec/mysql/migrate/migrate_change_column3_spec.rb +60 -60
- data/spec/mysql/migrate/migrate_change_column4_spec.rb +16 -16
- data/spec/mysql/migrate/migrate_change_column5_spec.rb +24 -24
- data/spec/mysql/migrate/migrate_change_column6_spec.rb +40 -40
- data/spec/mysql/migrate/migrate_change_column7_spec.rb +4 -4
- data/spec/mysql/migrate/migrate_change_column8_spec.rb +9 -9
- data/spec/mysql/migrate/migrate_change_column_default_spec.rb +48 -48
- data/spec/mysql/migrate/migrate_change_column_float_spec.rb +16 -16
- data/spec/mysql/migrate/migrate_change_column_spec.rb +11 -11
- data/spec/mysql/migrate/migrate_change_index2_spec.rb +20 -20
- data/spec/mysql/migrate/migrate_change_index3_spec.rb +43 -43
- data/spec/mysql/migrate/migrate_change_index4_spec.rb +24 -24
- data/spec/mysql/migrate/migrate_change_index5_spec.rb +16 -16
- data/spec/mysql/migrate/migrate_change_index6_spec.rb +12 -12
- data/spec/mysql/migrate/migrate_change_index7_spec.rb +20 -20
- data/spec/mysql/migrate/migrate_change_index8_spec.rb +20 -20
- data/spec/mysql/migrate/migrate_change_index_spec.rb +17 -17
- data/spec/mysql/migrate/migrate_change_table_comment_spec.rb +10 -10
- data/spec/mysql/migrate/migrate_change_table_option_spec.rb +18 -18
- data/spec/mysql/migrate/migrate_check_relation_column_type_spec.rb +37 -37
- data/spec/mysql/migrate/migrate_create_index2_spec.rb +12 -12
- data/spec/mysql/migrate/migrate_create_index_spec.rb +11 -11
- data/spec/mysql/migrate/migrate_create_table_spec.rb +8 -8
- data/spec/mysql/migrate/migrate_create_table_with_index_spec.rb +6 -6
- data/spec/mysql/migrate/migrate_create_table_with_options_spec.rb +12 -12
- data/spec/mysql/migrate/migrate_create_table_with_script_spec.rb +11 -11
- data/spec/mysql/migrate/migrate_drop_column_and_index2_spec.rb +9 -9
- data/spec/mysql/migrate/migrate_drop_column_and_index_spec.rb +12 -12
- data/spec/mysql/migrate/migrate_drop_column_and_unique_index_spec.rb +10 -10
- data/spec/mysql/migrate/migrate_drop_column_spec.rb +11 -11
- data/spec/mysql/migrate/migrate_drop_index_spec.rb +11 -11
- data/spec/mysql/migrate/migrate_drop_table_spec.rb +8 -8
- data/spec/mysql/migrate/migrate_duplicate_index_spec.rb +6 -6
- data/spec/mysql/migrate/migrate_duplicate_table_spec.rb +6 -6
- data/spec/mysql/migrate/migrate_empty_spec.rb +4 -4
- data/spec/mysql/migrate/migrate_execute_spec.rb +55 -55
- data/spec/mysql/migrate/migrate_log_file_spec.rb +9 -9
- data/spec/mysql/migrate/migrate_merge_mode_spec.rb +9 -9
- data/spec/mysql/migrate/migrate_noop_spec.rb +19 -19
- data/spec/mysql/migrate/migrate_primary_key_spec.rb +18 -18
- data/spec/mysql/migrate/migrate_rename_column_spec.rb +17 -17
- data/spec/mysql/migrate/migrate_rename_table_spec.rb +26 -26
- data/spec/mysql/migrate/migrate_same_default_null_spec.rb +16 -16
- data/spec/mysql/migrate/migrate_same_spec.rb +4 -4
- data/spec/mysql/migrate/migrate_script_error_spec.rb +8 -8
- data/spec/mysql/migrate/migrate_skip_column_comment_change_spec.rb +8 -8
- data/spec/mysql/migrate/migrate_skip_drop_table_spec.rb +9 -9
- data/spec/mysql/migrate/migrate_skip_rename_column_spec.rb +8 -8
- data/spec/mysql/migrate/migrate_skip_rename_table_spec.rb +9 -9
- data/spec/mysql/migrate/migrate_with_ignore_tables_spec.rb +64 -64
- data/spec/mysql/migrate/migrate_with_pre_post_query_spec.rb +12 -12
- data/spec/mysql/migrate/migrate_with_tables_spec.rb +29 -29
- data/spec/mysql/migrate/migrate_with_verbose_log_spec.rb +18 -18
- data/spec/mysql/migrate_/migrate_create_index_with_alter_spec.rb +8 -8
- data/spec/mysql/migrate_/migrate_drop_index_with_alter_spec.rb +8 -8
- data/spec/mysql/text_blob_types/text_blob_types_spec.rb +8 -8
- data/spec/mysql/~default_name_fk/migrate_change_fk_spec.rb +12 -12
- data/spec/mysql/~default_name_fk/migrate_create_fk_spec.rb +30 -30
- data/spec/mysql/~default_name_fk/migrate_drop_fk_spec.rb +22 -22
- data/spec/mysql/~dump_auto_increment/migrate_create_table_with_index_spec.rb +7 -7
- data/spec/mysql57/json/add_json_column_spec.rb +8 -8
- data/spec/mysql57/json/change_json_column_spec.rb +24 -24
- data/spec/mysql57/json/drop_json_column_spec.rb +8 -8
- data/spec/mysql57/virtual/add_virtual_column_spec.rb +9 -9
- data/spec/mysql57/virtual/change_virtual_column_spec.rb +9 -9
- data/spec/mysql57/virtual/drop_virtual_column_spec.rb +9 -9
- data/spec/postgresql/diff/diff_spec.rb +18 -18
- data/spec/postgresql/dump/dump_spec.rb +2 -2
- data/spec/postgresql/fk/migrate_change_fk_spec.rb +24 -24
- data/spec/postgresql/fk/migrate_create_fk_spec.rb +40 -40
- data/spec/postgresql/fk/migrate_drop_fk_spec.rb +44 -44
- data/spec/postgresql/migrate/migrate_add_column_spec.rb +21 -21
- data/spec/postgresql/migrate/migrate_add_expression_index_spec.rb +2 -2
- data/spec/postgresql/migrate/migrate_bigint_spec.rb +12 -12
- data/spec/postgresql/migrate/migrate_change_column_default_spec.rb +8 -8
- data/spec/postgresql/migrate/migrate_change_column_spec.rb +19 -19
- data/spec/postgresql/migrate/migrate_change_index_spec.rb +13 -13
- data/spec/postgresql/migrate/migrate_check_relation_column_type_spec.rb +19 -19
- data/spec/postgresql/migrate/migrate_create_table_spec.rb +8 -8
- data/spec/postgresql/migrate/migrate_create_table_with_default_proc_spec.rb +18 -18
- data/spec/postgresql/migrate/migrate_drop_column_spec.rb +11 -11
- data/spec/postgresql/migrate/migrate_drop_column_with_index_spec.rb +12 -12
- data/spec/postgresql/migrate/migrate_drop_expression_index_spec.rb +8 -8
- data/spec/postgresql/migrate/migrate_drop_index_spec.rb +11 -11
- data/spec/postgresql/migrate/migrate_drop_table_spec.rb +8 -8
- data/spec/postgresql/migrate/migrate_ext_cols_spec.rb +8 -8
- data/spec/postgresql/migrate/migrate_primary_key_spec.rb +97 -0
- data/spec/postgresql/migrate/migrate_references_spec.rb +24 -24
- data/spec/postgresql/migrate/migrate_rename_column_spec.rb +11 -11
- data/spec/postgresql/migrate/migrate_rename_table_spec.rb +16 -16
- data/spec/postgresql/migrate/migrate_same_spec.rb +4 -4
- data/spec/postgresql/~default_name_fk/migrate_change_fk_spec.rb +12 -12
- data/spec/postgresql/~default_name_fk/migrate_create_fk_spec.rb +30 -30
- data/spec/postgresql/~default_name_fk/migrate_drop_fk_spec.rb +22 -22
- data/spec/processing_for_travis.rb +1 -1
- data/spec/spec_condition.rb +2 -2
- data/spec/spec_const.rb +9 -9
- data/spec/spec_helper.rb +21 -23
- metadata +54 -37
data/lib/ridgepole/dsl_parser.rb
CHANGED
@@ -1,29 +1,31 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module Ridgepole
|
2
|
+
class DSLParser
|
3
|
+
def initialize(options = {})
|
4
|
+
@options = options
|
5
|
+
end
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
14
|
+
private
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
2
|
-
class
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
13
|
-
|
16
|
+
if opts[:path]
|
17
|
+
ctx.instance_eval(dsl, opts[:path])
|
18
|
+
else
|
19
|
+
ctx.instance_eval(dsl)
|
20
|
+
end
|
14
21
|
|
15
|
-
|
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
|
-
|
22
|
-
|
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
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
29
|
-
|
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
|
-
|
38
|
-
|
41
|
+
if @__definition[table_name][:definition]
|
42
|
+
raise "Table `#{table_name}` already defined"
|
43
|
+
end
|
39
44
|
|
40
|
-
|
41
|
-
|
45
|
+
@__definition[table_name][:definition] = table_definition.__definition
|
46
|
+
options.delete(:force)
|
47
|
+
@__definition[table_name][:options] = options
|
42
48
|
end
|
43
49
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
50
|
-
|
51
|
-
|
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
|
-
|
62
|
-
|
63
|
-
|
66
|
+
if options[:length].is_a?(Numeric)
|
67
|
+
index_length = options[:length]
|
68
|
+
options[:length] = {}
|
64
69
|
|
65
|
-
|
66
|
-
|
67
|
-
|
70
|
+
column_name.each do |col|
|
71
|
+
options[:length][col] = index_length
|
72
|
+
end
|
73
|
+
end
|
68
74
|
|
69
|
-
|
70
|
-
options[:length]
|
75
|
+
if options[:length]
|
76
|
+
options[:length] = options[:length].compact.symbolize_keys
|
71
77
|
end
|
72
|
-
end
|
73
78
|
|
74
|
-
|
75
|
-
|
79
|
+
@__definition[table_name][:indices][idx] = {
|
80
|
+
column_name: column_name,
|
81
|
+
options: options
|
82
|
+
}
|
76
83
|
end
|
77
84
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
-
|
85
|
-
|
86
|
-
|
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
|
-
|
93
|
-
|
97
|
+
@__definition[from_table][:foreign_keys][idx] = {
|
98
|
+
to_table: to_table,
|
99
|
+
options: options
|
100
|
+
}
|
94
101
|
end
|
95
102
|
|
96
|
-
|
97
|
-
:
|
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
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
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
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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
|
-
|
2
|
-
class
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
12
|
-
|
12
|
+
def column(name, type, options = {})
|
13
|
+
name = name.to_s
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
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
|
-
|
105
|
-
|
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 =
|
96
|
+
options = { limit: 65_535 }.merge(options)
|
108
97
|
column_names = args
|
109
|
-
|
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
|
-
|
114
|
-
|
115
|
-
|
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
|
-
|
118
|
-
|
119
|
-
|
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
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
args
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
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
|