Class: Rodauth::Tools::Migration Deprecated

Inherits:
Object
  • Object
show all
Defined in:
lib/rodauth/tools/migration.rb

Overview

Deprecated.

This static migration generator is deprecated in favor of the dynamic table_guard feature with sequel generation modes. Use table_guard_sequel_mode instead for automatic migration generation.

Sequel migration generator for Rodauth database tables.

Generates migrations for Sequel ORM, supporting PostgreSQL, MySQL, and SQLite databases.

Examples:

Generate a migration (DEPRECATED)

generator = Rodauth::Tools::Migration.new(
  features: [:base, :verify_account, :otp],
  prefix: "account",
  db_adapter: :postgresql
)

generator.generate # => migration content

Use table_guard instead (RECOMMENDED)

plugin :rodauth do
  enable :base, :verify_account, :otp, :table_guard
  table_guard_sequel_mode :migration
end

Defined Under Namespace

Classes: MockSequelDatabase

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(features:, prefix: nil, db_adapter: nil, db: nil) ⇒ Migration

Initialize the migration generator

Parameters:

  • features (Array<Symbol>)

    List of Rodauth features to generate tables for

  • prefix (String) (defaults to: nil)

    Table name prefix (default: "account")

  • db_adapter (Symbol) (defaults to: nil)

    Database adapter (:postgresql, :mysql2, :sqlite3)

  • db (Sequel::Database) (defaults to: nil)

    Sequel database connection



42
43
44
45
46
47
48
49
50
# File 'lib/rodauth/tools/migration.rb', line 42

def initialize(features:, prefix: nil, db_adapter: nil, db: nil)
  @features = Array(features).map(&:to_sym)
  @prefix = prefix
  @db_adapter = db_adapter&.to_sym
  @db = db || create_mock_db

  validate_features!
  validate_feature_templates!
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



34
35
36
# File 'lib/rodauth/tools/migration.rb', line 34

def db
  @db
end

#db_adapterObject (readonly)

Returns the value of attribute db_adapter.



34
35
36
# File 'lib/rodauth/tools/migration.rb', line 34

def db_adapter
  @db_adapter
end

#featuresObject (readonly)

Returns the value of attribute features.



34
35
36
# File 'lib/rodauth/tools/migration.rb', line 34

def features
  @features
end

#prefixObject (readonly)

Returns the value of attribute prefix.



34
35
36
# File 'lib/rodauth/tools/migration.rb', line 34

def prefix
  @prefix
end

Class Method Details

.template_exists?(feature) ⇒ Boolean

Check if an ERB template exists for a given feature

Parameters:

  • feature (Symbol)

    Feature name

Returns:

  • (Boolean)

    True if template exists



110
111
112
113
# File 'lib/rodauth/tools/migration.rb', line 110

def self.template_exists?(feature)
  template_path = File.join(__dir__, 'migration', 'sequel', "#{feature}.erb")
  File.exist?(template_path)
end

Instance Method Details

#execute_create_tables(db) ⇒ void

This method returns an undefined value.

Execute CREATE TABLE operations directly against the database

This evaluates the ERB templates and executes the resulting Sequel migration code against the provided database connection.

Parameters:

  • db (Sequel::Database)

    Database connection



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rodauth/tools/migration.rb', line 69

def execute_create_tables(db)
  # Update the db reference for template binding
  @db = db

  # Generate migration code from ERB templates
  migration_code = generate

  # Load Sequel's migration extension
  require 'sequel/extensions/migration'

  # Wrap in Sequel.migration block and execute
  # NOTE: Using eval here is appropriate because:
  # 1. The code is generated from our own trusted ERB templates
  # 2. This is how Sequel migrations work - they're Ruby DSL code
  # 3. The alternative would require re-implementing Sequel's migration DSL
  migration = eval(<<~RUBY, binding, __FILE__, __LINE__ + 1)
    Sequel.migration do
      up do
        #{migration_code}
      end
    end
  RUBY

  # Apply the migration
  migration.apply(db, :up)
end

#generateString

Generate the migration content

Returns:

  • (String)

    Complete migration file content



55
56
57
58
59
60
# File 'lib/rodauth/tools/migration.rb', line 55

def generate
  features
    .map { |feature| load_template(feature) }
    .map { |content| evaluate_erb(content) }
    .join("\n")
end

#migration_nameString

Get the migration name

Returns:

  • (String)

    Migration name



99
100
101
102
103
104
# File 'lib/rodauth/tools/migration.rb', line 99

def migration_name
  parts = ['create_rodauth']
  parts << prefix if prefix && prefix != 'account'
  parts.concat(features)
  parts.join('_')
end