1

I'm setting up self-hosted continuous integration using buildbox.io. I need to create the tables and columns in my test database.

On my own computer, I've been using public $import = 'MyTable'; for every fixture, to copy the table defenitions from the $default database connection in database.php. It works well, since my development database is always up to date with the latest migrations.

Also, it seems like a massive pain to do it the other way, where you'd have to manually keep your database field definitions up to date in your fixtures, each time you make changes to your database. This seems especially redundant given the list of fields will already be up to date in app/Config/Schema/schema.php

On the server, using public $import = 'MyTable'; won't work. Even if I did want to make the staging database my $default config when running tests, the staging database can't be relied upon to be up to date at all times.

So, my question is, how can I do it? Is there a way to tell Cake to use the definitions in schema.php for building its test database from fixtures? Or is the only way for me to manually add field definitions in all my fixtures? (that seems like a massive pain!)

joshua.paling
  • 13,762
  • 4
  • 45
  • 60

2 Answers2

1

What I do for fixtures is just update a set of sql statements that contain test data. The file is something like schema_MD5HASH.sql.

When my tests run, it gets the sql hash of the schema.php file for my app and uses that to run the associated schema_MD5HASH.sql. If the file doesn't exist, the tests fail, and then it's just one extra build to remember to build my fixtures.

Something else I've been playing with is:

  • annotating the schema with extra data for each field - something akin to a field_type
  • loading up the schema file in a fixture and reading in the specific table
  • parsing the field_type and generating fake data according to a library like Faker

This way is sort of weird, and you have to be careful that a schema dump doesn't override your annotations, but it works pretty okay.

Jose Diaz-Gonzalez
  • 2,066
  • 1
  • 15
  • 19
  • Cool that's an approach I hadn't thought of. I think I'd still rather keep my fixture data the way it is and pull in definitions from schema.php. I'll try get that happening and if not, give those methods you mentioned a go. Cheers Jose. – joshua.paling Jun 30 '14 at 02:44
0

OK, I've got a solution I'm happy with, that requires minimal changes to my code.

For the Buildbox CI environment, I was previously using only one database - the test database. I've now added another, so I've got a test and default database (along with $test and $default database configs).

During the Buildbox bootstrap process (where it runs bootstrap.sh - a file that sits on your CI server, outside your app) I call Console/cake schema create, which will populate the default database from schema.php.

Now, I can run my tests as normal, and they will copy the table definitions from the default database, as my $import setting recommends.

So, the default database never contains any data - it only exists so it can be created from schema.php, and can therefore be used to import table definitions during my tests.

Here's the Cake-sepcific lines in my Buildbox bootstrap.sh:

echo -e "--- Installing plugins via composer"
buildbox-run "composer install"

echo -e "--- Setting up database"
buildbox-run "cp ./app/Config/database.buildbox.php ./app/Config/database.php"

# create the default database, so that we can use $import as a means of generating fixture data on the test database.
# say yes at all prompts: http://askubuntu.com/questions/338857/automatically-enter-input-in-command-line
buildbox-run "yes | ./app/Console/cake schema create"
joshua.paling
  • 13,762
  • 4
  • 45
  • 60