activerecord-jdbc-adapter 1.3.0.beta1 → 1.3.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +12 -11
- data/.travis.yml +36 -7
- data/Appraisals +3 -3
- data/Gemfile +1 -1
- data/Gemfile.lock +13 -6
- data/History.txt +64 -0
- data/README.md +8 -1
- data/Rakefile +3 -1
- data/gemfiles/rails23.gemfile +1 -1
- data/gemfiles/rails23.gemfile.lock +6 -5
- data/gemfiles/rails30.gemfile +1 -1
- data/gemfiles/rails30.gemfile.lock +7 -6
- data/gemfiles/rails31.gemfile +1 -1
- data/gemfiles/rails31.gemfile.lock +6 -5
- data/gemfiles/rails32.gemfile +1 -1
- data/gemfiles/rails32.gemfile.lock +6 -5
- data/gemfiles/rails40.gemfile +2 -4
- data/gemfiles/rails40.gemfile.lock +37 -51
- data/lib/active_record/connection_adapters/as400_adapter.rb +2 -0
- data/lib/active_record/connection_adapters/db2_adapter.rb +1 -1
- data/lib/arel/visitors/db2.rb +5 -1
- data/lib/arel/visitors/hsqldb.rb +1 -0
- data/lib/arel/visitors/sql_server.rb +55 -13
- data/lib/arjdbc/db2/adapter.rb +197 -227
- data/lib/arjdbc/db2/as400.rb +124 -0
- data/lib/arjdbc/db2/connection_methods.rb +20 -1
- data/lib/arjdbc/derby/adapter.rb +17 -85
- data/lib/arjdbc/derby/connection_methods.rb +2 -1
- data/lib/arjdbc/discover.rb +55 -47
- data/lib/arjdbc/h2/adapter.rb +52 -18
- data/lib/arjdbc/h2/connection_methods.rb +10 -2
- data/lib/arjdbc/hsqldb/adapter.rb +33 -9
- data/lib/arjdbc/hsqldb/connection_methods.rb +10 -2
- data/lib/arjdbc/informix.rb +2 -1
- data/lib/arjdbc/jdbc.rb +5 -1
- data/lib/arjdbc/jdbc/adapter.rb +167 -89
- data/lib/arjdbc/jdbc/adapter_java.jar +0 -0
- data/lib/arjdbc/jdbc/adapter_require.rb +46 -0
- data/lib/arjdbc/jdbc/base_ext.rb +25 -3
- data/lib/arjdbc/jdbc/callbacks.rb +9 -8
- data/lib/arjdbc/jdbc/column.rb +8 -20
- data/lib/arjdbc/jdbc/connection.rb +69 -80
- data/lib/arjdbc/jdbc/extension.rb +6 -8
- data/lib/arjdbc/jdbc/jdbc.rake +3 -141
- data/lib/arjdbc/jdbc/rake_tasks.rb +3 -10
- data/lib/arjdbc/mssql/adapter.rb +108 -34
- data/lib/arjdbc/mssql/connection_methods.rb +3 -1
- data/lib/arjdbc/mssql/limit_helpers.rb +3 -2
- data/lib/arjdbc/mssql/lock_helpers.rb +5 -1
- data/lib/arjdbc/mysql/adapter.rb +127 -70
- data/lib/arjdbc/mysql/connection_methods.rb +5 -2
- data/lib/arjdbc/oracle/adapter.rb +124 -94
- data/lib/arjdbc/oracle/connection_methods.rb +2 -1
- data/lib/arjdbc/postgresql/adapter.rb +99 -67
- data/lib/arjdbc/postgresql/column_cast.rb +3 -5
- data/lib/arjdbc/postgresql/connection_methods.rb +6 -6
- data/lib/arjdbc/railtie.rb +3 -1
- data/lib/arjdbc/sqlite3/adapter.rb +60 -43
- data/lib/arjdbc/sqlite3/connection_methods.rb +9 -9
- data/lib/arjdbc/sybase.rb +1 -1
- data/lib/arjdbc/tasks.rb +13 -0
- data/lib/arjdbc/tasks/database_tasks.rb +50 -0
- data/lib/arjdbc/tasks/databases.rake +89 -0
- data/lib/arjdbc/tasks/databases3.rake +203 -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 +29 -0
- data/lib/arjdbc/tasks/hsqldb_database_tasks.rb +70 -0
- data/lib/arjdbc/tasks/jdbc_database_tasks.rb +122 -0
- data/lib/arjdbc/tasks/mssql_database_tasks.rb +36 -0
- data/lib/arjdbc/tasks/oracle/enhanced_structure_dump.rb +297 -0
- data/lib/arjdbc/tasks/oracle_database_tasks.rb +62 -0
- data/lib/arjdbc/version.rb +1 -1
- data/pom.xml +11 -12
- data/rails_generators/jdbc_generator.rb +1 -1
- data/rails_generators/templates/config/initializers/jdbc.rb +8 -5
- data/rails_generators/templates/lib/tasks/jdbc.rake +7 -4
- data/rakelib/02-test.rake +42 -15
- data/rakelib/compile.rake +29 -2
- data/rakelib/db.rake +2 -1
- data/rakelib/rails.rake +23 -6
- data/src/java/arjdbc/ArJdbcModule.java +175 -0
- data/src/java/arjdbc/db2/DB2Module.java +2 -1
- data/src/java/arjdbc/derby/DerbyModule.java +5 -24
- data/src/java/arjdbc/hsqldb/HSQLDBModule.java +3 -2
- data/src/java/arjdbc/jdbc/AdapterJavaService.java +3 -46
- data/src/java/arjdbc/jdbc/RubyJdbcConnection.java +1001 -259
- data/src/java/arjdbc/mssql/MSSQLModule.java +2 -1
- data/src/java/arjdbc/mysql/MySQLModule.java +4 -3
- data/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java +12 -7
- data/src/java/arjdbc/oracle/OracleModule.java +2 -1
- data/src/java/arjdbc/sqlite3/SQLite3Module.java +2 -1
- data/src/java/arjdbc/sqlite3/SQLite3RubyJdbcConnection.java +12 -0
- data/test/db/db2.rb +14 -7
- data/test/db/db2/rake_test.rb +82 -0
- data/test/db/db2/rake_test_data.sql +35 -0
- data/test/db/db2/simple_test.rb +20 -0
- data/test/db/db2/unit_test.rb +3 -1
- data/test/db/derby.rb +7 -5
- data/test/db/derby/rake_test.rb +96 -0
- data/test/db/derby/simple_test.rb +10 -2
- data/test/db/h2.rb +6 -8
- data/test/db/h2/identity_column_test.rb +35 -0
- data/test/db/h2/offset_test.rb +49 -0
- data/test/db/h2/rake_test.rb +98 -0
- data/test/db/h2/schema_dump_test.rb +5 -1
- data/test/db/hsqldb.rb +6 -10
- data/test/db/hsqldb/rake_test.rb +101 -0
- data/test/db/hsqldb/schema_dump_test.rb +5 -1
- data/test/db/hsqldb/simple_test.rb +8 -0
- data/test/db/jndi_config.rb +1 -3
- data/test/db/jndi_pooled_config.rb +1 -3
- data/test/db/mssql/limit_offset_test.rb +23 -14
- data/test/db/mssql/rake_test.rb +143 -0
- data/test/db/mysql/_rails_test_mysql.32.out +1069 -1252
- data/test/db/mysql/nonstandard_primary_key_test.rb +21 -24
- data/test/db/mysql/rake_test.rb +97 -0
- data/test/db/mysql/schema_dump_test.rb +11 -11
- data/test/db/mysql/simple_test.rb +52 -3
- data/test/db/mysql/statement_escaping_test.rb +46 -0
- data/test/db/oracle/rake_test.rb +100 -0
- data/test/db/oracle/simple_test.rb +48 -0
- data/test/db/postgres/_rails_test_postgres.32.out +998 -1370
- data/test/db/postgres/active_schema_unit_test.rb +68 -0
- data/test/db/postgres/connection_test.rb +10 -2
- data/test/db/postgres/data_types_test.rb +2 -2
- data/test/db/postgres/ltree_test.rb +6 -5
- data/test/db/postgres/native_types_test.rb +1 -5
- data/test/db/postgres/rake_test.rb +117 -0
- data/test/db/postgres/schema_dump_test.rb +9 -2
- data/test/db/postgres/schema_test.rb +4 -2
- data/test/db/postgres/simple_test.rb +57 -16
- data/test/db/sqlite3.rb +3 -10
- data/test/db/sqlite3/_rails_test_sqlite3.32.out +1070 -1298
- data/test/db/sqlite3/rake_test.rb +71 -0
- data/test/db/sqlite3/simple_test.rb +9 -9
- data/test/has_many_through.rb +4 -1
- data/test/jdbc/db2.rb +14 -1
- data/test/jdbc_column_test.rb +23 -0
- data/test/{generic_jdbc_connection_test.rb → jdbc_connection_test.rb} +22 -17
- data/test/jndi_callbacks_test.rb +26 -28
- data/test/jndi_test.rb +7 -16
- data/test/models/data_types.rb +2 -1
- data/test/models/thing.rb +1 -0
- data/test/rails/mysql.rb +13 -0
- data/test/rails/sqlite3/version.rb +6 -0
- data/test/rails_stub.rb +31 -0
- data/test/rake_test_support.rb +298 -0
- data/test/serialize.rb +2 -4
- data/test/{helper.rb → shared_helper.rb} +0 -0
- data/test/simple.rb +167 -93
- data/test/test_helper.rb +52 -16
- metadata +388 -354
- data/lib/pg.rb +0 -26
- data/test/abstract_db_create.rb +0 -139
- data/test/activerecord/connection_adapters/type_conversion_test.rb +0 -36
- data/test/db/mssql/db_create_test.rb +0 -29
- data/test/db/mysql/db_create_test.rb +0 -33
- data/test/db/postgres/db_create_test.rb +0 -44
- data/test/db/postgres/db_drop_test.rb +0 -17
@@ -0,0 +1,39 @@
|
|
1
|
+
module ActiveRecord::Tasks
|
2
|
+
|
3
|
+
DatabaseTasks.module_eval do
|
4
|
+
|
5
|
+
# patched to adapt jdbc configuration
|
6
|
+
def each_current_configuration(environment)
|
7
|
+
environments = [environment]
|
8
|
+
environments << 'test' if environment == 'development'
|
9
|
+
|
10
|
+
configurations = ActiveRecord::Base.configurations.values_at(*environments)
|
11
|
+
configurations.compact.each do |config|
|
12
|
+
yield adapt_jdbc_config(config) unless config['database'].blank?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# patched to adapt jdbc configuration
|
17
|
+
def each_local_configuration
|
18
|
+
ActiveRecord::Base.configurations.each_value do |config|
|
19
|
+
next unless config['database']
|
20
|
+
|
21
|
+
if local_database?(config)
|
22
|
+
yield adapt_jdbc_config(config)
|
23
|
+
else
|
24
|
+
$stderr.puts "This task only modifies local databases. #{config['database']} is on a remote host."
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
MySQLDatabaseTasks.class_eval do
|
32
|
+
|
33
|
+
def error_class
|
34
|
+
ActiveRecord::JDBCError
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'arjdbc/tasks/jdbc_database_tasks'
|
2
|
+
|
3
|
+
module ArJdbc
|
4
|
+
module Tasks
|
5
|
+
class DB2DatabaseTasks < JdbcDatabaseTasks
|
6
|
+
|
7
|
+
def create
|
8
|
+
raise "AR-JDBC adapter 'DB2' does not support create_database"
|
9
|
+
end
|
10
|
+
|
11
|
+
def purge
|
12
|
+
establish_connection(config)
|
13
|
+
connection.recreate_database
|
14
|
+
end
|
15
|
+
|
16
|
+
# NOTE: does not work correctly (on non AS400) due driver meta data issue
|
17
|
+
#
|
18
|
+
# also try db2move e.g. `db2move SAMPLE EXPORT -sn db2inst`
|
19
|
+
# - where SAMPLE is the database name
|
20
|
+
# - and -sn specified schema name
|
21
|
+
#
|
22
|
+
|
23
|
+
def structure_dump(filename)
|
24
|
+
establish_connection(config)
|
25
|
+
dump = File.open(filename, "w:utf-8")
|
26
|
+
|
27
|
+
schema_name = connection.schema.upcase if connection.schema
|
28
|
+
meta_data = connection.jdbc_connection.meta_data
|
29
|
+
tables_rs = meta_data.getTables(nil, schema_name, nil, ["TABLE"].to_java(:String))
|
30
|
+
|
31
|
+
have_scale = ArJdbc::DB2::HAVE_SCALE
|
32
|
+
have_precision = ArJdbc::DB2::HAVE_LIMIT + ArJdbc::DB2::HAVE_LIMIT
|
33
|
+
|
34
|
+
while tables_rs.next
|
35
|
+
table_name = tables_rs.getString('TABLE_NAME')
|
36
|
+
dump << "CREATE TABLE #{connection.quote_table_name(table_name)} (\n"
|
37
|
+
|
38
|
+
cols_rs = meta_data.getColumns(nil, schema_name, table_name, nil)
|
39
|
+
begin
|
40
|
+
first_col = true
|
41
|
+
while cols_rs.next
|
42
|
+
column_name = cols_rs.getString(4)
|
43
|
+
default = cols_rs.getString(13)
|
44
|
+
default = default.empty? ? "" : " DEFAULT #{default}" if default
|
45
|
+
type = cols_rs.getString(6)
|
46
|
+
precision, scale = cols_rs.getString(7), cols_rs.getString(9)
|
47
|
+
column_size = ""
|
48
|
+
if scale && have_scale.include?(type)
|
49
|
+
column_size = "(#{precision},#{scale})"
|
50
|
+
elsif precision && have_precision.include?(type)
|
51
|
+
column_size = "(#{precision})"
|
52
|
+
end
|
53
|
+
nulling = ( cols_rs.getString(18) == 'NO' ? " NOT NULL" : nil )
|
54
|
+
autoinc = ( cols_rs.getString(23) == 'YES' ? " GENERATED ALWAYS AS IDENTITY" : nil )
|
55
|
+
|
56
|
+
create_column = connection.quote_column_name(column_name)
|
57
|
+
create_column << " #{type}"
|
58
|
+
create_column << column_size
|
59
|
+
create_column << nulling.to_s
|
60
|
+
create_column << default.to_s
|
61
|
+
create_column << autoinc.to_s
|
62
|
+
|
63
|
+
create_column = first_col ? " #{create_column}" : ",\n #{create_column}"
|
64
|
+
dump << create_column
|
65
|
+
|
66
|
+
first_col = false
|
67
|
+
end
|
68
|
+
ensure
|
69
|
+
cols_rs.close
|
70
|
+
end
|
71
|
+
|
72
|
+
dump << "\n);\n\n"
|
73
|
+
|
74
|
+
pk_rs = meta_data.getPrimaryKeys(nil, schema_name, table_name)
|
75
|
+
primary_keys = {}
|
76
|
+
begin
|
77
|
+
while pk_rs.next
|
78
|
+
name = pk_rs.getString(6)
|
79
|
+
primary_keys[name] ||= []
|
80
|
+
primary_keys[name] << pk_rs.getString(4)
|
81
|
+
end
|
82
|
+
ensure
|
83
|
+
pk_rs.close
|
84
|
+
end
|
85
|
+
primary_keys.each do |name, cols|
|
86
|
+
dump << "ALTER TABLE #{connection.quote_table_name(table_name)}\n"
|
87
|
+
dump << " ADD CONSTRAINT #{name}\n"
|
88
|
+
dump << " PRIMARY KEY (#{cols.join(', ')});\n\n"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
dump.close
|
93
|
+
end
|
94
|
+
|
95
|
+
def structure_load(filename)
|
96
|
+
establish_connection(config)
|
97
|
+
IO.read(filename).split(/;\n*/m).each do |ddl|
|
98
|
+
connection.execute ddl.sub(/;$/, '')
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'arjdbc/tasks/jdbc_database_tasks'
|
2
|
+
|
3
|
+
module ArJdbc
|
4
|
+
module Tasks
|
5
|
+
class DerbyDatabaseTasks < JdbcDatabaseTasks
|
6
|
+
|
7
|
+
def create
|
8
|
+
establish_connection(config)
|
9
|
+
ActiveRecord::Base.connection
|
10
|
+
end
|
11
|
+
|
12
|
+
def drop
|
13
|
+
db_dir = expand_path(config['database'])
|
14
|
+
if File.exist?(db_dir)
|
15
|
+
FileUtils.rm_r(db_dir)
|
16
|
+
FileUtils.rmdir(db_dir) rescue nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
SIZEABLE = %w( VARCHAR CLOB BLOB )
|
21
|
+
|
22
|
+
def structure_dump(filename)
|
23
|
+
establish_connection(config)
|
24
|
+
dump = File.open(filename, "w:utf-8")
|
25
|
+
|
26
|
+
meta_data = connection.jdbc_connection.meta_data
|
27
|
+
tables_rs = meta_data.getTables(nil, nil, nil, ["TABLE"].to_java(:String))
|
28
|
+
|
29
|
+
while tables_rs.next
|
30
|
+
table_name = tables_rs.getString('TABLE_NAME') # getString(3)
|
31
|
+
dump << "CREATE TABLE #{connection.quote_table_name(table_name)} (\n"
|
32
|
+
|
33
|
+
columns_rs = meta_data.getColumns(nil, nil, table_name, nil)
|
34
|
+
first_col = true
|
35
|
+
while columns_rs.next
|
36
|
+
column_name = columns_rs.getString(4)
|
37
|
+
default = columns_rs.getString(13)
|
38
|
+
if default =~ /^GENERATED_/
|
39
|
+
default = column_auto_increment_def(table_name, column_name)
|
40
|
+
elsif default
|
41
|
+
default = " DEFAULT #{default}"
|
42
|
+
end
|
43
|
+
type = columns_rs.getString(6)
|
44
|
+
column_size = columns_rs.getString(7)
|
45
|
+
nulling = ( columns_rs.getString(18) == 'NO' ? " NOT NULL" : nil )
|
46
|
+
|
47
|
+
create_column = connection.quote_column_name(column_name)
|
48
|
+
create_column << " #{type}"
|
49
|
+
create_column << ( SIZEABLE.include?(type) ? "(#{column_size})" : "" )
|
50
|
+
create_column << nulling.to_s
|
51
|
+
create_column << default.to_s
|
52
|
+
|
53
|
+
create_column = first_col ? " #{create_column}" : ",\n #{create_column}"
|
54
|
+
dump << create_column
|
55
|
+
|
56
|
+
first_col = false
|
57
|
+
end
|
58
|
+
dump << "\n);\n\n"
|
59
|
+
end
|
60
|
+
|
61
|
+
dump.close
|
62
|
+
end
|
63
|
+
|
64
|
+
def structure_load(filename)
|
65
|
+
establish_connection(config)
|
66
|
+
IO.read(filename).split(/;\n*/m).each { |ddl| connection.execute(ddl) }
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
AUTO_INCREMENT_SQL = '' <<
|
72
|
+
"SELECT AUTOINCREMENTSTART, AUTOINCREMENTINC, COLUMNNAME, REFERENCEID, COLUMNDEFAULT " <<
|
73
|
+
"FROM SYS.SYSCOLUMNS WHERE REFERENCEID = " <<
|
74
|
+
"(SELECT T.TABLEID FROM SYS.SYSTABLES T WHERE T.TABLENAME = '%s') AND COLUMNNAME = '%s'"
|
75
|
+
|
76
|
+
def column_auto_increment_def(table_name, column_name)
|
77
|
+
sql = AUTO_INCREMENT_SQL % [ table_name, column_name ]
|
78
|
+
if data = connection.execute(sql).first
|
79
|
+
if start = data['autoincrementstart']
|
80
|
+
ai_def = ' GENERATED '
|
81
|
+
ai_def << ( data['columndefault'].nil? ? "ALWAYS" : "BY DEFAULT " )
|
82
|
+
ai_def << "AS IDENTITY (START WITH "
|
83
|
+
ai_def << start.to_s
|
84
|
+
ai_def << ", INCREMENT BY "
|
85
|
+
ai_def << data['autoincrementinc'].to_s
|
86
|
+
ai_def << ")"
|
87
|
+
return ai_def
|
88
|
+
end
|
89
|
+
end
|
90
|
+
''
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'arjdbc/tasks/hsqldb_database_tasks'
|
2
|
+
|
3
|
+
module ArJdbc
|
4
|
+
module Tasks
|
5
|
+
class H2DatabaseTasks < HSQLDBDatabaseTasks
|
6
|
+
|
7
|
+
protected
|
8
|
+
|
9
|
+
# @override
|
10
|
+
def do_drop_database(config)
|
11
|
+
# ActiveRecord::JDBCError: org.h2.jdbc.JdbcSQLException:
|
12
|
+
# Database is already closed (to disable automatic closing at VM
|
13
|
+
# shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-170]:
|
14
|
+
# SHUTDOWN COMPACT
|
15
|
+
#
|
16
|
+
# connection.shutdown
|
17
|
+
connection.drop_database config['database']
|
18
|
+
end
|
19
|
+
|
20
|
+
# @override
|
21
|
+
def delete_database_files(config)
|
22
|
+
return unless db_base = database_base_name(config)
|
23
|
+
db_files = [ "#{db_base}.h2.db", "#{db_base}.lock.db", "#{db_base}.trace.db" ]
|
24
|
+
db_files.each { |file| File.delete(file) if File.exist?(file) }
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'arjdbc/tasks/jdbc_database_tasks'
|
2
|
+
|
3
|
+
module ArJdbc
|
4
|
+
module Tasks
|
5
|
+
class HSQLDBDatabaseTasks < JdbcDatabaseTasks
|
6
|
+
|
7
|
+
def create
|
8
|
+
establish_connection(config)
|
9
|
+
ActiveRecord::Base.connection
|
10
|
+
end
|
11
|
+
|
12
|
+
def drop
|
13
|
+
error = nil
|
14
|
+
begin
|
15
|
+
establish_connection(config)
|
16
|
+
do_drop_database(config)
|
17
|
+
rescue => e
|
18
|
+
error = e
|
19
|
+
raise error
|
20
|
+
ensure
|
21
|
+
begin
|
22
|
+
keep_db_files = ENV['KEEP_DB_FILES'] && ENV['KEEP_DB_FILES'] != 'false'
|
23
|
+
delete_database_files(config) unless keep_db_files
|
24
|
+
rescue => e
|
25
|
+
raise e unless error
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
alias :purge :drop
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
def do_drop_database(config)
|
34
|
+
connection.drop_database config['database']
|
35
|
+
connection.shutdown
|
36
|
+
end
|
37
|
+
|
38
|
+
def delete_database_files(config)
|
39
|
+
return unless db_base = database_base_name(config)
|
40
|
+
Dir.glob("#{db_base}.*").each do |file|
|
41
|
+
# test.hsqldb.tmp (dir)
|
42
|
+
# test.hsqldb.lck
|
43
|
+
# test.hsqldb.lobs
|
44
|
+
# test.hsqldb.script
|
45
|
+
# test.hsqldb.properties
|
46
|
+
if File.directory?(file)
|
47
|
+
FileUtils.rm_r(file)
|
48
|
+
FileUtils.rmdir(file)
|
49
|
+
else
|
50
|
+
FileUtils.rm(file)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
if File.exist?(db_base)
|
54
|
+
FileUtils.rm_r(db_base)
|
55
|
+
FileUtils.rmdir(db_base)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def database_base_name(config)
|
62
|
+
db = config['database']
|
63
|
+
db[0, 4] == 'mem:' ? nil : begin
|
64
|
+
expand_path db[0, 4] == 'file:' ? db[4..-1] : db
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
module ArJdbc
|
2
|
+
module Tasks
|
3
|
+
# Sharing task related code between AR 3.x and 4.x
|
4
|
+
#
|
5
|
+
# @note this class needs to conform to the API available since AR 4.0
|
6
|
+
# mostly to be usable with ActiveRecord::Tasks::DatabaseTasks module
|
7
|
+
class JdbcDatabaseTasks
|
8
|
+
|
9
|
+
attr_reader :configuration
|
10
|
+
alias_method :config, :configuration
|
11
|
+
|
12
|
+
def initialize(configuration)
|
13
|
+
@configuration = configuration
|
14
|
+
end
|
15
|
+
|
16
|
+
delegate :connection, :establish_connection, :to => ActiveRecord::Base
|
17
|
+
|
18
|
+
def create
|
19
|
+
begin
|
20
|
+
establish_connection(config)
|
21
|
+
ActiveRecord::Base.connection
|
22
|
+
if defined? ActiveRecord::Tasks::DatabaseAlreadyExists
|
23
|
+
raise ActiveRecord::Tasks::DatabaseAlreadyExists # AR-4.x
|
24
|
+
end # silence on AR < 4.0
|
25
|
+
rescue #=> error # database does not exists :
|
26
|
+
url = config['url']
|
27
|
+
url = $1 if url && url =~ /^(.*(?<!\/)\/)(?=\w)/
|
28
|
+
|
29
|
+
establish_connection(config.merge('database' => nil, 'url' => url))
|
30
|
+
|
31
|
+
unless connection.respond_to?(:create_database)
|
32
|
+
raise "AR-JDBC adapter '#{adapter_with_spec}' does not support create_database"
|
33
|
+
end
|
34
|
+
connection.create_database(config['database'], config)
|
35
|
+
|
36
|
+
establish_connection(config)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def drop
|
41
|
+
establish_connection(config)
|
42
|
+
unless ActiveRecord::Base.connection.respond_to?(:drop_database)
|
43
|
+
raise "AR-JDBC adapter '#{adapter_with_spec}' does not support drop_database"
|
44
|
+
end
|
45
|
+
connection.drop_database config['database']
|
46
|
+
end
|
47
|
+
|
48
|
+
def purge
|
49
|
+
establish_connection(config) # :test
|
50
|
+
unless ActiveRecord::Base.connection.respond_to?(:recreate_database)
|
51
|
+
raise "AR-JDBC adapter '#{adapter_with_spec}' does not support recreate_database (purge)"
|
52
|
+
end
|
53
|
+
db_name = ActiveRecord::Base.connection.database_name
|
54
|
+
ActiveRecord::Base.connection.recreate_database(db_name, config)
|
55
|
+
end
|
56
|
+
|
57
|
+
def charset
|
58
|
+
establish_connection(config)
|
59
|
+
if connection.respond_to?(:charset)
|
60
|
+
puts connection.charset
|
61
|
+
elsif connection.respond_to?(:encoding)
|
62
|
+
puts connection.encoding
|
63
|
+
else
|
64
|
+
raise "AR-JDBC adapter '#{adapter_with_spec}' does not support charset/encoding"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def collation
|
69
|
+
establish_connection(config)
|
70
|
+
if connection.respond_to?(:collation)
|
71
|
+
puts connection.collation
|
72
|
+
else
|
73
|
+
raise "AR-JDBC adapter '#{adapter_with_spec}' does not support collation"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def structure_dump(filename)
|
78
|
+
establish_connection(config)
|
79
|
+
if connection.respond_to?(:structure_dump)
|
80
|
+
File.open(filename, "w:utf-8") { |f| f << connection.structure_dump }
|
81
|
+
else
|
82
|
+
raise "AR-JDBC adapter '#{adapter_with_spec}' does not support structure_dump"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def structure_load(filename)
|
87
|
+
establish_connection(config)
|
88
|
+
if connection.respond_to?(:structure_load)
|
89
|
+
connection.structure_load IO.read(filename)
|
90
|
+
else
|
91
|
+
#IO.read(filename).split(/;\n*/m).each do |ddl|
|
92
|
+
# connection.execute(ddl)
|
93
|
+
#end
|
94
|
+
raise "AR-JDBC adapter '#{adapter_with_spec}' does not support structure_load"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
def adapter_with_spec
|
101
|
+
adapter, spec = config['adapter'], config['adapter_spec']
|
102
|
+
spec ? "#{adapter} (#{spec})" : adapter
|
103
|
+
end
|
104
|
+
|
105
|
+
protected
|
106
|
+
|
107
|
+
def expand_path(path)
|
108
|
+
require 'pathname'
|
109
|
+
path = Pathname.new path
|
110
|
+
return path.to_s if path.absolute?
|
111
|
+
rails_root ? File.join(rails_root, path) : File.expand_path(path)
|
112
|
+
end
|
113
|
+
|
114
|
+
private
|
115
|
+
|
116
|
+
def rails_root
|
117
|
+
defined?(Rails.root) ? Rails.root : ( RAILS_ROOT )
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|