Skip to content

Commit 6a526ad

Browse files
committed
fix(test): latest schema.rb def
1 parent 68b6877 commit 6a526ad

File tree

1 file changed

+31
-53
lines changed

1 file changed

+31
-53
lines changed

test/cases/fixtures_test.rb

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,29 @@
1818
require "models/traffic_light"
1919
require "models/treasure"
2020

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+
2144
module CockroachDB
2245
class FixturesTest < ActiveRecord::TestCase
2346
include ConnectionHelper
@@ -347,43 +370,17 @@ def teardown
347370
super
348371
Account.lease_connection.drop_table :accounts, if_exists: true
349372
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)
358374

359375
Company.lease_connection.drop_table :companies, if_exists: true
360376
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])
380380

381381
Course.lease_connection.drop_table :courses, if_exists: true
382382
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)
387384

388385
recreate_parrots
389386

@@ -455,28 +452,9 @@ def recreate_parrots
455452
conn.drop_table :parrots_treasures, if_exists: true
456453
conn.drop_table :parrots, if_exists: true
457454

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)
480458
end
481459
end
482460
end

0 commit comments

Comments
 (0)