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 File.exist?(brick_initializer = Rails.root.join('config/initializers/brick.rb'))
initializer_loaded = load brick_initializer
end
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
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']
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'
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
measures = []
case ActiveRecord::Base.connection.adapter_name
when 'PostgreSQL', 'SQLite' ActiveRecord::Base.retrieve_schema_and_tables(sql, is_postgres, schema).each do |r|
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}"] ||= []
end
key << col_name if key
cols = relation[:cols] cols[col_name] = [r['data_type'], r['max_length'], measures&.include?(col_name), r['is_nullable'] == 'NO']
end
else ActiveRecord::Base.retrieve_schema_and_tables(sql).each do |r|
relation = relations[(relation_name = r[1])] relation[:isView] = true if r[2] == 'VIEW' col_name = r[3]
key = case r[6] when 'PRIMARY KEY'
relation[:pkey][r[7] || relation_name] ||= []
when 'UNIQUE'
relation[:ukeys][r[7] || "#{relation_name}.#{col_name}"] ||= []
end
key << col_name if key
cols = relation[:cols] cols[col_name] = [r[4], r[5], measures&.include?(col_name)]
end
end
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 }"
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
ActiveRecord::Base.execute_sql(sql).each do |fk|
fk = fk.values unless fk.is_a?(Array)
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
|