4.2 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |
|---|---|---|---|---|---|
|
2026-04-16T04:34:42.810625+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | ab2d5b23dd090ece |
DatabaseMigrationScripts
Purpose
This module provides utilities for reading embedded resources (e.g., SQL migration scripts) from a .NET assembly’s manifest resources. It exists to simplify access to versioned, compiled-in migration files without requiring external file system dependencies, enabling reliable deployment of database schema changes across environments.
Public Interface
-
static StreamReader GetStream(Assembly assembly, string name)
Searches the manifest resources of the givenassemblyfor a resource whose name ends withname(case-sensitive substring match), and returns aStreamReaderfor the first match. Returnsnullif no matching resource is found.
Note: Caller is responsible for disposing theStreamReader. -
static string GetString(Assembly assembly, string name)
Reads the full content of the resource matchingname(viaGetStream) from the specifiedassemblyand returns it as astring. Closes theStreamReaderinternally. Returnsnullif no matching resource is found. -
static string GetString(string name)
Overload ofGetStringthat uses the assembly containing theEmbeddedResourcetype itself (typeof(EmbeddedResource).Assembly) as the source. Behaves identically toGetString(assembly, name)otherwise.
Invariants
- Resource matching is performed via
EndsWith(name)on the full manifest resource name (e.g.,"MyApp.Migrations.V1_0_0.sql"matches forname = "V1_0_0.sql"). - Only the first matching resource (by enumeration order) is used—no error is raised if multiple resources match.
GetString(Assembly, string)andGetString(string)returnnullif no matching resource exists (not an exception).- The
StreamReaderreturned byGetStreammust be explicitly closed/disposed by the caller to avoid resource leaks. GetStringmethods assume the resource content is text-encoded (UTF-8 or system default encoding, perStreamReaderdefault behavior).
Dependencies
- Depends on:
System.IO(StreamReader,Stream)System.Reflection.Assembly(forGetManifestResourceNames,GetManifestResourceStream)
- Depended on by:
- Presumably database migration orchestration logic (e.g., a
MigrationRunnerclass) that loads.sqlfiles from embedded resources. - Inferred: Other types in the
DatabaseMigrationScriptsnamespace (not visible here) likely consumeEmbeddedResourceto retrieve migration scripts.
- Presumably database migration orchestration logic (e.g., a
Gotchas
- Substring matching is ambiguous: A request for
"V1.sql"could match"V1.sql","Old/V1.sql", or"Backup_V1.sql"—only the first match inGetManifestResourceNames()order is used. This may cause unexpected behavior if resource naming is not strictly controlled. - No encoding specification:
StreamReaderuses default encoding (typically UTF-8 without BOM on .NET Core/.NET 5+, but system default on .NET Framework), which may cause issues if migration files contain non-ASCII characters and were authored with a different encoding. - Resource enumeration order is undefined:
GetManifestResourceNames()does not guarantee consistent ordering across builds or platforms, so multiple matching resources may yield non-deterministic results. - No validation of resource existence before
ReadToEnd(): IfGetStreamreturnsnull,GetStringwill throw aNullReferenceExceptionwhen callingsr.ReadToEnd(). This is a critical bug:GetString(Assembly, string)lacks null-checking onsr. EmbeddedResource()constructor is private but unused: The class is never instantiated (all members are static), but the empty constructor serves no functional purpose.- No support for binary resources: Designed exclusively for text-based resources; binary files would require a different approach.
Note: The NullReferenceException risk in GetString(Assembly, string) when GetStream returns null is a likely source of runtime failures and should be addressed.