Skip to content

Commit ad0518a

Browse files
committed
Namespace backup dir by activerecord version and update CONTRIBUTING.md to demonstrate loading from a template.
1 parent 48bc65f commit ad0518a

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

CONTRIBUTING.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,26 @@ RAILS_SOURCE="path/to/local_copy" bundle exec rake test
7878

7979
`test/config.yml` assumes CockroachDB will be running at localhost:26257 with a root user. Make changes to `test/config.yml` as needed.
8080

81+
### Run Tests from a Backup
82+
83+
Loading the full test schema every time a test runs can take a while, so for cases where loading the schema sequentially is unimportant, it is possible to use a backup to set up the database. This is significantly faster than the standard method and is provided to run individual tests faster, but should not be used to validate a build.
84+
85+
First create the template database.
86+
87+
```bash
88+
bundle exec rake db:create_test_template
89+
```
90+
91+
This will create a template database for the current version (ex. `activerecord_test_template611` for version 6.1.1) and create a `BACKUP` in the `nodelocal://self/activerecord-crdb-adapter/#{activerecord_version}` directory.
92+
93+
To load from the template, use the `COCKROACH_LOAD_FROM_TEMPLATE` flag.
94+
95+
```bash
96+
COCKROACH_LOAD_FROM_TEMPLATE=1 TEST_FILES="test/cases/adapters/postgresql/ddl_test.rb" bundle exec rake test
97+
```
98+
99+
And the `activerecord_unittest` database will use the `RESTORE` command to load the schema from the template database.
100+
81101
# Improvements
82102

83103

test/support/template_creator.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ module TemplateCreator
1414
host: 'localhost'
1515
}.freeze
1616

17-
BACKUP_PATH = "nodelocal://self/activerecord-crdb-adapter".freeze
17+
BACKUP_DIR = "nodelocal://self/activerecord-crdb-adapter"
1818

1919
module_function
2020

21+
def ar_version
22+
ActiveRecord.version.version.gsub('.','')
23+
end
24+
25+
def version_backup_path
26+
BACKUP_DIR + "/#{ar_version}"
27+
end
28+
2129
def template_db_name
22-
"activerecord_unittest_template#{ActiveRecord.version.version.gsub('.', '')}"
30+
"activerecord_unittest_template#{ar_version}"
2331
end
2432

2533
def connect(connection_hash=nil)
@@ -60,11 +68,10 @@ def create_test_template
6068
conn['database'] = template_db_name
6169
connect(conn)
6270

63-
6471
load_schema
6572

6673
# create BACKUP to restore from
67-
ActiveRecord::Base.connection.execute("BACKUP DATABASE #{template_db_name} TO '#{BACKUP_PATH}'")
74+
ActiveRecord::Base.connection.execute("BACKUP DATABASE #{template_db_name} TO '#{version_backup_path}'")
6875
end
6976

7077
def restore_from_template
@@ -82,6 +89,6 @@ def restore_from_template
8289
end
8390
ActiveRecord::Base.connection.execute("CREATE DATABASE activerecord_unittest")
8491

85-
ActiveRecord::Base.connection.execute("RESTORE #{template_db_name}.* FROM '#{BACKUP_PATH}' WITH into_db = 'activerecord_unittest'")
92+
ActiveRecord::Base.connection.execute("RESTORE #{template_db_name}.* FROM '#{version_backup_path}' WITH into_db = 'activerecord_unittest'")
8693
end
8794
end

0 commit comments

Comments
 (0)