Class: DbdocEngine::TableManagementService

Inherits:
Object
  • Object
show all
Defined in:
app/services/dbdoc_engine/table_management_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ TableManagementService

Returns a new instance of TableManagementService.



6
7
8
9
# File 'app/services/dbdoc_engine/table_management_service.rb', line 6

def initialize(table)
  @table = table
  @errors = []
end

Instance Attribute Details

#errors ⇒ Object (readonly)

Returns the value of attribute errors.



4
5
6
# File 'app/services/dbdoc_engine/table_management_service.rb', line 4

def errors
  @errors
end

#table ⇒ Object (readonly)

Returns the value of attribute table.



4
5
6
# File 'app/services/dbdoc_engine/table_management_service.rb', line 4

def table
  @table
end

Instance Method Details

#permanent_destroy ⇒ Object

Permanently removes a table from the database



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/services/dbdoc_engine/table_management_service.rb', line 48

def permanent_destroy
  # Extract original table name from the current name
  original_name = extract_original_name(table.table_name)

  table_info = {
    table_name: original_name,
    group_id: table.db_design_table_group_id,
    description: table.description,
    permanently_deleted: true
  }

  if table.destroy
    # Log the permanent deletion to changelog
    DbDesignChangelog.log_change(
      DbDesignChangelog::ACTION_DELETE,
      DbDesignChangelog::ENTITY_TABLE,
      original_name,
      "Permanently deleted table '#{original_name}'",
      "Super Admin",
      table_info,
      nil
    )
    success_result
  else
    add_error("Unable to permanently delete table '#{original_name}'.")
    failure_result
  end
end

#restore ⇒ Object

Restores a soft-deleted table



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/services/dbdoc_engine/table_management_service.rb', line 78

def restore
  # Extract the original name from the current name
  original_name = extract_original_name(table.table_name)

  # Check if another table with the same name exists
  if name_conflict?(original_name)
    add_error(I18n.t("dbdoc.errors.messages.unable_to_restore_table_duplicate", original_name: original_name))
    return failure_result
  end

  # Check if the table's group is soft-deleted
  if table_group_is_deleted?
    group_name = get_table_group_name
    add_error(I18n.t("dbdoc.errors.messages.cannot_restore_table_deleted_group",
                    table_name: original_name,
                    group_name: group_name))
    return failure_result
  end

  if table.update(table_name: original_name, deleted_at: nil)
    # Log the restoration to changelog
    DbDesignChangelog.log_change(
      DbDesignChangelog::ACTION_UPDATE,
      DbDesignChangelog::ENTITY_TABLE,
      original_name,
      "Restored deleted table '#{original_name}'",
      "Super Admin",
      { deleted: true },
      { deleted: false }
    )
    success_result
  else
    add_error("Unable to restore table '#{original_name}'.")
    failure_result
  end
end

#soft_delete ⇒ Object

Soft deletes a table if it's not referenced by other tables



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
38
39
40
41
42
43
44
45
# File 'app/services/dbdoc_engine/table_management_service.rb', line 12

def soft_delete
  if foreign_key_dependencies?
    add_error(I18n.t("dbdoc.errors.messages.cannot_delete_referenced_table", table_name: table.table_name))
    return failure_result
  end

  # Store original name before changing it
  original_name = table.table_name

  # Append ID to the table name for soft deletion
  new_name = "#{original_name}_deleted_#{table.id}"

  if table.update(table_name: new_name, deleted_at: Time.current)
    # Log the soft deletion to changelog
    DbDesignChangelog.log_change(
      DbDesignChangelog::ACTION_DELETE,
      DbDesignChangelog::ENTITY_TABLE,
      original_name,
      "Soft deleted table '#{original_name}'",
      table.updated_by,
      {
        table_name: original_name,
        group_id: table.db_design_table_group_id,
        description: table.description,
        deleted: true
      },
      nil
    )
    success_result
  else
    add_error("Unable to delete table '#{original_name}'.")
    failure_result
  end
end