Skip to content

Commit

Permalink
Refactor change generator database implementation
Browse files Browse the repository at this point in the history
The database object does not need to know about the other packages in the Dockerfile. This info is only needed for the change generator so lets move it there.
  • Loading branch information
andrewn617 committed May 14, 2024
1 parent 0be846c commit 764489b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 52 deletions.
42 changes: 0 additions & 42 deletions railties/lib/rails/generators/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ def gem
raise NotImplementedError
end

def docker_base
raise NotImplementedError
end

def docker_build
raise NotImplementedError
end

def base_package
raise NotImplementedError
end
Expand Down Expand Up @@ -125,14 +117,6 @@ def gem
["mysql2", ["~> 0.5"]]
end

def docker_base
"curl default-mysql-client libvips"
end

def docker_build
"build-essential default-libmysqlclient-dev git"
end

def base_package
"default-mysql-client"
end
Expand Down Expand Up @@ -172,14 +156,6 @@ def gem
["pg", ["~> 1.1"]]
end

def docker_base
"curl libvips postgresql-client"
end

def docker_build
"build-essential git libpq-dev"
end

def base_package
"postgresql-client"
end
Expand Down Expand Up @@ -220,14 +196,6 @@ def gem
["trilogy", ["~> 2.7"]]
end

def docker_base
"curl libvips"
end

def docker_build
"build-essential git"
end

def base_package
nil
end
Expand Down Expand Up @@ -258,14 +226,6 @@ def gem
["sqlite3", [">= 1.4"]]
end

def docker_base
"curl libsqlite3-0 libvips"
end

def docker_build
"build-essential git"
end

def base_package
"libsqlite3-0"
end
Expand All @@ -284,8 +244,6 @@ def name; end
def service; end
def port; end
def volume; end
def docker_base; end
def docker_build; end
def base_package; end
def build_package; end
def feature_name; end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module System
class ChangeGenerator < Base # :nodoc:
include AppName

BASE_PACKAGES = %w( curl libvips )
BUILD_PACKAGES = %w( build-essential git )

class_option :to, required: true,
desc: "The database system to switch to."

Expand Down Expand Up @@ -45,14 +48,8 @@ def edit_dockerfile
dockerfile_path = File.expand_path("Dockerfile", destination_root)
return unless File.exist?(dockerfile_path)

base_name = database.docker_base
build_name = database.docker_build
if base_name
gsub_file("Dockerfile", all_docker_bases_regex, base_name)
end
if build_name
gsub_file("Dockerfile", all_docker_builds_regex, build_name)
end
gsub_file("Dockerfile", all_docker_bases_regex, docker_base_packages(database.base_package))
gsub_file("Dockerfile", all_docker_builds_regex, docker_build_packages(database.build_package))
end

def edit_devcontainer_files
Expand All @@ -69,11 +66,27 @@ def all_database_gems
end

def all_docker_bases
Database.all.filter_map { |database| database.docker_base }
Database.all.map { |database| docker_base_packages(database.base_package) }.uniq
end

def docker_base_packages(database_package)
if database_package
[database_package].concat(BASE_PACKAGES).sort
else
BASE_PACKAGES
end.join("\s")
end

def all_docker_builds
Database.all.filter_map { |database| database.docker_build }
Database.all.map { |database| docker_build_packages(database.build_package) }.uniq
end

def docker_build_packages(database_package)
if database_package
[database_package].concat(BUILD_PACKAGES).sort
else
BUILD_PACKAGES
end.join("\s")
end

def all_database_gems_regex
Expand Down

0 comments on commit 764489b

Please sign in to comment.