Class: PgMultitenantSchemas::Migrator

Inherits:
Object
  • Object
show all
Extended by:
MigrationDisplayReporter, MigrationExecutor, MigrationSchemaOperations, MigrationStatusReporter
Defined in:
lib/pg_multitenant_schemas/migrator.rb

Overview

Enhanced migration management for multi-tenant schemas Provides automated migration operations across tenant schemas

Class Method Summary collapse

Methods included from MigrationStatusReporter

migration_status

Class Method Details

.create_tenant_with_schema(attributes, verbose: true) ⇒ Object

Create a new tenant with schema and migrations



61
62
63
64
65
66
67
68
69
# File 'lib/pg_multitenant_schemas/migrator.rb', line 61

def create_tenant_with_schema(attributes, verbose: true)
  validate_tenant_model_exists
  tenant = Tenant.create!(attributes)
  schema_name = extract_schema_name(tenant)

  puts "🆕 Creating new tenant: #{schema_name}" if verbose
  setup_tenant(schema_name, verbose: verbose)
  tenant
end

.migrate_all(verbose: true, ignore_errors: false) ⇒ Object

Run migrations on all tenant schemas



19
20
21
22
23
24
25
# File 'lib/pg_multitenant_schemas/migrator.rb', line 19

def migrate_all(verbose: true, ignore_errors: false)
  schemas = tenant_schemas
  results = process_schemas_migration(schemas, verbose, ignore_errors)

  display_migration_summary(results, verbose)
  results
end

.migrate_tenant(schema_name, verbose: true, raise_on_error: true) ⇒ Object

Run migrations on a specific tenant schema



28
29
30
31
32
# File 'lib/pg_multitenant_schemas/migrator.rb', line 28

def migrate_tenant(schema_name, verbose: true, raise_on_error: true)
  return handle_missing_schema(schema_name, verbose, raise_on_error) unless schema_exists?(schema_name)

  execute_tenant_migration(schema_name, verbose, raise_on_error)
end

.rollback_tenant(schema_name, steps: 1, verbose: true) ⇒ Object

Rollback migrations for a specific tenant



72
73
74
75
76
77
78
79
# File 'lib/pg_multitenant_schemas/migrator.rb', line 72

def rollback_tenant(schema_name, steps: 1, verbose: true)
  puts "⏪ Rolling back #{steps} steps for #{schema_name}" if verbose
  original_schema = current_schema

  perform_rollback(schema_name, steps, verbose)
ensure
  switch_to_schema(original_schema) if original_schema
end

.setup_all_tenants(verbose: true) ⇒ Object

Setup all tenants from Tenant model



50
51
52
53
54
55
56
57
58
# File 'lib/pg_multitenant_schemas/migrator.rb', line 50

def setup_all_tenants(verbose: true)
  validate_tenant_model_exists
  tenants = Tenant.all
  puts "🏗️  Setting up #{tenants.count} tenants..." if verbose

  results = process_setup_for_tenants(tenants, verbose)
  display_setup_summary(results, verbose)
  results
end

.setup_tenant(schema_name, verbose: true) ⇒ Object

Setup tenant with schema creation and migrations



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pg_multitenant_schemas/migrator.rb', line 35

def setup_tenant(schema_name, verbose: true)
  puts "🏗️  Setting up tenant: #{schema_name}" if verbose

  begin
    create_tenant_schema_if_needed(schema_name, verbose)
    result = migrate_tenant(schema_name, verbose: verbose)
    puts "  🎉 Tenant setup completed!" if verbose
    result
  rescue StandardError => e
    puts "  ❌ Setup failed: #{e.message}" if verbose
    raise
  end
end