Adding database migrations.

This commit is contained in:
sergiotarxz 2022-03-14 15:31:37 +01:00
parent d5f21284c5
commit f21aae07de
2 changed files with 65 additions and 52 deletions

View File

@ -14,6 +14,9 @@ use Crypt::URandom q/urandom/;
use JSON;
use Path::Tiny;
use Peace;
use Peace::DB;
my ( $opt, $usage ) = describe_options(
'peace %o',
[ 'dbname|d=s', 'The database name for config generation.' ],
@ -66,6 +69,7 @@ if ( !-e $config_path) {
path ($config_path)->spew_utf8(encode_json $config);
}
Peace::DB->dbh( config => Peace->new->config );
@ARGV = ('daemon');
# Start command line interface for application

View File

@ -14,50 +14,61 @@ use Const::Fast;
my @migrations = (
'CREATE TABLE options (
key TEXT PRIMARY KEY,
value TEXT
);',
'CREATE TABLE users (
username TEXT PRIMARY KEY NOT NULL,
firstname TEXT NOT NULL,
lastname TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
date_creation TIMESTAMP NOT NULL DEFAULT NOW(),
last_connection TIMESTAMP NOT NULL DEFAULT NOW(),
verified boolean NOT NULL DEFAULT false
value INTEGER
);',
'CREATE EXTENSION "pgcrypto";',
'CREATE TABLE countries (
id SERIAL NOT NULL,
code VARCHAR[5] NOT NULL UNIQUE,
name TEXT NOT NULL UNIQUE,
PRIMARY KEY id
);',
q/CREATE TABLE addresses (
'CREATE TABLE customers (
uuid UUID NOT NULL DEFAULT gen_random_uuid(),
secret TEXT NOT NULL,
stripe_id TEXT NOT NULL,
PRIMARY KEY (uuid)
);',
'CREATE TABLE developers (
uuid UUID NOT NULL DEFAULT gen_random_uuid(),
username TEXT NOT NULL,
line1 TEXT NOT NULL,
line2 TEXT NOT NULL DEFAULT '',
city TEXT NOT NULL,
postal_code TEXT NOT NULL,
id_country INTEGER NOT NULL,
FOREIGN KEY (id_country) REFERENCES countries (id),
FOREIGN KEY (username) REFERENCES users (username)
);/,
q/CREATE TABLE categories (
id SERIAL NOT NULL,
name TEXT NOT NULL,
shortname TEXT NOT NULL,
image TEXT,
)/,
q/CREATE TABLE products (
ean TEXT NOT NULL PRIMARY KEY,
surname TEXT NOT NULL,
email TEXT NOT NULL,
stripe_id TEXT NOT NULL,
country TEXT NOT NULL,
verified BOOL DEFAULT false,
PRIMARY KEY (uuid)
);',
'CREATE TABLE applications (
uuid UUID NOT NULL DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
image TEXT,
url TEXT NOT NULL,
developer UUID NOT NULL,
price INTEGER NOT NULL,
id_category INTEGER NOT NULL,
FOREIGN KEY (id_category) REFERENCES categories (id_category)
);/,
git_repo TEXT NOT NULL,
flatpak_builder_file TEXT NOT NULL,
PRIMARY KEY (uuid),
FOREIGN KEY (developer) REFERENCES developers (uuid)
);',
'CREATE TABLE releases (
uuid UUID NOT NULL DEFAULT gen_random_uuid(),
application UUID NOT NULL,
date timestamp DEFAULT NOW(),
tag TEXT NOT NULL,
name TEXT NOT NULL,
PRIMARY KEY (uuid),
FOREIGN KEY (application) REFERENCES applications (uuid)
);',
'CREATE TABLE builds (
uuid UUID NOT NULL DEFAULT gen_random_uuid(),
release UUID NOT NULL,
date timestamp DEFAULT NOW(),
arch TEXT NOT NULL,
PRIMARY KEY (uuid),
FOREIGN KEY (release) REFERENCES releases (uuid)
);',
'CREATE TABLE purchases (
uuid UUID NOT NULL DEFAULT gen_random_uuid(),
customer UUID NOT NULL,
application UUID NOT NULL,
PRIMARY KEY (uuid),
FOREIGN KEY (application) REFERENCES applications (uuid),
FOREIGN KEY (customer) REFERENCES customers (uuid)
);',
);
{
@ -68,6 +79,7 @@ my @migrations = (
);
sub dbh {
my $self = shift;
my %params = $validator->(@_);
my $config = $params{config};
my $db_config = $config->{db_config};
@ -75,7 +87,7 @@ my @migrations = (
my $host = $db_config->{host} or die 'No host in db_config';
my $username = $db_config->{username} or die 'No username in db_config';
my $password = $db_config->{password} or die 'No password in db_config';
my $port = $db_config->{port} or die 'No port in db_config';
my $port = $db_config->{port};
my $dsn =
"dbi:Pg:"
. ( defined $dbname ? "dbname=$dbname;" : "" )
@ -83,7 +95,9 @@ my @migrations = (
. ( defined $port ? "port=$port;" : "" );
my $dbh = DBI->connect_cached(
$dsn, $username, $password,
$dsn,
$username,
$password,
{
AutoCommit => 1,
RaiseError => 1,
@ -103,24 +117,19 @@ my @migrations = (
sub run_migrations {
my $dbh = shift;
my $current_migration = _get_current_migration_number($dbh);
say $current_migration;
local $dbh->{RaiseError} = 0;
if ( $current_migration < scalar @migrations ) {
my @needed_migrations =
@migrations[ $current_migration .. $#migrations ];
for my $migration (@needed_migrations) {
$dbh->do($migration);
if (
!(
0 + $dbh->do(
'UPDATE options SET value = ? WHERE key = "migration"',
undef,
++$current_migration
)
)
)
{
my $update_result =
$dbh->do( 'UPDATE options SET value = ? WHERE key = \'migration\';',
undef, ++$current_migration );
unless ( defined $update_result ) {
$dbh->do(
'INSERT INTO options (key, value) VALUES ("migration", ?)',
'INSERT INTO options (key, value) VALUES (\"migration\", ?)',
undef, $current_migration
);
}