pg_saurus 2.3.1 → 2.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.markdown +39 -7
- data/lib/pg_saurus/connection_adapters.rb +3 -2
- data/lib/pg_saurus/connection_adapters/abstract_adapter.rb +2 -0
- data/lib/pg_saurus/connection_adapters/abstract_adapter/function_methods.rb +22 -0
- data/lib/pg_saurus/connection_adapters/abstract_adapter/trigger_methods.rb +42 -0
- data/lib/pg_saurus/connection_adapters/postgresql_adapter.rb +2 -0
- data/lib/pg_saurus/connection_adapters/postgresql_adapter/function_methods.rb +1 -1
- data/lib/pg_saurus/connection_adapters/postgresql_adapter/trigger_methods.rb +139 -0
- data/lib/pg_saurus/connection_adapters/table.rb +2 -1
- data/lib/pg_saurus/connection_adapters/table/trigger_methods.rb +33 -0
- data/lib/pg_saurus/connection_adapters/trigger_definition.rb +16 -0
- data/lib/pg_saurus/migration/command_recorder.rb +2 -0
- data/lib/pg_saurus/migration/command_recorder/function_methods.rb +3 -3
- data/lib/pg_saurus/migration/command_recorder/trigger_methods.rb +23 -0
- data/lib/pg_saurus/schema_dumper.rb +3 -0
- data/lib/pg_saurus/schema_dumper/trigger_methods.rb +33 -0
- data/lib/pg_saurus/version.rb +1 -1
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c7a1e92934deea6ba975df2c5794d6cbbe018095
|
4
|
+
data.tar.gz: 897243ea0241e00197b1bae4085d1401530e0fcc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc76e020d12815030f5fcea206bb639c70a3f97c16e8deeb75acbcc6dca011b5d299f8c1027e7265c35dd9a8bfb4ed2aa6474d6dedbd603a6e61483c1a6ce0d9
|
7
|
+
data.tar.gz: f255fb8349f53b7f7888486b841b8dcda64720032d3f27e010270f45903511430313ed37e0c054eba12224e5769dbf357ee50332a30f61e35470bf28835013ac
|
data/README.markdown
CHANGED
@@ -2,8 +2,7 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://secure.travis-ci.org/HornsAndHooves/pg_saurus.png)](http://travis-ci.org/HornsAndHooves/pg_saurus)
|
4
4
|
[![Dependency Status](https://gemnasium.com/HornsAndHooves/pg_saurus.png)](https://gemnasium.com/HornsAndHooves/pg_saurus)
|
5
|
-
[![Code Climate](https://codeclimate.com/
|
6
|
-
|
5
|
+
[![Code Climate](https://codeclimate.com/github/HornsAndHooves/pg_saurus/badges/gpa.svg)](https://codeclimate.com/github/HornsAndHooves/pg_saurus)
|
7
6
|
ActiveRecord extension to get more from PostgreSQL:
|
8
7
|
|
9
8
|
* Create/drop schemas.
|
@@ -11,6 +10,8 @@ ActiveRecord extension to get more from PostgreSQL:
|
|
11
10
|
* Use foreign keys.
|
12
11
|
* Use partial indexes.
|
13
12
|
* Run index creation concurrently.
|
13
|
+
* Create/drop functions.
|
14
|
+
* Create/drop triggers.
|
14
15
|
|
15
16
|
PgSaurus is a fork of PgPower.
|
16
17
|
|
@@ -317,6 +318,7 @@ You can create, list, and drop functions.
|
|
317
318
|
### Examples
|
318
319
|
|
319
320
|
```ruby
|
321
|
+
# Create a function
|
320
322
|
pets_not_empty_function = <<-SQL
|
321
323
|
BEGIN
|
322
324
|
IF (SELECT COUNT(*) FROM pets) > 0
|
@@ -327,13 +329,39 @@ BEGIN
|
|
327
329
|
END IF;
|
328
330
|
END;
|
329
331
|
SQL
|
330
|
-
|
332
|
+
# Arguments are: function_name, return_type, function_definition, options (currently, only :schema)
|
331
333
|
create_function 'pets_not_empty()', :boolean, pets_not_empty_function, schema: 'public'
|
332
|
-
|
333
|
-
#
|
334
|
+
|
335
|
+
# Drop a function
|
334
336
|
drop_function 'pets_not_empty()'
|
335
|
-
|
336
|
-
#
|
337
|
+
|
338
|
+
# Get a list of defined functions
|
339
|
+
ActiveRecord::Base.connection.functions
|
340
|
+
```
|
341
|
+
|
342
|
+
## Triggers
|
343
|
+
|
344
|
+
You can create and remove triggers on tables and views.
|
345
|
+
|
346
|
+
### Examples
|
347
|
+
|
348
|
+
```ruby
|
349
|
+
# Create a trigger
|
350
|
+
create_trigger :pets, # Table or view name
|
351
|
+
:pets_not_empty_trigger_proc, # Procedure name. Parentheses are optional if you have no arguments.
|
352
|
+
'AFTER INSERT', # Trigger event
|
353
|
+
for_each: 'ROW', # Can be row or statement. Default is row.
|
354
|
+
schema: 'public', # Optional schema name
|
355
|
+
constraint: true, # Sets if the trigger is a constraint. Default is false.
|
356
|
+
deferrable: true, # Sets if the trigger is immediate or deferrable. Default is immediate.
|
357
|
+
initially_deferred: true, # Sets if the trigger is initially deferred. Default is immediate. Only relevant if the trigger is deferrable.
|
358
|
+
condition: "new.name = 'fluffy'" # Optional when condition. Default is none.
|
359
|
+
|
360
|
+
# Drop a trigger
|
361
|
+
remove_trigger :pets, :pets_not_empty_trigger_proc
|
362
|
+
|
363
|
+
# Get a list of defined triggers on a table or view
|
364
|
+
ActiveRecord::Base.connection.triggers
|
337
365
|
```
|
338
366
|
|
339
367
|
## Tools
|
@@ -376,6 +404,10 @@ PgSaurus does not support Rails 3.
|
|
376
404
|
|
377
405
|
## TODO:
|
378
406
|
|
407
|
+
Support for Rails 4.2+
|
408
|
+
|
409
|
+
* This will likely necessitate a major rewrite.
|
410
|
+
|
379
411
|
Support for JRuby:
|
380
412
|
|
381
413
|
* Jdbc driver provides its own `create_schema(schema, user)` method - solve conflicts.
|
@@ -2,9 +2,10 @@ module PgSaurus::ConnectionAdapters # :nodoc:
|
|
2
2
|
extend ActiveSupport::Autoload
|
3
3
|
|
4
4
|
autoload :AbstractAdapter
|
5
|
-
autoload :PostgreSQLAdapter,
|
5
|
+
autoload :PostgreSQLAdapter, 'pg_saurus/connection_adapters/postgresql_adapter'
|
6
6
|
autoload :Table
|
7
7
|
autoload :ForeignKeyDefinition
|
8
|
-
autoload :IndexDefinition,
|
8
|
+
autoload :IndexDefinition, 'pg_saurus/connection_adapters/index_definition'
|
9
9
|
autoload :FunctionDefinition, 'pg_saurus/connection_adapters/function_definition'
|
10
|
+
autoload :TriggerDefinition, 'pg_saurus/connection_adapters/trigger_definition'
|
10
11
|
end
|
@@ -8,12 +8,14 @@ module PgSaurus::ConnectionAdapters::AbstractAdapter
|
|
8
8
|
autoload :SchemaMethods
|
9
9
|
autoload :IndexMethods
|
10
10
|
autoload :FunctionMethods
|
11
|
+
autoload :TriggerMethods
|
11
12
|
|
12
13
|
include CommentMethods
|
13
14
|
include ForeignerMethods
|
14
15
|
include SchemaMethods
|
15
16
|
include IndexMethods
|
16
17
|
include FunctionMethods
|
18
|
+
include TriggerMethods
|
17
19
|
|
18
20
|
included do
|
19
21
|
alias_method_chain :create_table, :schema_option
|
@@ -7,11 +7,33 @@ module PgSaurus::ConnectionAdapters::AbstractAdapter::FunctionMethods
|
|
7
7
|
end
|
8
8
|
|
9
9
|
# Create a database function.
|
10
|
+
#
|
11
|
+
# Example:
|
12
|
+
#
|
13
|
+
# # Arguments are: function_name, return_type, function_definition, options (currently, only :schema)
|
14
|
+
# create_function 'pets_not_empty()', :boolean, <<-FUNCTION, schema: 'public'
|
15
|
+
# BEGIN
|
16
|
+
# IF (SELECT COUNT(*) FROM pets) > 0
|
17
|
+
# THEN
|
18
|
+
# RETURN true;
|
19
|
+
# ELSE
|
20
|
+
# RETURN false;
|
21
|
+
# END IF;
|
22
|
+
# END;
|
23
|
+
# FUNCTION
|
24
|
+
#
|
25
|
+
# The schema is optional.
|
10
26
|
def create_function(function_name, returning, definition, options = {})
|
11
27
|
|
12
28
|
end
|
13
29
|
|
14
30
|
# Delete the database function.
|
31
|
+
#
|
32
|
+
# Example:
|
33
|
+
#
|
34
|
+
# drop_function 'pets_not_empty()', schema: 'public'
|
35
|
+
#
|
36
|
+
# The schema is optional.
|
15
37
|
def drop_function(function_name, options)
|
16
38
|
|
17
39
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# Adapter definitions for db functions
|
2
|
+
module PgSaurus::ConnectionAdapters::AbstractAdapter::TriggerMethods
|
3
|
+
|
4
|
+
# :nodoc
|
5
|
+
def supports_triggers?
|
6
|
+
false
|
7
|
+
end
|
8
|
+
|
9
|
+
# Returns the listing of currently defined db triggers
|
10
|
+
def triggers
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
# Creates a trigger.
|
15
|
+
#
|
16
|
+
# Example:
|
17
|
+
#
|
18
|
+
# create_trigger :pets, # Table or view name
|
19
|
+
# :pets_not_empty_trigger_proc, # Procedure name. Parentheses are optional if you have no arguments.
|
20
|
+
# 'AFTER INSERT', # Trigger event
|
21
|
+
# for_each: 'ROW', # Can be row or statement. Default is row.
|
22
|
+
# schema: 'public', # Optional schema name
|
23
|
+
# constraint: true, # Sets if the trigger is a constraint. Default is false.
|
24
|
+
# deferrable: true, # Sets if the trigger is immediate or deferrable. Default is immediate.
|
25
|
+
# initially_deferred: true, # Sets if the trigger is initially deferred. Default is immediate. Only relevant if the trigger is deferrable.
|
26
|
+
# condition: "new.name = 'fluffy'" # Optional when condition. Default is none.
|
27
|
+
#
|
28
|
+
def create_trigger(table_name, proc_name, event, options = {})
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
# Removes a trigger.
|
33
|
+
#
|
34
|
+
# Example:
|
35
|
+
#
|
36
|
+
# remove_trigger :pets, :pets_not_empty_trigger_proc
|
37
|
+
#
|
38
|
+
def remove_trigger(table_name, proc_name, options = {})
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -13,6 +13,7 @@ module PgSaurus::ConnectionAdapters::PostgreSQLAdapter
|
|
13
13
|
autoload :TranslateException, 'pg_saurus/connection_adapters/postgresql_adapter/translate_exception'
|
14
14
|
autoload :ViewMethods, 'pg_saurus/connection_adapters/postgresql_adapter/view_methods'
|
15
15
|
autoload :FunctionMethods, 'pg_saurus/connection_adapters/postgresql_adapter/function_methods'
|
16
|
+
autoload :TriggerMethods, 'pg_saurus/connection_adapters/postgresql_adapter/trigger_methods'
|
16
17
|
|
17
18
|
include ExtensionMethods
|
18
19
|
include SchemaMethods
|
@@ -22,6 +23,7 @@ module PgSaurus::ConnectionAdapters::PostgreSQLAdapter
|
|
22
23
|
include TranslateException
|
23
24
|
include ViewMethods
|
24
25
|
include FunctionMethods
|
26
|
+
include TriggerMethods
|
25
27
|
|
26
28
|
included do
|
27
29
|
alias_method_chain :tables, :non_public_schema_tables
|
@@ -102,7 +102,7 @@ module PgSaurus::ConnectionAdapters::PostgreSQLAdapter::FunctionMethods
|
|
102
102
|
# Write out the fully qualified function name if the :schema option is passed.
|
103
103
|
def full_function_name(function_name, options)
|
104
104
|
schema = options[:schema]
|
105
|
-
function_name = "#{schema}.#{function_name}
|
105
|
+
function_name = %Q{"#{schema}".#{function_name}} if schema
|
106
106
|
function_name
|
107
107
|
end
|
108
108
|
private :full_function_name
|
@@ -0,0 +1,139 @@
|
|
1
|
+
# Provides methods to extend {ActiveRecord::ConnectionAdapters::PostgreSQLAdapter}
|
2
|
+
# to support db triggers.
|
3
|
+
module PgSaurus::ConnectionAdapters::PostgreSQLAdapter::TriggerMethods
|
4
|
+
|
5
|
+
# :nodoc
|
6
|
+
def supports_triggers?
|
7
|
+
true
|
8
|
+
end
|
9
|
+
|
10
|
+
# See lib/pg_saurus/connection_adapters/trigger_methods.rb
|
11
|
+
def create_trigger(table_name, proc_name, event, options = {})
|
12
|
+
proc_name = "#{proc_name}"
|
13
|
+
proc_name = "#{proc_name}()" unless proc_name.end_with?(')')
|
14
|
+
|
15
|
+
for_each = options[:for_each] || 'ROW'
|
16
|
+
constraint = options[:constraint]
|
17
|
+
|
18
|
+
sql = if constraint
|
19
|
+
"CREATE CONSTRAINT TRIGGER #{trigger_name(proc_name, options)}\n #{event}\n"
|
20
|
+
else
|
21
|
+
"CREATE TRIGGER #{trigger_name(proc_name, options)}\n #{event}\n"
|
22
|
+
end
|
23
|
+
|
24
|
+
sql << " ON #{quote_table_or_view(table_name, options)}\n"
|
25
|
+
if constraint
|
26
|
+
sql << if options[:deferrable]
|
27
|
+
" DEFERRABLE INITIALLY #{!!options[:initially_deferred] ? 'DEFERRED' : 'IMMEDIATE'}\n"
|
28
|
+
else
|
29
|
+
" NOT DEFERRABLE\n"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
sql << " FOR EACH #{for_each}\n"
|
33
|
+
if condition = options[:condition]
|
34
|
+
sql << " WHEN (#{condition})\n"
|
35
|
+
end
|
36
|
+
sql << " EXECUTE PROCEDURE #{proc_name}"
|
37
|
+
|
38
|
+
execute sql
|
39
|
+
end
|
40
|
+
|
41
|
+
# See lib/pg_saurus/connection_adapters/trigger_methods.rb
|
42
|
+
def remove_trigger(table_name, proc_name, options = {})
|
43
|
+
execute "DROP TRIGGER #{trigger_name(proc_name, options)} ON #{quote_table_or_view(table_name, options)}"
|
44
|
+
end
|
45
|
+
|
46
|
+
# Returns the listing of currently defined db triggers
|
47
|
+
#
|
48
|
+
# @return [Array<::PgSaurus::ConnectionAdapters::TriggerDefinition>]
|
49
|
+
def triggers
|
50
|
+
res = select_all <<-SQL
|
51
|
+
SELECT n.nspname as schema,
|
52
|
+
c.relname as table,
|
53
|
+
t.tgname as trigger_name,
|
54
|
+
t.tgenabled as enable_mode,
|
55
|
+
t.tgdeferrable as is_deferrable,
|
56
|
+
t.tginitdeferred as is_initially_deferrable,
|
57
|
+
pg_catalog.pg_get_triggerdef(t.oid, true) as trigger_definition
|
58
|
+
FROM pg_catalog.pg_trigger t
|
59
|
+
INNER JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid
|
60
|
+
INNER JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
61
|
+
WHERE c.relkind IN ('r', 'v')
|
62
|
+
AND NOT t.tgisinternal
|
63
|
+
ORDER BY 1, 2, 3;
|
64
|
+
SQL
|
65
|
+
|
66
|
+
res.inject([]) do |buffer, row|
|
67
|
+
schema = row['schema']
|
68
|
+
table = row['table']
|
69
|
+
trigger_name = row['trigger_name']
|
70
|
+
is_deferrable = row['is_deferrable']
|
71
|
+
is_initially_deferred = row['is_initially_deferred']
|
72
|
+
|
73
|
+
trigger_definition = row['trigger_definition']
|
74
|
+
|
75
|
+
is_constraint = is_constraint?(trigger_definition)
|
76
|
+
proc_name = parse_proc_name(trigger_definition)
|
77
|
+
event = parse_event(trigger_definition, trigger_name)
|
78
|
+
condition = parse_condition(trigger_definition)
|
79
|
+
|
80
|
+
for_every = !!(trigger_definition =~ /FOR[\s]EACH[\s]ROW/) ? :row : :statement
|
81
|
+
|
82
|
+
if proc_name && event
|
83
|
+
buffer << ::PgSaurus::ConnectionAdapters::TriggerDefinition.new(
|
84
|
+
trigger_name,
|
85
|
+
proc_name,
|
86
|
+
is_constraint,
|
87
|
+
event,
|
88
|
+
for_every,
|
89
|
+
is_deferrable,
|
90
|
+
is_initially_deferred,
|
91
|
+
condition,
|
92
|
+
table,
|
93
|
+
schema
|
94
|
+
)
|
95
|
+
end
|
96
|
+
buffer
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def parse_condition(trigger_definition)
|
101
|
+
trigger_definition[/WHEN[\s](.*?)[\s]EXECUTE[\s]PROCEDURE/m, 1]
|
102
|
+
end
|
103
|
+
private :parse_condition
|
104
|
+
|
105
|
+
def parse_event(trigger_definition, trigger_name)
|
106
|
+
trigger_definition[/^CREATE[\sA-Z]+TRIGGER[\s]#{Regexp.escape(trigger_name)}[\s](.*?)[\s]ON[\s]/m, 1]
|
107
|
+
end
|
108
|
+
private :parse_event
|
109
|
+
|
110
|
+
def parse_proc_name(trigger_definition)
|
111
|
+
trigger_definition[/EXECUTE[\s]PROCEDURE[\s](.*?)$/m,1]
|
112
|
+
end
|
113
|
+
private :parse_proc_name
|
114
|
+
|
115
|
+
def is_constraint?(trigger_definition)
|
116
|
+
!!(trigger_definition =~ /^CREATE CONSTRAINT TRIGGER/)
|
117
|
+
end
|
118
|
+
private :is_constraint?
|
119
|
+
|
120
|
+
def quote_table_or_view(name, options)
|
121
|
+
schema = options[:schema]
|
122
|
+
if schema
|
123
|
+
"\"#{schema}\".\"#{name}\""
|
124
|
+
else
|
125
|
+
"\"#{name}\""
|
126
|
+
end
|
127
|
+
end
|
128
|
+
private :quote_table_or_view
|
129
|
+
|
130
|
+
def trigger_name(proc_name, options)
|
131
|
+
if name = options[:name]
|
132
|
+
name
|
133
|
+
else
|
134
|
+
"trigger_#{proc_name.gsub('(', '').gsub(')', '')}"
|
135
|
+
end
|
136
|
+
end
|
137
|
+
private :trigger_name
|
138
|
+
|
139
|
+
end
|
@@ -6,10 +6,11 @@ module PgSaurus::ConnectionAdapters::Table
|
|
6
6
|
|
7
7
|
autoload :CommentMethods
|
8
8
|
autoload :ForeignerMethods
|
9
|
+
autoload :TriggerMethods
|
9
10
|
|
10
11
|
include CommentMethods
|
11
12
|
include ForeignerMethods
|
12
|
-
|
13
|
+
include TriggerMethods
|
13
14
|
|
14
15
|
included do
|
15
16
|
alias_method_chain :references, :foreign_keys
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# Provides methods to extend ActiveRecord::ConnectionAdapters::Table
|
2
|
+
# to support database triggers.
|
3
|
+
module PgSaurus::ConnectionAdapters::Table::TriggerMethods
|
4
|
+
|
5
|
+
# Creates a trigger.
|
6
|
+
#
|
7
|
+
# Example:
|
8
|
+
#
|
9
|
+
# change_table :pets do |t|
|
10
|
+
# t.create_trigger :pets_not_empty_trigger_proc,
|
11
|
+
# 'AFTER INSERT',
|
12
|
+
# for_each: 'ROW',
|
13
|
+
# schema: 'public',
|
14
|
+
# constraint: true,
|
15
|
+
# deferrable: true,
|
16
|
+
# initially_deferred: true
|
17
|
+
# end
|
18
|
+
def create_trigger(proc_name, event, options = {})
|
19
|
+
@base.create_trigger(@table_name, proc_name, event, options)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Removes a trigger.
|
23
|
+
#
|
24
|
+
# Example:
|
25
|
+
#
|
26
|
+
# change_table :pets do |t|
|
27
|
+
# t.remove_trigger :pets_not_empty_trigger_proc
|
28
|
+
# end
|
29
|
+
def remove_trigger(proc_name, options = {})
|
30
|
+
@base.remove_trigger(@table_name, proc_name, options)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module PgSaurus::ConnectionAdapters
|
2
|
+
|
3
|
+
# Struct definition for a DB trigger.
|
4
|
+
class TriggerDefinition < Struct.new( :name,
|
5
|
+
:proc_name,
|
6
|
+
:constraint,
|
7
|
+
:event,
|
8
|
+
:for_each,
|
9
|
+
:deferrable,
|
10
|
+
:initially_deferred,
|
11
|
+
:condition,
|
12
|
+
:table,
|
13
|
+
:schema )
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
@@ -9,6 +9,7 @@ module PgSaurus::Migration::CommandRecorder
|
|
9
9
|
autoload :ForeignerMethods
|
10
10
|
autoload :ViewMethods
|
11
11
|
autoload :FunctionMethods
|
12
|
+
autoload :TriggerMethods
|
12
13
|
|
13
14
|
include ExtensionMethods
|
14
15
|
include SchemaMethods
|
@@ -16,4 +17,5 @@ module PgSaurus::Migration::CommandRecorder
|
|
16
17
|
include ForeignerMethods
|
17
18
|
include ViewMethods
|
18
19
|
include FunctionMethods
|
20
|
+
include TriggerMethods
|
19
21
|
end
|
@@ -1,15 +1,15 @@
|
|
1
1
|
# Methods to extend ActiveRecord::Migration::CommandRecorder to
|
2
|
-
# support
|
2
|
+
# support database functions.
|
3
3
|
module PgSaurus::Migration::CommandRecorder::FunctionMethods
|
4
4
|
|
5
5
|
# :nodoc
|
6
6
|
def create_function(*args)
|
7
|
-
record :create_function,
|
7
|
+
record :create_function, args
|
8
8
|
end
|
9
9
|
|
10
10
|
# :nodoc
|
11
11
|
def drop_function(*args)
|
12
|
-
record :drop_function,
|
12
|
+
record :drop_function, args
|
13
13
|
end
|
14
14
|
|
15
15
|
# :nodoc
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# Methods to extend ActiveRecord::Migration::CommandRecorder to
|
2
|
+
# support database triggers.
|
3
|
+
module PgSaurus::Migration::CommandRecorder::TriggerMethods
|
4
|
+
|
5
|
+
# :nodoc:
|
6
|
+
def create_trigger(*args)
|
7
|
+
record :create_trigger, args
|
8
|
+
end
|
9
|
+
|
10
|
+
# :nodoc:
|
11
|
+
def remove_trigger(*args)
|
12
|
+
record :remove_trigger, args
|
13
|
+
end
|
14
|
+
|
15
|
+
# :nodoc:
|
16
|
+
def invert_create_trigger(args)
|
17
|
+
table_name, proc_name, _, options = *args
|
18
|
+
options ||= {}
|
19
|
+
|
20
|
+
[:remove_trigger, [table_name, proc_name, options]]
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -11,6 +11,7 @@ module PgSaurus::SchemaDumper
|
|
11
11
|
autoload :ForeignerMethods
|
12
12
|
autoload :ViewMethods
|
13
13
|
autoload :FunctionMethods
|
14
|
+
autoload :TriggerMethods
|
14
15
|
|
15
16
|
include ExtensionMethods
|
16
17
|
include CommentMethods
|
@@ -18,6 +19,7 @@ module PgSaurus::SchemaDumper
|
|
18
19
|
include ForeignerMethods
|
19
20
|
include ViewMethods
|
20
21
|
include FunctionMethods
|
22
|
+
include TriggerMethods
|
21
23
|
|
22
24
|
included do
|
23
25
|
alias_method_chain :header, :schemas
|
@@ -26,6 +28,7 @@ module PgSaurus::SchemaDumper
|
|
26
28
|
alias_method_chain :tables, :views
|
27
29
|
alias_method_chain :tables, :foreign_keys
|
28
30
|
alias_method_chain :tables, :functions
|
31
|
+
alias_method_chain :tables, :triggers
|
29
32
|
alias_method_chain :tables, :comments
|
30
33
|
end
|
31
34
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# Support for dumping database triggers.
|
2
|
+
module PgSaurus::SchemaDumper::TriggerMethods
|
3
|
+
|
4
|
+
# :nodoc
|
5
|
+
def tables_with_triggers(stream)
|
6
|
+
tables_without_triggers(stream)
|
7
|
+
|
8
|
+
dump_triggers(stream)
|
9
|
+
stream.puts
|
10
|
+
|
11
|
+
stream
|
12
|
+
end
|
13
|
+
|
14
|
+
# Write out a command to create each detected trigger.
|
15
|
+
def dump_triggers(stream)
|
16
|
+
@connection.triggers.each do |trigger|
|
17
|
+
statement = " create_trigger '#{trigger.table}', '#{trigger.proc_name}', '#{trigger.event}', " \
|
18
|
+
"name: '#{trigger.name}', " \
|
19
|
+
"constraint: #{trigger.constraint ? :true : :false}, " \
|
20
|
+
"for_each: :#{trigger.for_each}, " \
|
21
|
+
"deferrable: #{trigger.deferrable ? :true : :false}, " \
|
22
|
+
"initially_deferred: #{trigger.initially_deferred ? :true : :false}, " \
|
23
|
+
"schema: '#{trigger.schema}'"
|
24
|
+
|
25
|
+
if trigger.condition
|
26
|
+
statement << %Q{, condition: '#{trigger.condition.gsub("'", %q(\\\'))}'}
|
27
|
+
end
|
28
|
+
|
29
|
+
stream.puts "#{statement}\n"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
data/lib/pg_saurus/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_saurus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Potapov Sergey
|
@@ -13,7 +13,7 @@ authors:
|
|
13
13
|
autorequire:
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
|
-
date: 2015-07-
|
16
|
+
date: 2015-07-16 00:00:00.000000000 Z
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
19
19
|
name: pg
|
@@ -177,6 +177,7 @@ files:
|
|
177
177
|
- lib/pg_saurus/connection_adapters/abstract_adapter/function_methods.rb
|
178
178
|
- lib/pg_saurus/connection_adapters/abstract_adapter/index_methods.rb
|
179
179
|
- lib/pg_saurus/connection_adapters/abstract_adapter/schema_methods.rb
|
180
|
+
- lib/pg_saurus/connection_adapters/abstract_adapter/trigger_methods.rb
|
180
181
|
- lib/pg_saurus/connection_adapters/foreign_key_definition.rb
|
181
182
|
- lib/pg_saurus/connection_adapters/function_definition.rb
|
182
183
|
- lib/pg_saurus/connection_adapters/index_definition.rb
|
@@ -188,10 +189,13 @@ files:
|
|
188
189
|
- lib/pg_saurus/connection_adapters/postgresql_adapter/index_methods.rb
|
189
190
|
- lib/pg_saurus/connection_adapters/postgresql_adapter/schema_methods.rb
|
190
191
|
- lib/pg_saurus/connection_adapters/postgresql_adapter/translate_exception.rb
|
192
|
+
- lib/pg_saurus/connection_adapters/postgresql_adapter/trigger_methods.rb
|
191
193
|
- lib/pg_saurus/connection_adapters/postgresql_adapter/view_methods.rb
|
192
194
|
- lib/pg_saurus/connection_adapters/table.rb
|
193
195
|
- lib/pg_saurus/connection_adapters/table/comment_methods.rb
|
194
196
|
- lib/pg_saurus/connection_adapters/table/foreigner_methods.rb
|
197
|
+
- lib/pg_saurus/connection_adapters/table/trigger_methods.rb
|
198
|
+
- lib/pg_saurus/connection_adapters/trigger_definition.rb
|
195
199
|
- lib/pg_saurus/create_index_concurrently.rb
|
196
200
|
- lib/pg_saurus/engine.rb
|
197
201
|
- lib/pg_saurus/errors.rb
|
@@ -202,6 +206,7 @@ files:
|
|
202
206
|
- lib/pg_saurus/migration/command_recorder/foreigner_methods.rb
|
203
207
|
- lib/pg_saurus/migration/command_recorder/function_methods.rb
|
204
208
|
- lib/pg_saurus/migration/command_recorder/schema_methods.rb
|
209
|
+
- lib/pg_saurus/migration/command_recorder/trigger_methods.rb
|
205
210
|
- lib/pg_saurus/migration/command_recorder/view_methods.rb
|
206
211
|
- lib/pg_saurus/migration/set_role_method.rb
|
207
212
|
- lib/pg_saurus/schema_dumper.rb
|
@@ -210,6 +215,7 @@ files:
|
|
210
215
|
- lib/pg_saurus/schema_dumper/foreigner_methods.rb
|
211
216
|
- lib/pg_saurus/schema_dumper/function_methods.rb
|
212
217
|
- lib/pg_saurus/schema_dumper/schema_methods.rb
|
218
|
+
- lib/pg_saurus/schema_dumper/trigger_methods.rb
|
213
219
|
- lib/pg_saurus/schema_dumper/view_methods.rb
|
214
220
|
- lib/pg_saurus/tools.rb
|
215
221
|
- lib/pg_saurus/version.rb
|