Re: [silverstripe-dev] Re: Database migrations?

87 views
Skip to first unread message

Gregory Smirnov

unread,
Feb 3, 2015, 5:12:29 AM2/3/15
to silverst...@googlegroups.com

Hello,

 

I would like to share the way we have implemented database migrations.

 

We have implemented data migrations and batch accounting the Laravel way:

 

1.       A special controller for DevelopmentAdmin that handles request:

 

/dev/db – Lists pending and last batch migrations.

/dev/db/migrate_data – migrates the data.

 

2.       All files with migration classes must be of form YYYYMMDD_HHMM_ClassName.php and extend default SS MigrationTask class.

3.       We keep all migrations in one folder, but they may be anywhere. SS class manifest is used to collect all the classes that extend MigrationTask.

4.       The migration class may implement 3 methods: prepare, up, down, but only up() is mandatory.

5.       The deployment runs like this:

 

a)      Copy files

 

b)      sake dev/db/migrate_data –prepare

Create backups or manipulate existing db data with old db schema

 

c)       sake dev/build

Update db schema

 

d)      sake dev/db/migrate_data

Run data migration tasks. This automatically creates a new batch of the tasks.

               

                If some error happened during lasts step, we call

                                sake dev/db/migrate_data --rollback

                and continue with standard rollback sequence.

 

During development you can run a separate task as standard build task: sake dev/tasks/MyMigrationTask

 

Cons: we cannot call prepare() or down() methods with existing MigrarionTask class, the logic of run() method should be changed.

 

Gregory

 

clyons

unread,
Feb 3, 2015, 10:22:08 AM2/3/15
to silverst...@googlegroups.com
Wouldn't you have to disable /dev/build? DB Migrations are nice, but I'd be curious to hear how they improve your workflow in SS.  Do you do any seeding?

Gregory Smirnov

unread,
Feb 3, 2015, 11:22:52 AM2/3/15
to silverst...@googlegroups.com

No, you should not disable /dev/build since they complement each other.

 

Imagine you have new ImprovedProduct class and need to migrate old data from Product.

 

dev/build

1.       Updates database schema: creates new ImprovedProduct table.

2.       Does data seeding by calling DataObject::requireDefaultRecords()

dev/db/migrate_data

1.       Moves/transforms data from table Product into ImprovedProduct

2.       Renames Product table into _obsolete_Product

 

Infrastructure ensures that such migration is run only once.

 

The benefits we got:

-          Method requireDefaultRecords() now has one responsibility: data seeding (we used to put small migration logic there)

-          No need to keep a list of extra changes to do after deployment (we used to create BuildTasks to do migrations manually)

-          We have a clean history of code and data changes in the repository.

 

 

Jedateach

unread,
Feb 8, 2015, 11:01:10 PM2/8/15
to silverst...@googlegroups.com
I'd love to see this stuff rolled into SS core. It's so vital to get right.

p...@catchyour.com

unread,
Feb 11, 2015, 1:49:47 PM2/11/15
to silverst...@googlegroups.com
Hi, just joined this list because of this topic. Coincidentally, I ended up posting a Github issue (#3868) about this very topic a few days ago (not long after this ended up coming up, prior to me signing up). It seems to be a subject that has happened to come up a few times on this list so I wanted to chime in and say "me too" :) I'd be willing to pitch in on contributing this to core somehow or as a module. Maybe for speed of adoption, a module would be best? That way I could get started on this pretty quickly and other people can either include it in their SS installation or fork it, customize it and use their variation of it (if it doesn't match their needs precisely, albeit my goal is to be as simple/baseline as possible to fit most needs).  

Github issue:

UserVoice suggestion:
Reply all
Reply to author
Forward
0 new messages