Skip to content

Commit

Permalink
BANKINT-4248: Add generator for index (#122)
Browse files Browse the repository at this point in the history
* 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
stevetrinh authored Sep 16, 2022
1 parent d426e36 commit 6abf12f
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ workflows:
- rspec:
matrix:
parameters:
ruby_version: ["2.5", "2.6", "2.7", "3.0", "3.1"]
ruby_version: ["2.6", "2.7", "3.0", "3.1"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Friendly Postgres migrations for people who don't want to take down their databa

## Supported

- Ruby 2.5 or above
- Ruby 2.6 or above
- Rails 5.2 or above
- Postgres 11 or above

Expand Down
31 changes: 31 additions & 0 deletions lib/generators/nandi/index/USAGE
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
53 changes: 53 additions & 0 deletions lib/generators/nandi/index/index_generator.rb
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
14 changes: 14 additions & 0 deletions lib/generators/nandi/index/templates/add_index.rb
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
2 changes: 1 addition & 1 deletion lib/nandi/compiled_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def validate!

unless validation.valid?
raise InvalidMigrationError, "Migration #{source_file_path} " \
"is not valid:\n#{validation.error_list}"
"is not valid:\n#{validation.error_list}"
end

self
Expand Down
2 changes: 1 addition & 1 deletion lib/nandi/version.rb
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
6 changes: 4 additions & 2 deletions nandi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")

spec.add_dependency "activesupport"
spec.add_dependency "cells"
Expand All @@ -28,12 +28,14 @@ Gem::Specification.new do |spec|

spec.add_development_dependency "bundler", "~> 2.0"
spec.add_development_dependency "byebug", "~> 11.0"
spec.add_development_dependency "gc_ruboconfig", "~> 2.25.0"
spec.add_development_dependency "gc_ruboconfig", "~> 3.3.0"
spec.add_development_dependency "pry-byebug", "~> 3.9.0"
spec.add_development_dependency "rails", "~> 5.2.3"
spec.add_development_dependency "rake", ">= 12.3.3", "~> 13.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "rspec_junit_formatter", "~> 0.4"
spec.add_development_dependency "rubocop", "~> 1.0"
spec.add_development_dependency "yard", "~> 0.9"

spec.metadata["rubygems_mfa_required"] = "true"
end

0 comments on commit 6abf12f

Please sign in to comment.