mirror of
https://github.com/azaion/satellite-provider.git
synced 2026-04-22 07:06:39 +00:00
database and migrations
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
using System.Reflection;
|
||||
using DbUp;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace SatelliteProvider.DataAccess;
|
||||
|
||||
public class DatabaseMigrator
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
private readonly ILogger<DatabaseMigrator>? _logger;
|
||||
|
||||
public DatabaseMigrator(string connectionString, ILogger<DatabaseMigrator>? logger = null)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public bool RunMigrations()
|
||||
{
|
||||
_logger?.LogInformation("Starting database migrations...");
|
||||
|
||||
EnsureDatabase.For.PostgresqlDatabase(_connectionString);
|
||||
|
||||
var upgrader = DeployChanges.To
|
||||
.PostgresqlDatabase(_connectionString)
|
||||
.WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly(),
|
||||
script => script.Contains(".Migrations."))
|
||||
.LogToConsole()
|
||||
.Build();
|
||||
|
||||
var result = upgrader.PerformUpgrade();
|
||||
|
||||
if (!result.Successful)
|
||||
{
|
||||
_logger?.LogError(result.Error, "Database migration failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
_logger?.LogInformation("Database migrations completed successfully");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
CREATE TABLE tiles (
|
||||
id UUID PRIMARY KEY,
|
||||
zoom_level INT NOT NULL,
|
||||
latitude DOUBLE PRECISION NOT NULL,
|
||||
longitude DOUBLE PRECISION NOT NULL,
|
||||
tile_size_meters DOUBLE PRECISION NOT NULL,
|
||||
tile_size_pixels INT NOT NULL,
|
||||
image_type VARCHAR(10) NOT NULL,
|
||||
maps_version VARCHAR(50),
|
||||
file_path VARCHAR(500) NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
CREATE TABLE regions (
|
||||
id UUID PRIMARY KEY,
|
||||
latitude DOUBLE PRECISION NOT NULL,
|
||||
longitude DOUBLE PRECISION NOT NULL,
|
||||
size_meters DOUBLE PRECISION NOT NULL,
|
||||
zoom_level INT NOT NULL,
|
||||
status VARCHAR(20) NOT NULL,
|
||||
csv_file_path VARCHAR(500),
|
||||
summary_file_path VARCHAR(500),
|
||||
tiles_downloaded INT DEFAULT 0,
|
||||
tiles_reused INT DEFAULT 0,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
CREATE INDEX idx_tiles_composite ON tiles(latitude, longitude, tile_size_meters);
|
||||
CREATE INDEX idx_tiles_zoom ON tiles(zoom_level);
|
||||
CREATE INDEX idx_regions_status ON regions(status);
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace SatelliteProvider.DataAccess.Models;
|
||||
|
||||
public class RegionEntity
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public double Latitude { get; set; }
|
||||
public double Longitude { get; set; }
|
||||
public double SizeMeters { get; set; }
|
||||
public int ZoomLevel { get; set; }
|
||||
public string Status { get; set; } = string.Empty;
|
||||
public string? CsvFilePath { get; set; }
|
||||
public string? SummaryFilePath { get; set; }
|
||||
public int TilesDownloaded { get; set; }
|
||||
public int TilesReused { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace SatelliteProvider.DataAccess.Models;
|
||||
|
||||
public class TileEntity
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public int ZoomLevel { get; set; }
|
||||
public double Latitude { get; set; }
|
||||
public double Longitude { get; set; }
|
||||
public double TileSizeMeters { get; set; }
|
||||
public int TileSizePixels { get; set; }
|
||||
public string ImageType { get; set; } = string.Empty;
|
||||
public string? MapsVersion { get; set; }
|
||||
public string FilePath { get; set; } = string.Empty;
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
using SatelliteProvider.DataAccess.Models;
|
||||
|
||||
namespace SatelliteProvider.DataAccess.Repositories;
|
||||
|
||||
public interface IRegionRepository
|
||||
{
|
||||
Task<RegionEntity?> GetByIdAsync(Guid id);
|
||||
Task<IEnumerable<RegionEntity>> GetByStatusAsync(string status);
|
||||
Task<Guid> InsertAsync(RegionEntity region);
|
||||
Task<int> UpdateAsync(RegionEntity region);
|
||||
Task<int> DeleteAsync(Guid id);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
using SatelliteProvider.DataAccess.Models;
|
||||
|
||||
namespace SatelliteProvider.DataAccess.Repositories;
|
||||
|
||||
public interface ITileRepository
|
||||
{
|
||||
Task<TileEntity?> GetByIdAsync(Guid id);
|
||||
Task<TileEntity?> FindExistingTileAsync(double latitude, double longitude, double tileSizeMeters, int zoomLevel);
|
||||
Task<IEnumerable<TileEntity>> GetTilesByRegionAsync(double latitude, double longitude, double sizeMeters, int zoomLevel);
|
||||
Task<Guid> InsertAsync(TileEntity tile);
|
||||
Task<int> UpdateAsync(TileEntity tile);
|
||||
Task<int> DeleteAsync(Guid id);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
using Dapper;
|
||||
using Npgsql;
|
||||
using SatelliteProvider.DataAccess.Models;
|
||||
|
||||
namespace SatelliteProvider.DataAccess.Repositories;
|
||||
|
||||
public class RegionRepository : IRegionRepository
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
|
||||
public RegionRepository(string connectionString)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
}
|
||||
|
||||
public async Task<RegionEntity?> GetByIdAsync(Guid id)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
SELECT id, latitude, longitude, size_meters as SizeMeters,
|
||||
zoom_level as ZoomLevel, status,
|
||||
csv_file_path as CsvFilePath, summary_file_path as SummaryFilePath,
|
||||
tiles_downloaded as TilesDownloaded, tiles_reused as TilesReused,
|
||||
created_at as CreatedAt, updated_at as UpdatedAt
|
||||
FROM regions
|
||||
WHERE id = @Id";
|
||||
|
||||
return await connection.QuerySingleOrDefaultAsync<RegionEntity>(sql, new { Id = id });
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<RegionEntity>> GetByStatusAsync(string status)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
SELECT id, latitude, longitude, size_meters as SizeMeters,
|
||||
zoom_level as ZoomLevel, status,
|
||||
csv_file_path as CsvFilePath, summary_file_path as SummaryFilePath,
|
||||
tiles_downloaded as TilesDownloaded, tiles_reused as TilesReused,
|
||||
created_at as CreatedAt, updated_at as UpdatedAt
|
||||
FROM regions
|
||||
WHERE status = @Status
|
||||
ORDER BY created_at ASC";
|
||||
|
||||
return await connection.QueryAsync<RegionEntity>(sql, new { Status = status });
|
||||
}
|
||||
|
||||
public async Task<Guid> InsertAsync(RegionEntity region)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
INSERT INTO regions (id, latitude, longitude, size_meters, zoom_level,
|
||||
status, csv_file_path, summary_file_path,
|
||||
tiles_downloaded, tiles_reused, created_at, updated_at)
|
||||
VALUES (@Id, @Latitude, @Longitude, @SizeMeters, @ZoomLevel,
|
||||
@Status, @CsvFilePath, @SummaryFilePath,
|
||||
@TilesDownloaded, @TilesReused, @CreatedAt, @UpdatedAt)
|
||||
RETURNING id";
|
||||
|
||||
return await connection.ExecuteScalarAsync<Guid>(sql, region);
|
||||
}
|
||||
|
||||
public async Task<int> UpdateAsync(RegionEntity region)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
UPDATE regions
|
||||
SET latitude = @Latitude,
|
||||
longitude = @Longitude,
|
||||
size_meters = @SizeMeters,
|
||||
zoom_level = @ZoomLevel,
|
||||
status = @Status,
|
||||
csv_file_path = @CsvFilePath,
|
||||
summary_file_path = @SummaryFilePath,
|
||||
tiles_downloaded = @TilesDownloaded,
|
||||
tiles_reused = @TilesReused,
|
||||
updated_at = @UpdatedAt
|
||||
WHERE id = @Id";
|
||||
|
||||
return await connection.ExecuteAsync(sql, region);
|
||||
}
|
||||
|
||||
public async Task<int> DeleteAsync(Guid id)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = "DELETE FROM regions WHERE id = @Id";
|
||||
return await connection.ExecuteAsync(sql, new { Id = id });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
using Dapper;
|
||||
using Npgsql;
|
||||
using SatelliteProvider.DataAccess.Models;
|
||||
|
||||
namespace SatelliteProvider.DataAccess.Repositories;
|
||||
|
||||
public class TileRepository : ITileRepository
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
|
||||
public TileRepository(string connectionString)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
}
|
||||
|
||||
public async Task<TileEntity?> GetByIdAsync(Guid id)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
SELECT id, zoom_level as ZoomLevel, latitude, longitude,
|
||||
tile_size_meters as TileSizeMeters, tile_size_pixels as TileSizePixels,
|
||||
image_type as ImageType, maps_version as MapsVersion,
|
||||
file_path as FilePath, created_at as CreatedAt, updated_at as UpdatedAt
|
||||
FROM tiles
|
||||
WHERE id = @Id";
|
||||
|
||||
return await connection.QuerySingleOrDefaultAsync<TileEntity>(sql, new { Id = id });
|
||||
}
|
||||
|
||||
public async Task<TileEntity?> FindExistingTileAsync(double latitude, double longitude, double tileSizeMeters, int zoomLevel)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
SELECT id, zoom_level as ZoomLevel, latitude, longitude,
|
||||
tile_size_meters as TileSizeMeters, tile_size_pixels as TileSizePixels,
|
||||
image_type as ImageType, maps_version as MapsVersion,
|
||||
file_path as FilePath, created_at as CreatedAt, updated_at as UpdatedAt
|
||||
FROM tiles
|
||||
WHERE ABS(latitude - @Latitude) < 0.0001
|
||||
AND ABS(longitude - @Longitude) < 0.0001
|
||||
AND ABS(tile_size_meters - @TileSizeMeters) < 1
|
||||
AND zoom_level = @ZoomLevel
|
||||
LIMIT 1";
|
||||
|
||||
return await connection.QuerySingleOrDefaultAsync<TileEntity>(sql, new
|
||||
{
|
||||
Latitude = latitude,
|
||||
Longitude = longitude,
|
||||
TileSizeMeters = tileSizeMeters,
|
||||
ZoomLevel = zoomLevel
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TileEntity>> GetTilesByRegionAsync(double latitude, double longitude, double sizeMeters, int zoomLevel)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
|
||||
var latRange = sizeMeters / 111000.0;
|
||||
var lonRange = sizeMeters / (111000.0 * Math.Cos(latitude * Math.PI / 180.0));
|
||||
|
||||
const string sql = @"
|
||||
SELECT id, zoom_level as ZoomLevel, latitude, longitude,
|
||||
tile_size_meters as TileSizeMeters, tile_size_pixels as TileSizePixels,
|
||||
image_type as ImageType, maps_version as MapsVersion,
|
||||
file_path as FilePath, created_at as CreatedAt, updated_at as UpdatedAt
|
||||
FROM tiles
|
||||
WHERE latitude BETWEEN @MinLat AND @MaxLat
|
||||
AND longitude BETWEEN @MinLon AND @MaxLon
|
||||
AND zoom_level = @ZoomLevel
|
||||
ORDER BY latitude DESC, longitude ASC";
|
||||
|
||||
return await connection.QueryAsync<TileEntity>(sql, new
|
||||
{
|
||||
MinLat = latitude - latRange / 2,
|
||||
MaxLat = latitude + latRange / 2,
|
||||
MinLon = longitude - lonRange / 2,
|
||||
MaxLon = longitude + lonRange / 2,
|
||||
ZoomLevel = zoomLevel
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<Guid> InsertAsync(TileEntity tile)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
INSERT INTO tiles (id, zoom_level, latitude, longitude, tile_size_meters,
|
||||
tile_size_pixels, image_type, maps_version, file_path,
|
||||
created_at, updated_at)
|
||||
VALUES (@Id, @ZoomLevel, @Latitude, @Longitude, @TileSizeMeters,
|
||||
@TileSizePixels, @ImageType, @MapsVersion, @FilePath,
|
||||
@CreatedAt, @UpdatedAt)
|
||||
RETURNING id";
|
||||
|
||||
return await connection.ExecuteScalarAsync<Guid>(sql, tile);
|
||||
}
|
||||
|
||||
public async Task<int> UpdateAsync(TileEntity tile)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = @"
|
||||
UPDATE tiles
|
||||
SET zoom_level = @ZoomLevel,
|
||||
latitude = @Latitude,
|
||||
longitude = @Longitude,
|
||||
tile_size_meters = @TileSizeMeters,
|
||||
tile_size_pixels = @TileSizePixels,
|
||||
image_type = @ImageType,
|
||||
maps_version = @MapsVersion,
|
||||
file_path = @FilePath,
|
||||
updated_at = @UpdatedAt
|
||||
WHERE id = @Id";
|
||||
|
||||
return await connection.ExecuteAsync(sql, tile);
|
||||
}
|
||||
|
||||
public async Task<int> DeleteAsync(Guid id)
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString);
|
||||
const string sql = "DELETE FROM tiles WHERE id = @Id";
|
||||
return await connection.ExecuteAsync(sql, new { Id = id });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||
<PackageReference Include="Npgsql" Version="9.0.2" />
|
||||
<PackageReference Include="dbup-postgresql" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.10" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.10" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Migrations\*.sql" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user