|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "cases/helper" |
| 4 | +require "active_record/tasks/database_tasks" |
| 5 | +require "active_record/connection_adapters/cockroachdb/database_tasks" |
| 6 | + |
| 7 | +module ActiveRecord |
| 8 | + class CockroachDBStructureDumpTest < ActiveRecord::TestCase |
| 9 | + def setup |
| 10 | + @configuration = { |
| 11 | + "adapter" => "cockroachdb", |
| 12 | + "database" => "my-app-db" |
| 13 | + } |
| 14 | + @filename = "/tmp/awesome-file.sql" |
| 15 | + FileUtils.touch(@filename) |
| 16 | + end |
| 17 | + |
| 18 | + def teardown |
| 19 | + FileUtils.rm_f(@filename) |
| 20 | + end |
| 21 | + |
| 22 | + def test_structure_dump |
| 23 | + assert_equal "", File.read(@filename) |
| 24 | + File.write(@filename, "NOT TODAY\n") |
| 25 | + |
| 26 | + config = @configuration.dup |
| 27 | + config["database"] = ARTest.config["connections"]["cockroachdb"]["arunit"]["database"] |
| 28 | + |
| 29 | + begin |
| 30 | + ActiveRecord::Base.connection.execute(<<~SQL) |
| 31 | + CREATE TYPE IF NOT EXISTS status AS ENUM ('open', 'closed', 'inactive'); |
| 32 | + SQL |
| 33 | + assert_called( |
| 34 | + ActiveRecord::SchemaDumper, |
| 35 | + :ignore_tables, |
| 36 | + returns: ["accounts", "articles"] |
| 37 | + ) do |
| 38 | + ActiveRecord::Tasks::DatabaseTasks.structure_dump(config, @filename) |
| 39 | + |
| 40 | + read = File.read(@filename) |
| 41 | + end |
| 42 | + ensure |
| 43 | + ActiveRecord::Base.connection.execute(<<~SQL) |
| 44 | + DROP TYPE IF EXISTS status; |
| 45 | + SQL |
| 46 | + end |
| 47 | + |
| 48 | + read = File.read(@filename) |
| 49 | + refute read.include?("NOT TODAY"), "The dump file previous content was not overwritten" |
| 50 | + assert read.include?("CREATE SCHEMA public;"), "Schemas are not dumped" |
| 51 | + assert read.include?("CREATE TYPE public.status AS ENUM ('open', 'closed', 'inactive');"), "Types are not dumped" |
| 52 | + assert read.include?("CREATE TABLE public.schema_migrations"), "No dump done" |
| 53 | + refute read.include?("CREATE TABLE public.articles ("), "\"articles\" table should be ignored" |
| 54 | + refute read.include?("CREATE TABLE public.accounts ("), "\"accounts\" table should be ignored" |
| 55 | + end |
| 56 | + end |
| 57 | +end |
0 commit comments