Class: ActiveRecord::ConnectionAdapters::PostgreSQL::Branched::Shadow
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::PostgreSQL::Branched::Shadow
- Defined in:
- lib/active_record/connection_adapters/postgresql/branched/shadow.rb
Constant Summary collapse
- DROPPED_SUFFIX =
"__dropped"
Instance Attribute Summary collapse
-
#dropped_schema ⇒ Object
readonly
Returns the value of attribute dropped_schema.
Instance Method Summary collapse
- #call(table_name) ⇒ Object
- #drop_table(table) ⇒ Object
- #dropped?(table_name) ⇒ Boolean
-
#initialize(connection, branch_schema) ⇒ Shadow
constructor
A new instance of Shadow.
- #shadow_enum(name) ⇒ Object
- #undrop_table(table) ⇒ Object
Constructor Details
#initialize(connection, branch_schema) ⇒ Shadow
Returns a new instance of Shadow.
8 9 10 11 12 |
# File 'lib/active_record/connection_adapters/postgresql/branched/shadow.rb', line 8 def initialize(connection, branch_schema) @connection = connection @branch_schema = branch_schema @dropped_schema = "#{branch_schema}#{DROPPED_SUFFIX}" end |
Instance Attribute Details
#dropped_schema ⇒ Object (readonly)
Returns the value of attribute dropped_schema.
14 15 16 |
# File 'lib/active_record/connection_adapters/postgresql/branched/shadow.rb', line 14 def dropped_schema @dropped_schema end |
Instance Method Details
#call(table_name) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/active_record/connection_adapters/postgresql/branched/shadow.rb', line 16 def call(table_name) table = table_name.to_s return unless exists_in_public?(table) return if already_shadowed?(table) return if dropped?(table) create_shadow(table) end |
#drop_table(table) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/active_record/connection_adapters/postgresql/branched/shadow.rb', line 46 def drop_table(table) table = table.to_s return unless exists_in_public?(table) ensure_dropped_schema quoted_dropped = @connection.quote_column_name(@dropped_schema) quoted_table = @connection.quote_column_name(table) @connection.execute(<<~SQL) CREATE TABLE #{quoted_dropped}.#{quoted_table} (LIKE public.#{quoted_table}) SQL end |
#dropped?(table_name) ⇒ Boolean
67 68 69 70 71 72 73 |
# File 'lib/active_record/connection_adapters/postgresql/branched/shadow.rb', line 67 def dropped?(table_name) @connection.select_value(<<~SQL) == 1 SELECT 1 FROM information_schema.tables WHERE table_schema = #{@connection.quote(@dropped_schema)} AND table_name = #{@connection.quote(table_name.to_s)} SQL end |
#shadow_enum(name) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/active_record/connection_adapters/postgresql/branched/shadow.rb', line 25 def shadow_enum(name) name = name.to_s return unless enum_exists_in_public?(name) return if enum_exists_in_branch?(name) labels = @connection.select_values(<<~SQL) SELECT e.enumlabel FROM pg_enum e JOIN pg_type t ON t.oid = e.enumtypid JOIN pg_namespace n ON n.oid = t.typnamespace WHERE n.nspname = 'public' AND t.typname = #{@connection.quote(name)} ORDER BY e.enumsortorder SQL quoted_branch = @connection.quote_column_name(@branch_schema) quoted_labels = labels.map { |l| @connection.quote(l) }.join(", ") @connection.execute(<<~SQL) CREATE TYPE #{quoted_branch}.#{@connection.quote_column_name(name)} AS ENUM (#{quoted_labels}) SQL end |
#undrop_table(table) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/active_record/connection_adapters/postgresql/branched/shadow.rb', line 59 def undrop_table(table) table = table.to_s return unless dropped?(table) quoted_dropped = @connection.quote_column_name(@dropped_schema) quoted_table = @connection.quote_column_name(table) @connection.execute("DROP TABLE #{quoted_dropped}.#{quoted_table}") end |