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/cli/config.rb
CHANGED
@@ -1,54 +1,54 @@
|
|
1
1
|
require 'erb'
|
2
2
|
require 'yaml'
|
3
3
|
|
4
|
-
|
5
|
-
class
|
6
|
-
|
7
|
-
|
8
|
-
config = ENV.fetch(
|
4
|
+
module Ridgepole
|
5
|
+
class Config
|
6
|
+
class << self
|
7
|
+
def load(config, env = 'development')
|
8
|
+
config = ENV.fetch(Regexp.last_match(1)) if config =~ /\Aenv:(.+)\z/
|
9
|
+
|
10
|
+
parsed_config = if File.exist?(config)
|
11
|
+
parse_config_file(config)
|
12
|
+
elsif (expanded = File.expand_path(config)) && File.exist?(expanded)
|
13
|
+
parse_config_file(expanded)
|
14
|
+
else
|
15
|
+
YAML.safe_load(ERB.new(config).result, [], [], true)
|
16
|
+
end
|
17
|
+
|
18
|
+
unless parsed_config.is_a?(Hash)
|
19
|
+
parsed_config = parse_database_url(config)
|
20
|
+
end
|
21
|
+
|
22
|
+
if parsed_config.key?(env.to_s)
|
23
|
+
parsed_config.fetch(env.to_s)
|
24
|
+
else
|
25
|
+
parsed_config
|
26
|
+
end
|
9
27
|
end
|
10
28
|
|
11
|
-
|
12
|
-
parsed_config = parse_config_file(config)
|
13
|
-
elsif (expanded = File.expand_path(config)) and File.exist?(expanded)
|
14
|
-
parsed_config = parse_config_file(expanded)
|
15
|
-
else
|
16
|
-
parsed_config = YAML.load(ERB.new(config).result)
|
17
|
-
end
|
29
|
+
private
|
18
30
|
|
19
|
-
|
20
|
-
|
31
|
+
def parse_config_file(path)
|
32
|
+
yaml = ERB.new(File.read(path)).result
|
33
|
+
YAML.safe_load(yaml, [], [], true)
|
21
34
|
end
|
22
35
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
36
|
+
def parse_database_url(config)
|
37
|
+
uri = URI.parse(config)
|
38
|
+
|
39
|
+
if [uri.scheme, uri.user, uri.host, uri.path].any? { |i| i.nil? || i.empty? }
|
40
|
+
raise "Invalid config: #{config.inspect}"
|
41
|
+
end
|
42
|
+
|
43
|
+
{
|
44
|
+
'adapter' => uri.scheme,
|
45
|
+
'username' => uri.user,
|
46
|
+
'password' => uri.password,
|
47
|
+
'host' => uri.host,
|
48
|
+
'port' => uri.port,
|
49
|
+
'database' => uri.path.sub(%r{\A/}, '')
|
50
|
+
}
|
27
51
|
end
|
28
52
|
end
|
29
|
-
|
30
|
-
private
|
31
|
-
|
32
|
-
def parse_config_file(path)
|
33
|
-
yaml = ERB.new(File.read(path)).result
|
34
|
-
YAML.load(yaml)
|
35
|
-
end
|
36
|
-
|
37
|
-
def parse_database_url(config)
|
38
|
-
uri = URI.parse(config)
|
39
|
-
|
40
|
-
if [uri.scheme, uri.user, uri.host, uri.path].any? {|i| i.nil? or i.empty? }
|
41
|
-
raise "Invalid config: #{config.inspect}"
|
42
|
-
end
|
43
|
-
|
44
|
-
{
|
45
|
-
'adapter' => uri.scheme,
|
46
|
-
'username' => uri.user,
|
47
|
-
'password' => uri.password,
|
48
|
-
'host' => uri.host,
|
49
|
-
'port' => uri.port,
|
50
|
-
'database' => uri.path.sub(%r|\A/|, ''),
|
51
|
-
}
|
52
|
-
end
|
53
|
-
end # of class methods
|
53
|
+
end
|
54
54
|
end
|
data/lib/ridgepole/client.rb
CHANGED
@@ -1,84 +1,86 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module Ridgepole
|
2
|
+
class Client
|
3
|
+
def initialize(conn_spec, options = {})
|
4
|
+
@options = options
|
4
5
|
|
5
|
-
|
6
|
+
ActiveRecord::Base.establish_connection(conn_spec)
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
# XXX: If the required processing in class method?
|
9
|
+
if !@options.key?(:index_removed_drop_column) && (Ridgepole::DefaultsLimit.adapter == :postgresql)
|
10
|
+
@options[:index_removed_drop_column] = true
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
Ridgepole::ExecuteExpander.expand_execute(ActiveRecord::Base.connection)
|
14
|
+
@dumper = Ridgepole::Dumper.new(@options)
|
15
|
+
@parser = Ridgepole::DSLParser.new(@options)
|
16
|
+
@diff = Ridgepole::Diff.new(@options)
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
if @options[:mysql_use_alter]
|
19
|
+
require 'ridgepole/ext/abstract_mysql_adapter/use_alter_index'
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
22
|
+
if @options[:mysql_dump_auto_increment]
|
23
|
+
require 'ridgepole/ext/abstract_mysql_adapter/dump_auto_increment'
|
24
|
+
end
|
23
25
|
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def dump(&block)
|
27
|
-
logger = Ridgepole::Logger.instance
|
28
|
-
logger.verbose_info('# Load tables')
|
29
|
-
@dumper.dump(&block)
|
30
|
-
end
|
31
26
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
expected_definition, expected_execute = @parser.parse(dsl, opts)
|
37
|
-
expected_definition.each do |table, definition|
|
38
|
-
definition[:options][:options] ||= @options[:table_options] if @options[:table_options]
|
27
|
+
def dump(&block)
|
28
|
+
logger = Ridgepole::Logger.instance
|
29
|
+
logger.verbose_info('# Load tables')
|
30
|
+
@dumper.dump(&block)
|
39
31
|
end
|
40
|
-
logger.verbose_info('# Load tables')
|
41
|
-
current_definition, _current_execute = @parser.parse(@dumper.dump, opts)
|
42
|
-
logger.verbose_info('# Compare definitions')
|
43
|
-
@diff.diff(current_definition, expected_definition, :execute => expected_execute)
|
44
|
-
end
|
45
32
|
|
46
|
-
|
47
|
-
def diff(dsl_or_config1, dsl_or_config2, options = {})
|
33
|
+
def diff(dsl, opts = {})
|
48
34
|
logger = Ridgepole::Logger.instance
|
49
35
|
|
50
|
-
logger.verbose_info('# Parse
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
36
|
+
logger.verbose_info('# Parse DSL')
|
37
|
+
expected_definition, expected_execute = @parser.parse(dsl, opts)
|
38
|
+
expected_definition.each do |_table, definition|
|
39
|
+
definition[:options][:options] ||= @options[:table_options] if @options[:table_options]
|
40
|
+
end
|
41
|
+
logger.verbose_info('# Load tables')
|
42
|
+
current_definition, _current_execute = @parser.parse(@dumper.dump, opts)
|
55
43
|
logger.verbose_info('# Compare definitions')
|
56
|
-
diff
|
57
|
-
diff.diff(definition1, definition2)
|
44
|
+
@diff.diff(current_definition, expected_definition, execute: expected_execute)
|
58
45
|
end
|
59
46
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
end
|
47
|
+
class << self
|
48
|
+
def diff(dsl_or_config1, dsl_or_config2, options = {})
|
49
|
+
logger = Ridgepole::Logger.instance
|
64
50
|
|
65
|
-
|
51
|
+
logger.verbose_info('# Parse DSL1')
|
52
|
+
definition1, _execute1 = load_definition(dsl_or_config1, options)
|
53
|
+
logger.verbose_info('# Parse DSL2')
|
54
|
+
definition2, _execute2 = load_definition(dsl_or_config2, options)
|
66
55
|
|
67
|
-
|
68
|
-
|
56
|
+
logger.verbose_info('# Compare definitions')
|
57
|
+
diff = Ridgepole::Diff.new(options)
|
58
|
+
diff.diff(definition1, definition2)
|
59
|
+
end
|
69
60
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
when File
|
74
|
-
file = dsl_or_config
|
75
|
-
parse_opts[:path] = file.path
|
76
|
-
dsl_or_config = file.read
|
77
|
-
file.close
|
61
|
+
def dump(conn_spec, options = {}, &block)
|
62
|
+
client = new(conn_spec, options)
|
63
|
+
client.dump(&block)
|
78
64
|
end
|
79
65
|
|
80
|
-
|
81
|
-
|
66
|
+
private
|
67
|
+
|
68
|
+
def load_definition(dsl_or_config, options = {})
|
69
|
+
parse_opts = {}
|
70
|
+
|
71
|
+
case dsl_or_config
|
72
|
+
when Hash
|
73
|
+
dsl_or_config = dump(dsl_or_config, options)
|
74
|
+
when File
|
75
|
+
file = dsl_or_config
|
76
|
+
parse_opts[:path] = file.path
|
77
|
+
dsl_or_config = file.read
|
78
|
+
file.close
|
79
|
+
end
|
80
|
+
|
81
|
+
parser = Ridgepole::DSLParser.new(options)
|
82
|
+
parser.parse(dsl_or_config, parse_opts)
|
83
|
+
end
|
82
84
|
end
|
83
|
-
end
|
85
|
+
end
|
84
86
|
end
|
@@ -1,28 +1,30 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
:
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
1
|
+
module Ridgepole
|
2
|
+
class DefaultsLimit
|
3
|
+
DEFAULTS_LIMITS = {
|
4
|
+
mysql2: {
|
5
|
+
boolean: 1,
|
6
|
+
integer: 4,
|
7
|
+
bigint: 8,
|
8
|
+
float: 24,
|
9
|
+
string: 255,
|
10
|
+
text: 65_535,
|
11
|
+
binary: 65_535
|
12
|
+
}
|
13
|
+
}.freeze
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
class << self
|
16
|
+
def default_limit(column_type, options)
|
17
|
+
defaults = DEFAULTS_LIMITS[adapter] || {}
|
18
|
+
option_key = :"default_#{column_type}_limit"
|
19
|
+
default_limit = options[option_key] || defaults[column_type] || 0
|
20
|
+
default_limit.zero? ? nil : default_limit
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
def adapter
|
24
|
+
ActiveRecord::Base.connection_config.fetch(:adapter).to_sym
|
25
|
+
rescue ActiveRecord::ConnectionNotEstablished
|
26
|
+
nil
|
27
|
+
end
|
26
28
|
end
|
27
|
-
end
|
29
|
+
end
|
28
30
|
end
|
data/lib/ridgepole/delta.rb
CHANGED
@@ -1,515 +1,513 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Ridgepole
|
2
|
+
class Delta
|
3
|
+
SCRIPT_NAME = '<Schema>'.freeze
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
def initialize(delta, options = {})
|
6
|
+
@delta = delta
|
7
|
+
@options = options
|
8
|
+
@logger = Ridgepole::Logger.instance
|
9
|
+
end
|
10
|
+
|
11
|
+
def migrate(options = {})
|
12
|
+
log_file = @options[:log_file]
|
13
|
+
|
14
|
+
if log_file
|
15
|
+
result = ActiveRecord::Migration.record_time do
|
16
|
+
migrate0(options)
|
17
|
+
end
|
9
18
|
|
10
|
-
|
11
|
-
|
12
|
-
|
19
|
+
File.open(log_file, 'wb') { |f| f.puts JSON.pretty_generate(result) }
|
20
|
+
result
|
21
|
+
else
|
13
22
|
migrate0(options)
|
14
23
|
end
|
15
|
-
|
16
|
-
open(log_file, 'wb') {|f| f.puts JSON.pretty_generate(result) }
|
17
|
-
result
|
18
|
-
else
|
19
|
-
migrate0(options)
|
20
24
|
end
|
21
|
-
end
|
22
25
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
26
|
+
def script
|
27
|
+
buf = StringIO.new
|
28
|
+
pre_buf_for_fk = StringIO.new
|
29
|
+
post_buf_for_fk = StringIO.new
|
27
30
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
+
(@delta[:add] || {}).each do |table_name, attrs|
|
32
|
+
append_create_table(table_name, attrs, buf, post_buf_for_fk)
|
33
|
+
end
|
31
34
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
+
(@delta[:rename] || {}).each do |table_name, attrs|
|
36
|
+
append_rename_table(table_name, attrs, buf)
|
37
|
+
end
|
35
38
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
+
(@delta[:change] || {}).each do |table_name, attrs|
|
40
|
+
append_change(table_name, attrs, buf, pre_buf_for_fk, post_buf_for_fk)
|
41
|
+
end
|
39
42
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
+
(@delta[:delete] || {}).each do |table_name, attrs|
|
44
|
+
append_drop_table(table_name, attrs, buf)
|
45
|
+
end
|
43
46
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
[
|
48
|
+
pre_buf_for_fk,
|
49
|
+
buf,
|
50
|
+
post_buf_for_fk
|
51
|
+
].map { |b| b.string.strip }.join("\n\n").strip
|
52
|
+
end
|
50
53
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
+
def differ?
|
55
|
+
!script.empty? || !delta_execute.empty?
|
56
|
+
end
|
54
57
|
|
55
|
-
|
58
|
+
private
|
56
59
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
+
def migrate0(options = {})
|
61
|
+
migrated = false
|
62
|
+
out = nil
|
60
63
|
|
61
|
-
|
62
|
-
|
64
|
+
if options[:noop]
|
65
|
+
disable_logging_orig = ActiveRecord::Migration.disable_logging
|
63
66
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
+
begin
|
68
|
+
ActiveRecord::Migration.disable_logging = true
|
69
|
+
buf = StringIO.new
|
67
70
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
+
callback = proc do |sql, _name|
|
72
|
+
buf.puts sql if sql =~ /\A(CREATE|ALTER|DROP|RENAME)\b/i
|
73
|
+
end
|
71
74
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
+
eval_script_block = proc do
|
76
|
+
Ridgepole::ExecuteExpander.without_operation(callback) do
|
77
|
+
migrated = eval_script(script, options.merge(out: buf))
|
78
|
+
end
|
75
79
|
end
|
76
|
-
end
|
77
80
|
|
78
|
-
|
79
|
-
|
81
|
+
if options[:alter_extra]
|
82
|
+
Ridgepole::ExecuteExpander.with_alter_extra(options[:alter_extra]) do
|
83
|
+
eval_script_block.call
|
84
|
+
end
|
85
|
+
else
|
80
86
|
eval_script_block.call
|
81
87
|
end
|
82
|
-
else
|
83
|
-
eval_script_block.call
|
84
|
-
end
|
85
88
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
89
|
+
out = buf.string.strip
|
90
|
+
ensure
|
91
|
+
ActiveRecord::Migration.disable_logging = disable_logging_orig
|
92
|
+
end
|
93
|
+
elsif options[:external_script]
|
94
|
+
Ridgepole::ExecuteExpander.with_script(options[:external_script], Ridgepole::Logger.instance) do
|
95
|
+
migrated = eval_script(script, options)
|
96
|
+
end
|
97
|
+
elsif options[:alter_extra]
|
98
|
+
Ridgepole::ExecuteExpander.with_alter_extra(options[:alter_extra]) do
|
99
|
+
migrated = eval_script(script, options)
|
100
|
+
end
|
101
|
+
else
|
96
102
|
migrated = eval_script(script, options)
|
97
103
|
end
|
98
|
-
|
99
|
-
migrated
|
104
|
+
|
105
|
+
[migrated, out]
|
100
106
|
end
|
101
107
|
|
102
|
-
|
103
|
-
|
108
|
+
def eval_script(script, options = {})
|
109
|
+
execute_count = 0
|
104
110
|
|
105
|
-
|
106
|
-
|
111
|
+
begin
|
112
|
+
with_pre_post_query(options) do
|
113
|
+
unless script.empty?
|
114
|
+
ActiveRecord::Schema.new.instance_eval(script, SCRIPT_NAME, 1)
|
115
|
+
end
|
107
116
|
|
108
|
-
|
109
|
-
with_pre_post_query(options) do
|
110
|
-
unless script.empty?
|
111
|
-
ActiveRecord::Schema.new.instance_eval(script, SCRIPT_NAME, 1)
|
117
|
+
execute_count = execute_sqls(options)
|
112
118
|
end
|
113
|
-
|
114
|
-
|
119
|
+
rescue StandardError => e
|
120
|
+
raise_exception(script, e)
|
115
121
|
end
|
116
|
-
|
117
|
-
|
122
|
+
|
123
|
+
!script.empty? || execute_count.nonzero?
|
118
124
|
end
|
119
125
|
|
120
|
-
|
121
|
-
|
126
|
+
def execute_sqls(options = {})
|
127
|
+
es = @delta[:execute] || []
|
128
|
+
out = options[:out] || $stdout
|
129
|
+
execute_count = 0
|
122
130
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
execute_count = 0
|
131
|
+
es.each do |exec|
|
132
|
+
sql, cond = exec.values_at(:sql, :condition)
|
133
|
+
executable = false
|
127
134
|
|
128
|
-
|
129
|
-
|
130
|
-
|
135
|
+
begin
|
136
|
+
executable = cond.nil? || cond.call(ActiveRecord::Base.connection)
|
137
|
+
rescue StandardError => e
|
138
|
+
errmsg = "[WARN] `#{sql}` is not executed: #{e.message}"
|
131
139
|
|
132
|
-
|
133
|
-
executable = cond.nil? || cond.call(ActiveRecord::Base.connection)
|
134
|
-
rescue => e
|
135
|
-
errmsg = "[WARN] `#{sql}` is not executed: #{e.message}"
|
140
|
+
errmsg = ([errmsg] + e.backtrace).join("\n\tfrom ") if @options[:debug]
|
136
141
|
|
137
|
-
|
138
|
-
|
142
|
+
Ridgepole::Logger.instance.warn(errmsg)
|
143
|
+
|
144
|
+
executable = false
|
139
145
|
end
|
140
146
|
|
141
|
-
|
147
|
+
next unless executable
|
142
148
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
149
|
+
if options[:noop]
|
150
|
+
out.puts(sql.strip_heredoc)
|
151
|
+
else
|
152
|
+
@logger.info(sql.strip_heredoc)
|
153
|
+
ActiveRecord::Base.connection.execute(sql)
|
154
|
+
end
|
147
155
|
|
148
|
-
|
149
|
-
out.puts(sql.strip_heredoc)
|
150
|
-
else
|
151
|
-
@logger.info(sql.strip_heredoc)
|
152
|
-
ActiveRecord::Base.connection.execute(sql)
|
156
|
+
execute_count += 1
|
153
157
|
end
|
154
158
|
|
155
|
-
execute_count
|
159
|
+
execute_count
|
156
160
|
end
|
157
161
|
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
def with_pre_post_query(options = {})
|
162
|
-
out = options[:out] || $stdout
|
162
|
+
def with_pre_post_query(options = {})
|
163
|
+
out = options[:out] || $stdout
|
163
164
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
165
|
+
if (pre_query = @options[:pre_query])
|
166
|
+
if options[:noop]
|
167
|
+
out.puts(pre_query)
|
168
|
+
else
|
169
|
+
ActiveRecord::Base.connection.execute(pre_query)
|
170
|
+
end
|
169
171
|
end
|
170
|
-
end
|
171
172
|
|
172
|
-
|
173
|
+
retval = yield
|
173
174
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
175
|
+
if (post_query = @options[:post_query])
|
176
|
+
if options[:noop]
|
177
|
+
out.puts(post_query)
|
178
|
+
else
|
179
|
+
ActiveRecord::Base.connection.execute(post_query)
|
180
|
+
end
|
179
181
|
end
|
182
|
+
|
183
|
+
retval
|
180
184
|
end
|
181
185
|
|
182
|
-
|
183
|
-
|
186
|
+
def raise_exception(script, org)
|
187
|
+
lines = script.each_line
|
188
|
+
digit_number = (lines.count + 1).to_s.length
|
189
|
+
err_num = detect_error_line(org)
|
184
190
|
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
191
|
+
errmsg = lines.with_index.map do |l, i|
|
192
|
+
line_num = i + 1
|
193
|
+
prefix = line_num == err_num ? '* ' : ' '
|
194
|
+
format("#{prefix}%*d: #{l}", digit_number, line_num)
|
195
|
+
end
|
189
196
|
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
197
|
+
if err_num > 0
|
198
|
+
from = err_num - 6
|
199
|
+
from = 0 if from < 0
|
200
|
+
to = err_num + 4
|
201
|
+
errmsg = errmsg.slice(from..to)
|
202
|
+
end
|
195
203
|
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
to = err_num + 4
|
200
|
-
errmsg = errmsg.slice(from..to)
|
204
|
+
e = RuntimeError.new(org.message + "\n" + errmsg.join)
|
205
|
+
e.set_backtrace(org.backtrace)
|
206
|
+
raise e
|
201
207
|
end
|
202
208
|
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
end
|
209
|
+
def detect_error_line(exception)
|
210
|
+
rgx = /\A#{Regexp.escape(SCRIPT_NAME)}:(\d+):/
|
211
|
+
line = exception.backtrace.find { |i| i =~ rgx }
|
207
212
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
m[1].to_i
|
214
|
-
else
|
215
|
-
0
|
213
|
+
if line && (m = rgx.match(line))
|
214
|
+
m[1].to_i
|
215
|
+
else
|
216
|
+
0
|
217
|
+
end
|
216
218
|
end
|
217
|
-
end
|
218
219
|
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
220
|
+
def append_create_table(table_name, attrs, buf, post_buf_for_fk)
|
221
|
+
options = attrs[:options] || {}
|
222
|
+
definition = attrs[:definition] || {}
|
223
|
+
indices = attrs[:indices] || {}
|
223
224
|
|
224
|
-
|
225
|
+
buf.puts(<<-RUBY)
|
225
226
|
create_table(#{table_name.inspect}, #{inspect_options_include_default_proc(options)}) do |t|
|
226
|
-
|
227
|
+
RUBY
|
227
228
|
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
229
|
+
definition.each do |column_name, column_attrs|
|
230
|
+
column_type = column_attrs.fetch(:type)
|
231
|
+
column_options = column_attrs[:options] || {}
|
232
|
+
normalize_limit(column_type, column_options)
|
232
233
|
|
233
|
-
|
234
|
+
buf.puts(<<-RUBY)
|
234
235
|
t.column(#{column_name.inspect}, :#{column_type.to_s.inspect}, #{inspect_options_include_default_proc(column_options)})
|
235
|
-
|
236
|
-
|
236
|
+
RUBY
|
237
|
+
end
|
237
238
|
|
238
|
-
|
239
|
-
|
240
|
-
|
239
|
+
if @options[:create_table_with_index] && !indices.empty?
|
240
|
+
indices.each do |index_name, index_attrs|
|
241
|
+
append_add_index(table_name, index_name, index_attrs, buf, true)
|
242
|
+
end
|
241
243
|
end
|
242
|
-
end
|
243
244
|
|
244
|
-
|
245
|
-
end
|
246
|
-
|
245
|
+
buf.puts(<<-RUBY)
|
246
|
+
end
|
247
|
+
RUBY
|
247
248
|
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
249
|
+
if !(@options[:create_table_with_index]) && !indices.empty?
|
250
|
+
append_change_table(table_name, buf) do
|
251
|
+
indices.each do |index_name, index_attrs|
|
252
|
+
append_add_index(table_name, index_name, index_attrs, buf)
|
253
|
+
end
|
252
254
|
end
|
253
255
|
end
|
254
|
-
end
|
255
256
|
|
256
|
-
|
257
|
-
|
258
|
-
|
257
|
+
unless (foreign_keys = attrs[:foreign_keys] || {}).empty?
|
258
|
+
foreign_keys.each do |_, foreign_key_attrs|
|
259
|
+
append_add_foreign_key(table_name, foreign_key_attrs, post_buf_for_fk, @options)
|
260
|
+
end
|
259
261
|
end
|
260
|
-
end
|
261
262
|
|
262
|
-
|
263
|
-
|
264
|
-
|
263
|
+
buf.puts
|
264
|
+
post_buf_for_fk.puts
|
265
|
+
end
|
265
266
|
|
266
|
-
|
267
|
-
|
267
|
+
def append_rename_table(to_table_name, from_table_name, buf)
|
268
|
+
buf.puts(<<-RUBY)
|
268
269
|
rename_table(#{from_table_name.inspect}, #{to_table_name.inspect})
|
269
|
-
|
270
|
+
RUBY
|
270
271
|
|
271
|
-
|
272
|
-
|
272
|
+
buf.puts
|
273
|
+
end
|
273
274
|
|
274
|
-
|
275
|
-
|
275
|
+
def append_drop_table(table_name, _attrs, buf)
|
276
|
+
buf.puts(<<-RUBY)
|
276
277
|
drop_table(#{table_name.inspect})
|
277
|
-
|
278
|
+
RUBY
|
278
279
|
|
279
|
-
|
280
|
-
|
280
|
+
buf.puts
|
281
|
+
end
|
281
282
|
|
282
|
-
|
283
|
-
|
284
|
-
|
283
|
+
def append_change_table_options(table_name, table_options, buf)
|
284
|
+
# XXX: MySQL only
|
285
|
+
buf.puts(<<-RUBY)
|
285
286
|
execute "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name)} #{table_options}"
|
286
|
-
|
287
|
-
|
288
|
-
buf.puts
|
289
|
-
end
|
287
|
+
RUBY
|
290
288
|
|
291
|
-
|
292
|
-
comment_literal = "COMMENT=#{ActiveRecord::Base.connection.quote(table_comment)}"
|
293
|
-
append_change_table_options(table_name, comment_literal, buf)
|
294
|
-
end
|
295
|
-
|
296
|
-
def append_change(table_name, attrs, buf, pre_buf_for_fk, post_buf_for_fk)
|
297
|
-
definition = attrs[:definition] || {}
|
298
|
-
primary_key_definition = attrs[:primary_key_definition] || {}
|
299
|
-
indices = attrs[:indices] || {}
|
300
|
-
foreign_keys = attrs[:foreign_keys] || {}
|
301
|
-
table_options = attrs[:table_options]
|
302
|
-
table_comment = attrs[:table_comment]
|
303
|
-
|
304
|
-
if not definition.empty? or not indices.empty? or not primary_key_definition.empty?
|
305
|
-
append_change_table(table_name, buf) do
|
306
|
-
append_delete_indices(table_name, indices, buf)
|
307
|
-
append_change_definition(table_name, definition, buf)
|
308
|
-
append_change_definition(table_name, primary_key_definition, buf)
|
309
|
-
append_add_indices(table_name, indices, buf)
|
310
|
-
end
|
289
|
+
buf.puts
|
311
290
|
end
|
312
291
|
|
313
|
-
|
314
|
-
|
292
|
+
def append_change_table_comment(table_name, table_comment, buf)
|
293
|
+
comment_literal = "COMMENT=#{ActiveRecord::Base.connection.quote(table_comment)}"
|
294
|
+
append_change_table_options(table_name, comment_literal, buf)
|
315
295
|
end
|
316
296
|
|
317
|
-
|
318
|
-
|
319
|
-
|
297
|
+
def append_change(table_name, attrs, buf, pre_buf_for_fk, post_buf_for_fk)
|
298
|
+
definition = attrs[:definition] || {}
|
299
|
+
primary_key_definition = attrs[:primary_key_definition] || {}
|
300
|
+
indices = attrs[:indices] || {}
|
301
|
+
foreign_keys = attrs[:foreign_keys] || {}
|
302
|
+
table_options = attrs[:table_options]
|
303
|
+
table_comment = attrs[:table_comment]
|
320
304
|
|
321
|
-
|
322
|
-
|
323
|
-
|
305
|
+
if !definition.empty? || !indices.empty? || !primary_key_definition.empty?
|
306
|
+
append_change_table(table_name, buf) do
|
307
|
+
append_delete_indices(table_name, indices, buf)
|
308
|
+
append_change_definition(table_name, definition, buf)
|
309
|
+
append_change_definition(table_name, primary_key_definition, buf)
|
310
|
+
append_add_indices(table_name, indices, buf)
|
311
|
+
end
|
312
|
+
end
|
324
313
|
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
end
|
314
|
+
unless foreign_keys.empty?
|
315
|
+
append_change_foreign_keys(table_name, foreign_keys, pre_buf_for_fk, post_buf_for_fk, @options)
|
316
|
+
end
|
329
317
|
|
330
|
-
|
331
|
-
buf.puts "change_table(#{table_name.inspect}, {:bulk => true}) do |t|" if @options[:bulk_change]
|
332
|
-
yield
|
333
|
-
buf.puts 'end' if @options[:bulk_change]
|
334
|
-
end
|
318
|
+
append_change_table_options(table_name, table_options, buf) if table_options
|
335
319
|
|
336
|
-
|
337
|
-
(delta[:add] || {}).each do |column_name, attrs|
|
338
|
-
append_add_column(table_name, column_name, attrs, buf)
|
339
|
-
end
|
320
|
+
append_change_table_comment(table_name, table_comment, buf) if table_comment
|
340
321
|
|
341
|
-
|
342
|
-
|
322
|
+
buf.puts
|
323
|
+
pre_buf_for_fk.puts
|
324
|
+
post_buf_for_fk.puts
|
343
325
|
end
|
344
326
|
|
345
|
-
(
|
346
|
-
|
327
|
+
def append_change_table(table_name, buf)
|
328
|
+
buf.puts "change_table(#{table_name.inspect}, {:bulk => true}) do |t|" if @options[:bulk_change]
|
329
|
+
yield
|
330
|
+
buf.puts 'end' if @options[:bulk_change]
|
347
331
|
end
|
348
332
|
|
349
|
-
(delta
|
350
|
-
|
333
|
+
def append_change_definition(table_name, delta, buf)
|
334
|
+
(delta[:add] || {}).each do |column_name, attrs|
|
335
|
+
append_add_column(table_name, column_name, attrs, buf)
|
336
|
+
end
|
337
|
+
|
338
|
+
(delta[:rename] || {}).each do |column_name, attrs|
|
339
|
+
append_rename_column(table_name, column_name, attrs, buf)
|
340
|
+
end
|
341
|
+
|
342
|
+
(delta[:change] || {}).each do |column_name, attrs|
|
343
|
+
append_change_column(table_name, column_name, attrs, buf)
|
344
|
+
end
|
345
|
+
|
346
|
+
(delta[:delete] || {}).each do |column_name, attrs|
|
347
|
+
append_remove_column(table_name, column_name, attrs, buf)
|
348
|
+
end
|
351
349
|
end
|
352
|
-
end
|
353
350
|
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
351
|
+
def append_add_column(table_name, column_name, attrs, buf)
|
352
|
+
type = attrs.fetch(:type)
|
353
|
+
options = attrs[:options] || {}
|
354
|
+
normalize_limit(type, options)
|
358
355
|
|
359
|
-
|
360
|
-
|
356
|
+
if @options[:bulk_change]
|
357
|
+
buf.puts(<<-RUBY)
|
361
358
|
t.column(#{column_name.inspect}, #{type.inspect}, #{inspect_options_include_default_proc(options)})
|
362
|
-
|
363
|
-
|
364
|
-
|
359
|
+
RUBY
|
360
|
+
else
|
361
|
+
buf.puts(<<-RUBY)
|
365
362
|
add_column(#{table_name.inspect}, #{column_name.inspect}, #{type.inspect}, #{inspect_options_include_default_proc(options)})
|
366
|
-
|
363
|
+
RUBY
|
364
|
+
end
|
367
365
|
end
|
368
|
-
end
|
369
366
|
|
370
|
-
|
371
|
-
|
372
|
-
|
367
|
+
def append_rename_column(table_name, to_column_name, from_column_name, buf)
|
368
|
+
if @options[:bulk_change]
|
369
|
+
buf.puts(<<-RUBY)
|
373
370
|
t.rename(#{from_column_name.inspect}, #{to_column_name.inspect})
|
374
|
-
|
375
|
-
|
376
|
-
|
371
|
+
RUBY
|
372
|
+
else
|
373
|
+
buf.puts(<<-RUBY)
|
377
374
|
rename_column(#{table_name.inspect}, #{from_column_name.inspect}, #{to_column_name.inspect})
|
378
|
-
|
375
|
+
RUBY
|
376
|
+
end
|
379
377
|
end
|
380
|
-
end
|
381
378
|
|
382
|
-
|
383
|
-
|
384
|
-
|
379
|
+
def append_change_column(table_name, column_name, attrs, buf)
|
380
|
+
type = attrs.fetch(:type)
|
381
|
+
options = attrs[:options] || {}
|
385
382
|
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
383
|
+
# Fix for https://github.com/rails/rails/commit/7f0567b43b73b1bd1a16bfac9cd32fcbf1321b51
|
384
|
+
if Ridgepole::ConnectionAdapters.mysql? && ActiveRecord::VERSION::STRING !~ /\A5\.0\./
|
385
|
+
options[:comment] = nil unless options.key?(:comment)
|
386
|
+
end
|
390
387
|
|
391
|
-
|
392
|
-
|
388
|
+
if @options[:bulk_change]
|
389
|
+
buf.puts(<<-RUBY)
|
393
390
|
t.change(#{column_name.inspect}, #{type.inspect}, #{inspect_options_include_default_proc(options)})
|
394
|
-
|
395
|
-
|
396
|
-
|
391
|
+
RUBY
|
392
|
+
else
|
393
|
+
buf.puts(<<-RUBY)
|
397
394
|
change_column(#{table_name.inspect}, #{column_name.inspect}, #{type.inspect}, #{inspect_options_include_default_proc(options)})
|
398
|
-
|
395
|
+
RUBY
|
396
|
+
end
|
399
397
|
end
|
400
|
-
end
|
401
398
|
|
402
|
-
|
403
|
-
|
404
|
-
|
399
|
+
def append_remove_column(table_name, column_name, _attrs, buf)
|
400
|
+
if @options[:bulk_change]
|
401
|
+
buf.puts(<<-RUBY)
|
405
402
|
t.remove(#{column_name.inspect})
|
406
|
-
|
407
|
-
|
408
|
-
|
403
|
+
RUBY
|
404
|
+
else
|
405
|
+
buf.puts(<<-RUBY)
|
409
406
|
remove_column(#{table_name.inspect}, #{column_name.inspect})
|
410
|
-
|
407
|
+
RUBY
|
408
|
+
end
|
411
409
|
end
|
412
|
-
end
|
413
410
|
|
414
|
-
|
415
|
-
|
416
|
-
|
411
|
+
def append_add_indices(table_name, delta, buf)
|
412
|
+
(delta[:add] || {}).each do |index_name, attrs|
|
413
|
+
append_add_index(table_name, index_name, attrs, buf)
|
414
|
+
end
|
417
415
|
end
|
418
|
-
end
|
419
416
|
|
420
|
-
|
421
|
-
|
422
|
-
|
417
|
+
def append_delete_indices(table_name, delta, buf)
|
418
|
+
(delta[:delete] || {}).each do |index_name, attrs|
|
419
|
+
append_remove_index(table_name, index_name, attrs, buf)
|
420
|
+
end
|
423
421
|
end
|
424
|
-
end
|
425
422
|
|
426
|
-
|
427
|
-
|
428
|
-
|
423
|
+
def append_add_index(table_name, _index_name, attrs, buf, force_bulk_change = false)
|
424
|
+
column_name = attrs.fetch(:column_name)
|
425
|
+
options = attrs[:options] || {}
|
429
426
|
|
430
|
-
|
431
|
-
|
427
|
+
if force_bulk_change || @options[:bulk_change]
|
428
|
+
buf.puts(<<-RUBY)
|
432
429
|
t.index(#{column_name.inspect}, #{options.inspect})
|
433
|
-
|
434
|
-
|
435
|
-
|
430
|
+
RUBY
|
431
|
+
else
|
432
|
+
buf.puts(<<-RUBY)
|
436
433
|
add_index(#{table_name.inspect}, #{column_name.inspect}, #{options.inspect})
|
437
|
-
|
434
|
+
RUBY
|
435
|
+
end
|
438
436
|
end
|
439
|
-
end
|
440
437
|
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
438
|
+
def append_remove_index(table_name, _index_name, attrs, buf)
|
439
|
+
column_name = attrs.fetch(:column_name)
|
440
|
+
options = attrs[:options] || {}
|
441
|
+
target = options[:name] ? { name: options[:name] } : column_name
|
445
442
|
|
446
|
-
|
447
|
-
|
443
|
+
if @options[:bulk_change]
|
444
|
+
buf.puts(<<-RUBY)
|
448
445
|
t.remove_index(#{target.inspect})
|
449
|
-
|
450
|
-
|
451
|
-
|
446
|
+
RUBY
|
447
|
+
else
|
448
|
+
buf.puts(<<-RUBY)
|
452
449
|
remove_index(#{table_name.inspect}, #{target.inspect})
|
453
|
-
|
450
|
+
RUBY
|
451
|
+
end
|
454
452
|
end
|
455
|
-
end
|
456
453
|
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
454
|
+
def append_change_foreign_keys(table_name, delta, pre_buf_for_fk, post_buf_for_fk, options)
|
455
|
+
(delta[:delete] || {}).each do |_, attrs|
|
456
|
+
append_remove_foreign_key(table_name, attrs, pre_buf_for_fk, options)
|
457
|
+
end
|
461
458
|
|
462
|
-
|
463
|
-
|
459
|
+
(delta[:add] || {}).each do |_, attrs|
|
460
|
+
append_add_foreign_key(table_name, attrs, post_buf_for_fk, options)
|
461
|
+
end
|
464
462
|
end
|
465
|
-
end
|
466
463
|
|
467
|
-
|
468
|
-
|
469
|
-
|
464
|
+
def append_add_foreign_key(table_name, attrs, buf, _options)
|
465
|
+
to_table = attrs.fetch(:to_table)
|
466
|
+
attrs_options = attrs[:options] || {}
|
470
467
|
|
471
|
-
|
468
|
+
buf.puts(<<-RUBY)
|
472
469
|
add_foreign_key(#{table_name.inspect}, #{to_table.inspect}, #{attrs_options.inspect})
|
473
|
-
|
474
|
-
|
470
|
+
RUBY
|
471
|
+
end
|
475
472
|
|
476
|
-
|
477
|
-
|
478
|
-
|
473
|
+
def append_remove_foreign_key(table_name, attrs, buf, _options)
|
474
|
+
attrs_options = attrs[:options] || {}
|
475
|
+
fk_name = attrs_options[:name]
|
479
476
|
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
477
|
+
target = if fk_name
|
478
|
+
{ name: fk_name }
|
479
|
+
else
|
480
|
+
attrs.fetch(:to_table)
|
481
|
+
end
|
485
482
|
|
486
|
-
|
483
|
+
buf.puts(<<-RUBY)
|
487
484
|
remove_foreign_key(#{table_name.inspect}, #{target.inspect})
|
488
|
-
|
489
|
-
|
485
|
+
RUBY
|
486
|
+
end
|
490
487
|
|
491
|
-
|
492
|
-
|
493
|
-
|
488
|
+
def delta_execute
|
489
|
+
@delta[:execute] || []
|
490
|
+
end
|
494
491
|
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
492
|
+
def normalize_limit(column_type, column_options)
|
493
|
+
default_limit = Ridgepole::DefaultsLimit.default_limit(column_type, @options)
|
494
|
+
column_options[:limit] ||= default_limit if default_limit
|
495
|
+
end
|
496
|
+
|
497
|
+
def inspect_options_include_default_proc(options)
|
498
|
+
options = options.dup
|
499
499
|
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
else
|
512
|
-
options.inspect
|
500
|
+
if options[:default].is_a?(Proc)
|
501
|
+
proc_default = options.delete(:default)
|
502
|
+
proc_default = ":default=>proc{#{proc_default.call.inspect}}"
|
503
|
+
options_inspect = options.inspect
|
504
|
+
options_inspect.sub!(/\}\z/, '')
|
505
|
+
options_inspect << ', ' if options_inspect !~ /\{\z/
|
506
|
+
options_inspect << proc_default << '}'
|
507
|
+
options_inspect
|
508
|
+
else
|
509
|
+
options.inspect
|
510
|
+
end
|
513
511
|
end
|
514
512
|
end
|
515
513
|
end
|