Module: Spectacles::SchemaStatements::AbstractAdapter

Included in:
Mysql2Adapter, PostgreSQLAdapter, SQLServerAdapter, SQLite3Adapter, VerticaAdapter
Defined in:
lib/spectacles/schema_statements/abstract_adapter.rb

Instance Method Summary collapse

Instance Method Details

#create_materialized_view(view_name, *args) ⇒ Object

Raises:

  • (NotImplementedError)


72
73
74
# File 'lib/spectacles/schema_statements/abstract_adapter.rb', line 72

def create_materialized_view(view_name, *args)
  raise NotImplementedError, "Override create_materialized_view for your db adapter in #{self.class}"
end

#create_view(view_name, *args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/spectacles/schema_statements/abstract_adapter.rb', line 4

def create_view(view_name, *args)
  options = args.extract_options!
  build_query = args.shift

  raise "#{self.class} requires a query or block" if build_query.nil? && !block_given?

  build_query = yield if block_given?
  build_query = build_query.to_sql if build_query.respond_to?(:to_sql)

  if options[:force] && view_exists?(view_name)
    drop_view(view_name)
  end

  query = create_view_statement(view_name, build_query)
  if defined?(ActiveRecord::Base.connection_handler) && ActiveRecord.respond_to?(:legacy_connection_handling) && ActiveRecord.legacy_connection_handling
    ActiveRecord::Base.connection_handler.while_preventing_writes(false) do
      execute(query)
    end
  else
    execute(query)
  end
end

#create_view_statement(view_name, create_query) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/spectacles/schema_statements/abstract_adapter.rb', line 27

def create_view_statement(view_name, create_query)
  # query = "CREATE VIEW ? AS #{create_query}"
  # query_array = [query, view_name.to_s]

  # return ActiveRecord::Base.__send__(:sanitize_sql_array, query_array)
  "CREATE VIEW #{view_name} AS #{create_query}"
end

#drop_materialized_view(view_name) ⇒ Object

Raises:

  • (NotImplementedError)


76
77
78
# File 'lib/spectacles/schema_statements/abstract_adapter.rb', line 76

def drop_materialized_view(view_name)
  raise NotImplementedError, "Override drop_materialized_view for your db adapter in #{self.class}"
end

#drop_view(view_name) ⇒ Object



35
36
37
38
# File 'lib/spectacles/schema_statements/abstract_adapter.rb', line 35

def drop_view(view_name)
  query = drop_view_statement(view_name)
  execute(query)
end

#drop_view_statement(view_name) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/spectacles/schema_statements/abstract_adapter.rb', line 40

def drop_view_statement(view_name)
  # query = "DROP VIEW IF EXISTS ? "
  # query_array = [query, view_name.to_s]

  # return ActiveRecord::Base.__send__(:sanitize_sql_array, query_array)
  "DROP VIEW IF EXISTS #{view_name} "
end

#materialized_view_build_query(view_name) ⇒ Object

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/spectacles/schema_statements/abstract_adapter.rb', line 68

def materialized_view_build_query(view_name)
  raise NotImplementedError, "Override materialized_view_build_query for your db adapter in #{self.class}"
end

#materialized_view_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/spectacles/schema_statements/abstract_adapter.rb', line 60

def materialized_view_exists?(name)
  materialized_views.include?(name.to_s)
end

#materialized_viewsObject

Raises:

  • (NotImplementedError)


64
65
66
# File 'lib/spectacles/schema_statements/abstract_adapter.rb', line 64

def materialized_views
  raise NotImplementedError, "Override materialized_views for your db adapter in #{self.class}"
end

#refresh_materialized_view(view_name) ⇒ Object

Raises:

  • (NotImplementedError)


80
81
82
# File 'lib/spectacles/schema_statements/abstract_adapter.rb', line 80

def refresh_materialized_view(view_name)
  raise NotImplementedError, "Override refresh_materialized_view for your db adapter in #{self.class}"
end

#refresh_materialized_view_concurrently(view_name) ⇒ Object

Raises:

  • (NotImplementedError)


84
85
86
# File 'lib/spectacles/schema_statements/abstract_adapter.rb', line 84

def refresh_materialized_view_concurrently(view_name)
  raise NotImplementedError, "Override refresh_materialized_view_concurrently for your db adapter in #{self.class}"
end

#supports_materialized_views?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/spectacles/schema_statements/abstract_adapter.rb', line 56

def supports_materialized_views?
  false
end

#view_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/spectacles/schema_statements/abstract_adapter.rb', line 48

def view_exists?(name)
  views.include?(name.to_s)
end

#viewsObject



52
53
54
# File 'lib/spectacles/schema_statements/abstract_adapter.rb', line 52

def views
  raise "Override view for your db adapter in #{self.class}"
end