Migration system added.

Install scripts for database schema and dummy data too.
This commit is contained in:
2022-02-26 22:23:30 +10:00
parent 6439b4326c
commit 207edf0de3
122 changed files with 774 additions and 148 deletions

View File

@@ -1,46 +1,98 @@
module db
module Database
open Dapper
open Dapper.FSharp
open Dapper.FSharp.PostgreSQL
open DredgeFramework
open DredgePos.Types
open Npgsql
let connString = "Server=localhost;Port=5432;User Id=postgres;Password=root;Database=dredgepos;Include Error Detail=true"
//let connString = "server=localhost;uid=root;pwd=;database=dredgepos;table cache = false"
let connection = new Npgsql.NpgsqlConnection(connString)
let connect connectionString = new NpgsqlConnection(connectionString)
let getDatabaseSettings () = (getConfig ()).database
let getConnectionString () =
let db = getDatabaseSettings ()
$"Server={db.host};Port={db.port};User Id={db.username};Password={db.password};Database={db.db_name};Include Error Detail=true"
let connectToDatabase () = connect (getConnectionString ())
let closeAndReturn (connection: NpgsqlConnection) (result: 'a) =
connection.Dispose()
result
let Select<'a> asyncQuery =
let connection = connectToDatabase ()
asyncQuery
|> connection.SelectAsync<'a>
|> RunSynchronously
|> EnumerableToArray
|> closeAndReturn connection
let SelectJoin<'a, 'b> asyncQuery =
let connection = connectToDatabase ()
asyncQuery
|> connection.SelectAsync<'a, 'b>
|> RunSynchronously
|> EnumerableToArray
|> closeAndReturn connection
let Insert<'a> asyncQuery =
let connection = connectToDatabase ()
asyncQuery
|> connection.InsertAsync<'a>
|> RunSynchronously
|> closeAndReturn connection
let InsertOutput<'a> asyncQuery =
let connection = connectToDatabase ()
asyncQuery
|> connection.InsertOutputAsync<'a, 'a>
|> RunSynchronously
|> EnumerableToArray
|> closeAndReturn connection
let Update<'a> asyncQuery =
let connection = connectToDatabase ()
asyncQuery
|> connection.UpdateOutputAsync<'a, 'a>
|> RunSynchronously
|> EnumerableToArray
|> closeAndReturn connection
let Delete<'a> asyncQuery =
let connection = connectToDatabase ()
asyncQuery
|> connection.DeleteAsync
|> RunSynchronously
|> RunSynchronously
|> closeAndReturn connection
let NonDbSpecificQuery (sql: string) (connection: NpgsqlConnection) =
sql
|> fun str -> System.IO.File.WriteAllText("sql.log", str); str
|> connection.Execute
|> closeAndReturn connection
let rawQuery (sql: string) = connectToDatabase () |> NonDbSpecificQuery sql
let CreateTable (tableName: string) (columnList: (string * string) list) =
let columns =
columnList
|> List.filter (fun (columnName, _) -> columnName <> "id")
|> List.map (fun (columnName, columnType) -> $""" "{columnName}" {columnType} not null""")
|> String.concat ",\n\t\t\t"
$"""
create table if not exists {tableName}
(
id serial
constraint {tableName}_pk
primary key,
{columns}
);
"""
|> fun str -> System.IO.File.WriteAllText("sql.log", str); str
|> rawQuery
|> ignore

View File

@@ -7,8 +7,8 @@ open System.Drawing
open System.IO
open System.Linq
open System.Xml;
open System.Xml.XPath;
open System.Xml.Xsl
open DredgePos.Types
open FSharp.Reflection
open Thoth.Json.Net
@@ -100,4 +100,14 @@ let GetImageSize image =
let loadedImage = loadImage image
loadedImage.Width, loadedImage.Height
let CurrentTime() = DateTimeOffset.Now.ToUnixTimeSeconds() |> int
let CurrentTime() = DateTimeOffset.Now.ToUnixTimeSeconds() |> int
let getConfig () =
"config.json"
|> GetFileContents
|> Decode.Auto.fromString<config>
|> (fun result ->
match result with
| Ok config -> config
| Error message -> failwith ("config.json is not valid :" + message)
)

View File

@@ -1,4 +1,5 @@
module Entity
open Dapper
open Dapper.FSharp
open DredgeFramework
open Pluralize.NET.Core
@@ -15,7 +16,7 @@ let Create (record: 'x)=
value record
excludeColumn "id"
}
|> db.InsertOutput
|> Database.InsertOutput
|> first
@@ -28,7 +29,7 @@ let inline Update (record: ^x) =
where (eq "id" id)
excludeColumn "id"
}
|> db.Update
|> Database.Update
let GetAll<'x> =
let tableName = GetDatabaseTable<'x>
@@ -36,7 +37,7 @@ let GetAll<'x> =
select {
table tableName
}
|> db.Select<'x>
|> Database.Select<'x>
let GetAllByColumn<'x> (column: string) (value: obj) =
let tableName = GetDatabaseTable<'x>
@@ -44,7 +45,9 @@ let GetAllByColumn<'x> (column: string) (value: obj) =
select {
table tableName
where (eq column value)
} |> db.Select<'x>
} |> Database.Select<'x>
let GetFirstByColumn<'x> (column: string) (value: obj) = (GetAllByColumn<'x> column value) |> first
let GetAllInVenue<'x> = GetAllByColumn<'x> "venue_id" (getCurrentVenue ())
let GetById<'x> (id: int) = GetAllByColumn<'x> "id" id |> first
@@ -67,7 +70,7 @@ let DeleteById<'x> id =
delete {
table tableName
where (eq "id" id)
} |> db.Delete |> ignore
} |> Database.Delete |> ignore
entity

View File

@@ -13,7 +13,7 @@ type reservation = {
[<CLIMutable>]
type venue = {
id: int
venue_name: string
name: string
}
[<CLIMutable>]
@@ -38,7 +38,7 @@ type floorplan_table = {
type print_group = {
id: int
name: string
printer: int
printer_id: int
venue_id: int
}
@@ -47,14 +47,14 @@ type sales_category = {
id: int
parent: int
name: string
print_group: int
print_group_id: int
venue_id: int
}
[<CLIMutable>]
type floorplan_room = {
type room = {
id: int
room_name: string
name: string
background_image: string
venue_id: int
}
@@ -62,18 +62,23 @@ type floorplan_room = {
[<CLIMutable>]
type floorplan_decoration = {
id: int
decoration_room: int
decoration_pos_x: int
decoration_pos_y: int
decoration_rotation: int
decoration_width: int
decoration_height: int
decoration_image: string
room_id: int
pos_x: int
pos_y: int
rotation: int
width: int
height: int
image: string
venue_id: int
}
[<CLIMutable>]
type clerk = {id: int; clerk_name: string; clerk_login_code: int; clerk_usergroup: int}
type clerk = {
id: int
name: string
login_code: int
user_group_id: int
}
[<CLIMutable>]
type session = {id: int; session_id: string; clerk_json: string; clerk_id: int; expires: int}
@@ -82,7 +87,7 @@ type session = {id: int; session_id: string; clerk_json: string; clerk_id: int;
type order_screen_page_group = {id: int; order: int; venue_id: int; label: string; grid_id: int}
[<CLIMutable>]
type grid = {id: int; grid_name: string; grid_rows: int; grid_cols: int; grid_data: string}
type grid = {id: int; name: string; rows: int; cols: int; data: string}
[<CLIMutable>]
type button = {
@@ -100,13 +105,30 @@ type button = {
[<CLIMutable>]
type item = {
id: int
item_code: string
item_category: int
item_name: string
code: string
sales_category_id: int
name: string
item_type: string
price1: int
price2: int
price3: int
price4: int
price5: int
}
[<CLIMutable>]
type db_config = {
db_name: string
username: string
password: string
host: string
port: int
}
[<CLIMutable>]
type config = {
database: db_config
}
[<CLIMutable>]
type migration = {
id: int
name: string
timestamp: int
}