Class: Charming::Generators::MigrationGenerator

Inherits:
AppFileGenerator show all
Defined in:
lib/charming/generators/migration_generator.rb

Overview

MigrationGenerator implements charming generate migration NAME [field:type ...]. Follows Rails naming conventions:

  • create_<table> generates a create_table migration (fields become columns)
  • add_<x>_to_<table> generates add_column lines for the supplied fields
  • remove_<x>_from_<table> generates remove_column lines
  • anything else generates an empty change method to fill in

Defined Under Namespace

Classes: Field

Constant Summary collapse

VALID_TYPES =

The set of ActiveRecord column types accepted on the command line.

ModelGenerator::VALID_TYPES

Instance Method Summary collapse

Constructor Details

#initialize(name, args, out:, destination:, force: false) ⇒ MigrationGenerator

Returns a new instance of MigrationGenerator.



18
19
20
21
# File 'lib/charming/generators/migration_generator.rb', line 18

def initialize(name, args, out:, destination:, force: false)
  super
  @fields = args.map { |arg| parse_field(arg) }
end

Instance Method Details

#generateObject

Validates database support, then writes the timestamped migration file.

Raises:



24
25
26
27
28
# File 'lib/charming/generators/migration_generator.rb', line 24

def generate
  raise Error, "Database support is not configured. Run `charming db:install sqlite3` first." unless database_configured?

  create_file(migration_path, migration)
end