-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
generate migration
command and generator (#201)
--------- Co-authored-by: Sean Collins <sean@cllns.com>
- Loading branch information
Showing
7 changed files
with
182 additions
and
3 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,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module Hanami | ||
module CLI | ||
module Commands | ||
module App | ||
module Generate | ||
# @since 2.2.0 | ||
# @api private | ||
class Migration < Command | ||
argument :name, required: true, desc: "Migration name" | ||
option :slice, required: false, desc: "Slice name" | ||
|
||
example [ | ||
%(create_posts), | ||
%(add_published_at_to_posts), | ||
%(create_users --slice=admin), | ||
] | ||
|
||
def generator_class | ||
Generators::App::Migration | ||
end | ||
end | ||
end | ||
end | ||
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
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,69 @@ | ||
# frozen_string_literal: true | ||
|
||
module Hanami | ||
module CLI | ||
module Generators | ||
module App | ||
# @since 2.2.0 | ||
# @api private | ||
class Migration | ||
# @since 2.2.0 | ||
# @api private | ||
def initialize(fs:, inflector:, out: $stdout) | ||
@fs = fs | ||
@inflector = inflector | ||
@out = out | ||
end | ||
|
||
# @since 2.2.0 | ||
# @api private | ||
def call(_app_namespace, name, slice, **_opts) | ||
normalized_name = inflector.underscore(name) | ||
ensure_valid_name(normalized_name) | ||
|
||
base = if slice | ||
fs.join("slices", slice, "config", "db", "migrate") | ||
else | ||
fs.join("config", "db", "migrate") | ||
end | ||
|
||
path = fs.join(base, file_name(normalized_name)) | ||
|
||
fs.write(path, FILE_CONTENTS) | ||
end | ||
|
||
private | ||
|
||
attr_reader :fs, :inflector, :out | ||
|
||
VALID_NAME_REGEX = /^[_a-z0-9]+$/ | ||
private_constant :VALID_NAME_REGEX | ||
|
||
def ensure_valid_name(name) | ||
unless VALID_NAME_REGEX.match?(name.downcase) | ||
raise InvalidMigrationNameError.new(name) | ||
end | ||
end | ||
|
||
def file_name(name) | ||
"#{Time.now.strftime(VERSION_FORMAT)}_#{name}.rb" | ||
end | ||
|
||
VERSION_FORMAT = "%Y%m%d%H%M%S" | ||
private_constant :VERSION_FORMAT | ||
|
||
FILE_CONTENTS = <<~RUBY | ||
# frozen_string_literal: true | ||
ROM::SQL.migration do | ||
# Add your migration here. | ||
# | ||
# See https://sequel.jeremyevans.net/rdoc/files/doc/migration_rdoc.html for details. | ||
end | ||
RUBY | ||
private_constant :FILE_CONTENTS | ||
end | ||
end | ||
end | ||
end | ||
end |
71 changes: 71 additions & 0 deletions
71
spec/unit/hanami/cli/commands/app/generate/migration_spec.rb
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,71 @@ | ||
# frozen_string_literal: true | ||
|
||
require "hanami" | ||
|
||
RSpec.describe Hanami::CLI::Commands::App::Generate::Migration, :app do | ||
subject { described_class.new(fs: fs, inflector: inflector) } | ||
|
||
let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) } | ||
let(:inflector) { Dry::Inflector.new } | ||
|
||
let(:out) { StringIO.new } | ||
|
||
def output | ||
out.string.strip | ||
end | ||
|
||
let(:app) { Hanami.app.namespace } | ||
|
||
let(:migration_file_contents) { | ||
<<~RUBY | ||
# frozen_string_literal: true | ||
ROM::SQL.migration do | ||
# Add your migration here. | ||
# | ||
# See https://sequel.jeremyevans.net/rdoc/files/doc/migration_rdoc.html for details. | ||
end | ||
RUBY | ||
} | ||
|
||
before do | ||
allow(Time).to receive(:now) { Time.new(2024, 7, 13, 14, 6, 0) } | ||
end | ||
|
||
context "generating for app" do | ||
it "generates a migration" do | ||
subject.call(name: "create_posts") | ||
|
||
expect(fs.read("config/db/migrate/20240713140600_create_posts.rb")).to eq migration_file_contents | ||
expect(output).to eq("Created config/db/migrate/20240713140600_create_posts.rb") | ||
end | ||
|
||
it "generates a migration with underscored version of camel cased name" do | ||
subject.call(name: "CreatePosts") | ||
|
||
expect(fs.read("config/db/migrate/20240713140600_create_posts.rb")).to eq migration_file_contents | ||
expect(output).to eq("Created config/db/migrate/20240713140600_create_posts.rb") | ||
end | ||
|
||
it "raises an error if given an invalid name" do | ||
expect { | ||
subject.call(name: "create posts") | ||
}.to raise_error(include_in_order( | ||
"Invalid migration name: create posts", | ||
"Name must contain only letters, numbers, and underscores." | ||
)) | ||
end | ||
end | ||
|
||
context "generating for a slice" do | ||
it "generates a migration" do | ||
fs.mkdir("slices/main") | ||
out.truncate 0 | ||
|
||
subject.call(name: "create_posts", slice: "main") | ||
|
||
expect(fs.read("slices/main/config/db/migrate/20240713140600_create_posts.rb")).to eq migration_file_contents | ||
expect(output).to eq("Created slices/main/config/db/migrate/20240713140600_create_posts.rb") | ||
end | ||
end | ||
end |