Adding database migrations.
This commit is contained in:
parent
d5f21284c5
commit
f21aae07de
@ -14,6 +14,9 @@ use Crypt::URandom q/urandom/;
|
|||||||
use JSON;
|
use JSON;
|
||||||
use Path::Tiny;
|
use Path::Tiny;
|
||||||
|
|
||||||
|
use Peace;
|
||||||
|
use Peace::DB;
|
||||||
|
|
||||||
my ( $opt, $usage ) = describe_options(
|
my ( $opt, $usage ) = describe_options(
|
||||||
'peace %o',
|
'peace %o',
|
||||||
[ 'dbname|d=s', 'The database name for config generation.' ],
|
[ '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);
|
path ($config_path)->spew_utf8(encode_json $config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Peace::DB->dbh( config => Peace->new->config );
|
||||||
@ARGV = ('daemon');
|
@ARGV = ('daemon');
|
||||||
|
|
||||||
# Start command line interface for application
|
# Start command line interface for application
|
||||||
|
113
lib/Peace/DB.pm
113
lib/Peace/DB.pm
@ -14,50 +14,61 @@ use Const::Fast;
|
|||||||
my @migrations = (
|
my @migrations = (
|
||||||
'CREATE TABLE options (
|
'CREATE TABLE options (
|
||||||
key TEXT PRIMARY KEY,
|
key TEXT PRIMARY KEY,
|
||||||
value TEXT
|
value INTEGER
|
||||||
);',
|
|
||||||
'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
|
|
||||||
);',
|
);',
|
||||||
'CREATE EXTENSION "pgcrypto";',
|
'CREATE EXTENSION "pgcrypto";',
|
||||||
'CREATE TABLE countries (
|
'CREATE TABLE customers (
|
||||||
id SERIAL NOT NULL,
|
uuid UUID NOT NULL DEFAULT gen_random_uuid(),
|
||||||
code VARCHAR[5] NOT NULL UNIQUE,
|
secret TEXT NOT NULL,
|
||||||
name TEXT NOT NULL UNIQUE,
|
stripe_id TEXT NOT NULL,
|
||||||
PRIMARY KEY id
|
PRIMARY KEY (uuid)
|
||||||
);',
|
);',
|
||||||
q/CREATE TABLE addresses (
|
'CREATE TABLE developers (
|
||||||
uuid UUID NOT NULL DEFAULT gen_random_uuid(),
|
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,
|
name TEXT NOT NULL,
|
||||||
shortname TEXT NOT NULL,
|
surname TEXT NOT NULL,
|
||||||
image TEXT,
|
email TEXT NOT NULL,
|
||||||
)/,
|
stripe_id TEXT NOT NULL,
|
||||||
q/CREATE TABLE products (
|
country TEXT NOT NULL,
|
||||||
ean TEXT NOT NULL PRIMARY KEY,
|
verified BOOL DEFAULT false,
|
||||||
|
PRIMARY KEY (uuid)
|
||||||
|
);',
|
||||||
|
'CREATE TABLE applications (
|
||||||
|
uuid UUID NOT NULL DEFAULT gen_random_uuid(),
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
image TEXT,
|
url TEXT NOT NULL,
|
||||||
|
developer UUID NOT NULL,
|
||||||
price INTEGER NOT NULL,
|
price INTEGER NOT NULL,
|
||||||
id_category INTEGER NOT NULL,
|
git_repo TEXT NOT NULL,
|
||||||
FOREIGN KEY (id_category) REFERENCES categories (id_category)
|
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 {
|
sub dbh {
|
||||||
|
my $self = shift;
|
||||||
my %params = $validator->(@_);
|
my %params = $validator->(@_);
|
||||||
my $config = $params{config};
|
my $config = $params{config};
|
||||||
my $db_config = $config->{db_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 $host = $db_config->{host} or die 'No host in db_config';
|
||||||
my $username = $db_config->{username} or die 'No username 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 $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 =
|
my $dsn =
|
||||||
"dbi:Pg:"
|
"dbi:Pg:"
|
||||||
. ( defined $dbname ? "dbname=$dbname;" : "" )
|
. ( defined $dbname ? "dbname=$dbname;" : "" )
|
||||||
@ -83,7 +95,9 @@ my @migrations = (
|
|||||||
. ( defined $port ? "port=$port;" : "" );
|
. ( defined $port ? "port=$port;" : "" );
|
||||||
|
|
||||||
my $dbh = DBI->connect_cached(
|
my $dbh = DBI->connect_cached(
|
||||||
$dsn, $username, $password,
|
$dsn,
|
||||||
|
$username,
|
||||||
|
$password,
|
||||||
{
|
{
|
||||||
AutoCommit => 1,
|
AutoCommit => 1,
|
||||||
RaiseError => 1,
|
RaiseError => 1,
|
||||||
@ -103,24 +117,19 @@ my @migrations = (
|
|||||||
sub run_migrations {
|
sub run_migrations {
|
||||||
my $dbh = shift;
|
my $dbh = shift;
|
||||||
my $current_migration = _get_current_migration_number($dbh);
|
my $current_migration = _get_current_migration_number($dbh);
|
||||||
say $current_migration;
|
local $dbh->{RaiseError} = 0;
|
||||||
if ( $current_migration < scalar @migrations ) {
|
if ( $current_migration < scalar @migrations ) {
|
||||||
my @needed_migrations =
|
my @needed_migrations =
|
||||||
@migrations[ $current_migration .. $#migrations ];
|
@migrations[ $current_migration .. $#migrations ];
|
||||||
for my $migration (@needed_migrations) {
|
for my $migration (@needed_migrations) {
|
||||||
$dbh->do($migration);
|
$dbh->do($migration);
|
||||||
if (
|
my $update_result =
|
||||||
!(
|
$dbh->do( 'UPDATE options SET value = ? WHERE key = \'migration\';',
|
||||||
0 + $dbh->do(
|
undef, ++$current_migration );
|
||||||
'UPDATE options SET value = ? WHERE key = "migration"',
|
|
||||||
undef,
|
unless ( defined $update_result ) {
|
||||||
++$current_migration
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
{
|
|
||||||
$dbh->do(
|
$dbh->do(
|
||||||
'INSERT INTO options (key, value) VALUES ("migration", ?)',
|
'INSERT INTO options (key, value) VALUES (\"migration\", ?)',
|
||||||
undef, $current_migration
|
undef, $current_migration
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user