Refactoring
This commit is contained in:
@@ -1,41 +1,52 @@
|
||||
import Foundation
|
||||
import DataLiteCore
|
||||
|
||||
/// Represents a database migration step associated with a specific version.
|
||||
/// A database migration step for a specific schema version.
|
||||
///
|
||||
/// Each `Migration` contains a reference to a migration script file (usually a `.sql` file) and the
|
||||
/// version to which this script corresponds. The script is expected to be bundled with the application.
|
||||
/// ## Overview
|
||||
///
|
||||
/// You can initialize a migration directly with a URL to the script, or load it from a resource
|
||||
/// embedded in a bundle.
|
||||
/// Each migration links a version identifier with a script file that modifies the database schema.
|
||||
/// Scripts are typically bundled with the application and executed sequentially during version
|
||||
/// upgrades.
|
||||
///
|
||||
/// ## Topics
|
||||
///
|
||||
/// ### Properties
|
||||
/// - ``version``
|
||||
/// - ``scriptURL``
|
||||
/// - ``script``
|
||||
///
|
||||
/// ### Initializers
|
||||
/// - ``init(version:scriptURL:)``
|
||||
/// - ``init(version:byResource:extension:in:)``
|
||||
public struct Migration<Version: VersionRepresentable>: Hashable, Sendable {
|
||||
// MARK: - Properties
|
||||
|
||||
/// The version associated with this migration step.
|
||||
public let version: Version
|
||||
|
||||
/// The URL pointing to the migration script (e.g., an SQL file).
|
||||
/// The file URL of the migration script (for example, an SQL file).
|
||||
public let scriptURL: URL
|
||||
|
||||
/// The SQL script associated with this migration.
|
||||
/// The migration script as a string.
|
||||
///
|
||||
/// This computed property reads the contents of the file at `scriptURL` and returns it as a
|
||||
/// `SQLScript` instance. Use this to access and execute the migration's SQL commands.
|
||||
/// Reads the contents of the file at ``scriptURL`` and trims surrounding whitespace and
|
||||
/// newlines.
|
||||
///
|
||||
/// - Throws: An error if the script file cannot be read or is invalid.
|
||||
public var script: SQLScript {
|
||||
/// - Throws: An error if the script file cannot be read.
|
||||
public var script: String {
|
||||
get throws {
|
||||
try SQLScript(contentsOf: scriptURL)
|
||||
try String(contentsOf: scriptURL)
|
||||
.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Inits
|
||||
|
||||
/// Creates a migration with a specified version and script URL.
|
||||
/// Creates a migration with the specified version and script URL.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - version: The version this migration corresponds to.
|
||||
/// - scriptURL: The file URL to the migration script.
|
||||
/// - scriptURL: The URL of the script file to execute.
|
||||
public init(version: Version, scriptURL: URL) {
|
||||
self.version = version
|
||||
self.scriptURL = scriptURL
|
||||
@@ -43,33 +54,25 @@ public struct Migration<Version: VersionRepresentable>: Hashable, Sendable {
|
||||
|
||||
/// Creates a migration by locating a script resource in the specified bundle.
|
||||
///
|
||||
/// This initializer attempts to locate a script file in the provided bundle using the specified
|
||||
/// resource `name` and optional `extension`. The `name` parameter may include or omit the file extension.
|
||||
///
|
||||
/// - If `name` includes an extension (e.g., `"001_init.sql"`), pass `extension` as `nil` or an empty string.
|
||||
/// - If `name` omits the extension (e.g., `"001_init"`), specify the extension separately
|
||||
/// (e.g., `"sql"`), or leave it `nil` if the file has no extension.
|
||||
///
|
||||
/// - Important: Passing a name that already includes the extension while also specifying a non-`nil`
|
||||
/// `extension` may result in failure to locate the file.
|
||||
/// Searches the given bundle for a script resource matching the provided name and optional file
|
||||
/// extension.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - version: The version this migration corresponds to.
|
||||
/// - name: The resource name of the script file. May include or omit the file extension.
|
||||
/// - extension: The file extension, if separated from the name. Defaults to `nil`.
|
||||
/// - bundle: The bundle in which to search for the resource. Defaults to `.main`.
|
||||
/// - name: The resource name of the script file. Can include or omit its extension.
|
||||
/// - extension: The file extension, if separate from the name. Defaults to `nil`.
|
||||
/// - bundle: The bundle in which to look for the resource. Defaults to `.main`.
|
||||
///
|
||||
/// - Returns: A `Migration` if the resource file is found; otherwise, `nil`.
|
||||
/// - Returns: A `Migration` instance if the resource is found; otherwise, `nil`.
|
||||
public init?(
|
||||
version: Version,
|
||||
byResource name: String,
|
||||
extension: String? = nil,
|
||||
in bundle: Bundle = .main
|
||||
) {
|
||||
guard let url = bundle.url(
|
||||
forResource: name,
|
||||
withExtension: `extension`
|
||||
) else { return nil }
|
||||
guard let url = bundle.url(forResource: name, withExtension: `extension`) else {
|
||||
return nil
|
||||
}
|
||||
self.init(version: version, scriptURL: url)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user