activerecord-jdbc-adapter-ficoh 1.3.21-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +35 -0
- data/.travis.yml +462 -0
- data/.yardopts +4 -0
- data/Appraisals +36 -0
- data/CONTRIBUTING.md +49 -0
- data/Gemfile +68 -0
- data/History.md +1191 -0
- data/LICENSE.txt +25 -0
- data/README.md +277 -0
- data/RUNNING_TESTS.md +88 -0
- data/Rakefile +298 -0
- data/Rakefile.jdbc +20 -0
- data/activerecord-jdbc-adapter.gemspec +63 -0
- data/lib/active_record/connection_adapters/as400_adapter.rb +2 -0
- data/lib/active_record/connection_adapters/db2_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/derby_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/firebird_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/h2_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/hsqldb_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/informix_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/jdbc_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/jndi_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/mariadb_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/mssql_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/mysql_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/oracle_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +1 -0
- data/lib/active_record/connection_adapters/sqlserver_adapter.rb +1 -0
- data/lib/activerecord-jdbc-adapter.rb +1 -0
- data/lib/arel/visitors/compat.rb +64 -0
- data/lib/arel/visitors/db2.rb +137 -0
- data/lib/arel/visitors/derby.rb +112 -0
- data/lib/arel/visitors/firebird.rb +79 -0
- data/lib/arel/visitors/h2.rb +25 -0
- data/lib/arel/visitors/hsqldb.rb +32 -0
- data/lib/arel/visitors/postgresql_jdbc.rb +6 -0
- data/lib/arel/visitors/sql_server.rb +225 -0
- data/lib/arel/visitors/sql_server/ng42.rb +293 -0
- data/lib/arjdbc.rb +22 -0
- data/lib/arjdbc/db2.rb +4 -0
- data/lib/arjdbc/db2/adapter.rb +802 -0
- data/lib/arjdbc/db2/as400.rb +137 -0
- data/lib/arjdbc/db2/column.rb +177 -0
- data/lib/arjdbc/db2/connection_methods.rb +45 -0
- data/lib/arjdbc/derby.rb +3 -0
- data/lib/arjdbc/derby/active_record_patch.rb +13 -0
- data/lib/arjdbc/derby/adapter.rb +567 -0
- data/lib/arjdbc/derby/connection_methods.rb +16 -0
- data/lib/arjdbc/derby/schema_creation.rb +15 -0
- data/lib/arjdbc/discover.rb +104 -0
- data/lib/arjdbc/firebird.rb +4 -0
- data/lib/arjdbc/firebird/adapter.rb +468 -0
- data/lib/arjdbc/firebird/connection_methods.rb +20 -0
- data/lib/arjdbc/h2.rb +3 -0
- data/lib/arjdbc/h2/adapter.rb +335 -0
- data/lib/arjdbc/h2/connection_methods.rb +22 -0
- data/lib/arjdbc/hsqldb.rb +3 -0
- data/lib/arjdbc/hsqldb/adapter.rb +304 -0
- data/lib/arjdbc/hsqldb/connection_methods.rb +23 -0
- data/lib/arjdbc/hsqldb/explain_support.rb +35 -0
- data/lib/arjdbc/hsqldb/schema_creation.rb +11 -0
- data/lib/arjdbc/informix.rb +5 -0
- data/lib/arjdbc/informix/adapter.rb +160 -0
- data/lib/arjdbc/informix/connection_methods.rb +9 -0
- data/lib/arjdbc/jdbc.rb +62 -0
- data/lib/arjdbc/jdbc/adapter.rb +997 -0
- data/lib/arjdbc/jdbc/adapter_require.rb +46 -0
- data/lib/arjdbc/jdbc/arel_support.rb +149 -0
- data/lib/arjdbc/jdbc/base_ext.rb +34 -0
- data/lib/arjdbc/jdbc/callbacks.rb +52 -0
- data/lib/arjdbc/jdbc/column.rb +83 -0
- data/lib/arjdbc/jdbc/connection.rb +26 -0
- data/lib/arjdbc/jdbc/connection_methods.rb +59 -0
- data/lib/arjdbc/jdbc/driver.rb +44 -0
- data/lib/arjdbc/jdbc/error.rb +75 -0
- data/lib/arjdbc/jdbc/extension.rb +69 -0
- data/lib/arjdbc/jdbc/java.rb +13 -0
- data/lib/arjdbc/jdbc/type_cast.rb +154 -0
- data/lib/arjdbc/jdbc/type_converter.rb +142 -0
- data/lib/arjdbc/mssql.rb +7 -0
- data/lib/arjdbc/mssql/adapter.rb +822 -0
- data/lib/arjdbc/mssql/column.rb +207 -0
- data/lib/arjdbc/mssql/connection_methods.rb +72 -0
- data/lib/arjdbc/mssql/explain_support.rb +99 -0
- data/lib/arjdbc/mssql/limit_helpers.rb +231 -0
- data/lib/arjdbc/mssql/lock_methods.rb +77 -0
- data/lib/arjdbc/mssql/types.rb +343 -0
- data/lib/arjdbc/mssql/utils.rb +82 -0
- data/lib/arjdbc/mysql.rb +3 -0
- data/lib/arjdbc/mysql/adapter.rb +998 -0
- data/lib/arjdbc/mysql/bulk_change_table.rb +150 -0
- data/lib/arjdbc/mysql/column.rb +167 -0
- data/lib/arjdbc/mysql/connection_methods.rb +137 -0
- data/lib/arjdbc/mysql/explain_support.rb +82 -0
- data/lib/arjdbc/mysql/schema_creation.rb +58 -0
- data/lib/arjdbc/oracle.rb +4 -0
- data/lib/arjdbc/oracle/adapter.rb +968 -0
- data/lib/arjdbc/oracle/column.rb +136 -0
- data/lib/arjdbc/oracle/connection_methods.rb +21 -0
- data/lib/arjdbc/postgresql.rb +3 -0
- data/lib/arjdbc/postgresql/_bc_time_cast_patch.rb +21 -0
- data/lib/arjdbc/postgresql/adapter.rb +1498 -0
- data/lib/arjdbc/postgresql/base/array_parser.rb +95 -0
- data/lib/arjdbc/postgresql/base/oid.rb +412 -0
- data/lib/arjdbc/postgresql/base/pgconn.rb +8 -0
- data/lib/arjdbc/postgresql/base/schema_definitions.rb +132 -0
- data/lib/arjdbc/postgresql/column.rb +640 -0
- data/lib/arjdbc/postgresql/connection_methods.rb +44 -0
- data/lib/arjdbc/postgresql/explain_support.rb +53 -0
- data/lib/arjdbc/postgresql/oid/bytea.rb +3 -0
- data/lib/arjdbc/postgresql/oid_types.rb +265 -0
- data/lib/arjdbc/postgresql/schema_creation.rb +60 -0
- data/lib/arjdbc/railtie.rb +11 -0
- data/lib/arjdbc/sqlite3.rb +3 -0
- data/lib/arjdbc/sqlite3/adapter.rb +654 -0
- data/lib/arjdbc/sqlite3/connection_methods.rb +36 -0
- data/lib/arjdbc/sqlite3/explain_support.rb +29 -0
- data/lib/arjdbc/sybase.rb +2 -0
- data/lib/arjdbc/sybase/adapter.rb +47 -0
- data/lib/arjdbc/tasks.rb +13 -0
- data/lib/arjdbc/tasks/database_tasks.rb +66 -0
- data/lib/arjdbc/tasks/databases.rake +91 -0
- data/lib/arjdbc/tasks/databases3.rake +239 -0
- data/lib/arjdbc/tasks/databases4.rake +39 -0
- data/lib/arjdbc/tasks/db2_database_tasks.rb +104 -0
- data/lib/arjdbc/tasks/derby_database_tasks.rb +95 -0
- data/lib/arjdbc/tasks/h2_database_tasks.rb +31 -0
- data/lib/arjdbc/tasks/hsqldb_database_tasks.rb +70 -0
- data/lib/arjdbc/tasks/jdbc_database_tasks.rb +169 -0
- data/lib/arjdbc/tasks/mssql_database_tasks.rb +46 -0
- data/lib/arjdbc/tasks/oracle/enhanced_structure_dump.rb +297 -0
- data/lib/arjdbc/tasks/oracle_database_tasks.rb +65 -0
- data/lib/arjdbc/util/quoted_cache.rb +60 -0
- data/lib/arjdbc/util/serialized_attributes.rb +98 -0
- data/lib/arjdbc/util/table_copier.rb +108 -0
- data/lib/arjdbc/version.rb +8 -0
- data/lib/generators/jdbc/USAGE +9 -0
- data/lib/generators/jdbc/jdbc_generator.rb +17 -0
- data/pom.xml +285 -0
- data/rails_generators/jdbc_generator.rb +15 -0
- data/rails_generators/templates/config/initializers/jdbc.rb +10 -0
- data/rails_generators/templates/lib/tasks/jdbc.rake +11 -0
- data/rakelib/01-tomcat.rake +51 -0
- data/rakelib/02-test.rake +151 -0
- data/rakelib/bundler_ext.rb +11 -0
- data/rakelib/db.rake +58 -0
- data/rakelib/rails.rake +77 -0
- data/src/java/arjdbc/ArJdbcModule.java +288 -0
- data/src/java/arjdbc/db2/DB2Module.java +77 -0
- data/src/java/arjdbc/db2/DB2RubyJdbcConnection.java +128 -0
- data/src/java/arjdbc/derby/DerbyModule.java +180 -0
- data/src/java/arjdbc/derby/DerbyRubyJdbcConnection.java +153 -0
- data/src/java/arjdbc/firebird/FirebirdRubyJdbcConnection.java +190 -0
- data/src/java/arjdbc/h2/H2Module.java +50 -0
- data/src/java/arjdbc/h2/H2RubyJdbcConnection.java +86 -0
- data/src/java/arjdbc/hsqldb/HSQLDBModule.java +74 -0
- data/src/java/arjdbc/informix/InformixRubyJdbcConnection.java +76 -0
- data/src/java/arjdbc/jdbc/AdapterJavaService.java +43 -0
- data/src/java/arjdbc/jdbc/Callable.java +44 -0
- data/src/java/arjdbc/jdbc/ConnectionFactory.java +77 -0
- data/src/java/arjdbc/jdbc/DataSourceConnectionFactory.java +156 -0
- data/src/java/arjdbc/jdbc/DriverConnectionFactory.java +63 -0
- data/src/java/arjdbc/jdbc/DriverWrapper.java +128 -0
- data/src/java/arjdbc/jdbc/JdbcConnectionFactory.java +32 -0
- data/src/java/arjdbc/jdbc/RubyJdbcConnection.java +4541 -0
- data/src/java/arjdbc/jdbc/SQLBlock.java +54 -0
- data/src/java/arjdbc/jdbc/WithResultSet.java +37 -0
- data/src/java/arjdbc/mssql/MSSQLModule.java +91 -0
- data/src/java/arjdbc/mssql/MSSQLRubyJdbcConnection.java +193 -0
- data/src/java/arjdbc/mysql/MySQLModule.java +140 -0
- data/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java +456 -0
- data/src/java/arjdbc/oracle/OracleModule.java +81 -0
- data/src/java/arjdbc/oracle/OracleRubyJdbcConnection.java +477 -0
- data/src/java/arjdbc/postgresql/ByteaUtils.java +171 -0
- data/src/java/arjdbc/postgresql/DriverImplementation.java +78 -0
- data/src/java/arjdbc/postgresql/PGDriverImplementation.java +535 -0
- data/src/java/arjdbc/postgresql/PostgreSQLModule.java +189 -0
- data/src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java +489 -0
- data/src/java/arjdbc/sqlite3/SQLite3Module.java +93 -0
- data/src/java/arjdbc/sqlite3/SQLite3RubyJdbcConnection.java +405 -0
- data/src/java/arjdbc/util/CallResultSet.java +826 -0
- data/src/java/arjdbc/util/DateTimeUtils.java +517 -0
- data/src/java/arjdbc/util/NumberUtils.java +50 -0
- data/src/java/arjdbc/util/ObjectSupport.java +65 -0
- data/src/java/arjdbc/util/QuotingUtils.java +139 -0
- data/src/java/arjdbc/util/StringCache.java +60 -0
- data/src/java/arjdbc/util/StringHelper.java +155 -0
- metadata +288 -0
@@ -0,0 +1,36 @@
|
|
1
|
+
ArJdbc::ConnectionMethods.module_eval do
|
2
|
+
|
3
|
+
def sqlite3_connection(config)
|
4
|
+
config[:adapter_spec] ||= ::ArJdbc::SQLite3
|
5
|
+
config[:adapter_class] = ActiveRecord::ConnectionAdapters::SQLite3Adapter unless config.key?(:adapter_class)
|
6
|
+
|
7
|
+
return jndi_connection(config) if jndi_config?(config)
|
8
|
+
|
9
|
+
ArJdbc.load_driver(:SQLite3) unless config[:load_driver] == false
|
10
|
+
config[:driver] ||= 'org.sqlite.JDBC'
|
11
|
+
parse_sqlite3_config!(config)
|
12
|
+
database = config[:database] # NOTE: "jdbc:sqlite::memory:" syntax is supported
|
13
|
+
config[:url] ||= "jdbc:sqlite:#{database == ':memory:' ? '' : database}"
|
14
|
+
config[:connection_alive_sql] ||= 'SELECT 1'
|
15
|
+
|
16
|
+
options = ( config[:properties] ||= {} )
|
17
|
+
# NOTE: configuring from JDBC properties not supported on 3.7.2 :
|
18
|
+
options['busy_timeout'] ||= config[:timeout] if config.key?(:timeout)
|
19
|
+
|
20
|
+
jdbc_connection(config)
|
21
|
+
end
|
22
|
+
alias_method :jdbcsqlite3_connection, :sqlite3_connection
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def parse_sqlite3_config!(config)
|
27
|
+
database = ( config[:database] ||= config[:dbfile] ) # allow Rails relative path :
|
28
|
+
if database != ':memory:' && defined?(Rails.root) || Object.const_defined?(:RAILS_ROOT)
|
29
|
+
rails_root = defined?(Rails.root) ? Rails.root : RAILS_ROOT
|
30
|
+
config[:database] = File.expand_path(database, rails_root.to_s)
|
31
|
+
dirname = File.dirname(config[:database])
|
32
|
+
Dir.mkdir(dirname) unless File.directory?(dirname)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module ArJdbc
|
2
|
+
module SQLite3
|
3
|
+
module ExplainSupport
|
4
|
+
def supports_explain?
|
5
|
+
true
|
6
|
+
end
|
7
|
+
|
8
|
+
def explain(arel, binds = [])
|
9
|
+
sql = "EXPLAIN QUERY PLAN #{to_sql(arel, binds)}"
|
10
|
+
result = exec_query(sql, "EXPLAIN", binds)
|
11
|
+
ExplainPrettyPrinter.new.pp result
|
12
|
+
end
|
13
|
+
|
14
|
+
class ExplainPrettyPrinter # :nodoc:
|
15
|
+
# Pretty prints the result of a EXPLAIN QUERY PLAN in a way that resembles
|
16
|
+
# the output of the SQLite shell:
|
17
|
+
#
|
18
|
+
# 0|0|0|SEARCH TABLE users USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)
|
19
|
+
# 0|1|1|SCAN TABLE posts (~100000 rows)
|
20
|
+
#
|
21
|
+
def pp(result)
|
22
|
+
result.rows.map do |row|
|
23
|
+
row.join('|')
|
24
|
+
end.join("\n") + "\n"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module ArJdbc
|
2
|
+
module Sybase
|
3
|
+
def add_limit_offset!(sql, options) # :nodoc:
|
4
|
+
@limit = options[:limit]
|
5
|
+
@offset = options[:offset]
|
6
|
+
if use_temp_table?
|
7
|
+
# Use temp table to hack offset with Sybase
|
8
|
+
sql.sub!(/ FROM /i, ' INTO #artemp FROM ')
|
9
|
+
elsif zero_limit?
|
10
|
+
# "SET ROWCOUNT 0" turns off limits, so we havesy
|
11
|
+
# to use a cheap trick.
|
12
|
+
if sql =~ /WHERE/i
|
13
|
+
sql.sub!(/WHERE/i, 'WHERE 1 = 2 AND ')
|
14
|
+
elsif sql =~ /ORDER\s+BY/i
|
15
|
+
sql.sub!(/ORDER\s+BY/i, 'WHERE 1 = 2 ORDER BY')
|
16
|
+
else
|
17
|
+
sql << 'WHERE 1 = 2'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# If limit is not set at all, we can ignore offset;
|
23
|
+
# if limit *is* set but offset is zero, use normal select
|
24
|
+
# with simple SET ROWCOUNT. Thus, only use the temp table
|
25
|
+
# if limit is set and offset > 0.
|
26
|
+
def use_temp_table?
|
27
|
+
!@limit.nil? && !@offset.nil? && @offset > 0
|
28
|
+
end
|
29
|
+
|
30
|
+
def zero_limit?
|
31
|
+
!@limit.nil? && @limit == 0
|
32
|
+
end
|
33
|
+
|
34
|
+
def modify_types(types) # :nodoc:
|
35
|
+
super(types)
|
36
|
+
types[:primary_key] = "NUMERIC(22,0) IDENTITY PRIMARY KEY"
|
37
|
+
types[:integer][:limit] = nil
|
38
|
+
types[:boolean] = {:name => "bit"}
|
39
|
+
types[:binary] = {:name => "image"}
|
40
|
+
types
|
41
|
+
end
|
42
|
+
|
43
|
+
def remove_index(table_name, options = {})
|
44
|
+
execute "DROP INDEX #{table_name}.#{index_name(table_name, options)}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/arjdbc/tasks.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
if defined?(Rake.application) && Rake.application
|
2
|
+
skip = ENV["SKIP_AR_JDBC_RAKE_REDEFINES"] # jruby -J-Darjdbc.tasks.skip=true -S rake ...
|
3
|
+
if !(Java::JavaLang::Boolean.getBoolean('arjdbc.tasks.skip') || ( skip && skip != 'false' ))
|
4
|
+
databases_rake = File.expand_path('tasks/databases.rake', File.dirname(__FILE__))
|
5
|
+
if Rake.application.lookup("db:create")
|
6
|
+
load databases_rake # load the override tasks now
|
7
|
+
else # rails tasks not loaded yet; load as an import
|
8
|
+
Rake.application.add_import(databases_rake)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
else
|
12
|
+
warn "ArJdbc: could not load rake tasks - rake not loaded ..."
|
13
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module ArJdbc
|
2
|
+
module Tasks
|
3
|
+
|
4
|
+
if defined? ActiveRecord::Tasks::DatabaseTasks # AR-4.x
|
5
|
+
|
6
|
+
def self.register_tasks(pattern, task)
|
7
|
+
ActiveRecord::Tasks::DatabaseTasks.register_task(pattern, task)
|
8
|
+
end
|
9
|
+
|
10
|
+
register_tasks(/mariadb/, ActiveRecord::Tasks::MySQLDatabaseTasks)
|
11
|
+
|
12
|
+
# patch-in lazy (auto) loading with AR registered tasks :
|
13
|
+
module ActiveRecord::Tasks::DatabaseTasks
|
14
|
+
def class_for_adapter(adapter)
|
15
|
+
unless key = @tasks.keys.detect { |pattern| adapter[pattern] }
|
16
|
+
raise DatabaseNotSupported, "Rake tasks not supported by '#{adapter}' adapter"
|
17
|
+
end
|
18
|
+
tasks = @tasks[key] || JdbcDatabaseTasks
|
19
|
+
tasks = ArJdbc::Tasks.const_get(tasks) unless tasks.is_a?(Module)
|
20
|
+
tasks
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
else
|
25
|
+
|
26
|
+
@@tasks = {}
|
27
|
+
|
28
|
+
def self.register_tasks(pattern, task)
|
29
|
+
@@tasks[pattern] = task
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.tasks_instance(config); adapter = config['adapter']
|
33
|
+
key = @@tasks.keys.detect { |pattern| adapter[pattern] }
|
34
|
+
tasks = @@tasks[key] || JdbcDatabaseTasks
|
35
|
+
tasks = const_get(tasks) unless tasks.is_a?(Module)
|
36
|
+
tasks.new(config)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
autoload :JdbcDatabaseTasks, 'arjdbc/tasks/jdbc_database_tasks'
|
42
|
+
autoload :DB2DatabaseTasks, 'arjdbc/tasks/db2_database_tasks'
|
43
|
+
autoload :DerbyDatabaseTasks, 'arjdbc/tasks/derby_database_tasks'
|
44
|
+
autoload :H2DatabaseTasks, 'arjdbc/tasks/h2_database_tasks'
|
45
|
+
autoload :HSQLDBDatabaseTasks, 'arjdbc/tasks/hsqldb_database_tasks'
|
46
|
+
autoload :MSSQLDatabaseTasks, 'arjdbc/tasks/mssql_database_tasks'
|
47
|
+
autoload :OracleDatabaseTasks, 'arjdbc/tasks/oracle_database_tasks'
|
48
|
+
|
49
|
+
# re-invent built-in (but deprecated on 4.0) tasks :
|
50
|
+
register_tasks(/sqlserver/, :MSSQLDatabaseTasks)
|
51
|
+
register_tasks(/(oci|oracle)/, :OracleDatabaseTasks)
|
52
|
+
register_tasks(/mssql/, :MSSQLDatabaseTasks) # (built-in) alias
|
53
|
+
# tasks for custom (JDBC) adapters :
|
54
|
+
register_tasks(/db2/, :DB2DatabaseTasks)
|
55
|
+
register_tasks(/derby/, :DerbyDatabaseTasks)
|
56
|
+
register_tasks(/h2/, :H2DatabaseTasks)
|
57
|
+
register_tasks(/hsqldb/, :HSQLDBDatabaseTasks)
|
58
|
+
# (default) generic JDBC task :
|
59
|
+
register_tasks(/^jdbc$/, :JdbcDatabaseTasks)
|
60
|
+
|
61
|
+
# NOTE: no need to register "built-in" adapters such as MySQL
|
62
|
+
# - on 4.0 these are registered and will be instantiated
|
63
|
+
# - while on 2.3/3.x we keep the AR built-in task behavior
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
raise "ArJdbc needs rake 0.9.x or newer" unless Rake.const_defined?(:VERSION)
|
2
|
+
|
3
|
+
Rake::DSL.module_eval do
|
4
|
+
|
5
|
+
def redefine_task(*args, &block)
|
6
|
+
if Hash === args.first
|
7
|
+
task_name = args.first.keys[0]
|
8
|
+
old_prereqs = false # leave as specified
|
9
|
+
else
|
10
|
+
task_name = args.first; old_prereqs = []
|
11
|
+
# args[0] = { task_name => old_prereqs }
|
12
|
+
end
|
13
|
+
|
14
|
+
full_name = Rake::Task.scope_name(Rake.application.current_scope, task_name)
|
15
|
+
|
16
|
+
if old_task = Rake.application.lookup(task_name)
|
17
|
+
old_comment = old_task.full_comment
|
18
|
+
old_prereqs = old_task.prerequisites.dup if old_prereqs
|
19
|
+
old_actions = old_task.actions.dup
|
20
|
+
old_actions.shift # remove the main 'action' block - we're redefining it
|
21
|
+
# old_task.clear_prerequisites if old_prereqs
|
22
|
+
# old_task.clear_actions
|
23
|
+
# remove the (old) task instance from the application :
|
24
|
+
Rake.application.send(:instance_variable_get, :@tasks)[full_name.to_s] = nil
|
25
|
+
else
|
26
|
+
# raise "could not find rake task with (full) name '#{full_name}'"
|
27
|
+
end
|
28
|
+
|
29
|
+
new_task = task(*args, &block)
|
30
|
+
new_task.comment = old_comment if old_comment
|
31
|
+
new_task.actions.concat(old_actions) if old_actions
|
32
|
+
new_task.prerequisites.concat(old_prereqs) if old_prereqs
|
33
|
+
new_task
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
namespace :db do
|
39
|
+
|
40
|
+
def rails_env
|
41
|
+
defined?(Rails.env) ? Rails.env : ( RAILS_ENV || 'development' )
|
42
|
+
end
|
43
|
+
|
44
|
+
if defined? adapt_jdbc_config
|
45
|
+
warn "double loading #{__FILE__} please delete lib/tasks/jdbc.rake if present!"
|
46
|
+
end
|
47
|
+
|
48
|
+
def adapt_jdbc_config(config)
|
49
|
+
return config unless config['adapter']
|
50
|
+
config.merge 'adapter' => config['adapter'].sub(/^jdbc/, '')
|
51
|
+
end
|
52
|
+
|
53
|
+
if defined? ActiveRecord::Tasks::DatabaseTasks # 4.0
|
54
|
+
|
55
|
+
def current_config(options = {})
|
56
|
+
ActiveRecord::Tasks::DatabaseTasks.current_config(options)
|
57
|
+
end
|
58
|
+
|
59
|
+
else # 3.x / 2.3
|
60
|
+
|
61
|
+
def current_config(options = {}) # not on 2.3
|
62
|
+
options = { :env => rails_env }.merge! options
|
63
|
+
if options[:config]
|
64
|
+
@current_config = options[:config]
|
65
|
+
else
|
66
|
+
@current_config ||= ENV['DATABASE_URL'] ?
|
67
|
+
database_url_config : ActiveRecord::Base.configurations[options[:env]]
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def database_url_config(url = ENV['DATABASE_URL'])
|
72
|
+
# NOTE: ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver
|
73
|
+
# since AR 4.0 that is handled by DatabaseTasks - only care about 2.3/3.x :
|
74
|
+
unless defined? ActiveRecord::Base::ConnectionSpecification::Resolver
|
75
|
+
raise "DATABASE_URL not supported on ActiveRecord #{ActiveRecord::VERSION::STRING}"
|
76
|
+
end
|
77
|
+
resolver = ActiveRecord::Base::ConnectionSpecification::Resolver.new(url, {})
|
78
|
+
resolver.spec.config.stringify_keys
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
require 'arjdbc/tasks/database_tasks'
|
86
|
+
|
87
|
+
if defined? ActiveRecord::Tasks::DatabaseTasks # 4.0
|
88
|
+
load File.expand_path('databases4.rake', File.dirname(__FILE__))
|
89
|
+
else # 3.x / 2.3
|
90
|
+
load File.expand_path('databases3.rake', File.dirname(__FILE__))
|
91
|
+
end
|
@@ -0,0 +1,239 @@
|
|
1
|
+
module ArJdbc
|
2
|
+
module Tasks
|
3
|
+
class << self
|
4
|
+
|
5
|
+
# API similar to ActiveRecord::Tasks::DatabaseTasks on AR 4.0
|
6
|
+
|
7
|
+
def create(config)
|
8
|
+
tasks_instance(config).create
|
9
|
+
end
|
10
|
+
|
11
|
+
def drop(config)
|
12
|
+
tasks_instance(config).drop
|
13
|
+
end
|
14
|
+
|
15
|
+
def purge(config)
|
16
|
+
tasks_instance(config).purge
|
17
|
+
end
|
18
|
+
|
19
|
+
def charset(config)
|
20
|
+
tasks_instance(config).charset
|
21
|
+
end
|
22
|
+
|
23
|
+
def collation(config)
|
24
|
+
tasks_instance(config).collation
|
25
|
+
end
|
26
|
+
|
27
|
+
def structure_dump(config, filename)
|
28
|
+
tasks_instance(config).structure_dump(filename)
|
29
|
+
end
|
30
|
+
|
31
|
+
def structure_load(config, filename)
|
32
|
+
tasks_instance(config).structure_load(filename)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
namespace :db do
|
40
|
+
|
41
|
+
class << self
|
42
|
+
alias_method :_rails_create_database, :create_database
|
43
|
+
alias_method :_rails_drop_database, :drop_database
|
44
|
+
end
|
45
|
+
|
46
|
+
def create_database(config)
|
47
|
+
case config['adapter']
|
48
|
+
when /mysql2/
|
49
|
+
unless defined? Mysql2::Error
|
50
|
+
# NOTE: fake it for create_database(config)
|
51
|
+
Object.const_set :Mysql2, Module.new
|
52
|
+
Mysql2.const_set :Error, ActiveRecord::JDBCError
|
53
|
+
ActiveRecord::JDBCError.class_eval do
|
54
|
+
def error; self end # Mysql2::Error#error
|
55
|
+
end
|
56
|
+
end
|
57
|
+
_rails_create_database adapt_jdbc_config(config)
|
58
|
+
when /mysql/
|
59
|
+
unless defined? Mysql::Error
|
60
|
+
# NOTE: fake it for create_database(config)
|
61
|
+
Object.const_set :Mysql, Module.new
|
62
|
+
Mysql.const_set :Error, ActiveRecord::JDBCError
|
63
|
+
ActiveRecord::JDBCError.class_eval do
|
64
|
+
def error; self end # Mysql::Error#error
|
65
|
+
end
|
66
|
+
end
|
67
|
+
_rails_create_database adapt_jdbc_config(config)
|
68
|
+
when /postgresql|sqlite/
|
69
|
+
_rails_create_database adapt_jdbc_config(config)
|
70
|
+
when /mariadb/ # fake mariadb as mysql for Rails
|
71
|
+
config = config.update('adapter' => 'mysql')
|
72
|
+
config['driver'] ||= 'org.mariadb.jdbc.Driver'
|
73
|
+
_rails_create_database adapt_jdbc_config(config)
|
74
|
+
else
|
75
|
+
ArJdbc::Tasks.create(config)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def drop_database(config)
|
80
|
+
case config['adapter']
|
81
|
+
when /mysql|postgresql|sqlite/
|
82
|
+
_rails_drop_database adapt_jdbc_config(config)
|
83
|
+
when /mariadb/ # fake mariadb as mysql for Rails
|
84
|
+
config = config.update('adapter' => 'mysql')
|
85
|
+
config['driver'] ||= 'org.mariadb.jdbc.Driver'
|
86
|
+
_rails_drop_database adapt_jdbc_config(config)
|
87
|
+
else
|
88
|
+
ArJdbc::Tasks.drop(config)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
redefine_task :charset do # available on 2.3
|
93
|
+
ArJdbc::Tasks.charset ActiveRecord::Base.configurations[rails_env]
|
94
|
+
end
|
95
|
+
|
96
|
+
redefine_task :collation do # available on 2.3
|
97
|
+
ArJdbc::Tasks.collation ActiveRecord::Base.configurations[rails_env]
|
98
|
+
end
|
99
|
+
|
100
|
+
namespace :structure do
|
101
|
+
|
102
|
+
redefine_task :dump do
|
103
|
+
config = ActiveRecord::Base.configurations[rails_env] # current_config
|
104
|
+
filename = structure_sql
|
105
|
+
|
106
|
+
case config['adapter']
|
107
|
+
when /mysql|mariadb/
|
108
|
+
args = _prepare_mysql_options('mysqldump', config)
|
109
|
+
args.concat(["--result-file", "#{filename}"])
|
110
|
+
args.concat(["--no-data"])
|
111
|
+
args.concat(["#{config['database']}"])
|
112
|
+
unless Kernel.system(*args)
|
113
|
+
puts "Could not dump the database structure. "\
|
114
|
+
"Make sure `mysqldump` is in your PATH and check the command output for warnings."
|
115
|
+
end
|
116
|
+
when /postgresql/
|
117
|
+
ActiveRecord::Base.establish_connection(config)
|
118
|
+
|
119
|
+
ENV['PGHOST'] = config['host'] if config['host']
|
120
|
+
ENV['PGPORT'] = config['port'].to_s if config['port']
|
121
|
+
ENV['PGPASSWORD'] = config['password'].to_s if config['password']
|
122
|
+
ENV['PGUSER'] = config['username'].to_s if config['username']
|
123
|
+
|
124
|
+
require 'shellwords'
|
125
|
+
search_path = config['schema_search_path']
|
126
|
+
unless search_path.blank?
|
127
|
+
search_path = search_path.split(",").map{ |part| "--schema=#{Shellwords.escape(part.strip)}" }.join(" ")
|
128
|
+
end
|
129
|
+
sh "pg_dump -i -s -x -O -f #{Shellwords.escape(filename)} #{search_path} #{Shellwords.escape(config['database'])}"
|
130
|
+
|
131
|
+
File.open(filename, 'a') { |f| f << "SET search_path TO #{ActiveRecord::Base.connection.schema_search_path};\n\n" }
|
132
|
+
when /sqlite/
|
133
|
+
dbfile = config['database']
|
134
|
+
sh "sqlite3 #{dbfile} .schema > #{filename}"
|
135
|
+
else
|
136
|
+
ActiveRecord::Base.establish_connection(config)
|
137
|
+
ArJdbc::Tasks.structure_dump(config, filename)
|
138
|
+
end
|
139
|
+
|
140
|
+
if ActiveRecord::Base.connection.supports_migrations?
|
141
|
+
File.open(filename, 'a') { |f| f << ActiveRecord::Base.connection.dump_schema_information }
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
145
|
+
|
146
|
+
redefine_task :load do
|
147
|
+
config = current_config
|
148
|
+
filename = structure_sql
|
149
|
+
|
150
|
+
case config['adapter']
|
151
|
+
when /mysql|mariadb/
|
152
|
+
args = _prepare_mysql_options('mysql', config)
|
153
|
+
args.concat(['--execute', %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}])
|
154
|
+
args.concat(["--database", "#{config['database']}"])
|
155
|
+
Kernel.system(*args)
|
156
|
+
when /postgresql/
|
157
|
+
ENV['PGHOST'] = config['host'] if config['host']
|
158
|
+
ENV['PGPORT'] = config['port'].to_s if config['port']
|
159
|
+
ENV['PGPASSWORD'] = config['password'].to_s if config['password']
|
160
|
+
ENV['PGUSER'] = config['username'].to_s if config['username']
|
161
|
+
|
162
|
+
`psql -f "#{filename}" #{config['database']}`
|
163
|
+
when /sqlite/
|
164
|
+
dbfile = config['database']
|
165
|
+
`sqlite3 #{dbfile} < "#{filename}"`
|
166
|
+
else
|
167
|
+
ArJdbc::Tasks.structure_load(config, filename)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def structure_sql
|
172
|
+
ENV['DB_STRUCTURE'] ||= begin
|
173
|
+
root = defined?(Rails.root) ? Rails.root : ( RAILS_ROOT rescue nil )
|
174
|
+
if ActiveRecord::VERSION::STRING > '3.2'
|
175
|
+
root ? File.join(root, "db", "structure.sql") : File.join("db", "structure.sql")
|
176
|
+
else
|
177
|
+
root ? File.join(root, "db/#{rails_env}_structure.sql") : "db/#{rails_env}_structure.sql"
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
def _prepare_mysql_options(command, config)
|
183
|
+
args = [ command ]
|
184
|
+
args.concat(['--user', config['username']]) if config['username']
|
185
|
+
args << "--password=#{config['password']}" if config['password']
|
186
|
+
args.concat(['--default-character-set', config['encoding']]) if config['encoding']
|
187
|
+
config.slice('host', 'port', 'socket').each do |k, v|
|
188
|
+
args.concat([ "--#{k}", v.to_s ]) if v
|
189
|
+
end
|
190
|
+
args
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
namespace :test do
|
196
|
+
|
197
|
+
# desc "Recreate the test database from an existent structure.sql file"
|
198
|
+
redefine_task :load_structure => 'db:test:purge' do # not on 2.3
|
199
|
+
begin
|
200
|
+
current_config(:config => ActiveRecord::Base.configurations['test'])
|
201
|
+
Rake::Task["db:structure:load"].invoke
|
202
|
+
ensure
|
203
|
+
current_config(:config => nil)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
# desc "Recreate the test database from a fresh structure.sql file"
|
208
|
+
redefine_task :clone_structure => [ "db:structure:dump", "db:test:load_structure" ]
|
209
|
+
# same as on 3.2 - but this task gets changed on 2.3 by depending on :load_structure
|
210
|
+
|
211
|
+
# desc "Empty the test database"
|
212
|
+
redefine_task :purge do
|
213
|
+
config = ActiveRecord::Base.configurations['test']
|
214
|
+
case config['adapter']
|
215
|
+
when /mysql|mariadb/
|
216
|
+
ActiveRecord::Base.establish_connection(:test)
|
217
|
+
options = mysql_creation_options(config) rescue config
|
218
|
+
ActiveRecord::Base.connection.recreate_database(config['database'], options)
|
219
|
+
when /postgresql/
|
220
|
+
ActiveRecord::Base.clear_active_connections!
|
221
|
+
# drop_database(config) :
|
222
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
|
223
|
+
ActiveRecord::Base.connection.drop_database config['database']
|
224
|
+
# create_database(config) :
|
225
|
+
encoding = config[:encoding] || ENV['CHARSET'] || 'utf8'
|
226
|
+
ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => encoding))
|
227
|
+
when /sqlite/
|
228
|
+
dbfile = config['database']
|
229
|
+
File.delete(dbfile) if File.exist?(dbfile)
|
230
|
+
else
|
231
|
+
ArJdbc::Tasks.purge(config)
|
232
|
+
end
|
233
|
+
end
|
234
|
+
# only does (:purge => :environment) on AR < 3.2
|
235
|
+
task :purge => :load_config if Rake::Task.task_defined?(:load_config)
|
236
|
+
|
237
|
+
end
|
238
|
+
|
239
|
+
end
|