Class: DbdocEngine::DbDesignDynamicTableCommands

Inherits:
Object
  • Object
show all
Defined in:
app/queries/dbdoc_engine/db_design_dynamic_table_commands.rb

Class Method Summary collapse

Class Method Details

.create_with_columns(params) ⇒ Hash

Creates a new dynamic table with associated columns in a transaction

Parameters:

  • params (Hash) —

    Table parameters including nested columns attributes

Returns:

  • (Hash) —

    Result hash with :success status and either the table or errors



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_commands.rb', line 11

def create_with_columns(params)
  table = DbDesignDynamicTable.new(params)

  # Important: Validate the table OUTSIDE the transaction first
  # This ensures uniqueness validations run before the transaction starts
  unless table.valid?
    return { success: false, errors: collect_errors(table) }
  end

  begin
    # Wrap operations in transaction to ensure all-or-nothing behavior
    ActiveRecord::Base.transaction do
      # Now validate columns too
      if valid_columns?(table)
        table.save!
        return { success: true, table: table }
      end
      # Rollback transaction if validations fail
      raise ActiveRecord::Rollback
    end
  rescue ActiveRecord::RecordInvalid
    # This would catch any exceptions if save! fails
  end

  # Return failure status with collected errors
  { success: false, errors: collect_errors(table) }
end

.update_with_columns(table, params) ⇒ Hash

Updates an existing dynamic table and its columns in a transaction

Parameters:

  • table (DbDesignDynamicTable) —

    Existing table to update

  • params (Hash) —

    New parameters including nested columns attributes

Returns:

  • (Hash) —

    Result hash with :success status and either the table or errors



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/queries/dbdoc_engine/db_design_dynamic_table_commands.rb', line 43

def update_with_columns(table, params)
  table.assign_attributes(params)

  # Validate table outside transaction first
  unless table.valid?
    return { success: false, errors: collect_errors(table) }
  end

  # Transaction block for atomic updates
  ActiveRecord::Base.transaction do
    # Now validate columns
    if valid_columns?(table)
      table.save!
      return { success: true, table: table }
    end

    # Collect errors and rollback if validations fail
    raise ActiveRecord::Rollback
  end

  # Return failure status with collected errors
  { success: false, errors: collect_errors(table) }
end