oracle_db_script_producer 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/odsp.rb +125 -0
  3. metadata +59 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bcfa6e4b5d5905cd60043821712ac7226fc585db42024ca186bef28ee9d73e9d
4
+ data.tar.gz: 2e7c6705064f96973150b6aa35bf064f53742a4adec1c8dadc67f7b083eb9615
5
+ SHA512:
6
+ metadata.gz: 23f5652c09cca90b06d236ff2995c5d9d0a4a019b51cae3b5f040ed3fd4a25e8793436add6adc88b205ee1abad62fa8cdb91ae2b34a6b8a678de3addf0ce32e8
7
+ data.tar.gz: a9d5e3a5ce91aa3a1d361e1916cb231f3289373afb4d9162d11d5b511a51f2de34f82f753cbe9c915c88ed9b26ee8b2863360be024858fc57e318f6cb23e3ea0
data/lib/odsp.rb ADDED
@@ -0,0 +1,125 @@
1
+ class ODSP
2
+ require 'oci8'
3
+
4
+ def initialize(
5
+ save_config: {
6
+ make: { folder: 'make', op_file: 'make_all.sql'},
7
+ drop: { folder: 'drop', op_file: 'drop_all.sql'}
8
+ },
9
+ connections: { dest: nil, src: nil},
10
+ params: nil,
11
+ debug_opt: { time: true, console: true, commit: false, comment: true }
12
+ )
13
+ raise "Connections can not be defined as nil!" if connections.any? { |c| !c }
14
+ @connections = connections
15
+
16
+ @save_config = save_config
17
+ @params = params || default_params
18
+ @params.all? { |hsh| check_param_hsh(hsh) }
19
+ @debug_opt = debug_opt
20
+ end
21
+
22
+ def produce
23
+ @save_config.each do |op_name, config|
24
+ raise "#{op_name} op_file or folder name can not be nil!" unless config[:op_file] || config[:folder]
25
+
26
+ Dir.mkdir(config[:folder]) unless Dir.exist?(config[:folder])
27
+ File.open(config[:op_file], 'w') { |f| f.write("-- script cleaned at #{Time.new}\n") if @debug_opt[:comment] }
28
+ end
29
+
30
+ @params.each { |params| process_hsh(params) }
31
+ true
32
+ end
33
+
34
+ private
35
+ def check_param_hsh(hsh)
36
+ raise 'hsh can not be false or nil!' unless hsh
37
+ raise 'op_type can not be nil!' unless hsh[:op_type]
38
+ raise 'Invalid op_type configuration in save_options!' unless @save_config[hsh[:op_type]]
39
+ raise "Invalid folder name in save_options[#{hsh[:op_type]}]!" unless @save_config[hsh[:op_type]][:folder]
40
+ raise "Invalid op_file name in save_options[#{hsh[:op_type]}]!" unless @save_config[hsh[:op_type]][:op_file]
41
+ raise 'Parameter type can not be nil!' unless hsh[:type]
42
+ raise 'Query can not be nil!' unless hsh[:query]
43
+ raise 'Lambda operation can not be nil!' unless hsh[:lambda]
44
+ raise 'Connection type can not be nil!' unless @connections[hsh[:connection]]
45
+ true
46
+ end
47
+
48
+ def process_hsh(hsh)
49
+ start = Time.new
50
+ file_name = @save_config[hsh[:op_type]][:folder] + '/' + hsh[:type] + '.sql'
51
+
52
+ File.open(file_name, 'w') do |f|
53
+ p "running => #{hsh[:query]}" if @debug_opt[:console]
54
+ f.write("-- #{hsh[:type]} script produced at #{Time.new}.\n") if @debug_opt[:comment]
55
+ @connections[hsh[:connection]].exec(hsh[:query]) { |record| f.write(hsh[:lambda].call(record)) }
56
+ f.write("COMMIT;") if @debug_opt[:commit]
57
+ end
58
+ operation_file = @save_config[hsh[:op_type]][:op_file]
59
+
60
+ add_operation_file(operation_file, file_name) if operation_file
61
+ puts "=> '#{operation_file}/#{hsh[:type]}' script done. #{Time.new - start} sn" if @debug_opt[:time] && @debug_opt[:console]
62
+ end
63
+
64
+ def add_operation_file(operation_file_name, file_name)
65
+ puts "File '#{file_name}' record will be added into #{operation_file_name}" if @debug_opt[:console]
66
+ File.open(operation_file_name, 'a') { |f| f.write("@#{file_name}\n") }
67
+ end
68
+
69
+ def default_params
70
+ return [
71
+ {
72
+ type: 'index',
73
+ connection: :dest,
74
+ op_type: :drop,
75
+ lambda: lambda { |arr| "DROP INDEX #{@connections[:dest].username}.#{arr[0]};\n" },
76
+ query: "SELECT object_name FROM user_objects WHERE status = 'VALID' AND object_type = 'INDEX' ORDER BY CREATED, TIMESTAMP\n"
77
+ },
78
+ {
79
+ type: 'table',
80
+ connection: :dest,
81
+ op_type: :drop,
82
+ lambda: lambda { |arr| "DROP TABLE #{@connections[:dest].username}.#{arr[0]};\n" },
83
+ query: "SELECT object_name FROM user_objects WHERE status = 'VALID' AND object_type = 'TABLE' ORDER BY CREATED, TIMESTAMP\n"
84
+ },
85
+ {
86
+ type: 'sequence',
87
+ connection: :dest,
88
+ op_type: :drop,
89
+ lambda: lambda { |arr| "DROP SEQUENCE #{@connections[:dest].username}.#{arr[0]};\n" },
90
+ query: "SELECT object_name FROM user_objects WHERE status = 'VALID' AND object_type = 'SEQUENCE' ORDER BY CREATED, TIMESTAMP\n"
91
+ },
92
+ {
93
+ type: 'create_table',
94
+ connection: :src,
95
+ op_type: :make,
96
+ lambda: lambda { |arr| "-- original index create time: #{arr[0]}, last ddl time: #{arr[1]}\n" +
97
+ arr[2].read.strip.gsub("\"#{@connections[:src].username}\"", "\"#{@connections[:dest].username}\"") .gsub(/\n TABLESPACE \"[^"]*\"/,'') + ";\n\n" },
98
+ query: "SELECT CREATED, LAST_DDL_TIME, DBMS_METADATA.GET_DDL(object_type, object_name, '#{@connections[:src].username}') val FROM user_objects WHERE status = 'VALID' AND object_type = 'TABLE' ORDER BY CREATED, TIMESTAMP\n"
99
+ },
100
+ {
101
+ type: 'insert',
102
+ connection: :src,
103
+ op_type: :make,
104
+ lambda: lambda { |arr| "INSERT INTO #{@connections[:dest].username}.#{arr[0]} \nSELECT * FROM #{@connections[:src].username}.#{arr[0]};\n\n" },
105
+ query: "SELECT object_name val FROM user_objects WHERE status = 'VALID' AND object_type = 'TABLE' ORDER BY CREATED, TIMESTAMP\n"
106
+ },
107
+ {
108
+ type: 'sequence',
109
+ connection: :src,
110
+ op_type: :make,
111
+ lambda: lambda { |arr| "-- original index create time: #{arr[0]}, last ddl time: #{arr[1]}\n" +
112
+ arr[2].read.strip.gsub("\"#{@connections[:src].username}\"", "\"#{@connections[:dest].username}\"") + ";\n" },
113
+ query: "SELECT CREATED, LAST_DDL_TIME, DBMS_METADATA.GET_DDL(object_type, object_name, '#{@connections[:src].username}') val FROM user_objects WHERE status = 'VALID' AND object_type = 'SEQUENCE' ORDER BY CREATED, TIMESTAMP\n"
114
+ },
115
+ {
116
+ type: 'index',
117
+ connection: :src,
118
+ op_type: :make,
119
+ lambda: lambda { |arr| "-- original index create time: #{arr[0]}, last ddl time: #{arr[1]}\n" +
120
+ arr[2].read.strip.gsub("\"#{@connections[:src].username}\"", "\"#{@connections[:dest].username}\"").gsub(/\n TABLESPACE \"[^"]*\"/,'') + ";\n\n"},
121
+ query: "SELECT CREATED, LAST_DDL_TIME, DBMS_METADATA.GET_DDL(object_type, object_name, '#{@connections[:src].username}') val FROM user_objects WHERE status = 'VALID' AND object_type = 'INDEX' ORDER BY CREATED, TIMESTAMP\n"
122
+ }
123
+ ]
124
+ end
125
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oracle_db_script_producer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - tayak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-oci8
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.2.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.2.2
27
+ description: SQL script tool that can programmatically create create, insert, update,
28
+ delete, trunc and similar queries for Oracle database.
29
+ email: yasir.kiroglu@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/odsp.rb
35
+ homepage: https://github.com/taiak/db_refresher
36
+ licenses:
37
+ - Apache-2.0
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.5.3
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: SQL script tool that can programmatically create create, insert, update,
58
+ delete, trunc and similar queries for Oracle database.
59
+ test_files: []