rails_sql_views4 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +121 -0
- data/lib/active_record/view.rb +80 -0
- data/lib/core_ext/module.rb +13 -0
- data/lib/rails_sql_views4/connection_adapters/abstract/schema_definitions.rb +63 -0
- data/lib/rails_sql_views4/connection_adapters/abstract/schema_statements.rb +85 -0
- data/lib/rails_sql_views4/connection_adapters/abstract_adapter.rb +41 -0
- data/lib/rails_sql_views4/connection_adapters/mysql2_adapter.rb +66 -0
- data/lib/rails_sql_views4/connection_adapters/mysql_adapter.rb +66 -0
- data/lib/rails_sql_views4/connection_adapters/oci_adapter.rb +33 -0
- data/lib/rails_sql_views4/connection_adapters/oracle_adapter.rb +33 -0
- data/lib/rails_sql_views4/connection_adapters/oracleenhanced_adapter.rb +39 -0
- data/lib/rails_sql_views4/connection_adapters/oracleenhanced_adapter.rb.orig +72 -0
- data/lib/rails_sql_views4/connection_adapters/postgresql_adapter.rb +69 -0
- data/lib/rails_sql_views4/connection_adapters/postgresql_adapter.rb.orig +69 -0
- data/lib/rails_sql_views4/connection_adapters/sqlite3_adapter.rb +68 -0
- data/lib/rails_sql_views4/connection_adapters/sqlite_adapter.rb +68 -0
- data/lib/rails_sql_views4/connection_adapters/sqlserver_adapter.rb +43 -0
- data/lib/rails_sql_views4/loader.rb +18 -0
- data/lib/rails_sql_views4/schema_dumper.rb +114 -0
- data/lib/rails_sql_views4/version.rb +3 -0
- data/lib/rails_sql_views4.rb +28 -0
- data/lib/tasks/rails_sql_views4_tasks.rake +4 -0
- data/test/README.NOT_UP_TO_DATE +63 -0
- data/test/adapter_test.rb +93 -0
- data/test/connection/native_mysql/connection.rb +32 -0
- data/test/connection/native_mysql/schema.sql +34 -0
- data/test/connection/native_postgresql/connection.rb +31 -0
- data/test/connection/native_postgresql/schema.sql +33 -0
- data/test/connection/oracle_enhanced/connection.rb +29 -0
- data/test/connection/oracle_enhanced/procedures.sql +15 -0
- data/test/connection/oracle_enhanced/schema.sql +39 -0
- data/test/connection.example.yml +12 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/item.rb +4 -0
- data/test/dummy/app/models/person.rb +5 -0
- data/test/dummy/app/models/person2.rb +4 -0
- data/test/dummy/app/models/place.rb +2 -0
- data/test/dummy/app/models/v_person.rb +8 -0
- data/test/dummy/app/models/v_profile.rb +3 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +78 -0
- data/test/dummy/config/environments/test.rb +39 -0
- data/test/dummy/config/initializers/assets.rb +8 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/migrate/20141228200436_create_people.rb +10 -0
- data/test/dummy/db/migrate/20141228200437_create_people2.rb +11 -0
- data/test/dummy/db/migrate/20141228200438_create_places.rb +12 -0
- data/test/dummy/db/migrate/20141228200439_create_items.rb +8 -0
- data/test/dummy/db/migrate/20141228200440_create_items_people.rb +9 -0
- data/test/dummy/db/schema.rb +53 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +439 -0
- data/test/dummy/log/test.log +29320 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/test/fixtures/persons.yml +7 -0
- data/test/dummy/test/models/person_test.rb +7 -0
- data/test/rails_sql_views4_test.rb +11 -0
- data/test/schema.native_mysql.expected.rb +51 -0
- data/test/schema.native_postgresql.expected.rb +51 -0
- data/test/schema.native_postgresql.out.rb +69 -0
- data/test/schema.oracle_enhanced.expected.rb +51 -0
- data/test/schema_dumper_test.rb +134 -0
- data/test/test_helper.rb +41 -0
- data/test/test_helper.rb.old +30 -0
- data/test/view_model_test.rb +68 -0
- data/test/view_operations_test.rb +50 -0
- metadata +225 -0
@@ -0,0 +1,72 @@
|
|
1
|
+
module RailsSqlViews4
|
2
|
+
module ConnectionAdapters
|
3
|
+
module OracleEnhancedAdapter
|
4
|
+
<<<<<<< HEAD
|
5
|
+
def self.included(base)
|
6
|
+
base.alias_method_chain :tables, :views_included
|
7
|
+
end
|
8
|
+
=======
|
9
|
+
>>>>>>> Add support for oracle_enhanced adapter. Don't alias tables in PostgreSQL adapter if it's already been aliased.
|
10
|
+
# Returns true as this adapter supports views.
|
11
|
+
def supports_views?
|
12
|
+
true
|
13
|
+
end
|
14
|
+
|
15
|
+
<<<<<<< HEAD
|
16
|
+
def tables_with_views_included(name = nil)
|
17
|
+
tables = []
|
18
|
+
sql = " SELECT TABLE_NAME FROM USER_TABLES
|
19
|
+
UNION
|
20
|
+
SELECT VIEW_NAME AS TABLE_NAME FROM USER_VIEWS"
|
21
|
+
cursor = execute(sql, name)
|
22
|
+
while row = cursor.fetch
|
23
|
+
tables << row[0].downcase
|
24
|
+
end
|
25
|
+
tables
|
26
|
+
end
|
27
|
+
|
28
|
+
=======
|
29
|
+
>>>>>>> Add support for oracle_enhanced adapter. Don't alias tables in PostgreSQL adapter if it's already been aliased.
|
30
|
+
def base_tables(name = nil) #:nodoc:
|
31
|
+
tables = []
|
32
|
+
cursor = execute("SELECT TABLE_NAME FROM USER_TABLES", name)
|
33
|
+
while row = cursor.fetch
|
34
|
+
<<<<<<< HEAD
|
35
|
+
tables << row[0].downcase
|
36
|
+
=======
|
37
|
+
tables << row[0]
|
38
|
+
>>>>>>> Add support for oracle_enhanced adapter. Don't alias tables in PostgreSQL adapter if it's already been aliased.
|
39
|
+
end
|
40
|
+
tables
|
41
|
+
end
|
42
|
+
alias nonview_tables base_tables
|
43
|
+
|
44
|
+
def views(name = nil) #:nodoc:
|
45
|
+
views = []
|
46
|
+
cursor = execute("SELECT VIEW_NAME FROM USER_VIEWS", name)
|
47
|
+
while row = cursor.fetch
|
48
|
+
<<<<<<< HEAD
|
49
|
+
views << row[0].downcase
|
50
|
+
=======
|
51
|
+
views << row[0]
|
52
|
+
>>>>>>> Add support for oracle_enhanced adapter. Don't alias tables in PostgreSQL adapter if it's already been aliased.
|
53
|
+
end
|
54
|
+
views
|
55
|
+
end
|
56
|
+
|
57
|
+
# Get the view select statement for the specified table.
|
58
|
+
def view_select_statement(view, name=nil)
|
59
|
+
<<<<<<< HEAD
|
60
|
+
view.upcase!
|
61
|
+
=======
|
62
|
+
>>>>>>> Add support for oracle_enhanced adapter. Don't alias tables in PostgreSQL adapter if it's already been aliased.
|
63
|
+
cursor = execute("SELECT TEXT FROM USER_VIEWS WHERE VIEW_NAME = '#{view}'", name)
|
64
|
+
if row = cursor.fetch
|
65
|
+
return row[0]
|
66
|
+
else
|
67
|
+
raise "No view called #{view} found"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module RailsSqlViews4
|
2
|
+
module ConnectionAdapters
|
3
|
+
module PostgreSQLAdapter
|
4
|
+
def self.included(base)
|
5
|
+
base.alias_method_chain :tables, :views_included
|
6
|
+
end
|
7
|
+
# Returns true as this adapter supports views.
|
8
|
+
def supports_views?
|
9
|
+
true
|
10
|
+
end
|
11
|
+
|
12
|
+
def tables_with_views_included(name = nil)
|
13
|
+
q = <<-SQL
|
14
|
+
SELECT table_name, table_type
|
15
|
+
FROM information_schema.tables
|
16
|
+
WHERE table_schema IN (#{schemas})
|
17
|
+
AND table_type IN ('BASE TABLE', 'VIEW')
|
18
|
+
SQL
|
19
|
+
|
20
|
+
query(q, name).map { |row| row[0] }
|
21
|
+
end
|
22
|
+
|
23
|
+
def base_tables(name = nil)
|
24
|
+
q = <<-SQL
|
25
|
+
SELECT table_name, table_type
|
26
|
+
FROM information_schema.tables
|
27
|
+
WHERE table_schema IN (#{schemas})
|
28
|
+
AND table_type = 'BASE TABLE'
|
29
|
+
SQL
|
30
|
+
|
31
|
+
query(q, name).map { |row| row[0] }
|
32
|
+
end
|
33
|
+
alias nonview_tables base_tables
|
34
|
+
|
35
|
+
def views(name = nil) #:nodoc:
|
36
|
+
q = <<-SQL
|
37
|
+
SELECT table_name, table_type
|
38
|
+
FROM information_schema.tables
|
39
|
+
WHERE table_schema IN (#{schemas})
|
40
|
+
AND table_type = 'VIEW'
|
41
|
+
SQL
|
42
|
+
|
43
|
+
query(q, name).map { |row| row[0] }
|
44
|
+
end
|
45
|
+
|
46
|
+
def view_select_statement(view, name = nil)
|
47
|
+
q = <<-SQL
|
48
|
+
SELECT view_definition
|
49
|
+
FROM information_schema.views
|
50
|
+
WHERE table_catalog = (SELECT catalog_name FROM information_schema.information_schema_catalog_name)
|
51
|
+
AND table_schema IN (#{schemas})
|
52
|
+
AND table_name = '#{view}'
|
53
|
+
SQL
|
54
|
+
|
55
|
+
# TODO
|
56
|
+
# puts in postgres adapter ??? not in SQLITE ?
|
57
|
+
# select_value(q, name) or raise "No view called #{view} found"
|
58
|
+
select_value(q, name).gsub("CREATE VIEW #{view} AS ", "")) or raise "No view called #{view} found"
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def schemas
|
65
|
+
schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module RailsSqlViews4
|
2
|
+
module ConnectionAdapters
|
3
|
+
module PostgreSQLAdapter
|
4
|
+
def self.included(base)
|
5
|
+
<<<<<<< HEAD
|
6
|
+
base.alias_method_chain :tables, :views_included unless method_defined?(:tables_with_views_included)
|
7
|
+
=======
|
8
|
+
base.alias_method_chain :tables, :views_included
|
9
|
+
>>>>>>> Make tests pass again for PostgreSQL and MySQL adapters.
|
10
|
+
end
|
11
|
+
# Returns true as this adapter supports views.
|
12
|
+
def supports_views?
|
13
|
+
true
|
14
|
+
end
|
15
|
+
|
16
|
+
def tables_with_views_included(name = nil)
|
17
|
+
q = <<-SQL
|
18
|
+
SELECT table_name, table_type
|
19
|
+
FROM information_schema.tables
|
20
|
+
WHERE table_schema IN (#{schemas})
|
21
|
+
AND table_type IN ('BASE TABLE', 'VIEW')
|
22
|
+
SQL
|
23
|
+
|
24
|
+
query(q, name).map { |row| row[0] }
|
25
|
+
end
|
26
|
+
|
27
|
+
def base_tables(name = nil)
|
28
|
+
q = <<-SQL
|
29
|
+
SELECT table_name, table_type
|
30
|
+
FROM information_schema.tables
|
31
|
+
WHERE table_schema IN (#{schemas})
|
32
|
+
AND table_type = 'BASE TABLE'
|
33
|
+
SQL
|
34
|
+
|
35
|
+
query(q, name).map { |row| row[0] }
|
36
|
+
end
|
37
|
+
alias nonview_tables base_tables
|
38
|
+
|
39
|
+
def views(name = nil) #:nodoc:
|
40
|
+
q = <<-SQL
|
41
|
+
SELECT table_name, table_type
|
42
|
+
FROM information_schema.tables
|
43
|
+
WHERE table_schema IN (#{schemas})
|
44
|
+
AND table_type = 'VIEW'
|
45
|
+
SQL
|
46
|
+
|
47
|
+
query(q, name).map { |row| row[0] }
|
48
|
+
end
|
49
|
+
|
50
|
+
def view_select_statement(view, name = nil)
|
51
|
+
q = <<-SQL
|
52
|
+
SELECT view_definition
|
53
|
+
FROM information_schema.views
|
54
|
+
WHERE table_catalog = (SELECT catalog_name FROM information_schema.information_schema_catalog_name)
|
55
|
+
AND table_schema IN (#{schemas})
|
56
|
+
AND table_name = '#{view}'
|
57
|
+
SQL
|
58
|
+
|
59
|
+
select_value(q, name) or raise "No view called #{view} found"
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def schemas
|
65
|
+
schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module RailsSqlViews4
|
2
|
+
module ConnectionAdapters
|
3
|
+
module SQLite3Adapter
|
4
|
+
def supports_views?
|
5
|
+
true
|
6
|
+
end
|
7
|
+
|
8
|
+
def supports_drop_table_cascade?
|
9
|
+
return false
|
10
|
+
end
|
11
|
+
|
12
|
+
def tables(name = nil) #:nodoc:
|
13
|
+
sql = <<-SQL
|
14
|
+
SELECT name
|
15
|
+
FROM sqlite_master
|
16
|
+
WHERE (type = 'table' OR type = 'view') AND NOT name = 'sqlite_sequence'
|
17
|
+
SQL
|
18
|
+
|
19
|
+
execute(sql, name).map do |row|
|
20
|
+
row[0]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def base_tables(name = nil)
|
25
|
+
sql = <<-SQL
|
26
|
+
SELECT name
|
27
|
+
FROM sqlite_master
|
28
|
+
WHERE (type = 'table') AND NOT name = 'sqlite_sequence'
|
29
|
+
SQL
|
30
|
+
|
31
|
+
execute(sql, name).map do |row|
|
32
|
+
row[0]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
alias nonview_tables base_tables
|
36
|
+
|
37
|
+
def views(name = nil)
|
38
|
+
sql = <<-SQL
|
39
|
+
SELECT name
|
40
|
+
FROM sqlite_master
|
41
|
+
WHERE type = 'view' AND NOT name = 'sqlite_sequence'
|
42
|
+
SQL
|
43
|
+
|
44
|
+
execute(sql, name).map do |row|
|
45
|
+
row[0]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Get the view select statement for the specified table.
|
50
|
+
def view_select_statement(view, name = nil)
|
51
|
+
sql = <<-SQL
|
52
|
+
SELECT sql
|
53
|
+
FROM sqlite_master
|
54
|
+
WHERE name = '#{view}' AND NOT name = 'sqlite_sequence'
|
55
|
+
SQL
|
56
|
+
|
57
|
+
# TODO
|
58
|
+
# (select_value(sql, name).gsub("CREATE VIEW #{view} AS ", "")) or raise "No view called #{view} found"
|
59
|
+
(select_value(sql, name).gsub("CREATE VIEW \"#{view}\" AS ", "")) or raise "No view called #{view} found"
|
60
|
+
end
|
61
|
+
|
62
|
+
def supports_view_columns_definition?
|
63
|
+
false
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module RailsSqlViews4
|
2
|
+
module ConnectionAdapters
|
3
|
+
module SQLiteAdapter
|
4
|
+
def supports_views?
|
5
|
+
true
|
6
|
+
end
|
7
|
+
|
8
|
+
def supports_drop_table_cascade?
|
9
|
+
return false
|
10
|
+
end
|
11
|
+
|
12
|
+
def tables(name = nil) #:nodoc:
|
13
|
+
sql = <<-SQL
|
14
|
+
SELECT name
|
15
|
+
FROM sqlite_master
|
16
|
+
WHERE (type = 'table' OR type = 'view') AND NOT name = 'sqlite_sequence'
|
17
|
+
SQL
|
18
|
+
|
19
|
+
execute(sql, name).map do |row|
|
20
|
+
row[0]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def base_tables(name = nil)
|
25
|
+
sql = <<-SQL
|
26
|
+
SELECT name
|
27
|
+
FROM sqlite_master
|
28
|
+
WHERE (type = 'table') AND NOT name = 'sqlite_sequence'
|
29
|
+
SQL
|
30
|
+
|
31
|
+
execute(sql, name).map do |row|
|
32
|
+
row[0]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
alias nonview_tables base_tables
|
36
|
+
|
37
|
+
def views(name = nil)
|
38
|
+
sql = <<-SQL
|
39
|
+
SELECT name
|
40
|
+
FROM sqlite_master
|
41
|
+
WHERE type = 'view' AND NOT name = 'sqlite_sequence'
|
42
|
+
SQL
|
43
|
+
|
44
|
+
execute(sql, name).map do |row|
|
45
|
+
row[0]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Get the view select statement for the specified table.
|
50
|
+
def view_select_statement(view, name = nil)
|
51
|
+
sql = <<-SQL
|
52
|
+
SELECT sql
|
53
|
+
FROM sqlite_master
|
54
|
+
WHERE name = '#{view}' AND NOT name = 'sqlite_sequence'
|
55
|
+
SQL
|
56
|
+
|
57
|
+
#TODO
|
58
|
+
# (select_value(sql, name).gsub("CREATE VIEW #{view} AS ", "")) or raise "No view called #{view} found"
|
59
|
+
(select_value(sql, name).gsub("CREATE VIEW \"#{view}\" AS ", "")) or raise "No view called #{view} found"
|
60
|
+
end
|
61
|
+
|
62
|
+
def supports_view_columns_definition?
|
63
|
+
false
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module RailsSqlViews4
|
2
|
+
module ConnectionAdapters
|
3
|
+
module SQLServerAdapter
|
4
|
+
# Returns true as this adapter supports views.
|
5
|
+
def supports_views?
|
6
|
+
true
|
7
|
+
end
|
8
|
+
|
9
|
+
# Get all of the non-view tables from the currently connected schema
|
10
|
+
def base_tables(name = nil)
|
11
|
+
# this is untested
|
12
|
+
select_values("SELECT table_name FROM information_schema.tables", name)
|
13
|
+
end
|
14
|
+
alias nonview_tables base_tables
|
15
|
+
|
16
|
+
# Returns all the view names from the currently connected schema.
|
17
|
+
def views(name = nil)
|
18
|
+
select_values("SELECT table_name FROM information_schema.views", name)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Get the view select statement for the specified view.
|
22
|
+
def view_select_statement(view, name=nil)
|
23
|
+
q =<<-ENDSQL
|
24
|
+
SELECT view_definition FROM information_schema.views
|
25
|
+
WHERE table_name = '#{view}'
|
26
|
+
ENDSQL
|
27
|
+
|
28
|
+
view_def = select_value(q, name)
|
29
|
+
|
30
|
+
if view_def
|
31
|
+
return convert_statement(view_def)
|
32
|
+
else
|
33
|
+
raise "No view called #{view} found"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def convert_statement(s)
|
39
|
+
s.sub(/^CREATE.* AS (select .*)/i, '\1').gsub(/\n/, '')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
|
2
|
+
module RailsSqlViews4
|
3
|
+
module Loader
|
4
|
+
SUPPORTED_ADAPTERS = %w( Mysql Mysql2 PostgreSQL SQLServer SQLite SQLite3 OracleEnhanced )
|
5
|
+
|
6
|
+
def self.load_extensions
|
7
|
+
SUPPORTED_ADAPTERS.each do |db|
|
8
|
+
if ActiveRecord::ConnectionAdapters.const_defined?("#{db}Adapter")
|
9
|
+
require "rails_sql_views4/connection_adapters/#{db.downcase}_adapter"
|
10
|
+
ActiveRecord::ConnectionAdapters.const_get("#{db}Adapter").class_eval do
|
11
|
+
include RailsSqlViews4::ConnectionAdapters::AbstractAdapter
|
12
|
+
include RailsSqlViews4::ConnectionAdapters.const_get("#{db}Adapter")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
module RailsSqlViews4
|
2
|
+
module SchemaDumper
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.alias_method_chain :trailer, :views
|
6
|
+
base.alias_method_chain :dump, :views
|
7
|
+
base.alias_method_chain :tables, :views_excluded
|
8
|
+
|
9
|
+
# A list of views which should not be dumped to the schema.
|
10
|
+
# Acceptable values are strings as well as regexp.
|
11
|
+
# This setting is only used if ActiveRecord::Base.schema_format == :ruby
|
12
|
+
base.cattr_accessor :ignore_views
|
13
|
+
base.ignore_views = []
|
14
|
+
# Optional: specify the order that in which views are created.
|
15
|
+
# This allows views to depend on and include fields from other views.
|
16
|
+
# It is not necessary to specify all the view names, just the ones that
|
17
|
+
# need to be created first
|
18
|
+
base.cattr_accessor :view_creation_order
|
19
|
+
base.view_creation_order = []
|
20
|
+
end
|
21
|
+
|
22
|
+
def trailer_with_views(stream)
|
23
|
+
# do nothing...we'll call this later
|
24
|
+
end
|
25
|
+
|
26
|
+
# Add views to the end of the dump stream
|
27
|
+
def dump_with_views(stream)
|
28
|
+
dump_without_views(stream)
|
29
|
+
begin
|
30
|
+
if @connection.supports_views?
|
31
|
+
views(stream)
|
32
|
+
end
|
33
|
+
rescue => e
|
34
|
+
if ActiveRecord::Base.logger
|
35
|
+
ActiveRecord::Base.logger.error "Unable to dump views: #{e}"
|
36
|
+
else
|
37
|
+
raise e
|
38
|
+
end
|
39
|
+
end
|
40
|
+
trailer_without_views(stream)
|
41
|
+
stream
|
42
|
+
end
|
43
|
+
|
44
|
+
# Add views to the stream
|
45
|
+
def views(stream)
|
46
|
+
if view_creation_order.empty?
|
47
|
+
sorted_views = @connection.views.sort
|
48
|
+
else
|
49
|
+
# set union, merge by joining arrays, removing dups
|
50
|
+
# this will float the view name sin view_creation_order to the top
|
51
|
+
# without requiring all the views to be specified
|
52
|
+
sorted_views = view_creation_order | @connection.views
|
53
|
+
end
|
54
|
+
sorted_views.each do |v|
|
55
|
+
next if [ActiveRecord::Migrator.schema_migrations_table_name, ignore_views].flatten.any? do |ignored|
|
56
|
+
case ignored
|
57
|
+
when String then v == ignored
|
58
|
+
when Symbol then v == ignored.to_s
|
59
|
+
when Regexp then v =~ ignored
|
60
|
+
else
|
61
|
+
raise StandardError, 'ActiveRecord::SchemaDumper.ignore_views accepts an array of String and / or Regexp values.'
|
62
|
+
end
|
63
|
+
end
|
64
|
+
view(v, stream)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Add the specified view to the stream
|
69
|
+
def view(view, stream)
|
70
|
+
columns = @connection.columns(view).collect { |c| c.name }
|
71
|
+
begin
|
72
|
+
v = StringIO.new
|
73
|
+
|
74
|
+
v.print " create_view #{view.inspect} "
|
75
|
+
|
76
|
+
v.print ", #{@connection.view_select_statement(view).dump}"
|
77
|
+
v.print ", :force => true"
|
78
|
+
v.puts " do |v|"
|
79
|
+
|
80
|
+
columns.each do |column|
|
81
|
+
v.print " v.column :#{column}"
|
82
|
+
v.puts
|
83
|
+
end
|
84
|
+
|
85
|
+
v.puts " end"
|
86
|
+
v.puts
|
87
|
+
|
88
|
+
v.rewind
|
89
|
+
stream.print v.read
|
90
|
+
rescue => e
|
91
|
+
stream.puts "# Could not dump view #{view.inspect} because of following #{e.class}"
|
92
|
+
stream.puts "# #{e.message}"
|
93
|
+
stream.puts
|
94
|
+
end
|
95
|
+
|
96
|
+
stream
|
97
|
+
end
|
98
|
+
|
99
|
+
def tables_with_views_excluded(stream)
|
100
|
+
@connection.base_tables.sort.each do |tbl|
|
101
|
+
next if [ActiveRecord::Migrator.schema_migrations_table_name, ignore_tables].flatten.any? do |ignored|
|
102
|
+
case ignored
|
103
|
+
when String then tbl == ignored
|
104
|
+
when Regexp then tbl =~ ignored
|
105
|
+
else
|
106
|
+
raise StandardError, 'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values.'
|
107
|
+
end
|
108
|
+
end
|
109
|
+
table(tbl, stream)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module RailsSqlViews4
|
2
|
+
end
|
3
|
+
|
4
|
+
|
5
|
+
$:.unshift(File.dirname(__FILE__))
|
6
|
+
|
7
|
+
require 'active_record'
|
8
|
+
|
9
|
+
require 'core_ext/module'
|
10
|
+
|
11
|
+
require 'rails_sql_views4/connection_adapters/abstract/schema_definitions'
|
12
|
+
require 'rails_sql_views4/connection_adapters/abstract/schema_statements'
|
13
|
+
require 'rails_sql_views4/connection_adapters/abstract_adapter'
|
14
|
+
require 'rails_sql_views4/schema_dumper'
|
15
|
+
require 'rails_sql_views4/loader'
|
16
|
+
|
17
|
+
ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do
|
18
|
+
include RailsSqlViews4::ConnectionAdapters::SchemaStatements
|
19
|
+
def self.inherited(sub)
|
20
|
+
RailsSqlViews4::Loader.load_extensions
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
ActiveRecord::SchemaDumper.class_eval do
|
25
|
+
include RailsSqlViews4::SchemaDumper
|
26
|
+
end
|
27
|
+
|
28
|
+
RailsSqlViews4::Loader.load_extensions
|
@@ -0,0 +1,63 @@
|
|
1
|
+
== Executing Tests
|
2
|
+
|
3
|
+
First create the test database: rails_sql_views_unittest
|
4
|
+
|
5
|
+
Next, copy test/connection.example.yml to test/connection.yml and change the
|
6
|
+
settings to match your database configuration that you are testing.
|
7
|
+
|
8
|
+
To run the tests either use:
|
9
|
+
|
10
|
+
rake
|
11
|
+
|
12
|
+
Which will run the MySQL tests, or use something like the following:
|
13
|
+
|
14
|
+
rake DB=native_postgresql
|
15
|
+
|
16
|
+
Substituting the directory name inside the connection directory.
|
17
|
+
|
18
|
+
== Status
|
19
|
+
|
20
|
+
Currently the following adapters have been tested:
|
21
|
+
|
22
|
+
* MySQL
|
23
|
+
* PostgreSQL Pure Ruby
|
24
|
+
|
25
|
+
The Oracle and SQL Server adapters have not been tested by me.
|
26
|
+
|
27
|
+
== Implementing Adapters
|
28
|
+
|
29
|
+
If you would like to implement an adapter, it should go in
|
30
|
+
lib/rails_sql_views/connection_adapters. Follow the conventions
|
31
|
+
of the other adapters currently implemented. Every adapter must implement the
|
32
|
+
following methods:
|
33
|
+
|
34
|
+
module RailsSqlViews4
|
35
|
+
base_tables(name = nil)
|
36
|
+
views(name = nil)
|
37
|
+
view_select_statement(view, name=nil)
|
38
|
+
|
39
|
+
The suports_views? method must return true. The views method must return an
|
40
|
+
array of all view names. The view_select_statement method must return the
|
41
|
+
select statement used to construct the specified view.
|
42
|
+
|
43
|
+
In addition you must include the following for testing purposes:
|
44
|
+
|
45
|
+
The script which establishes the database connection in ActiveRecord:
|
46
|
+
|
47
|
+
test/connection/driver_name/connection.rb
|
48
|
+
|
49
|
+
The schema to setup the test database in your drivers native form:
|
50
|
+
|
51
|
+
test/connection/driver_name/schema.sql
|
52
|
+
|
53
|
+
The expected schema output from a schema dump. Note that it must be formatted
|
54
|
+
*exactly* as the output would be:
|
55
|
+
|
56
|
+
test/schema.driver_name.out.rb
|
57
|
+
|
58
|
+
Once this is done you should send the diff of the changes to
|
59
|
+
anthonyeden@gmail.com. Any questions can also be emailed to this address.
|
60
|
+
|
61
|
+
== Known Issues
|
62
|
+
|
63
|
+
* If you are running on Rails 1.1.6 then the schema dumper test will fail because the formatting of the schema output has changed between 1.1.6 and 1.2.
|