Class: ActiveRecord::ConnectionAdapters::CockroachDB::DatabaseTasks

Inherits:
Tasks::PostgreSQLDatabaseTasks
  • Object
show all
Defined in:
lib/active_record/connection_adapters/cockroachdb/database_tasks.rb

Instance Method Summary collapse

Instance Method Details

#structure_dump(filename, extra_flags = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/active_record/connection_adapters/cockroachdb/database_tasks.rb', line 23

def structure_dump(filename, extra_flags=nil)
  if extra_flags
    raise "No flag supported yet, please raise an issue if needed. " \
      "https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/new"
  end

  # "See https://github.com/cockroachdb/cockroach/issues/26443."
  search_path =
    case ActiveRecord.dump_schemas
    when :schema_search_path
      configuration_hash[:schema_search_path]
    when :all
      nil
    when String
      ActiveRecord.dump_schemas
    end

  conn = ActiveRecord::Base.lease_connection
  begin
    old_search_path = conn.schema_search_path
    conn.schema_search_path = search_path
    File.open(filename, "w") do |file|
      # NOTE: There is no issue with the crdb_internal schema, it is ignored by SHOW CREATE.
      %w(SCHEMAS TYPES).each do |object_kind|
        conn.execute("SHOW CREATE ALL #{object_kind}").each_row { file.puts _1 }
      end

      ignore_tables = ActiveRecord::SchemaDumper.ignore_tables.to_set

      conn.execute("SHOW CREATE ALL TABLES").each_row do |(sql)|
        if sql.start_with?("CREATE")
          table_name = sql[/CREATE TABLE (?:.*?\.)?\"?(.*?)[\" ]/, 1]
          next if ignore_tables.member?(table_name)
        elsif sql.start_with?("ALTER")
          table_name = sql[/ALTER TABLE (?:.*?\.)?\"?(.*?)[\" ]/, 1]
          ref_table_name = sql[/REFERENCES (?:.*?\.)?\"?(.*?)[\" ]/, 1]
          next if ignore_tables.member?(table_name) || ignore_tables.member?(ref_table_name)
        end

        file.puts sql
      end
      file.puts "SET search_path TO #{conn.schema_search_path};\n\n"
    end
  ensure
    conn.schema_search_path = old_search_path
  end
end

#structure_load(filename, extra_flags = nil) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/active_record/connection_adapters/cockroachdb/database_tasks.rb', line 71

def structure_load(filename, extra_flags=nil)
  if extra_flags
    raise "No flag supported yet, please raise an issue if needed. " \
      "https://github.com/cockroachdb/activerecord-cockroachdb-adapter/issues/new"
  end

  run_cmd("cockroach", ["sql", "--set", "errexit=false", "--file", filename], "loading")
end