Module: AssociateJsonb::ConnectionAdapters::SchemaStatements
- Defined in:
- lib/associate_jsonb/connection_adapters/schema_statements.rb
Instance Method Summary collapse
- #add_constraint(table_name, **options) ⇒ Object
- #add_jsonb_foreign_key_function ⇒ Object
- #add_jsonb_nested_set_function ⇒ Object
-
#constraints(table_name) ⇒ Object
:nodoc:.
- #create_table(table_name, id: :primary_key, primary_key: nil, force: nil, **options) {|td| ... } ⇒ Object
Instance Method Details
#add_constraint(table_name, **options) ⇒ Object
62 63 64 65 66 |
# File 'lib/associate_jsonb/connection_adapters/schema_statements.rb', line 62 def add_constraint(table_name, **) at = create_alter_table table_name at.add_constraint(**) execute schema_creation.accept at end |
#add_jsonb_foreign_key_function ⇒ Object
11 12 13 |
# File 'lib/associate_jsonb/connection_adapters/schema_statements.rb', line 11 def add_jsonb_foreign_key_function execute schema_creation.accept(AddJsonbForeignKeyFunction.new) end |
#add_jsonb_nested_set_function ⇒ Object
7 8 9 |
# File 'lib/associate_jsonb/connection_adapters/schema_statements.rb', line 7 def add_jsonb_nested_set_function execute schema_creation.accept(AddJsonbNestedSetFunction.new) end |
#constraints(table_name) ⇒ Object
:nodoc:
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/associate_jsonb/connection_adapters/schema_statements.rb', line 68 def constraints(table_name) # :nodoc: scope = quoted_scope(table_name) result = query(" SELECT\n con.oid,\n con.conname,\n con.connamespace,\n con.contype,\n con.condeferrable,\n con.condeferred,\n con.convalidated,\n pg_get_constraintdef(con.oid) as consrc\n FROM pg_catalog.pg_constraint con\n INNER JOIN pg_catalog.pg_class rel\n ON rel.oid = con.conrelid\n INNER JOIN pg_catalog.pg_namespace nsp\n ON nsp.oid = connamespace\n WHERE nsp.nspname = \#{scope[:schema]}\n AND rel.relname = \#{scope[:name]}\n ORDER BY rel.relname\n SQL\n\n result.map do |row|\n {\n oid: row[0],\n name: row[1],\n deferrable: row[4],\n deferred: row[5],\n validated: row[6],\n definition: row[7],\n type:\n case row[3].to_s.downcase\n when \"c\"\n \"CHECK\"\n when \"f\"\n \"FOREIGN KEY\"\n when \"p\"\n \"PRIMARY KEY\"\n when \"u\"\n \"UNIQUE\"\n when \"t\"\n \"TRIGGER\"\n when \"x\"\n \"EXCLUDE\"\n else\n \"UNKNOWN\"\n end\n }\n end\nend\n", "SCHEMA") |
#create_table(table_name, id: :primary_key, primary_key: nil, force: nil, **options) {|td| ... } ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/associate_jsonb/connection_adapters/schema_statements.rb', line 15 def create_table(table_name, id: :primary_key, primary_key: nil, force: nil, **) td = create_table_definition(table_name, **()) if id && !td.as pk = primary_key || ActiveRecord::Base.get_primary_key(table_name.to_s.singularize) if id.is_a?(Hash) .merge!(id.except(:type)) id = id.fetch(:type, :primary_key) end if pk.is_a?(Array) td.primary_keys pk else td.primary_key pk, id, ** end end yield td if block_given? if force drop_table(table_name, force: force, if_exists: true) else schema_cache.clear_data_source_cache!(table_name.to_s) end result = execute schema_creation.accept td td.indexes.each do |column_name, | add_index(table_name, column_name, **, if_not_exists: td.if_not_exists) end td.constraints.each do |ct| add_constraint(table_name, **ct) end if table_comment = td.comment.presence change_table_comment(table_name, table_comment) end td.columns.each do |column| change_column_comment(table_name, column.name, column.comment) if column.comment.present? end result end |