-
Notifications
You must be signed in to change notification settings - Fork 12
Creating Rock Migrations
When your code is ready to check in and you're ready to include it in the database as well, here's a checklist to review and use.
- Run the
Dev Tools\Sql\CodeGen_PagesBlocksAttributesMigration.sqlscript. It will output stuff you'll want to use in your next step. - Run
AddMigration [YOUR_MIGRATION_NAME] - Edit this skeleton migration and paste in the stuff from the MigrationUp output into your migration's
Up()method and then the MigrationDown output into theDown()method. Pay particular attention to the order that things are deleted since you cannot delete many things unless any related items are first deleted. - REVIEW each item you just pasted. Not all of them will be things you should include in your migration.
- Manually add any other data items that were not already accounted for by the SQL script including:
- EntityTypes
- NoteTypes
- PageContext (if any of your blocks use Context you should double-check this one)
Important: You should consider a few things when you're creating your Up() and Down() migration. For example, if your migration adds a new NoteType you probably won't want to delete it if there are Note records using it. Those notes are things you did not create and it would probably be bad form if you destroyed that data. Similarly, if you could not delete it during a Down() then you probably should check if it exists already during the Up(). Therefore you may want to do something like this in the Up():
public override void Up()
{
Sql( @"
-- Add 'Prayer Comment' NoteType if it's not already there
DECLARE @PrayerCommentNoteTypeId int
SELECT @PrayerCommentNoteTypeId = [Id] FROM [NoteType] WHERE [Guid] = '0EBABD75-0890-4756-A9EE-62626282BB5D'
IF @PrayerCommentNoteTypeId IS NULL
BEGIN
INSERT INTO [NoteType] ...
...
");
// ...
}... and this in the Down():
public override void Down()
{
Sql( @"
-- Delete the NoteType but ONLY if it's not being used...
IF NOT EXISTS(SELECT * FROM [Note] WHERE [NoteTypeId] IN
(SELECT [Id] FROM [NoteType] WHERE [Guid] = '0EBABD75-0890-4756-A9EE-62626282BB5D' )
)
BEGIN
DELETE [NoteType] WHERE [Guid] = '0EBABD75-0890-4756-A9EE-62626282BB5D'
END
");
// ...
}- Replace your db name with a new database name into your RockContext connection string in your
web.connectionStrings.configfile. - Run Update-Database to see if it worked
- Verify everything by running Rock (F5) and checking every detail of your stuff.
- If it's not good return to step 3 in the previous section; otherwise proceed to next step.
- Lastly, test your
Down()migration by migrating back to the last migration that exists before your new migration. Use theUpdate-Database TargetMigration:[THE_PREVIOUS_ONE]. If that was successful, continue to the Commit/Push/Deploy section otherwise go back to step 3 in the previous section.
If everything looks good, you can commit your changes, merge (as needed) and push up to origin.