Added migration tool
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
using NpgsqlTypes;
|
||||
|
||||
namespace HaDbMigration.Models;
|
||||
|
||||
public sealed record ColumnDescriptor(
|
||||
string SqliteName,
|
||||
string PostgresName,
|
||||
NpgsqlDbType NpgsqlType,
|
||||
bool IsPrimaryKey = false,
|
||||
bool IsBooleanConversion = false);
|
||||
|
||||
public sealed record TableDescriptor(
|
||||
string TableName,
|
||||
IReadOnlyList<ColumnDescriptor> Columns,
|
||||
string? SequenceColumn = null);
|
||||
|
||||
public static class TableDescriptors
|
||||
{
|
||||
public static readonly TableDescriptor Events = new(
|
||||
"events",
|
||||
[
|
||||
new("event_id", "event_id", NpgsqlDbType.Bigint, IsPrimaryKey: true),
|
||||
new("event_type", "event_type", NpgsqlDbType.Char),
|
||||
new("event_data", "event_data", NpgsqlDbType.Char),
|
||||
new("origin", "origin", NpgsqlDbType.Char),
|
||||
new("origin_idx", "origin_idx", NpgsqlDbType.Smallint),
|
||||
new("time_fired", "time_fired", NpgsqlDbType.TimestampTz),
|
||||
new("time_fired_ts", "time_fired_ts", NpgsqlDbType.Double),
|
||||
new("context_id", "context_id", NpgsqlDbType.Char),
|
||||
new("context_user_id", "context_user_id", NpgsqlDbType.Char),
|
||||
new("context_parent_id", "context_parent_id", NpgsqlDbType.Char),
|
||||
new("data_id", "data_id", NpgsqlDbType.Bigint),
|
||||
new("context_id_bin", "context_id_bin", NpgsqlDbType.Bytea),
|
||||
new("context_user_id_bin", "context_user_id_bin", NpgsqlDbType.Bytea),
|
||||
new("context_parent_id_bin", "context_parent_id_bin", NpgsqlDbType.Bytea),
|
||||
new("event_type_id", "event_type_id", NpgsqlDbType.Bigint),
|
||||
],
|
||||
SequenceColumn: "event_id");
|
||||
|
||||
public static readonly TableDescriptor EventData = new(
|
||||
"event_data",
|
||||
[
|
||||
new("data_id", "data_id", NpgsqlDbType.Bigint, IsPrimaryKey: true),
|
||||
new("hash", "hash", NpgsqlDbType.Bigint),
|
||||
new("shared_data", "shared_data", NpgsqlDbType.Text),
|
||||
],
|
||||
SequenceColumn: "data_id");
|
||||
|
||||
public static readonly TableDescriptor EventTypes = new(
|
||||
"event_types",
|
||||
[
|
||||
new("event_type_id", "event_type_id", NpgsqlDbType.Bigint, IsPrimaryKey: true),
|
||||
new("event_type", "event_type", NpgsqlDbType.Varchar),
|
||||
],
|
||||
SequenceColumn: "event_type_id");
|
||||
|
||||
public static readonly TableDescriptor States = new(
|
||||
"states",
|
||||
[
|
||||
new("state_id", "state_id", NpgsqlDbType.Bigint, IsPrimaryKey: true),
|
||||
new("entity_id", "entity_id", NpgsqlDbType.Char),
|
||||
new("state", "state", NpgsqlDbType.Varchar),
|
||||
new("attributes", "attributes", NpgsqlDbType.Char),
|
||||
new("event_id", "event_id", NpgsqlDbType.Smallint),
|
||||
new("last_changed", "last_changed", NpgsqlDbType.TimestampTz),
|
||||
new("last_changed_ts", "last_changed_ts", NpgsqlDbType.Double),
|
||||
new("last_reported_ts", "last_reported_ts", NpgsqlDbType.Double),
|
||||
new("last_updated", "last_updated", NpgsqlDbType.TimestampTz),
|
||||
new("last_updated_ts", "last_updated_ts", NpgsqlDbType.Double),
|
||||
new("old_state_id", "old_state_id", NpgsqlDbType.Bigint),
|
||||
new("attributes_id", "attributes_id", NpgsqlDbType.Bigint),
|
||||
new("context_id", "context_id", NpgsqlDbType.Char),
|
||||
new("context_user_id", "context_user_id", NpgsqlDbType.Char),
|
||||
new("context_parent_id", "context_parent_id", NpgsqlDbType.Char),
|
||||
new("origin_idx", "origin_idx", NpgsqlDbType.Smallint),
|
||||
new("context_id_bin", "context_id_bin", NpgsqlDbType.Bytea),
|
||||
new("context_user_id_bin", "context_user_id_bin", NpgsqlDbType.Bytea),
|
||||
new("context_parent_id_bin", "context_parent_id_bin", NpgsqlDbType.Bytea),
|
||||
new("metadata_id", "metadata_id", NpgsqlDbType.Bigint),
|
||||
],
|
||||
SequenceColumn: "state_id");
|
||||
|
||||
public static readonly TableDescriptor StateAttributes = new(
|
||||
"state_attributes",
|
||||
[
|
||||
new("attributes_id", "attributes_id", NpgsqlDbType.Bigint, IsPrimaryKey: true),
|
||||
new("hash", "hash", NpgsqlDbType.Bigint),
|
||||
new("shared_attrs", "shared_attrs", NpgsqlDbType.Text),
|
||||
],
|
||||
SequenceColumn: "attributes_id");
|
||||
|
||||
public static readonly TableDescriptor StatesMeta = new(
|
||||
"states_meta",
|
||||
[
|
||||
new("metadata_id", "metadata_id", NpgsqlDbType.Bigint, IsPrimaryKey: true),
|
||||
new("entity_id", "entity_id", NpgsqlDbType.Varchar),
|
||||
],
|
||||
SequenceColumn: "metadata_id");
|
||||
|
||||
public static readonly TableDescriptor RecorderRuns = new(
|
||||
"recorder_runs",
|
||||
[
|
||||
new("run_id", "run_id", NpgsqlDbType.Bigint, IsPrimaryKey: true),
|
||||
new("start", "start", NpgsqlDbType.TimestampTz),
|
||||
new("end", "end", NpgsqlDbType.TimestampTz),
|
||||
new("closed_incorrect", "closed_incorrect", NpgsqlDbType.Boolean, IsBooleanConversion: true),
|
||||
new("created", "created", NpgsqlDbType.TimestampTz),
|
||||
],
|
||||
SequenceColumn: "run_id");
|
||||
|
||||
public static readonly TableDescriptor SchemaChanges = new(
|
||||
"schema_changes",
|
||||
[
|
||||
new("change_id", "change_id", NpgsqlDbType.Bigint, IsPrimaryKey: true),
|
||||
new("schema_version", "schema_version", NpgsqlDbType.Integer),
|
||||
new("changed", "changed", NpgsqlDbType.TimestampTz),
|
||||
],
|
||||
SequenceColumn: "change_id");
|
||||
|
||||
public static readonly TableDescriptor MigrationChanges = new(
|
||||
"migration_changes",
|
||||
[
|
||||
new("migration_id", "migration_id", NpgsqlDbType.Varchar),
|
||||
new("version", "version", NpgsqlDbType.Smallint),
|
||||
]);
|
||||
|
||||
public static readonly TableDescriptor Statistics = new(
|
||||
"statistics",
|
||||
[
|
||||
new("id", "id", NpgsqlDbType.Bigint, IsPrimaryKey: true),
|
||||
new("created", "created", NpgsqlDbType.TimestampTz),
|
||||
new("created_ts", "created_ts", NpgsqlDbType.Double),
|
||||
new("metadata_id", "metadata_id", NpgsqlDbType.Bigint),
|
||||
new("start", "start", NpgsqlDbType.TimestampTz),
|
||||
new("start_ts", "start_ts", NpgsqlDbType.Double),
|
||||
new("mean", "mean", NpgsqlDbType.Double),
|
||||
new("mean_weight", "mean_weight", NpgsqlDbType.Double),
|
||||
new("min", "min", NpgsqlDbType.Double),
|
||||
new("max", "max", NpgsqlDbType.Double),
|
||||
new("last_reset", "last_reset", NpgsqlDbType.TimestampTz),
|
||||
new("last_reset_ts", "last_reset_ts", NpgsqlDbType.Double),
|
||||
new("state", "state", NpgsqlDbType.Double),
|
||||
new("sum", "sum", NpgsqlDbType.Double),
|
||||
],
|
||||
SequenceColumn: "id");
|
||||
|
||||
public static readonly TableDescriptor StatisticsMeta = new(
|
||||
"statistics_meta",
|
||||
[
|
||||
new("id", "id", NpgsqlDbType.Bigint, IsPrimaryKey: true),
|
||||
new("statistic_id", "statistic_id", NpgsqlDbType.Varchar),
|
||||
new("source", "source", NpgsqlDbType.Varchar),
|
||||
new("unit_of_measurement", "unit_of_measurement", NpgsqlDbType.Varchar),
|
||||
new("unit_class", "unit_class", NpgsqlDbType.Varchar),
|
||||
new("has_mean", "has_mean", NpgsqlDbType.Boolean, IsBooleanConversion: true),
|
||||
new("has_sum", "has_sum", NpgsqlDbType.Boolean, IsBooleanConversion: true),
|
||||
new("name", "name", NpgsqlDbType.Varchar),
|
||||
new("mean_type", "mean_type", NpgsqlDbType.Smallint),
|
||||
],
|
||||
SequenceColumn: "id");
|
||||
|
||||
public static readonly TableDescriptor StatisticsShortTerm = new(
|
||||
"statistics_short_term",
|
||||
[
|
||||
new("id", "id", NpgsqlDbType.Bigint, IsPrimaryKey: true),
|
||||
new("created", "created", NpgsqlDbType.TimestampTz),
|
||||
new("created_ts", "created_ts", NpgsqlDbType.Double),
|
||||
new("metadata_id", "metadata_id", NpgsqlDbType.Bigint),
|
||||
new("start", "start", NpgsqlDbType.TimestampTz),
|
||||
new("start_ts", "start_ts", NpgsqlDbType.Double),
|
||||
new("mean", "mean", NpgsqlDbType.Double),
|
||||
new("mean_weight", "mean_weight", NpgsqlDbType.Double),
|
||||
new("min", "min", NpgsqlDbType.Double),
|
||||
new("max", "max", NpgsqlDbType.Double),
|
||||
new("last_reset", "last_reset", NpgsqlDbType.TimestampTz),
|
||||
new("last_reset_ts", "last_reset_ts", NpgsqlDbType.Double),
|
||||
new("state", "state", NpgsqlDbType.Double),
|
||||
new("sum", "sum", NpgsqlDbType.Double),
|
||||
],
|
||||
SequenceColumn: "id");
|
||||
|
||||
public static readonly TableDescriptor StatisticsRuns = new(
|
||||
"statistics_runs",
|
||||
[
|
||||
new("run_id", "run_id", NpgsqlDbType.Bigint, IsPrimaryKey: true),
|
||||
new("start", "start", NpgsqlDbType.TimestampTz),
|
||||
],
|
||||
SequenceColumn: "run_id");
|
||||
|
||||
public static IReadOnlyList<TableDescriptor> GetMigrationOrder() =>
|
||||
[
|
||||
EventTypes,
|
||||
EventData,
|
||||
StatesMeta,
|
||||
StateAttributes,
|
||||
StatisticsMeta,
|
||||
Events,
|
||||
States,
|
||||
Statistics,
|
||||
StatisticsShortTerm,
|
||||
StatisticsRuns,
|
||||
RecorderRuns,
|
||||
SchemaChanges,
|
||||
MigrationChanges,
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user