-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BANKINT-4248: Add generator for index (#122)
* BANKINT-4248: Add generator for index * BANKINT-4248: Fix rubocop * BANKINT-4248: Dropped support for Ruby <= 2.5 * BANKINT-4248: Bump version 0.13.0
- Loading branch information
1 parent
d426e36
commit 6abf12f
Showing
8 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Description: | ||
Generates new database indices | ||
|
||
Example: | ||
rails generate nandi:index table_name index_col | ||
|
||
This will create: | ||
db/safe_migrations/20190424123727_add_index_on_index_col_to_table_name.rb | ||
|
||
or | ||
|
||
rails generate nandi:index table_name_1,table_name_2 index_col | ||
|
||
This will create: | ||
db/safe_migrations/20190424123727_add_index_on_index_col_to_table_name_1.rb | ||
db/safe_migrations/20190424123728_add_index_on_index_col_to_table_name_2.rb | ||
|
||
or | ||
|
||
rails generate nandi:index table_name index_col_1,index_col_2 | ||
|
||
This will create: | ||
db/safe_migrations/20190424123727_add_index_on_index_col_1_index_col_2_to_table_name.rb | ||
|
||
or | ||
|
||
rails generate nandi:index table_name index_col --index_name bespoke_idx_name | ||
|
||
This will create: | ||
db/safe_migrations/20190424123727_add_index_on_index_col_to_table_name.rb | ||
with the specified bespoke index name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails/generators" | ||
require "nandi/formatting" | ||
|
||
module Nandi | ||
class IndexGenerator < Rails::Generators::Base | ||
include Nandi::Formatting | ||
|
||
argument :tables, type: :string | ||
argument :columns, type: :string | ||
class_option :index_name, type: :string | ||
|
||
source_root File.expand_path("templates", __dir__) | ||
|
||
attr_reader :add_index_name, :index_name, :table, :columns | ||
|
||
def add_index | ||
tables_list = tables.split(",") | ||
@columns = columns.split(",") | ||
|
||
tables_list.each_with_index do |table, idx| | ||
next if table.empty? | ||
|
||
@table = table.to_sym | ||
|
||
@add_index_name = "add_index_on_#{columns.join('_')}_to_#{table}" | ||
@index_name = ( | ||
override_index_name || "idx_#{table}_on_#{columns.join('_')}" | ||
).to_sym | ||
|
||
template( | ||
"add_index.rb", | ||
"#{base_path}/#{timestamp(idx)}_#{add_index_name}.rb", | ||
) | ||
end | ||
end | ||
|
||
private | ||
|
||
def timestamp(offset = 0) | ||
(Time.now.utc + offset).strftime("%Y%m%d%H%M%S") | ||
end | ||
|
||
def base_path | ||
Nandi.config.migration_directory || "db/safe_migrations" | ||
end | ||
|
||
def override_index_name | ||
options["index_name"]&.to_sym | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class <%= add_index_name.camelize %> < Nandi::Migration | ||
def up | ||
add_index <%= format_value(table) %>, | ||
%i<%= format_value(columns).tr('"', '') %>, | ||
name: <%= format_value(index_name) %> | ||
end | ||
def down | ||
remove_index <%= format_value(table) %>, | ||
%i<%= format_value(columns).tr('"', '') %> | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Nandi | ||
VERSION = "0.12.0" | ||
VERSION = "0.13.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters