Add 'tenant' table to the DB

Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/5e959d75
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/5e959d75
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/5e959d75

Branch: refs/heads/master
Commit: 5e959d753f4f73fb5471120f36d23d84f56813be
Parents: dc26feb
Author: nir-sopher <nirsop...@gmail.com>
Authored: Wed Mar 15 15:42:50 2017 +0200
Committer: Jeremy Mitchell <mitchell...@gmail.com>
Committed: Sun Mar 19 19:08:28 2017 -0600

----------------------------------------------------------------------
 .../migrations/20170315000000_basic_tenancy.sql |  38 +++++
 traffic_ops/app/lib/Schema/Result/Tenant.pm     | 157 +++++++++++++++++++
 2 files changed, 195 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/5e959d75/traffic_ops/app/db/migrations/20170315000000_basic_tenancy.sql
----------------------------------------------------------------------
diff --git a/traffic_ops/app/db/migrations/20170315000000_basic_tenancy.sql 
b/traffic_ops/app/db/migrations/20170315000000_basic_tenancy.sql
new file mode 100644
index 0000000..adfae5b
--- /dev/null
+++ b/traffic_ops/app/db/migrations/20170315000000_basic_tenancy.sql
@@ -0,0 +1,38 @@
+/*
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+        http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+*/
+
+-- +goose Up
+-- SQL in section 'Up' is executed when this migration is applied
+
+
+-- tenant
+CREATE TABLE tenant (
+    id BIGSERIAL primary key NOT NULL,
+    name text UNIQUE NOT NULL,
+    active boolean NOT NULL DEFAULT false,
+    parent_id bigint DEFAULT 1 CHECK (id != parent_id),
+    CONSTRAINT fk_parentid FOREIGN KEY (parent_id) REFERENCES tenant(id),    
+    last_updated timestamp with time zone DEFAULT now()
+); 
+CREATE INDEX idx_k_tenant_parent_tenant_idx ON tenant USING btree (parent_id);
+
+CREATE TRIGGER on_update_current_timestamp BEFORE UPDATE ON tenant FOR EACH 
ROW EXECUTE PROCEDURE on_update_current_timestamp_last_updated();
+
+-- +goose Down
+-- SQL section 'Down' is executed when this migration is rolled back
+
+DROP TRIGGER on_update_current_timestamp ON tenant;
+
+DROP TABLE tenant;

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/5e959d75/traffic_ops/app/lib/Schema/Result/Tenant.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/app/lib/Schema/Result/Tenant.pm 
b/traffic_ops/app/lib/Schema/Result/Tenant.pm
new file mode 100644
index 0000000..dd35a08
--- /dev/null
+++ b/traffic_ops/app/lib/Schema/Result/Tenant.pm
@@ -0,0 +1,157 @@
+use utf8;
+package Schema::Result::Tenant;
+
+# Created by DBIx::Class::Schema::Loader
+# DO NOT MODIFY THE FIRST PART OF THIS FILE
+
+=head1 NAME
+
+Schema::Result::Tenant
+
+=cut
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::Core';
+
+=head1 TABLE: C<tenant>
+
+=cut
+
+__PACKAGE__->table("tenant");
+
+=head1 ACCESSORS
+
+=head2 id
+
+  data_type: 'bigint'
+  is_auto_increment: 1
+  is_nullable: 0
+  sequence: 'tenant_id_seq'
+
+=head2 name
+
+  data_type: 'text'
+  is_nullable: 0
+
+=head2 active
+
+  data_type: 'boolean'
+  default_value: false
+  is_nullable: 0
+
+=head2 parent_id
+
+  data_type: 'bigint'
+  default_value: 1
+  is_foreign_key: 1
+  is_nullable: 1
+
+=head2 last_updated
+
+  data_type: 'timestamp with time zone'
+  default_value: current_timestamp
+  is_nullable: 1
+  original: {default_value => \"now()"}
+
+=cut
+
+__PACKAGE__->add_columns(
+  "id",
+  {
+    data_type         => "bigint",
+    is_auto_increment => 1,
+    is_nullable       => 0,
+    sequence          => "tenant_id_seq",
+  },
+  "name",
+  { data_type => "text", is_nullable => 0 },
+  "active",
+  { data_type => "boolean", default_value => \"false", is_nullable => 0 },
+  "parent_id",
+  {
+    data_type      => "bigint",
+    default_value  => 1,
+    is_foreign_key => 1,
+    is_nullable    => 1,
+  },
+  "last_updated",
+  {
+    data_type     => "timestamp with time zone",
+    default_value => \"current_timestamp",
+    is_nullable   => 1,
+    original      => { default_value => \"now()" },
+  },
+);
+
+=head1 PRIMARY KEY
+
+=over 4
+
+=item * L</id>
+
+=back
+
+=cut
+
+__PACKAGE__->set_primary_key("id");
+
+=head1 UNIQUE CONSTRAINTS
+
+=head2 C<tenant_name_key>
+
+=over 4
+
+=item * L</name>
+
+=back
+
+=cut
+
+__PACKAGE__->add_unique_constraint("tenant_name_key", ["name"]);
+
+=head1 RELATIONS
+
+=head2 parent
+
+Type: belongs_to
+
+Related object: L<Schema::Result::Tenant>
+
+=cut
+
+__PACKAGE__->belongs_to(
+  "parent",
+  "Schema::Result::Tenant",
+  { id => "parent_id" },
+  {
+    is_deferrable => 0,
+    join_type     => "LEFT",
+    on_delete     => "NO ACTION",
+    on_update     => "NO ACTION",
+  },
+);
+
+=head2 tenants
+
+Type: has_many
+
+Related object: L<Schema::Result::Tenant>
+
+=cut
+
+__PACKAGE__->has_many(
+  "tenants",
+  "Schema::Result::Tenant",
+  { "foreign.parent_id" => "self.id" },
+  { cascade_copy => 0, cascade_delete => 0 },
+);
+
+
+# Created by DBIx::Class::Schema::Loader v0.07046 @ 2017-03-13 09:41:00
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:a816RdXB0mjimo2LvnqX/Q
+
+
+# You can replace this text with custom code or comments, and it will be 
preserved on regeneration
+1;

Reply via email to