Class: ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter::DatabaseTasks

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

Constant Summary collapse

ORACLE_IDENTIFIER =
/\A[[:alpha:]][\w$#]*\z/
ORACLE_PRIVILEGE =

GRANT operand: one or more space-separated tokens consisting of word chars only. Covers system privileges (e.g. “create session”, “CREATE ANY TABLE”) and simple role names. Rejects separators such as ‘;`, `–`, `’‘, `“`, as well as newlines/tabs — anything that could turn a single GRANT into multiple statements or otherwise make the operand hard to reason about.

/\A\w+( \w+)*\z/

Instance Method Summary collapse

Instance Method Details

#createObject



19
20
21
22
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
# File 'lib/active_record/connection_adapters/oracle_enhanced/database_tasks.rb', line 19

def create
  username = configuration_hash[:username].to_s
  unless username.match?(ORACLE_IDENTIFIER)
    raise ArgumentError, "Invalid Oracle identifier for :username: #{username.inspect}"
  end
  OracleEnhancedAdapter.permissions.each do |permission|
    unless permission.to_s.match?(ORACLE_PRIVILEGE)
      raise ArgumentError, "Invalid Oracle privilege in OracleEnhancedAdapter.permissions: #{permission.inspect}"
    end
  end
  # Oracle Autonomous Database (OCI) ships with ADMIN -- not SYSTEM --
  # as the predefined administrative user, so the caller needs a way
  # to override the default. See
  # https://docs.oracle.com/en-us/iaas/autonomous-database-serverless/doc/autonomous-admin-user-roles.html
  system_username = ENV["ORACLE_SYSTEM_USER"].presence || "SYSTEM"
  unless system_username.match?(ORACLE_IDENTIFIER)
    raise ArgumentError, "Invalid Oracle identifier for ORACLE_SYSTEM_USER: #{system_username.inspect}"
  end
  quoted_password = %("#{configuration_hash[:password].to_s.gsub('"', '""')}")

  system_password = ENV.fetch("ORACLE_SYSTEM_PASSWORD") {
    print "Please provide the #{system_username} password for your Oracle installation (set ORACLE_SYSTEM_PASSWORD to avoid this prompt)\n>"
    $stdin.gets.strip
  }
  establish_connection(configuration_hash.merge(username: system_username, password: system_password))
  begin
    connection.execute "CREATE USER #{username} IDENTIFIED BY #{quoted_password}"
  rescue => e
    if /ORA-01920/.match?(e.message) # user name conflicts with another user or role name
      connection.execute "ALTER USER #{username} IDENTIFIED BY #{quoted_password}"
    else
      raise e
    end
  end

  OracleEnhancedAdapter.permissions.each do |permission|
    connection.execute "GRANT #{permission} TO #{username}"
  end
end

#dropObject



59
60
61
62
# File 'lib/active_record/connection_adapters/oracle_enhanced/database_tasks.rb', line 59

def drop
  establish_connection
  connection.execute_structure_dump(connection.full_drop)
end

#purgeObject



64
65
66
67
# File 'lib/active_record/connection_adapters/oracle_enhanced/database_tasks.rb', line 64

def purge
  drop
  connection.execute("PURGE RECYCLEBIN") rescue nil
end

#structure_dump(filename, extra_flags) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/active_record/connection_adapters/oracle_enhanced/database_tasks.rb', line 69

def structure_dump(filename, extra_flags)
  establish_connection
  File.open(filename, "w:utf-8") { |f| f << connection.structure_dump }
  if configuration_hash[:structure_dump] == "db_stored_code"
    File.open(filename, "a:utf-8") { |f| f << connection.structure_dump_db_stored_code }
  end
end

#structure_load(filename, extra_flags) ⇒ Object



77
78
79
80
# File 'lib/active_record/connection_adapters/oracle_enhanced/database_tasks.rb', line 77

def structure_load(filename, extra_flags)
  establish_connection
  connection.execute_structure_dump(File.read(filename))
end