Merge pull request #15 from Pear-Trading/TBSliver/Deployment-Handler

Deployment Handler
This commit is contained in:
Tom Bloor 2017-07-21 18:33:01 +01:00 committed by GitHub
commit a6724f2495
11 changed files with 1906 additions and 6 deletions

View file

@ -17,3 +17,6 @@ requires 'DateTime';
requires 'DateTime::Format::Strptime', "1.73"; requires 'DateTime::Format::Strptime', "1.73";
requires 'DateTime::Format::SQLite'; requires 'DateTime::Format::SQLite';
requires 'Try::Tiny'; requires 'Try::Tiny';
requires 'MooX::Options::Actions';
requires 'Module::Runtime';
requires 'DBIx::Class::DeploymentHandler';

View file

@ -6,6 +6,8 @@ use warnings;
use base 'DBIx::Class::Schema'; use base 'DBIx::Class::Schema';
our $VERSION = 1;
__PACKAGE__->load_namespaces; __PACKAGE__->load_namespaces;
1; 1;

View file

@ -0,0 +1,262 @@
package Pear::LocalLoop::Schema::Script::DeploymentHandler;
use MooX::Options::Actions;
use Module::Runtime qw/ use_module /;
use DBIx::Class::DeploymentHandler;
=head1 NAME
My::DeploymentScript - Deploy script using DBIx::Class::DeploymentHandler
=head1 SYNOPSIS
use My::DeploymentScript;
My::DeploymentScript->new_with_options( schema_class => 'My::Schema' );
=head1 DESCRIPTION
This module is for deploying and maintaining the ddl files, created by
DBIx::Class::DeploymentHandler.
=head1 OPTIONS
These are the options available on the command line
=head2 --connection ( -c )
This option is the connection you wish to deploy against, for example:
"DBI:mysql:database=<db name>;hostname=<host>"
This option is then provided to the Schema's 'connect' function.
=cut
option connection => (
is => 'ro',
format => 's',
required => 0,
short => 'c',
doc => "The DBI connection string to use",
);
=head2 --username ( -u )
The username to use for connection to the database
=cut
option username => (
is => 'ro',
format => 's',
default => '',
short => 'u',
doc => "The username for the DB connection",
);
=head2 --password ( -p )
The password to use for connection to the database
=cut
option password => (
is => 'ro',
format => 's',
default => '',
short => 'p',
doc => "The password for the supplied user",
);
=head2 --force ( -f )
This option will force the action if required - for example when overwriting
the same version of ddl items.
=cut
option force => (
is => 'ro',
default => sub { 0 },
short => 'f',
doc => "Force the action if required",
);
=head2 --version ( -v )
This option allows you to select a specific verison for installing and generating
ddl.
=cut
option version => (
is => 'ro',
format => 'i',
default => sub { shift->dh->schema->schema_version },
short => 'v',
doc => "Version to use as target",
);
=head1 ATTRIBUTES
These are options which can be passed to the constructor of the script
=head2 schema_class
This is the class of the schema you would like to connect to. This is required
to be defined in the 'new_with_options' call.
=cut
has schema_class => (
is => 'ro',
required => 1,
);
=head2 schema
This is the connected schema. This uses the 'schema_class' attribute and
'connection' option to provide a DBIx::Class schema connection.
=cut
has schema => (
is => 'lazy',
builder => sub {
my $self = shift;
return use_module( $self->schema_class )->connect(
$self->connection,
$self->username,
$self->password,
);
},
);
=head2 script_directory
This is the directory where the DeploymentHandler scripts will be put for all
versions. Defaults to 'share/ddl'.
=cut
has script_directory => (
is => 'ro',
default => 'share/ddl',
);
=head2 databases
This is an arrayref of the database types to prepare ddl scripts for. This
defaults to returning the following:
[
'mysql',
]
=cut
has databases => (
is => 'ro',
default => sub { [ qw/ PostgreSQL SQLite / ] },
);
=head2 dh
This returns the actual DeploymentHandler, set up using the 'schema',
'script_directory', and 'database' attributes, and the 'force' option.
=cut
has dh => (
is => 'lazy',
builder => sub {
my ( $self ) = @_;
return DBIx::Class::DeploymentHandler->new({
schema => $self->schema,
force_overwrite => $self->force,
script_directory => $self->script_directory,
databases => $self->databases,
});
}
);
=head1 COMMANDS
These are the available commands
=head2 write_ddl
This will create the ddl files required to perform an upgrade.
=cut
sub cmd_write_ddl {
my ( $self ) = @_;
$self->dh->prepare_install;
my $v = $self->version;
if ( $v > 1 ) {
$self->dh->prepare_upgrade({
from_version => $v - 1,
to_version => $v,
});
}
}
=head2 install_dh
This will install the tables required to use deployment handler on your
database. Only for use on a pre-existing database.
=cut
sub cmd_install_dh {
my ( $self ) = @_;
$self->dh->install_version_storage;
$self->dh->add_database_version({
version => $self->version,
});
}
=head2 install
This command will install all the tables to the provided database
=cut
sub cmd_install {
my ( $self ) = @_;
$self->dh->install({
version => $self->version,
});
}
=head2 upgrade
This command will upgrade all tables to the latest versions
=cut
sub cmd_upgrade {
my ( $self ) = @_;
$self->dh->upgrade;
}
=head1 AUTHOR
Tom Bloor E<lt>t.bloor@shadowcat.co.ukE<gt>
=head1 SEE ALSO
* L<MooX::Options>
* L<DBIx::Class::DeploymentHandler>
* L<Module::Runtime>
=cut
1;

View file

@ -6,10 +6,6 @@ use warnings;
use FindBin qw/ $Bin /; use FindBin qw/ $Bin /;
use lib "$Bin/../lib"; use lib "$Bin/../lib";
use Pear::LocalLoop::Schema; use Pear::LocalLoop::Schema::Script::DeploymentHandler;
my @con = @ARGV; Pear::LocalLoop::Schema::Script::DeploymentHandler->new_with_actions(schema_class => 'Pear::LocalLoop::Schema');
my $schema = Pear::LocalLoop::Schema->connect(@con);
$schema->deploy;

View file

@ -0,0 +1,18 @@
--
-- Created by SQL::Translator::Producer::PostgreSQL
-- Created on Thu Jul 20 17:05:36 2017
--
;
--
-- Table: dbix_class_deploymenthandler_versions
--
CREATE TABLE "dbix_class_deploymenthandler_versions" (
"id" serial NOT NULL,
"version" character varying(50) NOT NULL,
"ddl" text,
"upgrade_sql" text,
PRIMARY KEY ("id"),
CONSTRAINT "dbix_class_deploymenthandler_versions_version" UNIQUE ("version")
);
;

View file

@ -0,0 +1,226 @@
--
-- Created by SQL::Translator::Producer::PostgreSQL
-- Created on Thu Jul 20 17:05:36 2017
--
;
--
-- Table: account_tokens
--
CREATE TABLE "account_tokens" (
"id" serial NOT NULL,
"name" text NOT NULL,
"used" integer DEFAULT 0 NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "account_tokens_name" UNIQUE ("name")
);
;
--
-- Table: customers
--
CREATE TABLE "customers" (
"id" serial NOT NULL,
"display_name" character varying(255) NOT NULL,
"full_name" character varying(255) NOT NULL,
"year_of_birth" integer NOT NULL,
"postcode" character varying(16) NOT NULL,
PRIMARY KEY ("id")
);
;
--
-- Table: leaderboards
--
CREATE TABLE "leaderboards" (
"id" serial NOT NULL,
"name" character varying(255) NOT NULL,
"type" character varying(255) NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "leaderboards_type" UNIQUE ("type")
);
;
--
-- Table: organisations
--
CREATE TABLE "organisations" (
"id" serial NOT NULL,
"name" character varying(255) NOT NULL,
"street_name" text,
"town" character varying(255) NOT NULL,
"postcode" character varying(16),
PRIMARY KEY ("id")
);
;
--
-- Table: leaderboard_sets
--
CREATE TABLE "leaderboard_sets" (
"id" serial NOT NULL,
"leaderboard_id" integer NOT NULL,
"date" timestamp NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "leaderboard_sets_idx_leaderboard_id" on "leaderboard_sets" ("leaderboard_id");
;
--
-- Table: users
--
CREATE TABLE "users" (
"id" serial NOT NULL,
"customer_id" integer,
"organisation_id" integer,
"email" text NOT NULL,
"join_date" timestamp NOT NULL,
"password" character varying(100) NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "users_customer_id" UNIQUE ("customer_id"),
CONSTRAINT "users_email" UNIQUE ("email"),
CONSTRAINT "users_organisation_id" UNIQUE ("organisation_id")
);
CREATE INDEX "users_idx_customer_id" on "users" ("customer_id");
CREATE INDEX "users_idx_organisation_id" on "users" ("organisation_id");
;
--
-- Table: administrators
--
CREATE TABLE "administrators" (
"user_id" integer NOT NULL,
PRIMARY KEY ("user_id")
);
;
--
-- Table: pending_organisations
--
CREATE TABLE "pending_organisations" (
"id" serial NOT NULL,
"name" character varying(255) NOT NULL,
"street_name" text,
"town" character varying(255) NOT NULL,
"postcode" character varying(16),
"submitted_by_id" integer NOT NULL,
"submitted_at" timestamp NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "pending_organisations_idx_submitted_by_id" on "pending_organisations" ("submitted_by_id");
;
--
-- Table: session_tokens
--
CREATE TABLE "session_tokens" (
"id" serial NOT NULL,
"token" character varying(255) NOT NULL,
"user_id" integer NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "session_tokens_token" UNIQUE ("token")
);
CREATE INDEX "session_tokens_idx_user_id" on "session_tokens" ("user_id");
;
--
-- Table: transactions
--
CREATE TABLE "transactions" (
"id" serial NOT NULL,
"buyer_id" integer NOT NULL,
"seller_id" integer NOT NULL,
"value" numeric(16,2) NOT NULL,
"proof_image" text NOT NULL,
"submitted_at" timestamp NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "transactions_idx_buyer_id" on "transactions" ("buyer_id");
CREATE INDEX "transactions_idx_seller_id" on "transactions" ("seller_id");
;
--
-- Table: pending_transactions
--
CREATE TABLE "pending_transactions" (
"id" serial NOT NULL,
"buyer_id" integer NOT NULL,
"seller_id" integer NOT NULL,
"value" numeric(16,2) NOT NULL,
"proof_image" text NOT NULL,
"submitted_at" timestamp NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "pending_transactions_idx_buyer_id" on "pending_transactions" ("buyer_id");
CREATE INDEX "pending_transactions_idx_seller_id" on "pending_transactions" ("seller_id");
;
--
-- Table: leaderboard_values
--
CREATE TABLE "leaderboard_values" (
"id" serial NOT NULL,
"user_id" integer NOT NULL,
"set_id" integer NOT NULL,
"position" integer NOT NULL,
"value" numeric(16,2) NOT NULL,
"trend" integer DEFAULT 0 NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "leaderboard_values_user_id_set_id" UNIQUE ("user_id", "set_id")
);
CREATE INDEX "leaderboard_values_idx_set_id" on "leaderboard_values" ("set_id");
CREATE INDEX "leaderboard_values_idx_user_id" on "leaderboard_values" ("user_id");
;
--
-- Foreign Key Definitions
--
;
ALTER TABLE "leaderboard_sets" ADD CONSTRAINT "leaderboard_sets_fk_leaderboard_id" FOREIGN KEY ("leaderboard_id")
REFERENCES "leaderboards" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "users" ADD CONSTRAINT "users_fk_customer_id" FOREIGN KEY ("customer_id")
REFERENCES "customers" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "users" ADD CONSTRAINT "users_fk_organisation_id" FOREIGN KEY ("organisation_id")
REFERENCES "organisations" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "administrators" ADD CONSTRAINT "administrators_fk_user_id" FOREIGN KEY ("user_id")
REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "pending_organisations" ADD CONSTRAINT "pending_organisations_fk_submitted_by_id" FOREIGN KEY ("submitted_by_id")
REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "session_tokens" ADD CONSTRAINT "session_tokens_fk_user_id" FOREIGN KEY ("user_id")
REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "transactions" ADD CONSTRAINT "transactions_fk_buyer_id" FOREIGN KEY ("buyer_id")
REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "transactions" ADD CONSTRAINT "transactions_fk_seller_id" FOREIGN KEY ("seller_id")
REFERENCES "organisations" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "pending_transactions" ADD CONSTRAINT "pending_transactions_fk_buyer_id" FOREIGN KEY ("buyer_id")
REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "pending_transactions" ADD CONSTRAINT "pending_transactions_fk_seller_id" FOREIGN KEY ("seller_id")
REFERENCES "pending_organisations" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "leaderboard_values" ADD CONSTRAINT "leaderboard_values_fk_set_id" FOREIGN KEY ("set_id")
REFERENCES "leaderboard_sets" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;
ALTER TABLE "leaderboard_values" ADD CONSTRAINT "leaderboard_values_fk_user_id" FOREIGN KEY ("user_id")
REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
;

View file

@ -0,0 +1,18 @@
--
-- Created by SQL::Translator::Producer::SQLite
-- Created on Thu Jul 20 17:05:36 2017
--
;
BEGIN TRANSACTION;
--
-- Table: dbix_class_deploymenthandler_versions
--
CREATE TABLE dbix_class_deploymenthandler_versions (
id INTEGER PRIMARY KEY NOT NULL,
version varchar(50) NOT NULL,
ddl text,
upgrade_sql text
);
CREATE UNIQUE INDEX dbix_class_deploymenthandler_versions_version ON dbix_class_deploymenthandler_versions (version);
COMMIT;

View file

@ -0,0 +1,152 @@
--
-- Created by SQL::Translator::Producer::SQLite
-- Created on Thu Jul 20 17:05:36 2017
--
;
BEGIN TRANSACTION;
--
-- Table: account_tokens
--
CREATE TABLE account_tokens (
id INTEGER PRIMARY KEY NOT NULL,
name text NOT NULL,
used integer NOT NULL DEFAULT 0
);
CREATE UNIQUE INDEX account_tokens_name ON account_tokens (name);
--
-- Table: customers
--
CREATE TABLE customers (
id INTEGER PRIMARY KEY NOT NULL,
display_name varchar(255) NOT NULL,
full_name varchar(255) NOT NULL,
year_of_birth integer NOT NULL,
postcode varchar(16) NOT NULL
);
--
-- Table: leaderboards
--
CREATE TABLE leaderboards (
id INTEGER PRIMARY KEY NOT NULL,
name varchar(255) NOT NULL,
type varchar(255) NOT NULL
);
CREATE UNIQUE INDEX leaderboards_type ON leaderboards (type);
--
-- Table: organisations
--
CREATE TABLE organisations (
id INTEGER PRIMARY KEY NOT NULL,
name varchar(255) NOT NULL,
street_name text,
town varchar(255) NOT NULL,
postcode varchar(16)
);
--
-- Table: leaderboard_sets
--
CREATE TABLE leaderboard_sets (
id INTEGER PRIMARY KEY NOT NULL,
leaderboard_id integer NOT NULL,
date datetime NOT NULL,
FOREIGN KEY (leaderboard_id) REFERENCES leaderboards(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX leaderboard_sets_idx_leaderboard_id ON leaderboard_sets (leaderboard_id);
--
-- Table: users
--
CREATE TABLE users (
id INTEGER PRIMARY KEY NOT NULL,
customer_id integer,
organisation_id integer,
email text NOT NULL,
join_date datetime NOT NULL,
password varchar(100) NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE NO ACTION ON UPDATE NO ACTION,
FOREIGN KEY (organisation_id) REFERENCES organisations(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX users_idx_customer_id ON users (customer_id);
CREATE INDEX users_idx_organisation_id ON users (organisation_id);
CREATE UNIQUE INDEX users_customer_id ON users (customer_id);
CREATE UNIQUE INDEX users_email ON users (email);
CREATE UNIQUE INDEX users_organisation_id ON users (organisation_id);
--
-- Table: administrators
--
CREATE TABLE administrators (
user_id INTEGER PRIMARY KEY NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
--
-- Table: pending_organisations
--
CREATE TABLE pending_organisations (
id INTEGER PRIMARY KEY NOT NULL,
name varchar(255) NOT NULL,
street_name text,
town varchar(255) NOT NULL,
postcode varchar(16),
submitted_by_id integer NOT NULL,
submitted_at datetime NOT NULL,
FOREIGN KEY (submitted_by_id) REFERENCES users(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX pending_organisations_idx_submitted_by_id ON pending_organisations (submitted_by_id);
--
-- Table: session_tokens
--
CREATE TABLE session_tokens (
id INTEGER PRIMARY KEY NOT NULL,
token varchar(255) NOT NULL,
user_id integer NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX session_tokens_idx_user_id ON session_tokens (user_id);
CREATE UNIQUE INDEX session_tokens_token ON session_tokens (token);
--
-- Table: transactions
--
CREATE TABLE transactions (
id INTEGER PRIMARY KEY NOT NULL,
buyer_id integer NOT NULL,
seller_id integer NOT NULL,
value decimal(16,2) NOT NULL,
proof_image text NOT NULL,
submitted_at datetime NOT NULL,
FOREIGN KEY (buyer_id) REFERENCES users(id) ON DELETE NO ACTION ON UPDATE NO ACTION,
FOREIGN KEY (seller_id) REFERENCES organisations(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX transactions_idx_buyer_id ON transactions (buyer_id);
CREATE INDEX transactions_idx_seller_id ON transactions (seller_id);
--
-- Table: pending_transactions
--
CREATE TABLE pending_transactions (
id INTEGER PRIMARY KEY NOT NULL,
buyer_id integer NOT NULL,
seller_id integer NOT NULL,
value decimal(16,2) NOT NULL,
proof_image text NOT NULL,
submitted_at datetime NOT NULL,
FOREIGN KEY (buyer_id) REFERENCES users(id) ON DELETE NO ACTION ON UPDATE NO ACTION,
FOREIGN KEY (seller_id) REFERENCES pending_organisations(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX pending_transactions_idx_buyer_id ON pending_transactions (buyer_id);
CREATE INDEX pending_transactions_idx_seller_id ON pending_transactions (seller_id);
--
-- Table: leaderboard_values
--
CREATE TABLE leaderboard_values (
id INTEGER PRIMARY KEY NOT NULL,
user_id integer NOT NULL,
set_id integer NOT NULL,
position integer NOT NULL,
value decimal(16,2) NOT NULL,
trend integer NOT NULL DEFAULT 0,
FOREIGN KEY (set_id) REFERENCES leaderboard_sets(id) ON DELETE NO ACTION ON UPDATE NO ACTION,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX leaderboard_values_idx_set_id ON leaderboard_values (set_id);
CREATE INDEX leaderboard_values_idx_user_id ON leaderboard_values (user_id);
CREATE UNIQUE INDEX leaderboard_values_user_id_set_id ON leaderboard_values (user_id, set_id);
COMMIT;

View file

@ -0,0 +1,24 @@
#! perl
use strict;
use warnings;
use DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator::ScriptHelpers
'schema_from_schema_loader';
schema_from_schema_loader({ naming => 'v7' }, sub {
my $schema = shift;
$schema->resultset('Leaderboard')->populate([
[ qw/ name type / ],
[ 'Daily Total', 'daily_total' ],
[ 'Daily Count', 'daily_count' ],
[ 'Weekly Total', 'weekly_total' ],
[ 'Weekly Count', 'weekly_count' ],
[ 'Monthly Total', 'monthly_total' ],
[ 'Monthly Count', 'monthly_count' ],
[ 'All Time Total', 'all_time_total' ],
[ 'All Time Count', 'all_time_count' ],
]);
});

View file

@ -0,0 +1,91 @@
---
schema:
procedures: {}
tables:
dbix_class_deploymenthandler_versions:
constraints:
- deferrable: 1
expression: ''
fields:
- id
match_type: ''
name: ''
on_delete: ''
on_update: ''
options: []
reference_fields: []
reference_table: ''
type: PRIMARY KEY
- deferrable: 1
expression: ''
fields:
- version
match_type: ''
name: dbix_class_deploymenthandler_versions_version
on_delete: ''
on_update: ''
options: []
reference_fields: []
reference_table: ''
type: UNIQUE
fields:
ddl:
data_type: text
default_value: ~
is_nullable: 1
is_primary_key: 0
is_unique: 0
name: ddl
order: 3
size:
- 0
id:
data_type: int
default_value: ~
is_auto_increment: 1
is_nullable: 0
is_primary_key: 1
is_unique: 0
name: id
order: 1
size:
- 0
upgrade_sql:
data_type: text
default_value: ~
is_nullable: 1
is_primary_key: 0
is_unique: 0
name: upgrade_sql
order: 4
size:
- 0
version:
data_type: varchar
default_value: ~
is_nullable: 0
is_primary_key: 0
is_unique: 1
name: version
order: 2
size:
- 50
indices: []
name: dbix_class_deploymenthandler_versions
options: []
order: 1
triggers: {}
views: {}
translator:
add_drop_table: 0
filename: ~
no_comments: 0
parser_args:
sources:
- __VERSION
parser_type: SQL::Translator::Parser::DBIx::Class
producer_args: {}
producer_type: SQL::Translator::Producer::YAML
show_warnings: 0
trace: 0
version: 0.11021

File diff suppressed because it is too large Load diff