Class: Rodauth::Tools::Migration Deprecated
- Inherits:
-
Object
- Object
- Rodauth::Tools::Migration
- Defined in:
- lib/rodauth/tools/migration.rb
Overview
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.
Defined Under Namespace
Classes: MockSequelDatabase
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#db_adapter ⇒ Object
readonly
Returns the value of attribute db_adapter.
-
#features ⇒ Object
readonly
Returns the value of attribute features.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Class Method Summary collapse
-
.template_exists?(feature) ⇒ Boolean
Check if an ERB template exists for a given feature.
Instance Method Summary collapse
-
#execute_create_tables(db) ⇒ void
Execute CREATE TABLE operations directly against the database.
-
#generate ⇒ String
Generate the migration content.
-
#initialize(features:, prefix: nil, db_adapter: nil, db: nil) ⇒ Migration
constructor
Initialize the migration generator.
-
#migration_name ⇒ String
Get the migration name.
Constructor Details
#initialize(features:, prefix: nil, db_adapter: nil, db: nil) ⇒ Migration
Initialize the migration generator
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
#db ⇒ Object (readonly)
Returns the value of attribute db.
34 35 36 |
# File 'lib/rodauth/tools/migration.rb', line 34 def db @db end |
#db_adapter ⇒ Object (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 |
#features ⇒ Object (readonly)
Returns the value of attribute features.
34 35 36 |
# File 'lib/rodauth/tools/migration.rb', line 34 def features @features end |
#prefix ⇒ Object (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
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.
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 |
#generate ⇒ String
Generate the migration 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_name ⇒ String
Get the 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 |