Module: ActiveRecord::ConnectionAdapters::OracleEnhancedStructureDump
- Defined in:
- lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb
Overview
:nodoc:
Constant Summary collapse
- STATEMENT_TOKEN =
Statements separator used in structure dump to allow loading of structure dump also with SQL*Plus
"\n\n/\n\n"
Instance Method Summary collapse
-
#add_column_options!(sql, options) ⇒ Object
:nodoc:.
-
#dump_schema_information ⇒ Object
:nodoc:.
- #execute_structure_dump(string) ⇒ Object
-
#full_drop(preserve_tables = false) ⇒ Object
:nodoc:.
-
#structure_drop ⇒ Object
:nodoc:.
-
#structure_dump ⇒ Object
:nodoc:.
-
#structure_dump_column(column) ⇒ Object
:nodoc:.
-
#structure_dump_db_stored_code ⇒ Object
Extract all stored procedures, packages, synonyms and views.
-
#structure_dump_fk_constraints ⇒ Object
:nodoc:.
-
#structure_dump_indexes(table_name) ⇒ Object
:nodoc:.
-
#structure_dump_primary_key(table) ⇒ Object
:nodoc:.
-
#structure_dump_unique_keys(table) ⇒ Object
:nodoc:.
-
#structure_dump_virtual_column(column, data_default) ⇒ Object
:nodoc:.
-
#temp_table_drop ⇒ Object
:nodoc:.
Instance Method Details
#add_column_options!(sql, options) ⇒ Object
:nodoc:
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb', line 217 def (sql, ) #:nodoc: type = [:type] || ((column = [:column]) && column.type) type = type && type.to_sym # handle case of defaults for CLOB columns, which would otherwise get "quoted" incorrectly if () if type == :text sql << " DEFAULT #{quote([:default])}" else # from abstract adapter sql << " DEFAULT #{quote([:default], [:column])}" end end # must explicitly add NULL or NOT NULL to allow change_column to work on migrations if [:null] == false sql << " NOT NULL" elsif [:null] == true sql << " NULL" unless type == :primary_key end end |
#dump_schema_information ⇒ Object
:nodoc:
137 138 139 140 141 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb', line 137 def dump_schema_information #:nodoc: sm_table = ActiveRecord::Migrator.schema_migrations_table_name migrated = select_values("SELECT version FROM #{sm_table}") join_with_statement_token(migrated.map{|v| "INSERT INTO #{sm_table} (version) VALUES ('#{v}')" }) end |
#execute_structure_dump(string) ⇒ Object
237 238 239 240 241 242 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb', line 237 def execute_structure_dump(string) string.split(STATEMENT_TOKEN).each do |ddl| ddl.chop! if ddl.last == ";" execute(ddl) unless ddl.blank? end end |
#full_drop(preserve_tables = false) ⇒ Object
:nodoc:
204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb', line 204 def full_drop(preserve_tables=false) #:nodoc: s = preserve_tables ? [] : [structure_drop] s << temp_table_drop if preserve_tables s << drop_sql_for_feature("view") s << drop_sql_for_feature("materialized view") s << drop_sql_for_feature("synonym") s << drop_sql_for_feature("type") s << drop_sql_for_object("package") s << drop_sql_for_object("function") s << drop_sql_for_object("procedure") s.join end |
#structure_drop ⇒ Object
:nodoc:
182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb', line 182 def structure_drop #:nodoc: statements = select_values("SELECT sequence_name FROM user_sequences ORDER BY 1").map do |seq| "DROP SEQUENCE \"#{seq}\"" end select_values("SELECT table_name from all_tables t WHERE owner = SYS_CONTEXT('userenv', 'current_schema') AND secondary = 'N' AND NOT EXISTS (SELECT mv.mview_name FROM all_mviews mv WHERE mv.owner = t.owner AND mv.mview_name = t.table_name) AND NOT EXISTS (SELECT mvl.log_table FROM all_mview_logs mvl WHERE mvl.log_owner = t.owner AND mvl.log_table = t.table_name) ORDER BY 1").each do |table| statements << "DROP TABLE \"#{table}\" CASCADE CONSTRAINTS" end join_with_statement_token(statements) end |
#structure_dump ⇒ Object
:nodoc:
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb', line 8 def structure_dump #:nodoc: structure = select_values("SELECT sequence_name FROM user_sequences ORDER BY 1").map do |seq| "CREATE SEQUENCE \"#{seq}\"" end select_values("SELECT table_name FROM all_tables t WHERE owner = SYS_CONTEXT('userenv', 'current_schema') AND secondary = 'N' AND NOT EXISTS (SELECT mv.mview_name FROM all_mviews mv WHERE mv.owner = t.owner AND mv.mview_name = t.table_name) AND NOT EXISTS (SELECT mvl.log_table FROM all_mview_logs mvl WHERE mvl.log_owner = t.owner AND mvl.log_table = t.table_name) ORDER BY 1").each do |table_name| virtual_columns = virtual_columns_for(table_name) ddl = "CREATE#{ ' GLOBAL TEMPORARY' if temporary_table?(table_name)} TABLE \"#{table_name}\" (\n" cols = select_all(%Q{ SELECT column_name, data_type, data_length, char_used, char_length, data_precision, data_scale, data_default, nullable FROM user_tab_columns WHERE table_name = '#{table_name}' ORDER BY column_id }).map do |row| if(v = virtual_columns.find {|col| col['column_name'] == row['column_name']}) structure_dump_virtual_column(row, v['data_default']) else structure_dump_column(row) end end ddl << cols.join(",\n ") ddl << structure_dump_primary_key(table_name) ddl << "\n)" structure << ddl structure << structure_dump_indexes(table_name) structure << structure_dump_unique_keys(table_name) end join_with_statement_token(structure) << structure_dump_fk_constraints end |
#structure_dump_column(column) ⇒ Object
:nodoc:
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb', line 42 def structure_dump_column(column) #:nodoc: col = "\"#{column['column_name']}\" #{column['data_type']}" if column['data_type'] =='NUMBER' and !column['data_precision'].nil? col << "(#{column['data_precision'].to_i}" col << ",#{column['data_scale'].to_i}" if !column['data_scale'].nil? col << ')' elsif column['data_type'].include?('CHAR') length = column['char_used'] == 'C' ? column['char_length'].to_i : column['data_length'].to_i col << "(#{length})" end col << " DEFAULT #{column['data_default']}" if !column['data_default'].nil? col << ' NOT NULL' if column['nullable'] == 'N' col end |
#structure_dump_db_stored_code ⇒ Object
Extract all stored procedures, packages, synonyms and views.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb', line 144 def structure_dump_db_stored_code #:nodoc: structure = [] select_all("SELECT DISTINCT name, type FROM all_source WHERE type IN ('PROCEDURE', 'PACKAGE', 'PACKAGE BODY', 'FUNCTION', 'TRIGGER', 'TYPE') AND name NOT LIKE 'BIN$%' AND owner = SYS_CONTEXT('userenv', 'current_schema') ORDER BY type").each do |source| ddl = "CREATE OR REPLACE \n" lines = select_all(%Q{ SELECT text FROM all_source WHERE name = '#{source['name']}' AND type = '#{source['type']}' AND owner = SYS_CONTEXT('userenv', 'current_schema') ORDER BY line }).map do |row| ddl << row['text'] end ddl << ";" unless ddl.strip[-1,1] == ";" structure << ddl end # export views select_all("SELECT view_name, text FROM user_views").each do |view| structure << "CREATE OR REPLACE VIEW #{view['view_name']} AS\n #{view['text']}" end # export synonyms select_all("SELECT owner, synonym_name, table_name, table_owner FROM all_synonyms WHERE owner = SYS_CONTEXT('userenv', 'current_schema') ").each do |synonym| structure << "CREATE OR REPLACE #{synonym['owner'] == 'PUBLIC' ? 'PUBLIC' : '' } SYNONYM #{synonym['synonym_name']}" structure << " FOR #{synonym['table_owner']}.#{synonym['table_name']}" end join_with_statement_token(structure) end |
#structure_dump_fk_constraints ⇒ Object
:nodoc:
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb', line 125 def structure_dump_fk_constraints #:nodoc: fks = select_all("SELECT table_name FROM all_tables WHERE owner = SYS_CONTEXT('userenv', 'current_schema') ORDER BY 1").map do |table| if respond_to?(:foreign_keys) && (foreign_keys = foreign_keys(table["table_name"])).any? foreign_keys.map do |fk| sql = "ALTER TABLE #{quote_table_name(fk.from_table)} ADD CONSTRAINT #{quote_column_name(fk.[:name])} " sql << "#{foreign_key_definition(fk.to_table, fk.)}" end end end.flatten.compact join_with_statement_token(fks) end |
#structure_dump_indexes(table_name) ⇒ Object
:nodoc:
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb', line 109 def structure_dump_indexes(table_name) #:nodoc: indexes(table_name).map do || column_names = [:columns] = {:name => [:name], :unique => [:unique]} index_name = index_name(table_name, :column => column_names) if Hash === # legacy support, since this param was a string index_type = [:unique] ? "UNIQUE" : "" index_name = [:name] || index_name else index_type = end quoted_column_names = column_names.map { |e| quote_column_name(e) }.join(", ") "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} (#{quoted_column_names})" end end |
#structure_dump_primary_key(table) ⇒ Object
:nodoc:
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb', line 71 def structure_dump_primary_key(table) #:nodoc: opts = {:name => '', :cols => []} pks = select_all(<<-SQL, "Primary Keys") SELECT a.constraint_name, a.column_name, a.position FROM user_cons_columns a JOIN user_constraints c ON a.constraint_name = c.constraint_name WHERE c.table_name = '#{table.upcase}' AND c.constraint_type = 'P' AND c.owner = SYS_CONTEXT('userenv', 'current_schema') SQL pks.each do |row| opts[:name] = row['constraint_name'] opts[:cols][row['position']-1] = row['column_name'] end opts[:cols].length > 0 ? ",\n CONSTRAINT #{opts[:name]} PRIMARY KEY (#{opts[:cols].join(',')})" : '' end |
#structure_dump_unique_keys(table) ⇒ Object
:nodoc:
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb', line 89 def structure_dump_unique_keys(table) #:nodoc: keys = {} uks = select_all(<<-SQL, "Primary Keys") SELECT a.constraint_name, a.column_name, a.position FROM user_cons_columns a JOIN user_constraints c ON a.constraint_name = c.constraint_name WHERE c.table_name = '#{table.upcase}' AND c.constraint_type = 'U' AND c.owner = SYS_CONTEXT('userenv', 'current_schema') SQL uks.each do |uk| keys[uk['constraint_name']] ||= [] keys[uk['constraint_name']][uk['position']-1] = uk['column_name'] end keys.map do |k,v| "ALTER TABLE #{table.upcase} ADD CONSTRAINT #{k} UNIQUE (#{v.join(',')})" end end |
#structure_dump_virtual_column(column, data_default) ⇒ Object
:nodoc:
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb', line 57 def structure_dump_virtual_column(column, data_default) #:nodoc: data_default = data_default.gsub(/"/, '') col = "\"#{column['column_name']}\" #{column['data_type']}" if column['data_type'] =='NUMBER' and !column['data_precision'].nil? col << "(#{column['data_precision'].to_i}" col << ",#{column['data_scale'].to_i}" if !column['data_scale'].nil? col << ')' elsif column['data_type'].include?('CHAR') length = column['char_used'] == 'C' ? column['char_length'].to_i : column['data_length'].to_i col << "(#{length})" end col << " GENERATED ALWAYS AS (#{data_default}) VIRTUAL" end |
#temp_table_drop ⇒ Object
:nodoc:
196 197 198 199 200 201 202 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_structure_dump.rb', line 196 def temp_table_drop #:nodoc: join_with_statement_token(select_values( "SELECT table_name FROM all_tables WHERE owner = SYS_CONTEXT('userenv', 'current_schema') AND secondary = 'N' AND temporary = 'Y' ORDER BY 1").map do |table| "DROP TABLE \"#{table}\" CASCADE CONSTRAINTS" end) end |