Skip to content

Commit 3b893cb

Browse files
committed
review Postgres' SchemaCreation to match our convention
1 parent 75bf40c commit 3b893cb

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

lib/arjdbc/postgresql/adapter.rb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module PostgreSQL
1111

1212
require 'arjdbc/postgresql/column'
1313
require 'arjdbc/postgresql/explain_support'
14+
require 'arjdbc/postgresql/schema_creation' # AR 4.x
1415

1516
# @see ActiveRecord::ConnectionAdapters::JdbcAdapter#jdbc_connection_class
1617
def self.jdbc_connection_class
@@ -250,13 +251,6 @@ def migration_keys
250251
super + [:array]
251252
end if AR4_COMPAT
252253

253-
if ActiveRecord::VERSION::MAJOR > 3
254-
255-
require 'arjdbc/postgresql/schema_creation'
256-
def schema_creation; SchemaCreation.new(self); end
257-
258-
end
259-
260254
# Enable standard-conforming strings if available.
261255
def set_standard_conforming_strings
262256
self.standard_conforming_strings=(true)
@@ -975,7 +969,7 @@ def add_column(table_name, column_name, type, options = {})
975969

976970
change_column_default(table_name, column_name, default) if options_include_default?(options)
977971
change_column_null(table_name, column_name, false, default) if notnull
978-
end
972+
end if ActiveRecord::VERSION::MAJOR < 4
979973

980974
# Changes the column of a table.
981975
def change_column(table_name, column_name, type, options = {})
@@ -1004,24 +998,24 @@ def change_column(table_name, column_name, type, options = {})
1004998

1005999
change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
10061000
change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
1007-
end
1001+
end # unless const_defined? :SchemaCreation
10081002

10091003
# Changes the default value of a table column.
10101004
def change_column_default(table_name, column_name, default)
10111005
execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote(default)}"
1012-
end
1006+
end # unless const_defined? :SchemaCreation
10131007

10141008
def change_column_null(table_name, column_name, null, default = nil)
10151009
unless null || default.nil?
10161010
execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
10171011
end
10181012
execute("ALTER TABLE #{quote_table_name(table_name)} ALTER #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL")
1019-
end
1013+
end # unless const_defined? :SchemaCreation
10201014

10211015
def rename_column(table_name, column_name, new_column_name)
10221016
execute "ALTER TABLE #{quote_table_name(table_name)} RENAME COLUMN #{quote_column_name(column_name)} TO #{quote_column_name(new_column_name)}"
10231017
rename_column_indexes(table_name, column_name, new_column_name) if respond_to?(:rename_column_indexes) # AR-4.0 SchemaStatements
1024-
end
1018+
end # unless const_defined? :SchemaCreation
10251019

10261020
def add_index(table_name, column_name, options = {})
10271021
index_name, index_type, index_columns, index_options, index_algorithm, index_using = add_index_options(table_name, column_name, options)

lib/arjdbc/postgresql/schema_creation.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# NOTE: kindly borrowed from AR 4.0.0 (rc1) - only to be used on AR >= 4.0 !
21
module ArJdbc
32
module PostgreSQL
4-
class SchemaCreation < ActiveRecord::ConnectionAdapters::AbstractAdapter::SchemaCreation
3+
# @private copied from native adapter 4.0/4.1
4+
class SchemaCreation < ::ActiveRecord::ConnectionAdapters::AbstractAdapter::SchemaCreation
55

66
private
77

@@ -32,7 +32,11 @@ def add_column_options!(sql, options)
3232
super
3333
end
3434
end
35+
end
3536

37+
def schema_creation
38+
SchemaCreation.new self
3639
end
40+
3741
end
38-
end
42+
end if ::ActiveRecord::ConnectionAdapters::AbstractAdapter.const_defined? :SchemaCreation

0 commit comments

Comments
 (0)