Skip to content

Commit

Permalink
Support camelcase when generate migration file, Fix #1045 (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
zw963 authored Jun 16, 2024
1 parent af42b94 commit c1729f3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
11 changes: 5 additions & 6 deletions src/avram/tasks/gen/migration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ require "./migration_generator"
class Gen::Migration < LuckyTask::Task
summary "Generate a new migration"
help_message <<-TEXT
Generate a new migration using the passed in migration name.
Generate a new migration using the passed in migration file name.
The migration name must be CamelCase. No other options are available.
The migration file name can be underscore or CamelCase. No other options are available.
Examples:
lucky gen.migration CreateUsers
lucky gen.migration create_users
lucky gen.migration AddAgeToUsers
lucky gen.migration MakeUserNameOptional
TEXT

positional_arg :migration_name, "The migration class name", format: /^[A-Z]/
positional_arg :migration_file_name, "The migration file name"

Habitat.create do
setting io : IO = STDOUT
Expand All @@ -29,7 +28,7 @@ class Gen::Migration < LuckyTask::Task

def call
Avram::Migrator.run do
Avram::Migrator::MigrationGenerator.new(name: migration_name, io: output).generate
Avram::Migrator::MigrationGenerator.new(name: migration_file_name, io: output).generate
end
end
end
12 changes: 1 addition & 11 deletions src/avram/tasks/gen/migration_generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Avram::Migrator::MigrationGenerator
end

def generate(@_version = @_version)
ensure_camelcase_name
@name = name.camelcase
make_migrations_folder_if_missing
ensure_unique
File.write(file_path, contents)
Expand Down Expand Up @@ -51,16 +51,6 @@ class Avram::Migrator::MigrationGenerator
end.chomp
end

private def ensure_camelcase_name
if name.camelcase != name
raise <<-ERROR
Migration must be in camel case.
#{green_arrow} Try this instead: #{"lucky gen.migration #{name.camelcase}".colorize(:green)}
ERROR
end
end

private def ensure_unique
d = Dir.new(Dir.current + "/db/migrations")
d.each_child { |x|
Expand Down

0 comments on commit c1729f3

Please sign in to comment.