physique 0.3.10 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/FLUENT_MIGRATOR.md +300 -300
  3. data/Gemfile +3 -3
  4. data/Gemfile.lock +51 -11
  5. data/Guardfile +34 -0
  6. data/README.md +368 -368
  7. data/RUBY_SETUP.md +14 -14
  8. data/Rakefile +1 -1
  9. data/lib/physique.rb +22 -22
  10. data/lib/physique/config.rb +23 -23
  11. data/lib/physique/dsl.rb +46 -46
  12. data/lib/physique/project_path_resolver.rb +45 -45
  13. data/lib/physique/solution.rb +72 -72
  14. data/lib/physique/task_builders/build.rb +76 -69
  15. data/lib/physique/task_builders/default.rb +19 -19
  16. data/lib/physique/task_builders/fluent_migrator.rb +302 -299
  17. data/lib/physique/task_builders/nuget.rb +18 -6
  18. data/lib/physique/task_builders/octopus.rb +4 -2
  19. data/lib/physique/task_builders/publish_nugets.rb +5 -3
  20. data/lib/physique/task_builders/test.rb +4 -4
  21. data/lib/physique/tasks/fluent_migrator.rb +158 -158
  22. data/lib/physique/tasks/nugets_pack.rb +12 -12
  23. data/lib/physique/tasks/octopus_pack.rb +1 -1
  24. data/lib/physique/tasks/sqlcmd.rb +109 -109
  25. data/lib/physique/tasks/versionizer.rb +112 -112
  26. data/lib/physique/tasks_builder.rb +59 -59
  27. data/lib/physique/tool_locator.rb +50 -50
  28. data/lib/physique/version.rb +3 -3
  29. data/physique.gemspec +29 -28
  30. data/spec/fluent_migrator_config_spec.rb +39 -39
  31. data/spec/project_spec.rb +42 -42
  32. data/spec/publish_nugets_spec.rb +40 -40
  33. data/spec/solution_spec.rb +11 -11
  34. data/spec/sqlcmd_spec.rb +146 -146
  35. data/spec/support/shared_contexts/rake.rb +20 -21
  36. data/spec/task_builders/build_spec.rb +25 -25
  37. data/spec/task_builders/default_spec.rb +17 -17
  38. data/spec/task_builders/fluent_migrator_spec.rb +70 -68
  39. data/spec/task_builders/nspec_spec.rb +13 -13
  40. data/spec/task_builders/nuget_spec.rb +16 -16
  41. data/spec/test_data/solutions/.nuget/NuGet.Config +5 -5
  42. data/spec/test_data/solutions/.nuget/NuGet.exe +0 -0
  43. data/spec/test_data/solutions/.nuget/NuGet.targets +144 -144
  44. data/spec/test_data/solutions/.nuget/packages.config +3 -3
  45. data/spec/test_data/solutions/basic/.semver +6 -6
  46. data/spec/test_data/solutions/basic/Basic.Tests/Basic.Tests.csproj +58 -58
  47. data/spec/test_data/solutions/basic/Basic.Tests/Class1.cs +14 -14
  48. data/spec/test_data/solutions/basic/Basic.Tests/Properties/AssemblyInfo.cs +36 -36
  49. data/spec/test_data/solutions/basic/Basic.Tests/packages.config +3 -3
  50. data/spec/test_data/solutions/basic/Basic.sln +28 -28
  51. data/spec/test_data/solutions/basic/Basic/Basic.csproj +52 -52
  52. data/spec/test_data/solutions/basic/Basic/Class1.cs +12 -12
  53. data/spec/test_data/solutions/basic/Basic/Properties/AssemblyInfo.cs +36 -36
  54. data/spec/test_data/solutions/basic/Rakefile.rb +10 -10
  55. data/spec/test_data/solutions/basic/packages.config +3 -3
  56. data/spec/test_data/solutions/fluent-migrator/.semver +6 -6
  57. data/spec/test_data/solutions/fluent-migrator/Basic.Migrations/Migrations/.gitkeep +0 -0
  58. data/spec/test_data/solutions/fluent-migrator/Basic.Migrations/Properties/AssemblyInfo.cs +36 -36
  59. data/spec/test_data/solutions/fluent-migrator/Basic.Migrations/TestMigration.cs +19 -19
  60. data/spec/test_data/solutions/fluent-migrator/Basic.Migrations/_Scripts/create.sql +1 -1
  61. data/spec/test_data/solutions/fluent-migrator/Basic.Migrations/_Scripts/drop.sql +4 -4
  62. data/spec/test_data/solutions/fluent-migrator/Basic.Migrations/_Scripts/seed.sql +1 -1
  63. data/spec/test_data/solutions/fluent-migrator/Basic.Migrations/packages.config +3 -3
  64. data/spec/test_data/solutions/fluent-migrator/Basic.Specs/Basic.Specs.csproj +59 -59
  65. data/spec/test_data/solutions/fluent-migrator/Basic.Specs/Class1.cs +12 -12
  66. data/spec/test_data/solutions/fluent-migrator/Basic.Specs/DebuggerShim.cs +42 -42
  67. data/spec/test_data/solutions/fluent-migrator/Basic.Specs/Properties/AssemblyInfo.cs +36 -36
  68. data/spec/test_data/solutions/fluent-migrator/Basic.Specs/packages.config +3 -3
  69. data/spec/test_data/solutions/fluent-migrator/Basic.sln +39 -39
  70. data/spec/test_data/solutions/fluent-migrator/Basic/Basic.csproj +55 -55
  71. data/spec/test_data/solutions/fluent-migrator/Basic/Class1.cs +12 -12
  72. data/spec/test_data/solutions/fluent-migrator/Basic/Properties/AssemblyInfo.cs +36 -36
  73. data/spec/test_data/solutions/fluent-migrator/Basic/packages.config +2 -2
  74. data/spec/test_data/solutions/fluent-migrator/Rakefile.rb +20 -20
  75. data/spec/test_data/solutions/multiple-fluent-migrator/.semver +6 -6
  76. data/spec/test_data/solutions/multiple-fluent-migrator/Basic.Migrations1/Basic.Migrations1.csproj +52 -52
  77. data/spec/test_data/solutions/multiple-fluent-migrator/Basic.Migrations1/Class1.cs +12 -12
  78. data/spec/test_data/solutions/multiple-fluent-migrator/Basic.Migrations1/Properties/AssemblyInfo.cs +36 -36
  79. data/spec/test_data/solutions/multiple-fluent-migrator/Basic.Migrations2/Basic.Migrations2.csproj +52 -52
  80. data/spec/test_data/solutions/multiple-fluent-migrator/Basic.Migrations2/Class1.cs +12 -12
  81. data/spec/test_data/solutions/multiple-fluent-migrator/Basic.Migrations2/Properties/AssemblyInfo.cs +36 -36
  82. data/spec/test_data/solutions/multiple-fluent-migrator/Basic.Specs/Basic.Specs.csproj +59 -59
  83. data/spec/test_data/solutions/multiple-fluent-migrator/Basic.Specs/Class1.cs +12 -12
  84. data/spec/test_data/solutions/multiple-fluent-migrator/Basic.Specs/DebuggerShim.cs +42 -42
  85. data/spec/test_data/solutions/multiple-fluent-migrator/Basic.Specs/Properties/AssemblyInfo.cs +36 -36
  86. data/spec/test_data/solutions/multiple-fluent-migrator/Basic.Specs/packages.config +3 -3
  87. data/spec/test_data/solutions/multiple-fluent-migrator/Basic.sln +45 -45
  88. data/spec/test_data/solutions/multiple-fluent-migrator/Basic/Basic.csproj +55 -55
  89. data/spec/test_data/solutions/multiple-fluent-migrator/Basic/Class1.cs +12 -12
  90. data/spec/test_data/solutions/multiple-fluent-migrator/Basic/Properties/AssemblyInfo.cs +36 -36
  91. data/spec/test_data/solutions/multiple-fluent-migrator/Basic/packages.config +2 -2
  92. data/spec/test_data/solutions/multiple-fluent-migrator/Rakefile.rb +28 -28
  93. data/spec/test_data/solutions/nspec/.semver +6 -6
  94. data/spec/test_data/solutions/nspec/Basic.Specs/Basic.Specs.csproj +59 -59
  95. data/spec/test_data/solutions/nspec/Basic.Specs/Class1.cs +12 -12
  96. data/spec/test_data/solutions/nspec/Basic.Specs/DebuggerShim.cs +42 -42
  97. data/spec/test_data/solutions/nspec/Basic.Specs/Properties/AssemblyInfo.cs +36 -36
  98. data/spec/test_data/solutions/nspec/Basic.Specs/packages.config +3 -3
  99. data/spec/test_data/solutions/nspec/Basic.sln +28 -28
  100. data/spec/test_data/solutions/nspec/Basic/Basic.csproj +55 -55
  101. data/spec/test_data/solutions/nspec/Basic/Class1.cs +12 -12
  102. data/spec/test_data/solutions/nspec/Basic/Properties/AssemblyInfo.cs +36 -36
  103. data/spec/test_data/solutions/nspec/Basic/packages.config +2 -2
  104. data/spec/test_data/solutions/nspec/Rakefile.rb +14 -14
  105. data/spec/test_data/tool_locator/Program Files/Microsoft SQL Server/110/Tools/Binn/SQLCMD.exe.txt b/data/spec/test_data/tool_locator/Program Files/Microsoft SQL → Server/110/Tools/Binn/SQLCMD.exe.txt +0 -0
  106. data/spec/test_data/tool_locator/Windows/Microsoft.NET/Framework/v3.5/MSBuild.exe.txt +0 -0
  107. data/spec/test_data/tool_locator/Windows/Microsoft.NET/Framework/v4.0.30319/MSBuild.exe.txt +0 -0
  108. data/spec/tool_locator_spec.rb +27 -29
  109. metadata +33 -16
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9472491d580ff36a2b64972a029ae7ab234170f0
4
- data.tar.gz: 758027de97668f14a95fb2a528f4a66a118b4416
3
+ metadata.gz: 4943bcb5b2ca436e794be144a7214e73a66bd3ff
4
+ data.tar.gz: 39c2a517a15ce2ec2a16be7afcfca8d93e105ad3
5
5
  SHA512:
6
- metadata.gz: 963c1641603597f126431a2c3a063b195328cfa44d1169b69e321c37e98de2dd8e86576cb0c30ffef18fcd6d6d887f19a91e512b3e0de1355963bd4eda14dba0
7
- data.tar.gz: 065593a702d669f69a58c6fcf39fc11875cf8c47d468c4fd26f0cabfd7f6aff4438e649079b4c71b3d18c55eeda81708f880230c0a92301cbc4764bb40c68c34
6
+ metadata.gz: 36d294b716b6ecfbf65d08c9d0b7f29c0de7b63ddfd10f324331571db5d89186e112fb8a46882c0622a48ceae10b0e7070d395ffc2bde0761bc6581fc23c6629
7
+ data.tar.gz: 54f5203d65fe75db699109f8035b4da80bf49500a45825692cf44db735dbd4fa34eb2661169409a50d40ac4fab40b14cc6cfcace0b0a3b82398e8a1d0c2296b0
@@ -1,301 +1,301 @@
1
- # Using Physique with Fluent Migrator
2
-
3
- Physique paired with FluentMigrator creates a great developer workflow for managing databases as part of your build.
4
-
5
- ## Prerequisites
6
-
7
- For the purposes of this guide we are making the following assumptions:
8
-
9
- * You have already configured your solution to use Physique.
10
-
11
- * You are using a SQL Server database.
12
-
13
- NOTE: Physique currently only works with SQL Server. Support for other databases is certainly possible, but not yet implemented.
14
-
15
- * You have SQL Client Tools installed and available on your `PATH`.
16
-
17
- Physique uses `SQLCMD.exe` to execute scripts on the command line.
18
-
19
- ## Creating a FluentMigrator Project
20
-
21
- Create a new database project and add the following files and folders:
22
-
23
- ├─ MyProject.Database
24
- │ ├─ _Scripts
25
- │ │ ├─ create.sql
26
- │ │ ├─ drop.sql
27
- │ │ └─ seed.sql
28
- │ └─ Migrations
29
- └─ MyProject.Database.csproj
30
-
31
- In the `create.sql` script add the following:
32
-
33
- ```sql
34
- -- Create the database
35
- CREATE DATABASE $(DATABASE_NAME)
36
- GO
37
- ```
38
-
39
- In the `drop.sql` script add the following:
40
-
41
- ```sql
42
- -- Drop the database
43
- if EXISTS(select name from master.dbo.sysdatabases where name = '$(DATABASE_NAME)') begin
44
- drop database $(DATABASE_NAME)
45
- end
46
- GO
47
- ```
48
-
49
- As you can see we are not specifying the database name in the scripts. Instead, we are using a variable that will be substituted by Physique when we run our tasks.
50
-
51
- ## Configuring your Rakefile to use FluentMigrator
52
-
53
- Open your Rakefile and add the following to your solution's config:
54
-
55
- ```ruby
56
- Physique::Solution.new do |s|
57
- #...
58
- s.fluently_migrate do |db|
59
- db.instance = ENV['DATABASE_SERVER'] || '(local)'
60
- db.name = ENV['DATABASE_NAME'] || 'MyDatabase'
61
- db.project = 'src/MyProject.Database/MyProject.Database.csproj'
62
- db.scripts_dir = '_Scripts'
63
- end
64
- end
65
- ```
66
-
67
- Here we are telling Physique:
68
-
69
- * We will be managing a database named 'MyDatabase' on the '(local)' instance.
70
-
71
- These settings can be overridden when the DATABASE_NAME and DATABASE_INSTANCE variables are present. This is particularly useful when incorporating Physique into your continuous integration or automated deployment process.
72
-
73
- * The path to the project file where are migrations will be implemented.
74
-
75
- * The folder within the migrations project that contains our workflow scripts.
76
-
77
- ## FluentMigrator Tasks
78
-
79
- Now we can run `bundle exec rake -T` to examine the tasks Physique has created for us. It should look something like this:
80
-
81
- ```
82
- rake compile # Builds the solution
83
- rake compile:build # Builds the solution using the Bu...
84
- rake compile:clean # Builds the solution using the Cl...
85
- rake compile:rebuild # Builds the solution using the Re...
86
- rake db:create # Create the database
87
- rake db:drop # Drop the database
88
- rake db:migrate # Migrate database to the latest v...
89
- rake db:new_migration[name,description] # Create a new migration file with...
90
- rake db:rebuild # Drop and recreate the database
91
- rake db:rollback # Rollback the database to the pre...
92
- rake db:seed # Seed the database with test data
93
- rake db:setup # Create the database and run all ...
94
- rake db:try # Migrate and then immediately rol...
95
- rake restore # Restores all nugets as per the p...
96
- rake test # Run unit tests
97
- ```
98
-
99
- Look at all of our shiny database tasks!
100
-
101
- In case it wasn't obvious, all of the FluentMigrator tasks are prefixed with `db`. Here's a breakdown of what each task does.
102
-
103
- ### Basic Tasks
104
-
105
- * `db:create` - Creates the database.
106
- * `db:drop` - Drops the database.
107
- * `db:seed` - Seeds the database with test data.
108
-
109
- As you might have guessed, these three tasks simply execute the SQL scripts in your project's `_Scripts` folder. You probably won't call these directly too often but they are required by the other tasks.
110
-
111
- If you don't create these files in your project, FluentMigrator will provide a default implementation for you. However, it's recommended that you create the scripts so that you have fine grained control over these steps.
112
-
113
- #### A Note About Seed Data
114
-
115
- The seed script is the place for you to place all of the test data you will need for local development. It's also a great way to provide a consistent set of test data that can be used during integration testing.
116
-
117
- ### Migration Tasks
118
-
119
- * `db:migrate` - Builds the migrations DLL and migrates the database to the latest version.
120
- * `db:rollback` - Builds the migrations DLL and rolls back the most recent migration.
121
- * `db:try` - A `db:migrate` followed by a `db:rollback`. With this task, you can test a migration that you are working on, then roll it back so that you can continue to add to it.
122
-
123
- One of the more annoying things I run into when developing migrations is forgetting to build my migrations project before attempting to migrate the database. Physique will make sure the migrations assembly is built before running it.
124
-
125
- ### Build Tasks
126
-
127
- * `db:setup` - This task creates the DB, migrates it to the latest version, and executes the seed script to populate it with test data.
128
- * `db:rebuild` - Drops the database, then executes a `db:setup`. This task is useful in your Continuous Integration process for testing the integrity of your migrations.
129
-
130
- ### New Migration Task
131
-
132
- * `db:new_migration[name,description]` - Creates a new migration class and adds it to the migrations project. We'll talk more about this in the following section.
133
-
134
- ## Developer Workflow
135
-
136
- ### Starting Fresh
137
-
138
- Let's say I'm a new developer and it's my first day on the job working on an existing project. The team I joined (in their infinite wisdom) is using Physique to manage their build process. The first thing I want to do is checkout the source, build it, build all of the databases locally and then run all of the unit tests to make sure my local copy is configured correctly.
139
-
140
- Assuming I have ruby installed on my machine, all I need to do is run the following:
141
-
142
- ```
143
- $ git checkout repo-url
144
- $ bundle install
145
- $ bundle exec rake test
146
- ```
147
-
148
- Thats it!
149
-
150
- "But I didn't run any of the database tasks," you might be asking. That's true. And therein lies the power behind Physique's build phases. When you configure a migrations project in Physique, the `db:rebuild` step is automatically registered as a dependency of the `test` phase of the build. So when you run your tests, all of your databases are built too.
151
-
152
- There will be times when you will want to delete and rebuild your local databases: for example, if there were data model changes since your last pull, or if you were experimenting with some db changes on one branch, and need to switch to a different branch to work on something new.
153
-
154
- To rebuild the databases:
155
-
156
- ```
157
- bundle exec rake db:rebuild
158
- ```
159
-
160
- ### Creating a New Migration
161
-
162
- To create a new migration inside your project, run the following:
163
-
164
- ```
165
- rake db:new_migration[migration_name,"migration description"]
166
- ```
167
-
168
- This will create a new FluentMigrator class and add it to the migrations project. It will automatically add a timestamp to the filename and `MigrationAttribute` to ensure that your migrations are run in sequential order and don’t conflict with anyone else on your team.
169
-
170
- NOTE: Because this task actually edits the project file, you should make sure that all of your changes are saved to disk before running it.
171
-
172
- As you can see this task takes a few parameters.
173
-
174
- * `migration_name` - This is the name of the Migration class you want to create.
175
- * `migration_description` - (Optional) A description that will be stored in the VersionInfo table after the migration is applied.
176
-
177
- As a hypothetical example, let’s say we wanted to create a new migration to add a new status column to our Order table. The command might look something like this.
178
-
179
- ```
180
- rake db:new_migration[AddStatusToOrder, "Add a new status column to the order table"]
181
- ```
182
-
183
- After running this task, the IDE will alert you that the project file has been modified. Select Reload and you should see the new migration in the Migrations folder. At this point, all that’s left to do is add your migration code to the `Up` method.
184
-
185
- ### Running Your Migrations
186
-
187
- After creating a new migration, you will want to apply it to your local database. Unsurprisingly, this is done by running:
188
-
189
- ```
190
- bundle exec rake db:migrate
191
- ```
192
-
193
- This will execute any migrations that have not yet been applied.
194
-
195
- If you want to rollback the last migration applied, the following command will do the trick.
196
-
197
- ```
198
- bundle exec rake db:rollback
199
- ```
200
-
201
- It’s common (at least for me) that while you are working on a migration, you might want to test it then continue working on it. Physique provides a convenience task that will run a migration, then immediately roll it back so that you can continue modifying it.
202
-
203
- ```
204
- bundle exec rake db:try
205
- ```
206
-
207
- ## Working With Multiple Databases
208
-
209
- Physique also supports solutions with multiple migrations projects. To configure multiple databases, simply add the migrations projects to your `Rakefile`. In this hypothetical example, I have configured two migrations projects for a client and server database.
210
-
211
- ```ruby
212
- Physique::Solution.new do |s|
213
- #...
214
- s.fluently_migrate do |db|
215
- db.task_alias = 'client'
216
- db.instance = ENV['DATABASE_SERVER'] || '(local)'
217
- db.name = ENV['DATABASE_NAME_CLIENT'] || 'MyClientDatabase'
218
- db.project = 'src/MyProject.Client.Database/MyProject.Client.Database.csproj'
219
- db.scripts_dir = '_Scripts'
220
- end
221
-
222
- s.fluently_migrate do |db|
223
- db.task_alias = 'server'
224
- db.instance = ENV['DATABASE_SERVER'] || '(local)'
225
- db.name = ENV['DATABASE_NAME_SERVER'] || 'MyServerDatabase'
226
- db.project = 'src/MyProject.Server.Database/MyProject.Server.Database.csproj'
227
- db.scripts_dir = '_Scripts'
228
- end
229
- end
230
- ```
231
-
232
- A couple of things to note here:
233
-
234
- * I have specified a task alias for each migrations project. This alias will be used when constructing your rake task names. If no alias is provided, the database name is used.
235
- * I have added some additional environment variables to override the target database server from my continuous integration or deployment builds.
236
-
237
- Running `bundle exec rake -T` will now yield:
238
-
239
- ```
240
- rake compile # Builds the solution
241
- rake compile:build # Builds the solution using...
242
- rake compile:clean # Builds the solution using...
243
- rake compile:rebuild # Builds the solution using...
244
- rake db:client:create # Create the database
245
- rake db:client:drop # Drop the database
246
- rake db:client:migrate # Migrate database to the l...
247
- rake db:client:new_migration[name,description] # Create a new migration fi...
248
- rake db:client:rebuild # Drop and recreate the dat...
249
- rake db:client:rollback # Rollback the database to ...
250
- rake db:client:seed # Seed the database with te...
251
- rake db:client:setup # Create the database and r...
252
- rake db:client:try # Migrate and then immediat...
253
- rake db:create # Create all databases
254
- rake db:drop # Drop all databases
255
- rake db:migrate # Migrates all databases to...
256
- rake db:rebuild # Drop and recreate all dat...
257
- rake db:seed # Seed all databases with t...
258
- rake db:server:create # Create the database
259
- rake db:server:drop # Drop the database
260
- rake db:server:migrate # Migrate database to the l...
261
- rake db:server:new_migration[name,description] # Create a new migration fi...
262
- rake db:server:rebuild # Drop and recreate the dat...
263
- rake db:server:rollback # Rollback the database to ...
264
- rake db:server:seed # Seed the database with te...
265
- rake db:server:setup # Create the database and r...
266
- rake db:server:try # Migrate and then immediat...
267
- rake db:setup # Build all databases and m...
268
- rake restore # Restores all nugets as pe...
269
- rake test # Run unit tests
270
- ```
271
-
272
- We now have tasks that operate on each database individually as well as tasks that operate on all databases at once.
273
-
274
- ### Database Specific Tasks
275
-
276
- The naming convention for db specific tasks is `db:{db_alias}:{task}`. In this example, if you wanted to rebuild just the client database, you would run the following:
277
-
278
- ```
279
- bundle exec rake db:client:rebuild
280
- ```
281
-
282
- Some tasks, like `rollback` and `new_migration` only make sense in the context of a specific database, so remember to add the alias to your task name when running them.
283
-
284
- ```
285
- bundle exec rake db:client:rollback
286
- ```
287
-
288
- ### Global Tasks
289
-
290
- The following tasks operate on all databases at once.
291
-
292
- * `db:create` - Create all databases.
293
- * `db:drop` - Drop all databases.
294
- * `db:seed` - Seed all databases with test data.
295
- * `db:migrate` - Migrate all databases to the latest version.
296
- * `db:setup` - Create all databases, migrate them to the latest version and populate them with seed data.
297
- * `db:rebuild` - Drop and setup all databases.
298
-
299
- ## Happy Migrating!
300
-
1
+ # Using Physique with Fluent Migrator
2
+
3
+ Physique paired with FluentMigrator creates a great developer workflow for managing databases as part of your build.
4
+
5
+ ## Prerequisites
6
+
7
+ For the purposes of this guide we are making the following assumptions:
8
+
9
+ * You have already configured your solution to use Physique.
10
+
11
+ * You are using a SQL Server database.
12
+
13
+ NOTE: Physique currently only works with SQL Server. Support for other databases is certainly possible, but not yet implemented.
14
+
15
+ * You have SQL Client Tools installed and available on your `PATH`.
16
+
17
+ Physique uses `SQLCMD.exe` to execute scripts on the command line.
18
+
19
+ ## Creating a FluentMigrator Project
20
+
21
+ Create a new database project and add the following files and folders:
22
+
23
+ ├─ MyProject.Database
24
+ │ ├─ _Scripts
25
+ │ │ ├─ create.sql
26
+ │ │ ├─ drop.sql
27
+ │ │ └─ seed.sql
28
+ │ └─ Migrations
29
+ └─ MyProject.Database.csproj
30
+
31
+ In the `create.sql` script add the following:
32
+
33
+ ```sql
34
+ -- Create the database
35
+ CREATE DATABASE $(DATABASE_NAME)
36
+ GO
37
+ ```
38
+
39
+ In the `drop.sql` script add the following:
40
+
41
+ ```sql
42
+ -- Drop the database
43
+ if EXISTS(select name from master.dbo.sysdatabases where name = '$(DATABASE_NAME)') begin
44
+ drop database $(DATABASE_NAME)
45
+ end
46
+ GO
47
+ ```
48
+
49
+ As you can see we are not specifying the database name in the scripts. Instead, we are using a variable that will be substituted by Physique when we run our tasks.
50
+
51
+ ## Configuring your Rakefile to use FluentMigrator
52
+
53
+ Open your Rakefile and add the following to your solution's config:
54
+
55
+ ```ruby
56
+ Physique::Solution.new do |s|
57
+ #...
58
+ s.fluently_migrate do |db|
59
+ db.instance = ENV['DATABASE_SERVER'] || '(local)'
60
+ db.name = ENV['DATABASE_NAME'] || 'MyDatabase'
61
+ db.project = 'src/MyProject.Database/MyProject.Database.csproj'
62
+ db.scripts_dir = '_Scripts'
63
+ end
64
+ end
65
+ ```
66
+
67
+ Here we are telling Physique:
68
+
69
+ * We will be managing a database named 'MyDatabase' on the '(local)' instance.
70
+
71
+ These settings can be overridden when the DATABASE_NAME and DATABASE_INSTANCE variables are present. This is particularly useful when incorporating Physique into your continuous integration or automated deployment process.
72
+
73
+ * The path to the project file where are migrations will be implemented.
74
+
75
+ * The folder within the migrations project that contains our workflow scripts.
76
+
77
+ ## FluentMigrator Tasks
78
+
79
+ Now we can run `bundle exec rake -T` to examine the tasks Physique has created for us. It should look something like this:
80
+
81
+ ```
82
+ rake compile # Builds the solution
83
+ rake compile:build # Builds the solution using the Bu...
84
+ rake compile:clean # Builds the solution using the Cl...
85
+ rake compile:rebuild # Builds the solution using the Re...
86
+ rake db:create # Create the database
87
+ rake db:drop # Drop the database
88
+ rake db:migrate # Migrate database to the latest v...
89
+ rake db:new_migration[name,description] # Create a new migration file with...
90
+ rake db:rebuild # Drop and recreate the database
91
+ rake db:rollback # Rollback the database to the pre...
92
+ rake db:seed # Seed the database with test data
93
+ rake db:setup # Create the database and run all ...
94
+ rake db:try # Migrate and then immediately rol...
95
+ rake restore # Restores all nugets as per the p...
96
+ rake test # Run unit tests
97
+ ```
98
+
99
+ Look at all of our shiny database tasks!
100
+
101
+ In case it wasn't obvious, all of the FluentMigrator tasks are prefixed with `db`. Here's a breakdown of what each task does.
102
+
103
+ ### Basic Tasks
104
+
105
+ * `db:create` - Creates the database.
106
+ * `db:drop` - Drops the database.
107
+ * `db:seed` - Seeds the database with test data.
108
+
109
+ As you might have guessed, these three tasks simply execute the SQL scripts in your project's `_Scripts` folder. You probably won't call these directly too often but they are required by the other tasks.
110
+
111
+ If you don't create these files in your project, FluentMigrator will provide a default implementation for you. However, it's recommended that you create the scripts so that you have fine grained control over these steps.
112
+
113
+ #### A Note About Seed Data
114
+
115
+ The seed script is the place for you to place all of the test data you will need for local development. It's also a great way to provide a consistent set of test data that can be used during integration testing.
116
+
117
+ ### Migration Tasks
118
+
119
+ * `db:migrate` - Builds the migrations DLL and migrates the database to the latest version.
120
+ * `db:rollback` - Builds the migrations DLL and rolls back the most recent migration.
121
+ * `db:try` - A `db:migrate` followed by a `db:rollback`. With this task, you can test a migration that you are working on, then roll it back so that you can continue to add to it.
122
+
123
+ One of the more annoying things I run into when developing migrations is forgetting to build my migrations project before attempting to migrate the database. Physique will make sure the migrations assembly is built before running it.
124
+
125
+ ### Build Tasks
126
+
127
+ * `db:setup` - This task creates the DB, migrates it to the latest version, and executes the seed script to populate it with test data.
128
+ * `db:rebuild` - Drops the database, then executes a `db:setup`. This task is useful in your Continuous Integration process for testing the integrity of your migrations.
129
+
130
+ ### New Migration Task
131
+
132
+ * `db:new_migration[name,description]` - Creates a new migration class and adds it to the migrations project. We'll talk more about this in the following section.
133
+
134
+ ## Developer Workflow
135
+
136
+ ### Starting Fresh
137
+
138
+ Let's say I'm a new developer and it's my first day on the job working on an existing project. The team I joined (in their infinite wisdom) is using Physique to manage their build process. The first thing I want to do is checkout the source, build it, build all of the databases locally and then run all of the unit tests to make sure my local copy is configured correctly.
139
+
140
+ Assuming I have ruby installed on my machine, all I need to do is run the following:
141
+
142
+ ```
143
+ $ git checkout repo-url
144
+ $ bundle install
145
+ $ bundle exec rake test
146
+ ```
147
+
148
+ Thats it!
149
+
150
+ "But I didn't run any of the database tasks," you might be asking. That's true. And therein lies the power behind Physique's build phases. When you configure a migrations project in Physique, the `db:rebuild` step is automatically registered as a dependency of the `test` phase of the build. So when you run your tests, all of your databases are built too.
151
+
152
+ There will be times when you will want to delete and rebuild your local databases: for example, if there were data model changes since your last pull, or if you were experimenting with some db changes on one branch, and need to switch to a different branch to work on something new.
153
+
154
+ To rebuild the databases:
155
+
156
+ ```
157
+ bundle exec rake db:rebuild
158
+ ```
159
+
160
+ ### Creating a New Migration
161
+
162
+ To create a new migration inside your project, run the following:
163
+
164
+ ```
165
+ rake db:new_migration[migration_name,"migration description"]
166
+ ```
167
+
168
+ This will create a new FluentMigrator class and add it to the migrations project. It will automatically add a timestamp to the filename and `MigrationAttribute` to ensure that your migrations are run in sequential order and don’t conflict with anyone else on your team.
169
+
170
+ NOTE: Because this task actually edits the project file, you should make sure that all of your changes are saved to disk before running it.
171
+
172
+ As you can see this task takes a few parameters.
173
+
174
+ * `migration_name` - This is the name of the Migration class you want to create.
175
+ * `migration_description` - (Optional) A description that will be stored in the VersionInfo table after the migration is applied.
176
+
177
+ As a hypothetical example, let’s say we wanted to create a new migration to add a new status column to our Order table. The command might look something like this.
178
+
179
+ ```
180
+ rake db:new_migration[AddStatusToOrder, "Add a new status column to the order table"]
181
+ ```
182
+
183
+ After running this task, the IDE will alert you that the project file has been modified. Select Reload and you should see the new migration in the Migrations folder. At this point, all that’s left to do is add your migration code to the `Up` method.
184
+
185
+ ### Running Your Migrations
186
+
187
+ After creating a new migration, you will want to apply it to your local database. Unsurprisingly, this is done by running:
188
+
189
+ ```
190
+ bundle exec rake db:migrate
191
+ ```
192
+
193
+ This will execute any migrations that have not yet been applied.
194
+
195
+ If you want to rollback the last migration applied, the following command will do the trick.
196
+
197
+ ```
198
+ bundle exec rake db:rollback
199
+ ```
200
+
201
+ It’s common (at least for me) that while you are working on a migration, you might want to test it then continue working on it. Physique provides a convenience task that will run a migration, then immediately roll it back so that you can continue modifying it.
202
+
203
+ ```
204
+ bundle exec rake db:try
205
+ ```
206
+
207
+ ## Working With Multiple Databases
208
+
209
+ Physique also supports solutions with multiple migrations projects. To configure multiple databases, simply add the migrations projects to your `Rakefile`. In this hypothetical example, I have configured two migrations projects for a client and server database.
210
+
211
+ ```ruby
212
+ Physique::Solution.new do |s|
213
+ #...
214
+ s.fluently_migrate do |db|
215
+ db.task_alias = 'client'
216
+ db.instance = ENV['DATABASE_SERVER'] || '(local)'
217
+ db.name = ENV['DATABASE_NAME_CLIENT'] || 'MyClientDatabase'
218
+ db.project = 'src/MyProject.Client.Database/MyProject.Client.Database.csproj'
219
+ db.scripts_dir = '_Scripts'
220
+ end
221
+
222
+ s.fluently_migrate do |db|
223
+ db.task_alias = 'server'
224
+ db.instance = ENV['DATABASE_SERVER'] || '(local)'
225
+ db.name = ENV['DATABASE_NAME_SERVER'] || 'MyServerDatabase'
226
+ db.project = 'src/MyProject.Server.Database/MyProject.Server.Database.csproj'
227
+ db.scripts_dir = '_Scripts'
228
+ end
229
+ end
230
+ ```
231
+
232
+ A couple of things to note here:
233
+
234
+ * I have specified a task alias for each migrations project. This alias will be used when constructing your rake task names. If no alias is provided, the database name is used.
235
+ * I have added some additional environment variables to override the target database server from my continuous integration or deployment builds.
236
+
237
+ Running `bundle exec rake -T` will now yield:
238
+
239
+ ```
240
+ rake compile # Builds the solution
241
+ rake compile:build # Builds the solution using...
242
+ rake compile:clean # Builds the solution using...
243
+ rake compile:rebuild # Builds the solution using...
244
+ rake db:client:create # Create the database
245
+ rake db:client:drop # Drop the database
246
+ rake db:client:migrate # Migrate database to the l...
247
+ rake db:client:new_migration[name,description] # Create a new migration fi...
248
+ rake db:client:rebuild # Drop and recreate the dat...
249
+ rake db:client:rollback # Rollback the database to ...
250
+ rake db:client:seed # Seed the database with te...
251
+ rake db:client:setup # Create the database and r...
252
+ rake db:client:try # Migrate and then immediat...
253
+ rake db:create # Create all databases
254
+ rake db:drop # Drop all databases
255
+ rake db:migrate # Migrates all databases to...
256
+ rake db:rebuild # Drop and recreate all dat...
257
+ rake db:seed # Seed all databases with t...
258
+ rake db:server:create # Create the database
259
+ rake db:server:drop # Drop the database
260
+ rake db:server:migrate # Migrate database to the l...
261
+ rake db:server:new_migration[name,description] # Create a new migration fi...
262
+ rake db:server:rebuild # Drop and recreate the dat...
263
+ rake db:server:rollback # Rollback the database to ...
264
+ rake db:server:seed # Seed the database with te...
265
+ rake db:server:setup # Create the database and r...
266
+ rake db:server:try # Migrate and then immediat...
267
+ rake db:setup # Build all databases and m...
268
+ rake restore # Restores all nugets as pe...
269
+ rake test # Run unit tests
270
+ ```
271
+
272
+ We now have tasks that operate on each database individually as well as tasks that operate on all databases at once.
273
+
274
+ ### Database Specific Tasks
275
+
276
+ The naming convention for db specific tasks is `db:{db_alias}:{task}`. In this example, if you wanted to rebuild just the client database, you would run the following:
277
+
278
+ ```
279
+ bundle exec rake db:client:rebuild
280
+ ```
281
+
282
+ Some tasks, like `rollback` and `new_migration` only make sense in the context of a specific database, so remember to add the alias to your task name when running them.
283
+
284
+ ```
285
+ bundle exec rake db:client:rollback
286
+ ```
287
+
288
+ ### Global Tasks
289
+
290
+ The following tasks operate on all databases at once.
291
+
292
+ * `db:create` - Create all databases.
293
+ * `db:drop` - Drop all databases.
294
+ * `db:seed` - Seed all databases with test data.
295
+ * `db:migrate` - Migrate all databases to the latest version.
296
+ * `db:setup` - Create all databases, migrate them to the latest version and populate them with seed data.
297
+ * `db:rebuild` - Drop and setup all databases.
298
+
299
+ ## Happy Migrating!
300
+
301
301
  At this point you should be a Physique migration pro! Feel free to contact me [@scardetto](https://twitter.com/scardetto) if you have any questions.