|
18 | 18 | require "models/traffic_light" |
19 | 19 | require "models/treasure" |
20 | 20 |
|
| 21 | +# Hacky tool that searches for table definition code in schema.rb |
| 22 | +# and evals it. Doing this ensure we're always using the latest |
| 23 | +# schema. |
| 24 | +module TableCreator |
| 25 | + def self.create_table_using_test_schema(table_name, connection) |
| 26 | + table_name = table_name.to_sym |
| 27 | + @schema_file ||= SCHEMA_ROOT + "/schema.rb" |
| 28 | + @ast ||= Prism::Translation::Parser.parse_file(@schema_file) |
| 29 | + to_search = [@ast] |
| 30 | + found = nil |
| 31 | + while !to_search.empty? |
| 32 | + node = to_search.shift |
| 33 | + next unless node.is_a?(Parser::AST::Node) |
| 34 | + if node in [:block, [:send, _, :create_table, [:sym, ^table_name], *], *] |
| 35 | + break found = node |
| 36 | + end |
| 37 | + to_search += node.children |
| 38 | + end |
| 39 | + raise "Schema #{table_name.inspect} not found" unless found |
| 40 | + connection.instance_eval(found.location.expression.source) |
| 41 | + end |
| 42 | +end |
| 43 | + |
21 | 44 | module CockroachDB |
22 | 45 | class FixturesTest < ActiveRecord::TestCase |
23 | 46 | include ConnectionHelper |
@@ -347,43 +370,17 @@ def teardown |
347 | 370 | super |
348 | 371 | Account.lease_connection.drop_table :accounts, if_exists: true |
349 | 372 | Account.lease_connection.exec_query("DROP SEQUENCE IF EXISTS accounts_id_seq") |
350 | | - Account.lease_connection.create_table :accounts, force: true do |t| |
351 | | - t.timestamps null: true |
352 | | - t.references :firm, index: false |
353 | | - t.string :firm_name |
354 | | - t.integer :credit_limit |
355 | | - t.string :status |
356 | | - t.integer "a" * max_identifier_length |
357 | | - end |
| 373 | + TableCreator.create_table_using_test_schema(:accounts, Account.lease_connection) |
358 | 374 |
|
359 | 375 | Company.lease_connection.drop_table :companies, if_exists: true |
360 | 376 | Company.lease_connection.exec_query("DROP SEQUENCE IF EXISTS companies_nonstd_seq CASCADE") |
361 | | - Company.lease_connection.create_table :companies, force: true do |t| |
362 | | - t.string :type |
363 | | - t.references :firm, index: false |
364 | | - t.string :firm_name |
365 | | - t.string :name |
366 | | - t.bigint :client_of |
367 | | - t.bigint :rating, default: 1 |
368 | | - t.integer :account_id |
369 | | - t.string :description, default: "" |
370 | | - t.integer :status, default: 0 |
371 | | - t.index [:name, :rating], order: :desc |
372 | | - t.index [:name, :description], length: 10 |
373 | | - t.index [:firm_id, :type, :rating], name: "company_index", length: { type: 10 }, order: { rating: :desc } |
374 | | - t.index [:firm_id, :type], name: "company_partial_index", where: "(rating > 10)" |
375 | | - t.index [:firm_id], name: "company_nulls_not_distinct", nulls_not_distinct: true |
376 | | - t.index :name, name: "company_name_index", using: :btree |
377 | | - t.index "(CASE WHEN rating > 0 THEN lower(name) END) DESC", name: "company_expression_index" if Company.lease_connection.supports_expression_index? |
378 | | - t.index [:firm_id, :type], name: "company_include_index", include: [:name, :account_id] |
379 | | - end |
| 377 | + TableCreator.create_table_using_test_schema(:companies, Company.lease_connection) |
| 378 | + # CockroachDB specific schema addition |
| 379 | + Company.lease_connection.add_index(:companies, [:firm_id, :type], name: "company_include_index", include: [:name, :account_id]) |
380 | 380 |
|
381 | 381 | Course.lease_connection.drop_table :courses, if_exists: true |
382 | 382 | Course.lease_connection.exec_query("DROP SEQUENCE IF EXISTS courses_id_seq") |
383 | | - Course.lease_connection.create_table :courses, force: true do |t| |
384 | | - t.column :name, :string, null: false |
385 | | - t.column :college_id, :integer, index: true |
386 | | - end |
| 383 | + TableCreator.create_table_using_test_schema(:courses, Course.lease_connection) |
387 | 384 |
|
388 | 385 | recreate_parrots |
389 | 386 |
|
@@ -455,28 +452,9 @@ def recreate_parrots |
455 | 452 | conn.drop_table :parrots_treasures, if_exists: true |
456 | 453 | conn.drop_table :parrots, if_exists: true |
457 | 454 |
|
458 | | - conn.create_table :parrots, force: :cascade do |t| |
459 | | - t.string :name |
460 | | - t.integer :breed, default: 0 |
461 | | - t.string :color |
462 | | - t.string :parrot_sti_class |
463 | | - t.integer :killer_id |
464 | | - t.integer :updated_count, :integer, default: 0 |
465 | | - t.datetime :created_at |
466 | | - t.datetime :created_on |
467 | | - t.datetime :updated_at |
468 | | - t.datetime :updated_on |
469 | | - end |
470 | | - |
471 | | - conn.create_table :parrots_pirates, id: false, force: true do |t| |
472 | | - t.references :parrot, foreign_key: true |
473 | | - t.references :pirate, foreign_key: true |
474 | | - end |
475 | | - |
476 | | - conn.create_table :parrots_treasures, id: false, force: true do |t| |
477 | | - t.references :parrot, foreign_key: true |
478 | | - t.references :treasure, foreign_key: true |
479 | | - end |
| 455 | + TableCreator.create_table_using_test_schema(:parrots, conn) |
| 456 | + TableCreator.create_table_using_test_schema(:parrots_pirates, conn) |
| 457 | + TableCreator.create_table_using_test_schema(:parrots_treasures, conn) |
480 | 458 | end |
481 | 459 | end |
482 | 460 | end |
|
0 commit comments