15.3 Migration

Migrations provide a way to do complicated modification of tables in a database by GNU Artanis. 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.

Then you’d edit the 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))

Then you run the up command for 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 the down command of migration, as:

art migrate down person

The table person will be dropped.