@@ -3,12 +3,16 @@ about_PSql_Deploy_Migrations
33
44SHORT DESCRIPTION
55
6- A migration is a SQL script that evolves a target database's schema from its
7- current version to the next version.
6+ A PSql.Deploy migration is a T-SQL script that evolves a target SQL Server or
7+ Azure SQL database's schema from its current version to the next version. The
8+ Invoke-SqlMigrations cmdlet applies outstanding migrations to one or more
9+ target databases.
810
911
1012LONG DESCRIPTION
1113
14+ :: Source Directory Structure ::
15+
1216PSql.Deploy expects migrations to have a specific filesystem layout.
1317
1418src\ The source directory: a set of migrations and
@@ -32,10 +36,63 @@ src\ The source directory: a set of migrations and
3236 └─ ... PSql.Deploy does not care about other files or
3337 directories present in the source directory.
3438
39+ Given an arbitrary source directory, PSql.Deploy expects to find migrations in
40+ a Migrations subdirectory. Within the Migrations directory, each subdirectory
41+ containing a _Main.sql file is an individual migration. The name of the
42+ subdirectory determines the name of the migration. A migration name must be a
43+ valid directory name but is otherwise unrestricted.
44+
45+ The _Main.sql file is the entry point for the migration. The file is a T-SQL
46+ script with a few extensions to support modularity.
47+
48+
49+ :: Migration Script SQLCMD Support ::
50+
51+ PSql.Deploy migration scripts support a limited set of SQLCMD directives.
52+ Migrations may use these directives to organize code into multiple files, to
53+ separate SQL batches, and to replace hard-coded values with variables.
54+
55+ GO
56+ Ends the current SQL batch and begins a new one.
57+
58+ $(<name>)
59+ Replaced by the value of the SQLCMD variable <name>.
60+
61+ :r <file>
62+ Replaced by the contents of the specified file. The path is relative
63+ to the current PowerShell directory ($PWD or Get-Location), matching
64+ SQLCMD.EXE behavior.
65+
66+ To enable inclusion relative to the migration, PSql.Deploy predefines
67+ the SQLCMD variable 'Path' as the full path of the directory containing
68+ the _Main.sql file. Thus the directive
69+
70+ :r $(Path)\Foo.sql
71+
72+ includes the file Foo.sql from the same directory as _Main.sql.
73+
74+ The path may be enclosed within double quotes. This is mandatory if
75+ the path contains spaces, tabs, or double quotes. To include a double
76+ quote in the path, use two double quotes. Examples:
77+
78+ :r "$(Path)\My Script.sql"
79+ :r "$(Path)/My ""Special"" Script.sql"
3580
36- Each subdirectory of {source-directory}\Migrations containing a _Main.sql file
37- is a migration. The name of the subdirectory is the name of the migration.
38- The _Main.sql file is the entry point for the migration.
81+ Note that Windows does not support tabs or double quotes in file names.
82+
83+ :setvar <name> <value>
84+ Sets the SQLCMD variable <name> to the spacified <value>.
85+
86+ The value may be enclosed within double quotes. This is mandatory if
87+ the value contains spaces, tabs, or double quotes. To include a double
88+ quote in the value, use two double quotes.
89+
90+ :setvar MyVar MyValue
91+ :setvar MyVar "My Value"
92+ :setvar MyVar "My ""Special"" Value"
93+
94+
95+ :: How Migrations Work ::
3996
4097PSql.Deploy's Invoke-SqlMigrations cmdlet applies (runs) each migration exactly
4198once per target database. After applying a migration, the cmdlet records the
@@ -86,7 +143,8 @@ to a prior state, then author a new migration that makes the necessary changes,
86143or restore the database from a backup. Use automated testing to find migration
87144problems early, before they reach production.
88145
89- Phases
146+
147+ :: Phases ::
90148
91149To facilitate zero- and low-downtime deployments, PSql.Deploy splits deployment
92150into three logical phases: Pre, Core, and Post. Each phase represents a
116174 phase should finalize or clean up after the changes made in earlier phases
117175 while retaining compatibility with the deployed workloads.
118176
119- Dependencies
177+
178+ :: Dependencies ::
120179
121180PSql.Deploy enables one migration to declare a dependency on another migration.
122181The Invoke-SqlMigrations cmdlet handles a dependency in one of two ways. If
@@ -129,7 +188,8 @@ dependency and preserve the guarantees that the cmdlet makes about the order of
129188migration statements. Note that this makes dependency resolution incompatible
130189with zero-downtime deployment scenarios, because Core entails downtime.
131190
132- Ordering Guarantees
191+
192+ :: Ordering Guarantees ::
133193
134194The Invoke-SqlMigrations cmdlet makes the following guarantees about the order
135195of migration statement execution:
@@ -184,7 +244,8 @@ Yield this order operations:
1842445 │ │ ^^^ Pre Core │ Post │
185245 Time──> ^^^
186246
187- Magic Comments
247+
248+ :: Magic Comments ::
188249
189250A migration may contain 'magic comments', which are special comments that the
190251Invoke-SqlMigrations cmdlet interprets as directives. The cmdlet supports the
@@ -202,7 +263,8 @@ following magic comments:
202263--# REQUIRES: <migration-name> ...
203264 Declares a dependency on one or more migrations.
204265
205- Example Migration Naming Schemes
266+
267+ :: Example Migration Naming Schemes ::
206268
207269Here are some example migration naming schemes:
208270
@@ -232,21 +294,31 @@ Here are some example migration naming schemes:
232294 Best for: teams with multiple migration authors where it is useful to
233295 associate migrations with work items, pull requests, etc.
234296
235- Squashes
297+
298+ :: Squashes ::
236299
237300A long-lived project with frequent schema changes will accumulate a large
238301number of migrations over time. For such projects, it is sometimes useful to
239302delete old migrations and instead use a create script that creates a database
240- already migrated to that poitn in time. This is known as a 'squash'. Create
241- scripts are outside the scope of PSql.Deploy, but PSql.Deploy supports squashes
303+ already migrated to that point in time. This is known as a 'squash'. Create
304+ scripts are beyond the scope of PSql.Deploy, but PSql.Deploy supports squashes
242305by not caring about completed migrations that are no longer present in the
243306source directory. Nevertheless, there are two rules that must be oserved when
244307performing a squash:
245308
2463091. The oldest unsquashed migration must be same age or older than the oldest
247310 backup that is likely to be restored.
248311
249- 1 . The oldest unsquashed migration must be same age or older than the create
312+ 2 . The oldest unsquashed migration must be same age or older than the create
250313 script.
251314
252- TODO: Describe this better when more brain cells are available.
315+ Failure to observe these rules can cause restore-and-migrate or
316+ create-and-migrate operations to miss migrations, causing later migrations to
317+ fail or leaving the schema in an unexpected state.
318+
319+ :: See Also ::
320+
321+ - about_PSql_Deploy
322+ - Get-SqlMigrations
323+ - Invoke-SqlMigrations
324+ - https://github.com/sharpjs/PSql.Deploy
0 commit comments