Module: ActiveRecord::ConnectionHandling

Defined in:
lib/brick/extensions.rb

Overview

Get info on all relations during first database connection

Instance Method Summary collapse

Instance Method Details

#_brick_establish_connectionObject



1308
# File 'lib/brick/extensions.rb', line 1308

alias _brick_establish_connection establish_connection

#_brick_reflect_tablesObject

This is done separately so that during testing it can be called right after a migration in order to make sure everything is good.



1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
# File 'lib/brick/extensions.rb', line 1320

def _brick_reflect_tables
  initializer_loaded = false
  if (relations = ::Brick.relations).empty?
    # If there's schema things configured then we only expect our initializer to be named exactly this
    if File.exist?(brick_initializer = Rails.root.join('config/initializers/brick.rb'))
      initializer_loaded = load brick_initializer
    end
    # Load the initializer for the Apartment gem a little early so that if .excluded_models and
    # .default_schema are specified then we can work with non-tenanted models more appropriately
    apartment = Object.const_defined?('Apartment')
    if apartment && File.exist?(apartment_initializer = Rails.root.join('config/initializers/apartment.rb'))
      load apartment_initializer
      apartment_excluded = Apartment.excluded_models
    end
    # Only for Postgres  (Doesn't work in sqlite3 or MySQL)
    # puts ActiveRecord::Base.execute_sql("SELECT current_setting('SEARCH_PATH')").to_a.inspect

    is_postgres = nil
    case ActiveRecord::Base.connection.adapter_name
    when 'PostgreSQL'
      is_postgres = true
      db_schemas = ActiveRecord::Base.execute_sql('SELECT DISTINCT table_schema FROM INFORMATION_SCHEMA.tables;')
      ::Brick.db_schemas = db_schemas.each_with_object({}) do |row, s|
        row = row.is_a?(String) ? row : row['table_schema']
        # Remove any system schemas
        s[row] = nil unless ['information_schema', 'pg_catalog'].include?(row)
      end
      if (is_multitenant = (multitenancy = ::Brick.config.schema_behavior[:multitenant]) &&
         (sta = multitenancy[:schema_to_analyse]) != 'public') &&
         ::Brick.db_schemas.include?(sta)
        ::Brick.default_schema = schema = sta
        ActiveRecord::Base.execute_sql("SET SEARCH_PATH = ?", schema)
      end
    when 'Mysql2'
      ::Brick.default_schema = schema = ActiveRecord::Base.connection.current_database
    when 'SQLite'
      # %%% Retrieve internal ActiveRecord table names like this:
      # ActiveRecord::Base.internal_metadata_table_name, ActiveRecord::Base.schema_migrations_table_name
      sql = "SELECT m.name AS relation_name, UPPER(m.type) AS table_type,
        p.name AS column_name, p.type AS data_type,
        CASE p.pk WHEN 1 THEN 'PRIMARY KEY' END AS const
      FROM sqlite_master AS m
        INNER JOIN pragma_table_info(m.name) AS p
      WHERE m.name NOT IN (?, ?)
      ORDER BY m.name, p.cid"
    else
      puts "Unfamiliar with connection adapter #{ActiveRecord::Base.connection.adapter_name}"
    end

    ::Brick.db_schemas ||= {}

    if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
      if (possible_schema = ::Brick.config.schema_behavior&.[](:multitenant)&.[](:schema_to_analyse))
        if ::Brick.db_schemas.key?(possible_schema)
          ::Brick.default_schema = schema = possible_schema
          ActiveRecord::Base.execute_sql("SET SEARCH_PATH = ?", schema)
        else
          puts "*** In the brick.rb initializer the line \"::Brick.schema_behavior = ...\" refers to a schema called \"#{possible_schema}\".  This schema does not exist. ***"
        end
      end
    end

    # %%% Retrieve internal ActiveRecord table names like this:
    # ActiveRecord::Base.internal_metadata_table_name, ActiveRecord::Base.schema_migrations_table_name
    # For if it's not SQLite -- so this is the Postgres and MySQL version
    measures = []
    case ActiveRecord::Base.connection.adapter_name
    when 'PostgreSQL', 'SQLite' # These bring back a hash for each row because the query uses column aliases
      # schema ||= 'public' if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
      ActiveRecord::Base.retrieve_schema_and_tables(sql, is_postgres, schema).each do |r|
        # If Apartment gem lists the table as being associated with a non-tenanted model then use whatever it thinks
        # is the default schema, usually 'public'.
        schema_name = if ::Brick.config.schema_behavior[:multitenant]
                        Apartment.default_schema if apartment_excluded&.include?(r['relation_name'].singularize.camelize)
                      elsif ![schema, 'public'].include?(r['schema'])
                        r['schema']
                      end
        relation_name = schema_name ? "#{schema_name}.#{r['relation_name']}" : r['relation_name']
        relation = relations[relation_name]
        relation[:isView] = true if r['table_type'] == 'VIEW'
        relation[:description] = r['table_description'] if r['table_description']
        col_name = r['column_name']
        key = case r['const']
              when 'PRIMARY KEY'
                relation[:pkey][r['key'] || relation_name] ||= []
              when 'UNIQUE'
                relation[:ukeys][r['key'] || "#{relation_name}.#{col_name}"] ||= []
                # key = (relation[:ukeys] = Hash.new { |h, k| h[k] = [] }) if key.is_a?(Array)
                # key[r['key']]
              end
        key << col_name if key
        cols = relation[:cols] # relation.fetch(:cols) { relation[:cols] = [] }
        cols[col_name] = [r['data_type'], r['max_length'], measures&.include?(col_name), r['is_nullable'] == 'NO']
        # puts "KEY! #{r['relation_name']}.#{col_name} #{r['key']} #{r['const']}" if r['key']
      end
    else # MySQL2 acts a little differently, bringing back an array for each row
      ActiveRecord::Base.retrieve_schema_and_tables(sql).each do |r|
        relation = relations[(relation_name = r[1])] # here relation represents a table or view from the database
        relation[:isView] = true if r[2] == 'VIEW' # table_type
        col_name = r[3]
        key = case r[6] # constraint type
              when 'PRIMARY KEY'
                # key
                relation[:pkey][r[7] || relation_name] ||= []
              when 'UNIQUE'
                relation[:ukeys][r[7] || "#{relation_name}.#{col_name}"] ||= []
                # key = (relation[:ukeys] = Hash.new { |h, k| h[k] = [] }) if key.is_a?(Array)
                # key[r['key']]
              end
        key << col_name if key
        cols = relation[:cols] # relation.fetch(:cols) { relation[:cols] = [] }
        # 'data_type', 'max_length'
        cols[col_name] = [r[4], r[5], measures&.include?(col_name)]
        # puts "KEY! #{r['relation_name']}.#{col_name} #{r['key']} #{r['const']}" if r['key']
      end
    end

    # # Add unique OIDs
    # if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
    #   ActiveRecord::Base.execute_sql(
    #     "SELECT c.oid, n.nspname, c.relname
    #     FROM pg_catalog.pg_namespace AS n
    #       INNER JOIN pg_catalog.pg_class AS c ON n.oid = c.relnamespace
    #     WHERE c.relkind IN ('r', 'v')"
    #   ).each do |r|
    #     next if ['pg_catalog', 'information_schema', ''].include?(r['nspname']) ||
    #       ['ar_internal_metadata', 'schema_migrations'].include?(r['relname'])
    #     relation = relations.fetch(r['relname'], nil)
    #     if relation
    #       (relation[:oid] ||= {})[r['nspname']] = r['oid']
    #     else
    #       puts "Where is #{r['nspname']} #{r['relname']} ?"
    #     end
    #   end
    # end
    # schema = ::Brick.default_schema # Reset back for this next round of fun
    case ActiveRecord::Base.connection.adapter_name
    when 'PostgreSQL', 'Mysql2'
      sql = "SELECT kcu1.CONSTRAINT_SCHEMA, kcu1.TABLE_NAME, kcu1.COLUMN_NAME,
                kcu2.CONSTRAINT_SCHEMA AS primary_schema, kcu2.TABLE_NAME AS primary_table, kcu1.CONSTRAINT_NAME AS CONSTRAINT_SCHEMA_FK
        FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS rc
          INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS kcu1
            ON kcu1.CONSTRAINT_CATALOG = rc.CONSTRAINT_CATALOG
            AND kcu1.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA
            AND kcu1.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
          INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS kcu2
            ON kcu2.CONSTRAINT_CATALOG = rc.UNIQUE_CONSTRAINT_CATALOG
            AND kcu2.CONSTRAINT_SCHEMA = rc.UNIQUE_CONSTRAINT_SCHEMA
            AND kcu2.CONSTRAINT_NAME = rc.UNIQUE_CONSTRAINT_NAME#{"
            AND kcu2.TABLE_NAME = kcu1.REFERENCED_TABLE_NAME
            AND kcu2.COLUMN_NAME = kcu1.REFERENCED_COLUMN_NAME" unless is_postgres }
            AND kcu2.ORDINAL_POSITION = kcu1.ORDINAL_POSITION#{"
        WHERE kcu1.CONSTRAINT_SCHEMA = COALESCE(current_setting('SEARCH_PATH'), 'public')" if is_postgres && schema }"
        # AND kcu2.TABLE_NAME = ?;", Apartment::Tenant.current, table_name
    when 'SQLite'
      sql = "SELECT m.name, fkl.\"from\", fkl.\"table\", m.name || '_' || fkl.\"from\" AS constraint_name
      FROM sqlite_master m
        INNER JOIN pragma_foreign_key_list(m.name) fkl ON m.type = 'table'
      ORDER BY m.name, fkl.seq"
    else
    end
    if sql
      # ::Brick.default_schema ||= schema ||= 'public' if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
      ActiveRecord::Base.execute_sql(sql).each do |fk|
        fk = fk.values unless fk.is_a?(Array)
        # Multitenancy makes things a little more general overall, except for non-tenanted tables
        if apartment_excluded&.include?(fk[1].singularize.camelize)
          fk[0] = Apartment.default_schema
        elsif is_postgres && (fk[0] == 'public' || (is_multitenant && fk[0] == schema)) ||
              !is_postgres && ['mysql', 'performance_schema', 'sys'].exclude?(fk[0])
          fk[0] = nil
        end
        if apartment_excluded&.include?(fk[4].singularize.camelize)
          fk[3] = Apartment.default_schema
        elsif is_postgres && (fk[3] == 'public' || (is_multitenant && fk[3] == schema)) ||
              !is_postgres && ['mysql', 'performance_schema', 'sys'].exclude?(fk[3])
          fk[3] = nil
        end
        ::Brick._add_bt_and_hm(fk, relations)
      end
    end
  end

  tables = []
  views = []
  relations.each do |k, v|
    name_parts = k.split('.')
    idx = 1
    name_parts = name_parts.map do |x|
      ((idx += 1) < name_parts.length ? x.singularize : x).camelize
    end
    if v.key?(:isView)
      views
    else
      name_parts.shift if apartment && name_parts.length > 1 && name_parts.first == Apartment.default_schema
      tables
    end << name_parts.join('::')
  end
  unless tables.empty?
    puts "\nClasses that can be built from tables:"
    tables.sort.each { |x| puts x }
  end
  unless views.empty?
    puts "\nClasses that can be built from views:"
    views.sort.each { |x| puts x }
  end

  ::Brick.load_additional_references if initializer_loaded
end

#establish_connection(*args) ⇒ Object



1309
1310
1311
1312
1313
1314
1315
1316
# File 'lib/brick/extensions.rb', line 1309

def establish_connection(*args)
  conn = _brick_establish_connection(*args)
  begin
    _brick_reflect_tables
  rescue ActiveRecord::NoDatabaseError
  end
  conn
end

#retrieve_schema_and_tables(sql = nil, is_postgres = nil, schema = nil) ⇒ Object



1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
# File 'lib/brick/extensions.rb', line 1530

def retrieve_schema_and_tables(sql = nil, is_postgres = nil, schema = nil)
  sql ||= "SELECT t.table_schema AS \"schema\", t.table_name AS relation_name, t.table_type,#{"
    pg_catalog.obj_description(
      ('\"' || t.table_schema || '\".\"' || t.table_name || '\"')::regclass, 'pg_class'
    ) AS table_description," if is_postgres}
    c.column_name, c.data_type,
    COALESCE(c.character_maximum_length, c.numeric_precision) AS max_length,
    tc.constraint_type AS const, kcu.constraint_name AS \"key\",
    c.is_nullable
  FROM INFORMATION_SCHEMA.tables AS t
    LEFT OUTER JOIN INFORMATION_SCHEMA.columns AS c ON t.table_schema = c.table_schema
      AND t.table_name = c.table_name
    LEFT OUTER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS kcu ON
      -- ON kcu.CONSTRAINT_CATALOG = t.table_catalog AND
      kcu.CONSTRAINT_SCHEMA = c.table_schema
      AND kcu.TABLE_NAME = c.table_name
      AND kcu.position_in_unique_constraint IS NULL
      AND kcu.ordinal_position = c.ordinal_position
    LEFT OUTER JOIN INFORMATION_SCHEMA.table_constraints AS tc
      ON kcu.CONSTRAINT_SCHEMA = tc.CONSTRAINT_SCHEMA
      AND kcu.TABLE_NAME = tc.TABLE_NAME
      AND kcu.CONSTRAINT_NAME = tc.constraint_name
  WHERE t.table_schema #{is_postgres ?
      "NOT IN ('information_schema', 'pg_catalog')"
      :
      "= '#{ActiveRecord::Base.connection.current_database.tr("'", "''")}'"}#{"
    AND t.table_schema = COALESCE(current_setting('SEARCH_PATH'), 'public')" if is_postgres && schema }
--          AND t.table_type IN ('VIEW') -- 'BASE TABLE', 'FOREIGN TABLE'
    AND t.table_name NOT IN ('pg_stat_statements', ?, ?)
  ORDER BY 1, t.table_type DESC, 2, c.ordinal_position"
  ar_smtn = if ActiveRecord::Base.respond_to?(:schema_migrations_table_name)
              ActiveRecord::Base.schema_migrations_table_name
            else
              'schema_migrations'
            end
  ar_imtn = ActiveRecord.version >= ::Gem::Version.new('5.0') ? ActiveRecord::Base. : ''
  ActiveRecord::Base.execute_sql(sql, ar_smtn, ar_imtn)
end