A demo application for maglev record.
There is a Makefile for the make
command for this tutorial only.
Its purpose is to access some commads faster.
It is not required by a real Rails application.
Whereas the Rakefile is used by the command rake
and is used by all Rails applications.
Open 4 command line windows (terminals) in the project directory.
cd project
Three of them are used to track the changes while you code.
-
track the file system:
make watch_files
output:
./migrations [error opening dir] ./app/models
It uses
tree
to show the models and migrations of the project folder. -
see which migrations shall be applied:
make watch_migrations
output:
Already applied all migrations.
This calls
bundle exec rake migrate:up?
to show which migrations have to be applied -
track the changes in your stone, your database.
make watch_changes
output:
rake migrate:up has to be done first
It calls
bundle exec rake migrate:auto?
to show what changes would be written to an automigration generated bybundle exec rake migrate:auto
.
If you want to restart you can always do make clean
. This will
- destroy and restart the stone
- delete the example files
You can do it now. make watch_changes
and make watch_migrations
need to be restarted.
bundle exec rake migrate:up
This applies all migrations in the migrations
folder and creates a snapshot of the stone in the stone that no classes are defined yet.
make example
This creates a file app/models/my_model.rb
with a class MyModel
:
See the ouput of make watch_files
:
./migrations
./app/models
`-- my_model.rb
Automatically the changes will update:
loaded app/my_model.rb
loaded app/my_model.rb
#new class: MyModel
You can edit app/models/my_model.rb
with your favourite editor.
bundle exec rake migrate:auto
creates a new migration in the migration folder and puts out the migration file:
loaded app/my_model.rb
loaded app/my_model.rb
loaded app/my_model.rb
./migrations/migration_2013-05-May-27_12.19.00.rb
See how the file system updates:
./migrations
`-- migration_2013-05-May-27_12.19.00.rb
./app/models
`-- my_model.rb
And the new migration has to be applied
to do: 'fill in description here' from Mon May 27 14:19:00 +0000 2013
Migrations look like this and can be edited:
require "maglev_record/migration"
require "time"
MaglevRecord::Migration.new(Time.parse('Mon May 27 12:19:00 +0000 2013'), 'fill in description here') do
def up
# here is the code that is shown by bundle exec rake migrate:auto?
#new class: MyModel
end
def down
# put the code that reverses the code in up here
# remove the next line that throws he error
raise IrreversibleMigration, 'The migration has no downcode'
end
end
After you applied it you have to do
rake migrate:up
to apply the changes. (No changes since there is only a command but this will also create a new snapshot of all models from your model files).
and add some methods and accessors like this:
class MyModel
# ...
def my_method
end
attr_accessor :age
end
changes:
#new accessor :attribute of MyModel
#new instance method: MyModel.new.age
#new instance method: MyModel.new.age=
#new instance method: MyModel.new.my_method
This can also be migrated since it creates a new snapshot.
bundle exec rake migrate:auto
bundle exec rake migrate:up
If one of the commands aborts with a CommitFailedException
this is because of the monitoring scripts that commit to the stone.
Just repeat it until it works.
Just do it and see what happens.
Just do it and see what happens.
make example2
bundle exec rake migrate:auto
bundle exec migrate:up
creates a new model MyModel2
.
going to the source of MyModel2 the superclass can be changed:
# ...
class MyModel2 < MyModel # let it inherit
# ...
The changes show:
# TypeError: superclass mismatch for MyModel2
# in ./app/models/my_model2.rb
MyModel2.change_superclass_to MyModel
To resolve it and let the migration automatically set the new superclass one can do:
bundle exec rake migrate:auto
bundle exec migrate:up