Skip to content
This repository was archived by the owner on Oct 13, 2024. It is now read-only.

Commit ee77992

Browse files
author
corey
committed
migrations
1 parent 7f0c977 commit ee77992

File tree

7 files changed

+58
-5
lines changed

7 files changed

+58
-5
lines changed

MyClasses/bd_db.cs

+25-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
using System.Collections.Generic;
55
using Microsoft.AspNetCore.Mvc.Rendering;
66
using System.Threading.Tasks;
7-
7+
using System.Linq;
8+
using System.IO;
89

910
namespace budoco
1011
{
@@ -219,5 +220,27 @@ public static dynamic format(dynamic data)
219220

220221
}
221222

223+
public static void update_db_schema(string content_root_path)
224+
{
225+
int current_db_version = (int)bd_db.exec_scalar("select db_version from db_version");
226+
227+
string db_changes_folder = content_root_path + "/sql/db_changes/";
228+
229+
string[] files = System.IO.Directory.GetFiles(db_changes_folder);
230+
231+
Array.Sort(files, StringComparer.InvariantCulture);
232+
233+
foreach (string file in files)
234+
{
235+
string digits = new String(file.Where(Char.IsDigit).ToArray());
236+
int version_from_filename = Convert.ToInt32(digits);
237+
238+
if (version_from_filename > current_db_version)
239+
{
240+
string sql = File.ReadAllText(file);
241+
bd_db.exec(sql);
242+
}
243+
}
244+
}
222245
}
223-
}
246+
}

Pages/Admin/Logs.cshtml.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void OnGet()
3333

3434
bd_util.log(log_folder);
3535

36-
files = System.IO.Directory.GetFiles(log_folder); // , ";");
36+
files = System.IO.Directory.GetFiles(log_folder);
3737

3838
for (int i = 0; i < files.Length; i++)
3939
{

Startup.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace budoco
1616
{
1717
public class Startup
1818
{
19-
public Startup(IConfiguration configuration)
19+
public Startup(IConfiguration configuration, IWebHostEnvironment env)
2020
{
2121
bd_util.log("Startup");
2222
Configuration = configuration;
@@ -26,14 +26,15 @@ public Startup(IConfiguration configuration)
2626
//NpgsqlLogManager.Provider = new ConsoleLoggingProvider(NpgsqlLogLevel.Debug, false, false);
2727
NpgsqlLogManager.Provider = new bd_pg_log_provider();
2828

29+
bd_db.update_db_schema(env.ContentRootPath);
30+
2931
// If there are pending outgoing emails try to send them.
3032
bd_email.spawn_email_sending_thread();
3133

3234
bd_email.spawn_email_receiving_thread();
3335

3436
bd_email.spawn_registration_request_expiration_thread();
3537

36-
3738
}
3839

3940
public IConfiguration Configuration { get; }

budoco.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
1414
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
1515
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
16+
17+
18+
<Content Include="sql\**">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</Content>
1621
</ItemGroup>
1722

23+
1824
</Project>

sql/db_changes/002_one_to_two.sql

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
BEGIN;
2+
3+
create table junk1
4+
(
5+
my_col int
6+
);
7+
8+
update db_version set db_version = 2;
9+
10+
11+
COMMIT;

sql/db_changes/003_two_to_three.sql

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
BEGIN;
2+
3+
update db_version set db_version = 3;
4+
5+
COMMIT;

sql/setup.sql

+7
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,10 @@ insert into queries (qu_name, qu_sql) values (
268268
'Raw "select * from issues" Please run queries.sql',
269269
'select * from issues order by i_id desc');
270270

271+
-- one row, for schema changes, "migrations"
272+
drop table db_version;
273+
create table db_version
274+
(
275+
db_version int
276+
);
277+
insert into db_version (db_version) values(1);

0 commit comments

Comments
 (0)