added import function for migration tool.
This commit is contained in:
374
Controllers/MigrationController.cs
Normal file
374
Controllers/MigrationController.cs
Normal file
@@ -0,0 +1,374 @@
|
|||||||
|
using CarCareTracker.External.Implementations;
|
||||||
|
using CarCareTracker.Helper;
|
||||||
|
using CarCareTracker.Models;
|
||||||
|
using LiteDB;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Npgsql;
|
||||||
|
using System.Data.Common;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
|
||||||
|
namespace CarCareTracker.Controllers
|
||||||
|
{
|
||||||
|
[Authorize(Roles = nameof(UserData.IsRootUser))]
|
||||||
|
public class MigrationController : Controller
|
||||||
|
{
|
||||||
|
private IConfigHelper _configHelper;
|
||||||
|
private IConfiguration _serverConfig;
|
||||||
|
private IFileHelper _fileHelper;
|
||||||
|
private readonly ILogger<MigrationController> _logger;
|
||||||
|
public MigrationController(IConfigHelper configHelper, IFileHelper fileHelper, IConfiguration serverConfig, ILogger<MigrationController> logger)
|
||||||
|
{
|
||||||
|
_configHelper = configHelper;
|
||||||
|
_fileHelper = fileHelper;
|
||||||
|
_serverConfig = serverConfig;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
public IActionResult Index()
|
||||||
|
{
|
||||||
|
if (_configHelper.GetServerHasPostgresConnection())
|
||||||
|
{
|
||||||
|
return View();
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
return new RedirectResult("/Error/Unauthorized");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void InitializeTables(NpgsqlConnection conn)
|
||||||
|
{
|
||||||
|
var cmds = new List<string>
|
||||||
|
{
|
||||||
|
"CREATE TABLE IF NOT EXISTS app.vehicles (id INT GENERATED BY DEFAULT AS IDENTITY primary key, data jsonb not null)",
|
||||||
|
"CREATE TABLE IF NOT EXISTS app.collisionrecords (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)",
|
||||||
|
"CREATE TABLE IF NOT EXISTS app.upgraderecords (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)",
|
||||||
|
"CREATE TABLE IF NOT EXISTS app.servicerecords (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)",
|
||||||
|
"CREATE TABLE IF NOT EXISTS app.gasrecords (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)",
|
||||||
|
"CREATE TABLE IF NOT EXISTS app.notes (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)",
|
||||||
|
"CREATE TABLE IF NOT EXISTS app.odometerrecords (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)",
|
||||||
|
"CREATE TABLE IF NOT EXISTS app.reminderrecords (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)",
|
||||||
|
"CREATE TABLE IF NOT EXISTS app.planrecords (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)",
|
||||||
|
"CREATE TABLE IF NOT EXISTS app.planrecordtemplates (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)",
|
||||||
|
"CREATE TABLE IF NOT EXISTS app.supplyrecords (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)",
|
||||||
|
"CREATE TABLE IF NOT EXISTS app.taxrecords (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)",
|
||||||
|
"CREATE TABLE IF NOT EXISTS app.userrecords (id INT GENERATED BY DEFAULT AS IDENTITY primary key, username TEXT not null, emailaddress TEXT not null, password TEXT not null, isadmin BOOLEAN)",
|
||||||
|
"CREATE TABLE IF NOT EXISTS app.tokenrecords (id INT GENERATED BY DEFAULT AS IDENTITY primary key, body TEXT not null, emailaddress TEXT not null)",
|
||||||
|
"CREATE TABLE IF NOT EXISTS app.userconfigrecords (id INT primary key, data jsonb not null)",
|
||||||
|
"CREATE TABLE IF NOT EXISTS app.useraccessrecords (userId INT, vehicleId INT, PRIMARY KEY(userId, vehicleId))"
|
||||||
|
};
|
||||||
|
foreach(string cmd in cmds)
|
||||||
|
{
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, conn))
|
||||||
|
{
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public IActionResult Import(string fileName)
|
||||||
|
{
|
||||||
|
if (!_configHelper.GetServerHasPostgresConnection())
|
||||||
|
{
|
||||||
|
return new RedirectResult("/Error/Unauthorized");
|
||||||
|
}
|
||||||
|
var fullFileName = _fileHelper.GetFullFilePath(fileName);
|
||||||
|
if (string.IsNullOrWhiteSpace(fullFileName))
|
||||||
|
{
|
||||||
|
return Json(new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage });
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var pgDataSource = new NpgsqlConnection(_serverConfig["POSTGRES_CONNECTION"]);
|
||||||
|
pgDataSource.Open();
|
||||||
|
InitializeTables(pgDataSource);
|
||||||
|
//pull records
|
||||||
|
var vehicles = new List<Vehicle>();
|
||||||
|
var repairrecords = new List<CollisionRecord>();
|
||||||
|
var upgraderecords = new List<UpgradeRecord>();
|
||||||
|
var servicerecords = new List<ServiceRecord>();
|
||||||
|
|
||||||
|
var gasrecords = new List<GasRecord>();
|
||||||
|
var noterecords = new List<Note>();
|
||||||
|
var odometerrecords = new List<OdometerRecord>();
|
||||||
|
var reminderrecords = new List<ReminderRecord>();
|
||||||
|
|
||||||
|
var planrecords = new List<PlanRecord>();
|
||||||
|
var planrecordtemplates = new List<PlanRecordInput>();
|
||||||
|
var supplyrecords = new List<SupplyRecord>();
|
||||||
|
var taxrecords = new List<TaxRecord>();
|
||||||
|
|
||||||
|
var userrecords = new List<UserData>();
|
||||||
|
var tokenrecords = new List<Token>();
|
||||||
|
var userconfigrecords = new List<UserConfigData>();
|
||||||
|
var useraccessrecords = new List<UserAccess>();
|
||||||
|
#region "Part1"
|
||||||
|
using (var db = new LiteDatabase(fullFileName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<Vehicle>("vehicles");
|
||||||
|
vehicles = table.FindAll().ToList();
|
||||||
|
};
|
||||||
|
foreach(var vehicle in vehicles)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.vehicles (id, data) VALUES(@id, CAST(@data AS jsonb))";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", vehicle.Id);
|
||||||
|
ctext.Parameters.AddWithValue("data", System.Text.Json.JsonSerializer.Serialize(vehicle));
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
using (var db = new LiteDatabase(fullFileName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<CollisionRecord>("collisionrecords");
|
||||||
|
repairrecords = table.FindAll().ToList();
|
||||||
|
};
|
||||||
|
foreach (var record in repairrecords)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.collisionrecords (id, vehicleId, data) VALUES(@id, @vehicleId, CAST(@data AS jsonb))";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", record.Id);
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", record.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", System.Text.Json.JsonSerializer.Serialize(record));
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
using (var db = new LiteDatabase(fullFileName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<ServiceRecord>("servicerecords");
|
||||||
|
servicerecords = table.FindAll().ToList();
|
||||||
|
};
|
||||||
|
foreach (var record in servicerecords)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.servicerecords (id, vehicleId, data) VALUES(@id, @vehicleId, CAST(@data AS jsonb))";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", record.Id);
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", record.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", System.Text.Json.JsonSerializer.Serialize(record));
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
using (var db = new LiteDatabase(fullFileName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<UpgradeRecord>("upgraderecords");
|
||||||
|
upgraderecords = table.FindAll().ToList();
|
||||||
|
};
|
||||||
|
foreach (var record in upgraderecords)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.upgraderecords (id, vehicleId, data) VALUES(@id, @vehicleId, CAST(@data AS jsonb))";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", record.Id);
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", record.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", System.Text.Json.JsonSerializer.Serialize(record));
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
#region "Part2"
|
||||||
|
using (var db = new LiteDatabase(fullFileName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<GasRecord>("gasrecords");
|
||||||
|
gasrecords = table.FindAll().ToList();
|
||||||
|
};
|
||||||
|
foreach (var record in gasrecords)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.gasrecords (id, vehicleId, data) VALUES(@id, @vehicleId, CAST(@data AS jsonb))";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", record.Id);
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", record.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", System.Text.Json.JsonSerializer.Serialize(record));
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
using (var db = new LiteDatabase(fullFileName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<Note>("notes");
|
||||||
|
noterecords = table.FindAll().ToList();
|
||||||
|
};
|
||||||
|
foreach (var record in noterecords)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.notes (id, vehicleId, data) VALUES(@id, @vehicleId, CAST(@data AS jsonb))";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", record.Id);
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", record.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", System.Text.Json.JsonSerializer.Serialize(record));
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
using (var db = new LiteDatabase(fullFileName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<OdometerRecord>("odometerrecords");
|
||||||
|
odometerrecords = table.FindAll().ToList();
|
||||||
|
};
|
||||||
|
foreach (var record in odometerrecords)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.odometerrecords (id, vehicleId, data) VALUES(@id, @vehicleId, CAST(@data AS jsonb))";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", record.Id);
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", record.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", System.Text.Json.JsonSerializer.Serialize(record));
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
using (var db = new LiteDatabase(fullFileName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<ReminderRecord>("reminderrecords");
|
||||||
|
reminderrecords = table.FindAll().ToList();
|
||||||
|
};
|
||||||
|
foreach (var record in reminderrecords)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.reminderrecords (id, vehicleId, data) VALUES(@id, @vehicleId, CAST(@data AS jsonb))";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", record.Id);
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", record.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", System.Text.Json.JsonSerializer.Serialize(record));
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
#region "Part3"
|
||||||
|
using (var db = new LiteDatabase(fullFileName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<PlanRecord>("planrecords");
|
||||||
|
planrecords = table.FindAll().ToList();
|
||||||
|
};
|
||||||
|
foreach (var record in planrecords)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.planrecords (id, vehicleId, data) VALUES(@id, @vehicleId, CAST(@data AS jsonb))";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", record.Id);
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", record.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", System.Text.Json.JsonSerializer.Serialize(record));
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
using (var db = new LiteDatabase(fullFileName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<PlanRecordInput>("planrecordtemplates");
|
||||||
|
planrecordtemplates = table.FindAll().ToList();
|
||||||
|
};
|
||||||
|
foreach (var record in planrecordtemplates)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.planrecordtemplates (id, vehicleId, data) VALUES(@id, @vehicleId, CAST(@data AS jsonb))";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", record.Id);
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", record.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", System.Text.Json.JsonSerializer.Serialize(record));
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
using (var db = new LiteDatabase(fullFileName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<SupplyRecord>("supplyrecords");
|
||||||
|
supplyrecords = table.FindAll().ToList();
|
||||||
|
};
|
||||||
|
foreach (var record in supplyrecords)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.supplyrecords (id, vehicleId, data) VALUES(@id, @vehicleId, CAST(@data AS jsonb))";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", record.Id);
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", record.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", System.Text.Json.JsonSerializer.Serialize(record));
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
using (var db = new LiteDatabase(fullFileName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<TaxRecord>("taxrecords");
|
||||||
|
taxrecords = table.FindAll().ToList();
|
||||||
|
};
|
||||||
|
foreach (var record in taxrecords)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.taxrecords (id, vehicleId, data) VALUES(@id, @vehicleId, CAST(@data AS jsonb))";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", record.Id);
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", record.VehicleId);
|
||||||
|
ctext.Parameters.AddWithValue("data", System.Text.Json.JsonSerializer.Serialize(record));
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
#region "Part4"
|
||||||
|
using (var db = new LiteDatabase(fullFileName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<UserData>("userrecords");
|
||||||
|
userrecords = table.FindAll().ToList();
|
||||||
|
};
|
||||||
|
foreach (var record in userrecords)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.userrecords (id, username, emailaddress, password, isadmin) VALUES(@id, @username, @emailaddress, @password, @isadmin)";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", record.Id);
|
||||||
|
ctext.Parameters.AddWithValue("username", record.UserName);
|
||||||
|
ctext.Parameters.AddWithValue("emailaddress", record.EmailAddress);
|
||||||
|
ctext.Parameters.AddWithValue("password", record.Password);
|
||||||
|
ctext.Parameters.AddWithValue("isadmin", record.IsAdmin);
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
using (var db = new LiteDatabase(fullFileName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<Token>("tokenrecords");
|
||||||
|
tokenrecords = table.FindAll().ToList();
|
||||||
|
};
|
||||||
|
foreach (var record in tokenrecords)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.tokenrecords (id, emailaddress, body) VALUES(@id, @emailaddress, @body)";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", record.Id);
|
||||||
|
ctext.Parameters.AddWithValue("emailaddress", record.EmailAddress);
|
||||||
|
ctext.Parameters.AddWithValue("body", record.Body);
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
using (var db = new LiteDatabase(fullFileName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<UserConfigData>("userconfigrecords");
|
||||||
|
userconfigrecords = table.FindAll().ToList();
|
||||||
|
};
|
||||||
|
foreach (var record in userconfigrecords)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.userconfigrecords (id, data) VALUES(@id, CAST(@data AS jsonb))";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("id", record.Id);
|
||||||
|
ctext.Parameters.AddWithValue("data", System.Text.Json.JsonSerializer.Serialize(record));
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
using (var db = new LiteDatabase(fullFileName))
|
||||||
|
{
|
||||||
|
var table = db.GetCollection<UserAccess>("useraccessrecords");
|
||||||
|
useraccessrecords = table.FindAll().ToList();
|
||||||
|
};
|
||||||
|
foreach (var record in useraccessrecords)
|
||||||
|
{
|
||||||
|
string cmd = $"INSERT INTO app.useraccessrecords (userId, vehicleId) VALUES(@userId, @vehicleId)";
|
||||||
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
{
|
||||||
|
ctext.Parameters.AddWithValue("userId", record.Id.UserId);
|
||||||
|
ctext.Parameters.AddWithValue("vehicleId", record.Id.VehicleId);
|
||||||
|
ctext.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
return Json(new OperationResponse { Success = true, Message = "Data Imported Successfully" });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
return Json(new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,7 +18,7 @@ namespace CarCareTracker.External.Implementations
|
|||||||
{
|
{
|
||||||
pgDataSource.Open();
|
pgDataSource.Open();
|
||||||
//create table if not exist.
|
//create table if not exist.
|
||||||
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
||||||
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
{
|
{
|
||||||
ctext.ExecuteNonQuery();
|
ctext.ExecuteNonQuery();
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace CarCareTracker.External.Implementations
|
|||||||
{
|
{
|
||||||
pgDataSource.Open();
|
pgDataSource.Open();
|
||||||
//create table if not exist.
|
//create table if not exist.
|
||||||
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
||||||
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
{
|
{
|
||||||
ctext.ExecuteNonQuery();
|
ctext.ExecuteNonQuery();
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace CarCareTracker.External.Implementations
|
|||||||
{
|
{
|
||||||
pgDataSource.Open();
|
pgDataSource.Open();
|
||||||
//create table if not exist.
|
//create table if not exist.
|
||||||
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
||||||
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
{
|
{
|
||||||
ctext.ExecuteNonQuery();
|
ctext.ExecuteNonQuery();
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace CarCareTracker.External.Implementations
|
|||||||
{
|
{
|
||||||
pgDataSource.Open();
|
pgDataSource.Open();
|
||||||
//create table if not exist.
|
//create table if not exist.
|
||||||
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
||||||
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
{
|
{
|
||||||
ctext.ExecuteNonQuery();
|
ctext.ExecuteNonQuery();
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace CarCareTracker.External.Implementations
|
|||||||
{
|
{
|
||||||
pgDataSource.Open();
|
pgDataSource.Open();
|
||||||
//create table if not exist.
|
//create table if not exist.
|
||||||
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
||||||
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
{
|
{
|
||||||
ctext.ExecuteNonQuery();
|
ctext.ExecuteNonQuery();
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace CarCareTracker.External.Implementations
|
|||||||
{
|
{
|
||||||
pgDataSource.Open();
|
pgDataSource.Open();
|
||||||
//create table if not exist.
|
//create table if not exist.
|
||||||
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
||||||
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
{
|
{
|
||||||
ctext.ExecuteNonQuery();
|
ctext.ExecuteNonQuery();
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace CarCareTracker.External.Implementations
|
|||||||
{
|
{
|
||||||
pgDataSource.Open();
|
pgDataSource.Open();
|
||||||
//create table if not exist.
|
//create table if not exist.
|
||||||
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
||||||
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
{
|
{
|
||||||
ctext.ExecuteNonQuery();
|
ctext.ExecuteNonQuery();
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace CarCareTracker.External.Implementations
|
|||||||
{
|
{
|
||||||
pgDataSource.Open();
|
pgDataSource.Open();
|
||||||
//create table if not exist.
|
//create table if not exist.
|
||||||
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
||||||
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
{
|
{
|
||||||
ctext.ExecuteNonQuery();
|
ctext.ExecuteNonQuery();
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace CarCareTracker.External.Implementations
|
|||||||
{
|
{
|
||||||
pgDataSource.Open();
|
pgDataSource.Open();
|
||||||
//create table if not exist.
|
//create table if not exist.
|
||||||
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
||||||
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
{
|
{
|
||||||
ctext.ExecuteNonQuery();
|
ctext.ExecuteNonQuery();
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace CarCareTracker.External.Implementations
|
|||||||
{
|
{
|
||||||
pgDataSource.Open();
|
pgDataSource.Open();
|
||||||
//create table if not exist.
|
//create table if not exist.
|
||||||
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
||||||
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
{
|
{
|
||||||
ctext.ExecuteNonQuery();
|
ctext.ExecuteNonQuery();
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ namespace CarCareTracker.External.Implementations
|
|||||||
{
|
{
|
||||||
pgDataSource.Open();
|
pgDataSource.Open();
|
||||||
//create table if not exist.
|
//create table if not exist.
|
||||||
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, body TEXT not null, emailaddress TEXT not null)";
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, body TEXT not null, emailaddress TEXT not null)";
|
||||||
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
{
|
{
|
||||||
ctext.ExecuteNonQuery();
|
ctext.ExecuteNonQuery();
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace CarCareTracker.External.Implementations
|
|||||||
{
|
{
|
||||||
pgDataSource.Open();
|
pgDataSource.Open();
|
||||||
//create table if not exist.
|
//create table if not exist.
|
||||||
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)";
|
||||||
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
{
|
{
|
||||||
ctext.ExecuteNonQuery();
|
ctext.ExecuteNonQuery();
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ namespace CarCareTracker.External.Implementations
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
string cmd = $"SELECT data FROM app.{tableName} WHERE id = @id";
|
string cmd = $"SELECT data FROM app.{tableName} WHERE id = @id";
|
||||||
var result = new UserConfigData();
|
UserConfigData result = null;
|
||||||
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
{
|
{
|
||||||
ctext.Parameters.AddWithValue("id", userId);
|
ctext.Parameters.AddWithValue("id", userId);
|
||||||
@@ -45,7 +45,6 @@ namespace CarCareTracker.External.Implementations
|
|||||||
result = userConfig;
|
result = userConfig;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (result.UserConfig == null) { return null; }
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -59,7 +58,7 @@ namespace CarCareTracker.External.Implementations
|
|||||||
var existingRecord = GetUserConfig(userConfigData.Id);
|
var existingRecord = GetUserConfig(userConfigData.Id);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (existingRecord == null || existingRecord.Id == default)
|
if (existingRecord == null)
|
||||||
{
|
{
|
||||||
string cmd = $"INSERT INTO app.{tableName} (id, data) VALUES(@id, CAST(@data AS jsonb))";
|
string cmd = $"INSERT INTO app.{tableName} (id, data) VALUES(@id, CAST(@data AS jsonb))";
|
||||||
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
using (var ctext = new NpgsqlCommand(cmd, pgDataSource))
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ namespace CarCareTracker.External.Implementations
|
|||||||
{
|
{
|
||||||
pgDataSource.Open();
|
pgDataSource.Open();
|
||||||
//create table if not exist.
|
//create table if not exist.
|
||||||
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, username TEXT not null, emailaddress TEXT not null, password TEXT not null, isadmin BOOLEAN)";
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, username TEXT not null, emailaddress TEXT not null, password TEXT not null, isadmin BOOLEAN)";
|
||||||
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
{
|
{
|
||||||
ctext.ExecuteNonQuery();
|
ctext.ExecuteNonQuery();
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace CarCareTracker.External.Implementations
|
|||||||
{
|
{
|
||||||
pgDataSource.Open();
|
pgDataSource.Open();
|
||||||
//create table if not exist.
|
//create table if not exist.
|
||||||
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED ALWAYS AS IDENTITY primary key, data jsonb not null)";
|
string initCMD = $"CREATE TABLE IF NOT EXISTS app.{tableName} (id INT GENERATED BY DEFAULT AS IDENTITY primary key, data jsonb not null)";
|
||||||
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
using (var ctext = new NpgsqlCommand(initCMD, pgDataSource))
|
||||||
{
|
{
|
||||||
ctext.ExecuteNonQuery();
|
ctext.ExecuteNonQuery();
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ namespace CarCareTracker.Helper
|
|||||||
string GetLogoUrl();
|
string GetLogoUrl();
|
||||||
string GetServerLanguage();
|
string GetServerLanguage();
|
||||||
bool GetServerEnableShopSupplies();
|
bool GetServerEnableShopSupplies();
|
||||||
|
bool GetServerHasPostgresConnection();
|
||||||
public bool DeleteUserConfig(int userId);
|
public bool DeleteUserConfig(int userId);
|
||||||
}
|
}
|
||||||
public class ConfigHelper : IConfigHelper
|
public class ConfigHelper : IConfigHelper
|
||||||
@@ -41,6 +42,10 @@ namespace CarCareTracker.Helper
|
|||||||
var serverLanguage = _config[nameof(UserConfig.UserLanguage)] ?? "en_US";
|
var serverLanguage = _config[nameof(UserConfig.UserLanguage)] ?? "en_US";
|
||||||
return serverLanguage;
|
return serverLanguage;
|
||||||
}
|
}
|
||||||
|
public bool GetServerHasPostgresConnection()
|
||||||
|
{
|
||||||
|
return !string.IsNullOrWhiteSpace(_config["POSTGRES_CONNECTION"]);
|
||||||
|
}
|
||||||
public bool GetServerEnableShopSupplies()
|
public bool GetServerEnableShopSupplies()
|
||||||
{
|
{
|
||||||
return bool.Parse(_config[nameof(UserConfig.EnableShopSupplies)] ?? "false");
|
return bool.Parse(_config[nameof(UserConfig.EnableShopSupplies)] ?? "false");
|
||||||
|
|||||||
77
Views/Migration/Index.cshtml
Normal file
77
Views/Migration/Index.cshtml
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
@using CarCareTracker.Helper
|
||||||
|
@{
|
||||||
|
ViewData["Title"] = "Admin";
|
||||||
|
}
|
||||||
|
@inject IConfiguration config;
|
||||||
|
@inject ITranslationHelper translator
|
||||||
|
@{
|
||||||
|
var userLanguage = config[nameof(UserConfig.UserLanguage)] ?? "en_US";
|
||||||
|
}
|
||||||
|
@model AdminViewModel
|
||||||
|
<div class="container">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-1">
|
||||||
|
<a href="/Home" class="btn btn-secondary btn-md mt-1 mb-1"><i class="bi bi-arrow-left-square"></i></a>
|
||||||
|
</div>
|
||||||
|
<div class="col-11">
|
||||||
|
<span class="display-6">@translator.Translate(userLanguage, "Database Migration")</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<hr />
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-12">
|
||||||
|
<ul class="list-group list-group-flush">
|
||||||
|
<li class="list-group-item">Instructions</li>
|
||||||
|
<li class="list-group-item">Use this tool to migrate data between LiteDB and Postgres</li>
|
||||||
|
<li class="list-group-item">Note that it is recommended that the Postgres DB is empty when importing from LiteDB to prevent primary key errors.</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<hr />
|
||||||
|
<div class="row d-flex justify-content-center">
|
||||||
|
<div class="col-3">
|
||||||
|
<div class="d-grid">
|
||||||
|
<input onChange="importPostgresData(this)" type="file" accept=".db" class="d-none" id="inputImport">
|
||||||
|
<button type="button" class="btn btn-warning mt-2" onclick="importToPostgres()"><i class="bi bi-upload me-2"></i>@translator.Translate(userLanguage, "Import To Postgres")</button>
|
||||||
|
</div>
|
||||||
|
<div class="d-grid">
|
||||||
|
<button type="button" class="btn btn-warning mt-2" onclick="performLogin()"><i class="bi bi-download me-2"></i>@translator.Translate(userLanguage, "Export From Postgres")</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
function importPostgresData(event) {
|
||||||
|
let formData = new FormData();
|
||||||
|
formData.append("file", event.files[0]);
|
||||||
|
sloader.show();
|
||||||
|
$.ajax({
|
||||||
|
url: "/Files/HandleFileUpload",
|
||||||
|
data: formData,
|
||||||
|
cache: false,
|
||||||
|
processData: false,
|
||||||
|
contentType: false,
|
||||||
|
type: 'POST',
|
||||||
|
success: function (response) {
|
||||||
|
if (response.trim() != '') {
|
||||||
|
$.post('/Migration/Import', { fileName: response }, function (data) {
|
||||||
|
sloader.hide();
|
||||||
|
if (data.success) {
|
||||||
|
successToast(data.message);
|
||||||
|
//setTimeout(function () { window.location.href = '/Home/Index' }, 500);
|
||||||
|
} else {
|
||||||
|
errorToast(genericErrorMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function () {
|
||||||
|
sloader.hide();
|
||||||
|
errorToast("An error has occurred, please check the file size and try again later.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
function importToPostgres() {
|
||||||
|
$("#inputImport").click();
|
||||||
|
}
|
||||||
|
</script>
|
||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user