Next: , Up: Database   [Contents]

12.1 Migration

Migration provides a way do complicated modification of tables in database automatically. Here’s an example.

First, draw a migration:

# art draw migration person
drawing    migration person
working    Migration `20151107040209_person.scm'

You’ll see something similar like above.

In this case, you may edit file db/migration/20151107040209_person.scm:

(migrate-up
  (create-table
   'person
   '(id auto (#:primary-key))
   '(name char-field (#:not-null #:maxlen 10))
   '(age tiny-integer (#:not-null))
   '(email char-field (#:maxlen 20))))

(migrate-down
  (drop-table 'person))

Now you may run up command of migration:

art migrate up person

Then migrate-up function will be called, and this will create a table named person:

+-------+---------------------+------+-----+---------+----------------+
| Field | Type                | Null | Key | Default | Extra          |
+-------+---------------------+------+-----+---------+----------------+
| id    | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(10)         | NO   |     | NULL    |                |
| age   | tinyint(4)          | NO   |     | NULL    |                |
| email | varchar(20)         | YES  |     | NULL    |                |
+-------+---------------------+------+-----+---------+----------------+

If you run down command of migration:

art migrate down person

Obviously, the table person will be dropped.