Class: Databasium::MigrationsController

Inherits:
ApplicationController show all
Includes:
Pagy::Method
Defined in:
app/controllers/databasium/migrations_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/databasium/migrations_controller.rb', line 37

def create
  require "rails/generators"
  Rails.application.load_generators
  require "rails/generators/active_record/migration/migration_generator"

  if params[:add_migration] == "Save"
    success = @migration_service.save_migration(migration_params)
  else
    content = @migration_service.generate_migration(migration_params)
  end

  if success
    flash[:success] = "Migration for table #{migration_params[:table_name]} saved successfully."
    redirect_to migrations_path, status: :see_other
  elsif content
    render turbo_stream:
             turbo_stream.replace(
               "migration_preview",
               Components::Databasium::Migrations::Preview.new(content: content)
             )
  else
    head :unprocessable_entity
  end
end

#indexObject



7
8
9
# File 'app/controllers/databasium/migrations_controller.rb', line 7

def index
  render Views::Databasium::Migrations::Index.new
end

#newObject



32
33
34
35
# File 'app/controllers/databasium/migrations_controller.rb', line 32

def new
  @tables = Databasium::Schema.new.tables
  render Views::Databasium::Migrations::New.new(tables: @tables)
end

#rollback_migrationObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/databasium/migrations_controller.rb', line 110

def rollback_migration
  version = rollback_migration_params[:version]
  success =
    @migration_service.rollback_migration(
      version,
      rollback_migration_params[:rollback_steps],
      rollback_migration_params[:till_this_migration]
    )
  message = "Migration rolled back successfully"
  if rollback_migration_params[:till_this_migration] == "true" ||
       rollback_migration_params[:rollback_steps].present?
    set_action_flash(message, nil)
    redirect_to migrations_path(version: version)
  else
    response_to_action(message, nil, version, success ? "pending" : nil)
  end
end

#run_migrationObject



128
129
130
131
132
# File 'app/controllers/databasium/migrations_controller.rb', line 128

def run_migration
  version = run_migration_params[:version]
  @migration_service.run_migration(version)
  response_to_action("Migration run successfully", nil, version, "applied")
end

#run_pending_migrationsObject



74
75
76
77
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
# File 'app/controllers/databasium/migrations_controller.rb', line 74

def run_pending_migrations
  versions = @migration_service.run_pending_migrations
  message =
    if versions.any?
      "Pending migrations(#{versions.count}) run successfully"
    else
      "No pending migrations to run"
    end

  respond_to do |format|
    format.html { redirect_to migrations_path, notice: message }
    format.turbo_stream do
      streams = [
        turbo_stream.replace("error", Components::Databasium::Global::Error.new),
        turbo_stream.replace(
          "flash",
          Components::Databasium::Global::Flash.new(success: message, error: nil)
        )
      ]

      streams +=
        versions.map do |version|
          turbo_stream.replace(
            "migration_#{version}_status",
            Components::Databasium::Migrations::MigrationStatus.new(
              status: "applied",
              version: version
            )
          )
        end

      render turbo_stream: streams
    end
  end
end

#showObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/databasium/migrations_controller.rb', line 11

def show
  @migration = @migration_service.find_migration!(params[:id])
  @content = File.read(@migration.filename)

  respond_to do |format|
    format.html do
      render Components::Databasium::Migrations::File.new(
               migration: @migration,
               content: @content
             )
    end
    format.turbo_stream do
      render Components::Databasium::Migrations::ShowTurboStream.new(
               migration: @migration,
               content: @content
             ),
             layout: false
    end
  end
end


62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/databasium/migrations_controller.rb', line 62

def sidebar
  pagy, migrations =
    pagy(@migration_service.get_migrations(params[:search]), limit: 5, root_key: "migrations")
  pending_migrations = @migration_service.pending_migrations

  render Components::Databasium::SearchResults::Migrations.new(
           migrations: migrations,
           pending_migrations: pending_migrations,
           pagy: pagy
         )
end