niccokunzmann / maglevrecord-demo

A demo application for maglev record

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

maglevrecord-demo

A demo application for maglev record.

Make vs. Rake

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.

keeping track of changes

Open 4 command line windows (terminals) in the project directory.

cd project

Three of them are used to track the changes while you code.

  1. 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.

  2. 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

  3. 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 by bundle exec rake migrate:auto.

reset and restart

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.

apply all migrations

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.

create a model class

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.

create a migration

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).

change the model

Edit app/models/my_model.rb

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.

remove methods

Just do it and see what happens.

change accessors

Just do it and see what happens.

change superclass

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

About

A demo application for maglev record


Languages

Language:Ruby 89.3%Language:Shell 6.5%Language:JavaScript 4.2%