From 42f234e1038853af123a1f43c466ac32eb3df8a4 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Thu, 11 Nov 2021 22:49:35 +0100 Subject: [PATCH] Adding DB.pm --- cualsea-server/lib/Cualsea/Server/DB.pm | 45 +++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 cualsea-server/lib/Cualsea/Server/DB.pm diff --git a/cualsea-server/lib/Cualsea/Server/DB.pm b/cualsea-server/lib/Cualsea/Server/DB.pm new file mode 100644 index 0000000..386240b --- /dev/null +++ b/cualsea-server/lib/Cualsea/Server/DB.pm @@ -0,0 +1,45 @@ +package Cualsea::Server::DB; + +use v5.30.0; + +use strict; +use warnings; + +use DBI; + +my $dbname = $ENV{HOME} . '/cualsea.db'; + +my @migrations = ( + 'CREATE TABLE options ( + key TEXT PRIMARY KEY, + value TEXT + );', + 'CREATE TABLE services ( + name TEXT PRIMARY KEY, + init TEXT, + pidfile TEXT, + binpath TEXT + );' +); + +sub dbh { + my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", '', '' , { + AutoCommit => 1, + RaiseError => 1, + }); + state $migrations_run = 0; + if (!$migrations_run) { + run_migrations($dbh); + $migrations_run = 1; + } +} + +sub run_migrations { + my $dbh = shift; + + my $migration = $dbh->selectrow_hashref(<<'EOF', {}); +SELECT value FROM options WHERE key = 'migration' +EOF + +} +1;