|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +# Called by bin/generate_sql_schemas with TARGET_DB environment variable set |
| 5 | + |
| 6 | +require "bundler/setup" |
| 7 | +require "fileutils" |
| 8 | + |
| 9 | +# Change to test/dummy directory |
| 10 | +dummy_dir = File.expand_path("../test/dummy", __dir__) |
| 11 | +Dir.chdir(dummy_dir) |
| 12 | + |
| 13 | +# Load the test/dummy Rails app |
| 14 | +APP_PATH = File.expand_path("config/application", Dir.pwd) |
| 15 | +require File.expand_path("config/boot", Dir.pwd) |
| 16 | +require APP_PATH |
| 17 | +Dummy::Application.load_tasks |
| 18 | + |
| 19 | +# Patch Rails to use Docker containers for mysqldump/pg_dump for repeatable results |
| 20 | +module DockerDatabaseCommands |
| 21 | + private |
| 22 | + def run_cmd(cmd, *args, **opts) |
| 23 | + case cmd |
| 24 | + when "mysqldump" |
| 25 | + database, output_file = extract_database_and_output_file(args, file_arg: "--result-file") |
| 26 | + docker_cmd = ["docker", "exec", "solid_cache-mysql-1", "mysqldump", |
| 27 | + "--set-gtid-purged=OFF", "--no-data", "--routines", "--skip-comments", database] |
| 28 | + |
| 29 | + fail run_cmd_error(cmd, args, "structure_dump") unless Kernel.system(*docker_cmd, out: output_file, **opts) |
| 30 | + when "pg_dump" |
| 31 | + database, output_file = extract_database_and_output_file(args, file_arg: "--file") |
| 32 | + docker_cmd = ["docker", "exec", "solid_cache-postgres-1", "pg_dump", |
| 33 | + "-U", "postgres", "--schema-only", "--no-privileges", "--no-owner", database] |
| 34 | + |
| 35 | + fail run_cmd_error(cmd, args) unless Kernel.system(*docker_cmd, out: output_file, **opts) |
| 36 | + else |
| 37 | + super |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + def extract_database_and_output_file(args, file_arg:) |
| 42 | + args = args.flatten |
| 43 | + database = args.last |
| 44 | + output_file = nil |
| 45 | + |
| 46 | + args.each_with_index do |arg, i| |
| 47 | + if arg == file_arg |
| 48 | + output_file = args[i + 1] |
| 49 | + break |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + [database, output_file] |
| 54 | + end |
| 55 | +end |
| 56 | + |
| 57 | +ActiveRecord::Tasks::MySQLDatabaseTasks.prepend(DockerDatabaseCommands) |
| 58 | +ActiveRecord::Tasks::PostgreSQLDatabaseTasks.prepend(DockerDatabaseCommands) |
| 59 | + |
| 60 | +OUTPUT_FILES = { |
| 61 | + "sqlite" => "cache_structure.sqlite3.sql", |
| 62 | + "mysql" => "cache_structure.mysql.sql", |
| 63 | + "postgres" => "cache_structure.postgresql.sql" |
| 64 | +} |
| 65 | + |
| 66 | +target_db = ENV["TARGET_DB"] || "sqlite" |
| 67 | +output_file = OUTPUT_FILES[target_db] |
| 68 | + |
| 69 | +unless output_file |
| 70 | + puts " ✗ Error: Unknown TARGET_DB: #{target_db}" |
| 71 | + exit 1 |
| 72 | +end |
| 73 | + |
| 74 | +templates_dir = File.expand_path("../../lib/generators/solid_cache/install/templates/db", Dir.pwd) |
| 75 | +output_path = File.join(templates_dir, output_file) |
| 76 | +cache_schema_path = File.join(templates_dir, "cache_schema.rb") |
| 77 | + |
| 78 | +begin |
| 79 | + puts " Creating database..." |
| 80 | + Rake::Task["db:create"].invoke |
| 81 | + |
| 82 | + puts " Loading Ruby schema..." |
| 83 | + load cache_schema_path |
| 84 | + |
| 85 | + puts " Dumping structure..." |
| 86 | + db_config = ActiveRecord::Base.connection_db_config |
| 87 | + ActiveRecord::Tasks::DatabaseTasks.structure_dump(db_config, output_path) |
| 88 | + |
| 89 | + puts " ✓ Saved to #{output_file}" |
| 90 | +rescue => e |
| 91 | + puts " ✗ Error: #{e.message}" |
| 92 | + puts " #{e.backtrace.first(5).join("\n ")}" |
| 93 | + exit 1 |
| 94 | +end |
0 commit comments