Module: TenantTaskHelpers

Defined in:
lib/pg_multitenant_schemas/tenant_task_helpers.rb

Overview

Tenant task helper methods

Class Method Summary collapse

Class Method Details

.create_tenant_with_attributes(attributes_json) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pg_multitenant_schemas/tenant_task_helpers.rb', line 39

def create_tenant_with_attributes(attributes_json)
  if attributes_json.blank?
    puts "Usage: rails tenants:new['{\"subdomain\":\"acme\",\"name\":\"ACME Corp\"}']"
    exit 1
  end

  begin
    attributes = JSON.parse(attributes_json)
    tenant = PgMultitenantSchemas::Migrator.create_tenant_with_schema(attributes)
    puts "šŸŽ‰ Created tenant: #{tenant.subdomain}"
  rescue JSON::ParserError
    puts "āŒ Invalid JSON format"
    exit 1
  rescue StandardError => e
    puts "āŒ Error creating tenant: #{e.message}"
    exit 1
  end
end

.create_tenant_with_schema(schema_name) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/pg_multitenant_schemas/tenant_task_helpers.rb', line 29

def create_tenant_with_schema(schema_name)
  if schema_name.blank?
    puts "Usage: rails tenants:create[schema_name]"
    puts "Example: rails tenants:create[acme_corp]"
    exit 1
  end

  PgMultitenantSchemas::Migrator.setup_tenant(schema_name)
end

.drop_tenant_schema(schema_name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pg_multitenant_schemas/tenant_task_helpers.rb', line 58

def drop_tenant_schema(schema_name)
  if schema_name.blank?
    puts "Usage: rails tenants:drop[schema_name]"
    exit 1
  end

  if schema_name == "public"
    puts "āŒ Cannot drop public schema"
    exit 1
  end

  confirm_schema_drop(schema_name)
end

.list_tenant_schemasObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/pg_multitenant_schemas/tenant_task_helpers.rb', line 6

def list_tenant_schemas
  puts "šŸ“‹ Available tenant schemas:"

  schemas = PgMultitenantSchemas::Migrator.send(:tenant_schemas)

  if schemas.any?
    schemas.each { |schema| puts "  - #{schema}" }
    puts "\nšŸ“Š Total: #{schemas.count} tenant schemas"
  else
    puts "  No tenant schemas found"
  end
end

.migrate_specific_tenant(schema_name) ⇒ Object



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

def migrate_specific_tenant(schema_name)
  if schema_name.blank?
    puts "Usage: rails tenants:migrate_tenant[schema_name]"
    puts "Example: rails tenants:migrate_tenant[acme_corp]"
    exit 1
  end

  PgMultitenantSchemas::Migrator.migrate_tenant(schema_name)
end

.rollback_tenant_migrations(schema_name, steps) ⇒ Object



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

def rollback_tenant_migrations(schema_name, steps)
  if schema_name.blank?
    puts "Usage: rails tenants:rollback[schema_name,steps]"
    puts "Example: rails tenants:rollback[acme_corp,2]"
    exit 1
  end

  PgMultitenantSchemas::Migrator.rollback_tenant(schema_name, steps: steps)
end