|
1483 | 1483 | end
|
1484 | 1484 | end
|
1485 | 1485 |
|
1486 |
| -if ActiveRecord::TestCase.current_adapter?(:PostgreSQLAdapter) |
| 1486 | +if ActiveRecord::Base.connection.supports_insert_returning? && !ActiveRecord::TestCase.current_adapter?(:SQLite3Adapter) |
1487 | 1487 | ActiveRecord::Base.connection.create_table :pk_autopopulated_by_a_trigger_records, force: true, id: false do |t|
|
1488 | 1488 | t.integer :id, null: false
|
1489 | 1489 | end
|
1490 | 1490 |
|
1491 |
| - ActiveRecord::Base.connection.execute( |
1492 |
| - <<-SQL |
1493 |
| - CREATE OR REPLACE FUNCTION populate_column() |
1494 |
| - RETURNS TRIGGER AS $$ |
1495 |
| - DECLARE |
1496 |
| - max_value INTEGER; |
1497 |
| - BEGIN |
1498 |
| - SELECT MAX(id) INTO max_value FROM pk_autopopulated_by_a_trigger_records; |
1499 |
| - NEW.id = COALESCE(max_value, 0) + 1; |
1500 |
| - RETURN NEW; |
1501 |
| - END; |
1502 |
| - $$ LANGUAGE plpgsql; |
1503 |
| -
|
1504 |
| - CREATE TRIGGER before_insert_trigger |
1505 |
| - BEFORE INSERT ON pk_autopopulated_by_a_trigger_records |
1506 |
| - FOR EACH ROW |
1507 |
| - EXECUTE FUNCTION populate_column(); |
1508 |
| - SQL |
1509 |
| - ) |
| 1491 | + if ActiveRecord::TestCase.current_adapter?(:PostgreSQLAdapter) |
| 1492 | + ActiveRecord::Base.connection.execute( |
| 1493 | + <<-SQL |
| 1494 | + CREATE OR REPLACE FUNCTION populate_column() |
| 1495 | + RETURNS TRIGGER AS $$ |
| 1496 | + DECLARE |
| 1497 | + max_value INTEGER; |
| 1498 | + BEGIN |
| 1499 | + SELECT MAX(id) INTO max_value FROM pk_autopopulated_by_a_trigger_records; |
| 1500 | + NEW.id = COALESCE(max_value, 0) + 1; |
| 1501 | + RETURN NEW; |
| 1502 | + END; |
| 1503 | + $$ LANGUAGE plpgsql; |
| 1504 | +
|
| 1505 | + CREATE TRIGGER before_insert_trigger |
| 1506 | + BEFORE INSERT ON pk_autopopulated_by_a_trigger_records |
| 1507 | + FOR EACH ROW |
| 1508 | + EXECUTE FUNCTION populate_column(); |
| 1509 | + SQL |
| 1510 | + ) |
| 1511 | + elsif ActiveRecord::TestCase.current_adapter?(:Mysql2Adapter, :TrilogyAdapter) |
| 1512 | + ActiveRecord::Base.connection.execute( |
| 1513 | + <<-SQL |
| 1514 | + CREATE TRIGGER before_insert_trigger |
| 1515 | + BEFORE INSERT ON pk_autopopulated_by_a_trigger_records |
| 1516 | + FOR EACH ROW |
| 1517 | + SET NEW.id = (SELECT COALESCE(MAX(id), 0) + 1 FROM pk_autopopulated_by_a_trigger_records); |
| 1518 | + SQL |
| 1519 | + ) |
| 1520 | + end |
1510 | 1521 | end
|
1511 | 1522 |
|
1512 | 1523 | Course.connection.create_table :courses, force: true do |t|
|
|
0 commit comments