using Khaemhat.Data.Schema;
namespace Khaemhat.Data.Migrations;
///
/// A schema migration (SPEC §5.3). Describe the change in neutral form in ; add dialect-specific SQL in
/// only for what the neutral form cannot express. Once released, a migration never changes: write a
/// new one. Create the file with scripts/new-migration.sh.
///
/// [Migration(20261111_002, "create_sys_settings", Target = MigrationTarget.System)]
/// public sealed class M20261111_002_CreateSysSettings : IMigration
/// {
/// public void Up(ISchemaBuilder schema) => schema.CreateTable("sys_settings", t => { … });
/// }
///
///
public interface IMigration
{
/// Describes the schema change.
/// The schema builder.
void Up(ISchemaBuilder schema);
/// Dialect-specific SQL run after , or .
/// The dialect of the database being migrated.
/// Trusted SQL (several statements separated by semicolons are allowed), or .
string? UpSql(ISqlDialect dialect) => null;
}
/// Which catalog a migration belongs to.
public enum MigrationTarget
{
/// Every company database (khaemhat_<code>).
Company = 0,
/// The system database (khaemhat_system).
System = 1,
}
/// Marks an with its version and name.
/// The version yyyymmdd_nnn written as a number, for example 20261111_001.
/// The name in snake_case, for example create_sys_companies.
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class MigrationAttribute(long version, string name) : Attribute
{
/// The version yyyymmddnnn.
public long Version { get; } = version;
/// The name.
public string Name { get; } = name;
/// The catalog (default ).
public MigrationTarget Target { get; init; } = MigrationTarget.Company;
/// The module that owns the migration, as stored in schema_version.module (default core).
public string Module { get; init; } = "core";
}