15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/generators/steroid/migration/migration_generator.rb', line 15
def add_migration
say "Applying steroid: Migration", [:bold, :magenta]
action_choices = [
{name: 'Create table', value: 'create_table'},
{name: 'Create join table', value: 'create_join_table'},
{name: 'Drop table', value: 'drop_table'},
{name: 'Modify table columns/index', value: 'modify_table'},
]
@action = prompt.select("\nWhat would you like to do?", action_choices)
case @action
when 'create_table'
table_name = prompt.ask("\nWhat is the name of table to be created?", required: true) { |q| q.modify :remove }
@content = content_for_create_table(table_name, collect_columns_data)
migration_template "migration.rb", "#{db_migrate_path}/create_#{table_name}.rb"
when 'create_join_table'
table1_name = prompt.ask("\nWhat is the name of first table to be joined?", required: true) { |q| q.modify :remove }
table2_name = prompt.ask("\nWhat is the name of second table to be joined?", required: true) { |q| q.modify :remove }
table_name = prompt.ask("\nWhat is the custom name for join table?", default: find_join_table_name(table1_name, table2_name), required: true) { |q| q.modify :remove }
columns_data = []
if prompt.select("\nAdd `id` column?", boolean_choices)
columns_data << {name: 'id', type: 'primary_key'}
end
if prompt.select("\nAdd index for #{table1_name} foreign_key?", boolean_choices)
columns_data << {name: "#{table1_name.singularize}_id", type: 'index'}
end
if prompt.select("\nAdd index for #{table2_name} foreign_key?", boolean_choices)
columns_data << {name: "#{table2_name.singularize}_id", type: 'index'}
end
if prompt.select("\nAdd composite index for #{table1_name} and #{table2_name} foreign_key?", boolean_choices)
uniq_index_option = prompt.select("Unique combination index?", boolean_choices) ? {meta: {unique: true}} : {}
columns_data << {name: ["#{table2_name.singularize}_id", "#{table2_name.singularize}_id"], type: 'index'}.merge(uniq_index_option)
end
@content = content_for_create_join_table(table1_name, table2_name, table_name, (columns_data + collect_columns_data))
migration_template "migration.rb", "#{db_migrate_path}/create_join_table_#{table_name}.rb"
when 'drop_table'
table_name = prompt.ask("\nWhat is the name of table to be dropped?", required: true) { |q| q.modify :remove }
@content = content_for_drop_table(table_name)
migration_template "migration.rb", "#{db_migrate_path}/drop_#{table_name}.rb"
when 'modify_table'
table_name = prompt.ask("\nWhat is the name of table to add/remove columns/index?", required: true) { |q| q.modify :remove }
modify_table_choices = [
{name: 'Add column', value: 'add_column'}, {name: 'Remove column', value: 'remove_column'},
{name: 'Add index', value: 'add_index'}, {name: 'Remove index', value: 'remove_index'},
{name: 'Add reference', value: 'add_reference'}, {name: 'Remove reference', value: 'remove_reference'},
{name: 'Exit', value: 'exit'}
]
@content = []
file_name = []
while (modify_table_action = prompt.select("\nWhat would you like to do?", modify_table_choices)) != 'exit'
for_removal = modify_table_action.start_with?('remove_')
if modify_table_action.end_with?('_index') || modify_table_action.end_with?('_reference')
data = modify_table_action.end_with?('_index') ? ask_index_data(for_removal: for_removal) : ask_reference_data(for_removal: for_removal)
file_name << modify_table_action << data[:name]
else
data = ask_column_data(for_removal: for_removal)
file_name << (for_removal ? 'remove' : 'add') << data[:name]
end
@content << format_statement_for_modify_table(modify_table_action, table_name, data)
end
@content = content_for_modify_table(@content.join("\n "))
migration_template "migration.rb", "#{db_migrate_path}/#{file_name.join('_')}_in_#{table_name}.rb"
end
end
|