Executable that will resequence ids in all tables of a postgres database built with .netcore 8.0 Cross Platform Compatible
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

94 Zeilen
3.4 KiB

  1. using Npgsql;
  2. using Renci.SshNet;
  3. using System.Data;
  4. namespace resequence_pgsql
  5. {
  6. public class PGHelper : IDisposable
  7. {
  8. string _ipaddress = "192.168.0.108";
  9. string _username = "mcarman";
  10. string _pwd = "@ng31F@rm0823262";
  11. string _localip = "127.0.0.1";
  12. uint _port = (uint)5432;
  13. string _connString = "Server={0};Database={1};Port={2};User Id={3};Password={4};";
  14. string _db = "cm";
  15. SshClient _client;
  16. ForwardedPortLocal _tunnel;
  17. public PGHelper()
  18. {
  19. _client = new SshClient(_ipaddress, _username, _pwd);
  20. _client.Connect();
  21. _tunnel = new ForwardedPortLocal(_localip, _localip, _port);
  22. _client.AddForwardedPort(_tunnel);
  23. _tunnel.Start();
  24. _connString = string.Format(_connString, _tunnel.BoundHost, _db, _tunnel.BoundPort, _username, _pwd);
  25. }
  26. public PGHelper(string ipaddress, string username, string pwd, string localip, int port, string db)
  27. {
  28. _ipaddress = ipaddress;
  29. _username = username;
  30. _pwd = pwd;
  31. _localip = localip;
  32. _port = (uint)port;
  33. _db = db;
  34. _client = new SshClient(_ipaddress, _username, _pwd);
  35. _client.Connect();
  36. _tunnel = new ForwardedPortLocal(_localip, _localip, _port);
  37. _client.AddForwardedPort(_tunnel);
  38. _tunnel.Start();
  39. _connString = string.Format(_connString, _tunnel.BoundHost, _db, _tunnel.BoundPort, _username, _pwd);
  40. }
  41. public void ResequenceTables()
  42. {
  43. if (_client.IsConnected)
  44. {
  45. try
  46. {
  47. using (NpgsqlConnection conn = new NpgsqlConnection(_connString))
  48. {
  49. conn.Open();
  50. NpgsqlCommand cmd = conn.CreateCommand();
  51. cmd.CommandType = System.Data.CommandType.Text;
  52. cmd.CommandText = "SELECT c.relname FROM pg_class c WHERE c.relkind = 'S' order BY c.relname;";
  53. DataTable tbl = new DataTable();
  54. NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd);
  55. adapter.Fill(tbl);
  56. foreach (DataRow row in tbl.Rows)
  57. {
  58. cmd.CommandText = string.Format("SELECT last_value + 1 from \"{0}\"", row[0]);
  59. int seqVal = Convert.ToInt32(cmd.ExecuteScalar());
  60. cmd.CommandText = string.Format("ALTER SEQUENCE public.\"{0}\" RESTART WITH {1};", row[0], seqVal);
  61. cmd.ExecuteScalar();
  62. }
  63. }
  64. }
  65. catch { throw; }
  66. }
  67. else { throw new Exception("There was a problem with your ssh connection."); }
  68. }
  69. private bool isDisposed = false;
  70. public void Dispose()
  71. {
  72. Dispose(true);
  73. GC.SuppressFinalize(this);
  74. }
  75. protected bool Disposed { get; private set; }
  76. protected virtual void Dispose(bool disposing)
  77. {
  78. if (!isDisposed)
  79. {
  80. if (disposing)
  81. {
  82. if (_client != null) _client.Dispose();
  83. }
  84. }
  85. isDisposed = true;
  86. }
  87. }
  88. }