|
| 1 | +var dbm = require('db-migrate'), |
| 2 | + async = require('async'); |
| 3 | +var type = dbm.dataType; |
| 4 | + |
| 5 | +exports.up = function(db, callback) { |
| 6 | + async.series([ |
| 7 | + db.createTable.bind(db, 'state_machines', { |
| 8 | + id: { type: 'int', primaryKey: true, autoIncrement: true }, |
| 9 | + current_state_id: { type: 'int' }, |
| 10 | + created_at: 'datetime', |
| 11 | + updated_at: 'datetime' |
| 12 | + }), |
| 13 | + db.createTable.bind(db, 'states', { |
| 14 | + id: { type: 'int', primaryKey: true, autoIncrement: true }, |
| 15 | + name: { type: 'string', notNull: true }, |
| 16 | + state_machine_id: { type: 'int', notNull: true }, |
| 17 | + data: { type: 'text' }, |
| 18 | + created_at: 'datetime', |
| 19 | + updated_at: 'datetime' |
| 20 | + }), |
| 21 | + db.createTable.bind(db, 'transitions', { |
| 22 | + id: { type: 'int', primaryKey: true, autoIncrement: true }, |
| 23 | + state_id: { type: 'int', notNull: true }, |
| 24 | + input: { type: 'string', notNull: true }, |
| 25 | + next_state_id: { type: 'int', notNull: true }, |
| 26 | + created_at: 'datetime', |
| 27 | + updated_at: 'datetime' |
| 28 | + }), |
| 29 | + db.runSql.bind(db, 'ALTER TABLE state_machines ADD CONSTRAINT fk_state_id FOREIGN KEY (current_state_id) REFERENCES states(id)'), |
| 30 | + db.runSql.bind(db, 'ALTER TABLE states ADD CONSTRAINT fk_state_machine_id FOREIGN KEY (state_machine_id) REFERENCES state_machines(id)'), |
| 31 | + db.runSql.bind(db, 'ALTER TABLE transitions ADD CONSTRAINT fk_state_id FOREIGN KEY (state_id) REFERENCES states(id)'), |
| 32 | + db.runSql.bind(db, 'ALTER TABLE transitions ADD CONSTRAINT fk_next_state_id FOREIGN KEY (next_state_id) REFERENCES states(id)') |
| 33 | + ], callback) |
| 34 | +}; |
| 35 | + |
| 36 | +exports.down = function(db, callback) { |
| 37 | + async.series([ |
| 38 | + db.dropTable.bind(db, 'transitions'), |
| 39 | + db.dropTable.bind(db, 'states'), |
| 40 | + db.dropTable.bind(db, 'state_machines') |
| 41 | + ], callback) |
| 42 | +}; |
0 commit comments