Class: ActiveRecord::Tasks::PostgreSQLDatabaseTasks
Do not use. This class is for internal use only.
Relationships & Source Files | |
Inherits: | Object |
Defined in: | activerecord/lib/active_record/tasks/postgresql_database_tasks.rb |
Constant Summary
-
DEFAULT_ENCODING =
# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 8ENV["CHARSET"] || "utf8"
-
ON_ERROR_STOP_1 =
# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 9"ON_ERROR_STOP=1"
-
SQL_COMMENT_BEGIN =
# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 10"--"
Class Attribute Summary
Class Method Summary
- .new(db_config) ⇒ PostgreSQLDatabaseTasks constructor
Instance Attribute Summary
- #configuration_hash readonly private
- #db_config readonly private
Instance Method Summary
- #charset
- #collation
- #create(connection_already_established = false)
- #drop
- #purge
- #structure_dump(filename, extra_flags)
- #structure_load(filename, extra_flags)
- #connection private
- #encoding private
- #establish_connection(config = db_config) private
- #psql_env private
- #public_schema_config private
- #remove_sql_header_comments(filename) private
- #run_cmd(cmd, args, action) private
- #run_cmd_error(cmd, args, action) private
Constructor Details
.new(db_config) ⇒ PostgreSQLDatabaseTasks
# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 16
def initialize(db_config) @db_config = db_config @configuration_hash = db_config.configuration_hash end
Class Attribute Details
.using_database_configurations? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 12
def self.using_database_configurations? true end
Instance Attribute Details
#configuration_hash (readonly, private)
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 88
attr_reader :db_config, :configuration_hash
#db_config (readonly, private)
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 88
attr_reader :db_config, :configuration_hash
Instance Method Details
#charset
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 32
def charset connection.encoding end
#collation
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 36
def collation connection.collation end
#connection (private)
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 90
def connection ActiveRecord::Base.lease_connection end
#create(connection_already_established = false)
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 21
def create(connection_already_established = false) establish_connection(public_schema_config) unless connection_already_established connection.create_database(db_config.database, configuration_hash.merge(encoding: encoding)) establish_connection end
#drop
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 27
def drop establish_connection(public_schema_config) connection.drop_database(db_config.database) end
#encoding (private)
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 98
def encoding configuration_hash[:encoding] || DEFAULT_ENCODING end
#establish_connection(config = db_config) (private)
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 94
def establish_connection(config = db_config) ActiveRecord::Base.establish_connection(config) end
#psql_env (private)
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 106
def psql_env {}.tap do |env| env["PGHOST"] = db_config.host if db_config.host env["PGPORT"] = configuration_hash[:port].to_s if configuration_hash[:port] env["PGPASSWORD"] = configuration_hash[:password].to_s if configuration_hash[:password] env["PGUSER"] = configuration_hash[:username].to_s if configuration_hash[:username] env["PGSSLMODE"] = configuration_hash[:sslmode].to_s if configuration_hash[:sslmode] env["PGSSLCERT"] = configuration_hash[:sslcert].to_s if configuration_hash[:sslcert] env["PGSSLKEY"] = configuration_hash[:sslkey].to_s if configuration_hash[:sslkey] env["PGSSLROOTCERT"] = configuration_hash[:sslrootcert].to_s if configuration_hash[:sslrootcert] end end
#public_schema_config (private)
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 102
def public_schema_config configuration_hash.merge(database: "postgres", schema_search_path: "public") end
#purge
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 40
def purge ActiveRecord::Base.connection_handler.clear_active_connections!(:all) drop create true end
#remove_sql_header_comments(filename) (private)
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 130
def remove_sql_header_comments(filename) removing_comments = true tempfile = Tempfile.open("uncommented_structure.sql") begin File.foreach(filename) do |line| unless removing_comments && (line.start_with?(SQL_COMMENT_BEGIN) || line.blank?) tempfile << line removing_comments = false end end ensure tempfile.close end FileUtils.cp(tempfile.path, filename) end
#run_cmd(cmd, args, action) (private)
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 119
def run_cmd(cmd, args, action) fail run_cmd_error(cmd, args, action) unless Kernel.system(psql_env, cmd, *args) end
#run_cmd_error(cmd, args, action) (private)
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 123
def run_cmd_error(cmd, args, action) msg = +"failed to execute:\n" msg << "#{cmd} #{args.join(' ')}\n\n" msg << "Please check the output above for any errors and make sure that `#{cmd}` is installed in your PATH and has proper permissions.\n\n" msg end
#structure_dump(filename, extra_flags)
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 46
def structure_dump(filename, extra_flags) search_path = \ case ActiveRecord.dump_schemas when :schema_search_path configuration_hash[:schema_search_path] when :all nil when String ActiveRecord.dump_schemas end args = ["--schema-only", "--no-privileges", "--no-owner"] args.concat(["--file", filename]) args.concat(Array(extra_flags)) if extra_flags unless search_path.blank? args += search_path.split(",").map do |part| "--schema=#{part.strip}" end end ignore_tables = ActiveRecord::SchemaDumper.ignore_tables if ignore_tables.any? ignore_tables = connection.data_sources.select { |table| ignore_tables.any? { |pattern| pattern === table } } args += ignore_tables.flat_map { |table| ["-T", table] } end args << db_config.database run_cmd("pg_dump", args, "dumping") remove_sql_header_comments(filename) File.open(filename, "a") { |f| f << "SET search_path TO #{connection.schema_search_path};\n\n" } end
#structure_load(filename, extra_flags)
[ GitHub ]# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 80
def structure_load(filename, extra_flags) args = ["--set", ON_ERROR_STOP_1, "--quiet", "--no-psqlrc", "--output", File::NULL, "--file", filename] args.concat(Array(extra_flags)) if extra_flags args << db_config.database run_cmd("psql", args, "loading") end