Class: PgMultitenantSchemas::UI::TenantsController
- Inherits:
-
ActionController::Base
- Object
- ActionController::Base
- PgMultitenantSchemas::UI::TenantsController
- Defined in:
- lib/pg_multitenant_schemas/ui/app/controllers/pg_multitenant_schemas/ui/tenants_controller.rb
Overview
Controller backing the UI engine for listing and managing tenants and schemas.
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
- #index ⇒ Object
- #migrate ⇒ Object
- #migrate_all ⇒ Object
- #migration_status ⇒ Object
- #new ⇒ Object
- #switch ⇒ Object
Instance Method Details
#create ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/pg_multitenant_schemas/ui/app/controllers/pg_multitenant_schemas/ui/tenants_controller.rb', line 22 def create @tenant = tenant_model.new(tenant_params) if @tenant.save PgMultitenantSchemas::Migrator.setup_tenant(@tenant.subdomain, verbose: false) redirect_to "/pg_multitenant_schemas/tenants", notice: "Tenant '#{@tenant.subdomain}' created and schema provisioned." else render :new, status: :unprocessable_entity end rescue StandardError => e @tenant&.destroy if @tenant&.persisted? redirect_to "/pg_multitenant_schemas/tenants", alert: "Failed to provision schema: #{e.}" end |
#destroy ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/pg_multitenant_schemas/ui/app/controllers/pg_multitenant_schemas/ui/tenants_controller.rb', line 38 def destroy @tenant = tenant_model.find(params[:id]) schema = @tenant.subdomain @tenant.destroy PgMultitenantSchemas::SchemaSwitcher.drop_schema(schema) redirect_to "/pg_multitenant_schemas/tenants", notice: "Tenant '#{schema}' and its schema deleted." rescue StandardError => e redirect_to "/pg_multitenant_schemas/tenants", alert: "Error deleting tenant: #{e.}" end |
#index ⇒ Object
12 13 14 15 16 |
# File 'lib/pg_multitenant_schemas/ui/app/controllers/pg_multitenant_schemas/ui/tenants_controller.rb', line 12 def index @tenants = tenant_model.all.order(:subdomain) @schemas = load_available_schemas @current_schema = resolve_selected_schema end |
#migrate ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/pg_multitenant_schemas/ui/app/controllers/pg_multitenant_schemas/ui/tenants_controller.rb', line 51 def migrate @tenant = tenant_model.find(params[:id]) result = PgMultitenantSchemas::Migrator.migrate_tenant(@tenant.subdomain, verbose: false) status = result[:status] == :success ? "notice" : "alert" msg = result[:message] || "Migration completed." redirect_to "/pg_multitenant_schemas/tenants", status => msg rescue StandardError => e redirect_to "/pg_multitenant_schemas/tenants", alert: "Migration failed: #{e.}" end |
#migrate_all ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/pg_multitenant_schemas/ui/app/controllers/pg_multitenant_schemas/ui/tenants_controller.rb', line 61 def migrate_all results = PgMultitenantSchemas::Migrator.migrate_all(verbose: false) failed = results.count { |r| r[:status] == :error } if failed.zero? redirect_to "/pg_multitenant_schemas/tenants", notice: "All #{results.size} tenant schemas migrated successfully." else redirect_to "/pg_multitenant_schemas/tenants", alert: "#{failed} of #{results.size} migrations failed." end end |
#migration_status ⇒ Object
73 74 75 76 77 78 |
# File 'lib/pg_multitenant_schemas/ui/app/controllers/pg_multitenant_schemas/ui/tenants_controller.rb', line 73 def migration_status @tenant = tenant_model.find(params[:id]) statuses = PgMultitenantSchemas::Migrator.migration_status(verbose: false) @status = statuses.find { |s| s[:schema] == @tenant.subdomain } || {} render :migration_status end |
#new ⇒ Object
18 19 20 |
# File 'lib/pg_multitenant_schemas/ui/app/controllers/pg_multitenant_schemas/ui/tenants_controller.rb', line 18 def new @tenant = tenant_model.new end |
#switch ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/pg_multitenant_schemas/ui/app/controllers/pg_multitenant_schemas/ui/tenants_controller.rb', line 80 def switch schema = params[:schema].to_s.strip return redirect_invalid_schema(schema) unless PgMultitenantSchemas::SchemaSwitcher.schema_exists?(schema) PgMultitenantSchemas::Context.current_schema = schema (schema) redirect_to "/pg_multitenant_schemas/tenants", notice: "Switched active context to schema '#{schema}'." end |