Difference between revisions of "Talk:Fgdb tables"

From FreekiWiki
Jump to navigation Jump to search
 
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
CREATE TABLE contact_method_types (
+
== Useful SQL snippets ==
    parent_id integer,
+
Here is where to put things that might be useful to cut and paste into scripts that could be modified as needed for ad hoc reporting.
CREATE TABLE contact_methods (
+
 
    contact_method_type_id integer,
+
=== Count of volunteers worked over time ===
    contact_id integer,
+
SELECT
CREATE TABLE contact_types (
+
    EXTRACT( YEAR FROM date_performed ) AS year,
CREATE TABLE contact_types_contacts (
+
    EXTRACT( MONTH FROM date_performed ) AS month,
    contact_id integer DEFAULT 0 NOT NULL,
+
    COUNT( DISTINCT volunteer_tasks.contact_id ) AS count
    contact_type_id integer DEFAULT 0 NOT NULL
+
  FROM volunteer_tasks
CREATE TABLE contacts (
+
  WHERE
CREATE TABLE "defaults" (
+
    volunteer_tasks.duration > 0
CREATE TABLE discount_schedules (
+
  GROUP BY 1, 2
CREATE TABLE discount_schedules_gizmo_types (
+
  ORDER BY 1, 2 ;
    gizmo_type_id integer NOT NULL,
+
 
    discount_schedule_id integer NOT NULL,
+
* same but for a given period of time
CREATE TABLE dispersement_types (
+
SELECT
CREATE TABLE dispersements (
+
    COUNT( DISTINCT volunteer_tasks.contact_id ) AS count
    contact_id integer NOT NULL,
+
  FROM volunteer_tasks
    dispersement_type_id integer NOT NULL,
+
  WHERE
CREATE TABLE donations (
+
    volunteer_tasks.duration > 0 AND
    contact_id integer,
+
    volunteer_tasks.date_performed BETWEEN '2007-01-01' AND now()::date ;
CREATE TABLE engine_schema_info (
+
 
CREATE TABLE gizmo_attrs (
+
 
CREATE TABLE gizmo_contexts (
+
=== Amount of volunteer hours worked over time ===
CREATE TABLE gizmo_contexts_gizmo_typeattrs (
+
SELECT
    gizmo_context_id integer NOT NULL,
+
    EXTRACT( YEAR FROM date_performed ) AS year,
    gizmo_typeattr_id integer NOT NULL,
+
    EXTRACT( MONTH FROM date_performed ) AS month,
CREATE TABLE gizmo_contexts_gizmo_types (
+
    SUM(volunteer_tasks.duration) AS hours_worked
    gizmo_context_id integer NOT NULL,
+
  FROM volunteer_tasks
    gizmo_type_id integer NOT NULL,
+
  WHERE
CREATE TABLE gizmo_events (
+
    volunteer_tasks.duration > 0
    donation_id integer,
+
  GROUP BY 1, 2
    sale_id integer,
+
  ORDER BY 1, 2 ;
    dispersement_id integer,
+
 
    recycling_id integer,
+
=== All volunteers that have accumulated more than X hours ===
    gizmo_type_id integer NOT NULL,
+
 
    gizmo_context_id integer NOT NULL,
+
SELECT
CREATE TABLE gizmo_events_gizmo_typeattrs (
+
    volunteer_tasks.contact_id,
    gizmo_event_id integer NOT NULL,
+
    contacts.first_name,
    gizmo_typeattr_id integer NOT NULL,
+
    contacts.surname,
CREATE TABLE gizmo_typeattrs (
+
    SUM(volunteer_tasks.duration) AS hours_worked,
    gizmo_type_id integer NOT NULL,
+
    MAX( volunteer_tasks.date_performed )
    gizmo_attr_id integer NOT NULL,
+
  FROM volunteer_tasks
CREATE TABLE gizmo_types (
+
  LEFT JOIN contacts ON volunteer_tasks.contact_id = contacts.id
    parent_id integer,
+
  WHERE
CREATE TABLE payment_methods (
+
    volunteer_tasks.duration > 0
CREATE TABLE payments (
+
  GROUP BY
    donation_id integer,
+
    volunteer_tasks.contact_id,
    sale_id integer,
+
    contacts.first_name,
    payment_method_id integer NOT NULL,
+
    contacts.surname
CREATE TABLE recyclings (
+
  HAVING SUM(volunteer_tasks.duration) > 1000
CREATE TABLE relationship_types (
+
  ORDER BY 4 DESC ;
CREATE TABLE relationships (
+
 
    source_id integer,
+
=== Still needed ===
    sink_id integer,
+
* Number of computer received in a given month (or another specific time period)
    relationship_type_id integer,
+
** same but over time, each month or each year
CREATE TABLE rfs_high_sales (
+
* Amount of volunteer hours worked by job in a given month (or another specific time period)
    contact_id integer,
+
** same but over time, each month or each year
CREATE TABLE rfs_high_sales_summary (
+
 
    contact_id integer,
+
== Basic SQL for table creation ==
CREATE TABLE rfs_sales_violations (
+
CREATE TABLE contact_method_types (
    contact_id integer,
+
    id serial NOT NULL,
CREATE TABLE rfs_sales_violations_summary (
+
    description character varying(100),
    contact_id integer,
+
    parent_id integer,
CREATE TABLE rfs_total_discount_taken (
+
    lock_version integer DEFAULT 0 NOT NULL,
    contact_id integer,
+
    updated_at timestamp with time zone DEFAULT now(),
CREATE TABLE rfs_volunteer_hours_worked (
+
    created_at timestamp with time zone DEFAULT now(),
    contact_id integer,
+
    created_by bigint DEFAULT 1 NOT NULL,
CREATE TABLE sales (
+
    updated_by bigint DEFAULT 1 NOT NULL
    contact_id integer,
+
);
    discount_schedule_id integer,
+
CREATE TABLE schema_info (
+
COMMENT ON TABLE contact_method_types IS 'types of ways we can contact someone, i.e. phone, email website, online chat name -- not physical address';
CREATE TABLE volunteer_hours_worked (
+
    contact_id integer,
+
CREATE TABLE contact_methods (
CREATE TABLE volunteer_task_types (
+
    id serial NOT NULL,
    parent_id integer,
+
    contact_method_type_id integer,
CREATE TABLE volunteer_task_types_volunteer_tasks (
+
    description character varying(100),
    volunteer_task_id integer NOT NULL,
+
    ok boolean,
    volunteer_task_type_id integer NOT NULL
+
    contact_id integer,
CREATE TABLE volunteer_tasks (
+
    lock_version integer DEFAULT 0 NOT NULL,
    contact_id integer,
+
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
COMMENT ON TABLE contact_methods IS 'actual ways a specific contact can be contacted (i.e. phone, email, etc.) note: not physical address';
 +
 +
CREATE TABLE contact_types (
 +
    id serial NOT NULL,
 +
    description character varying(100),
 +
    for_who character varying(3) DEFAULT 'any'::character varying,
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
COMMENT ON TABLE contact_types IS 'types of contacts we track, for instance media contact, member, organization';
 +
 +
CREATE TABLE contact_types_contacts (
 +
    contact_id integer DEFAULT 0 NOT NULL,
 +
    contact_type_id integer DEFAULT 0 NOT NULL
 +
);
 +
 +
CREATE TABLE contacts (
 +
    id serial NOT NULL,
 +
    is_organization boolean DEFAULT false,
 +
    sort_name character varying(25),
 +
    first_name character varying(25),
 +
    middle_name character varying(25),
 +
    surname character varying(50),
 +
    organization character varying(100),
 +
    extra_address character varying(52),
 +
    address character varying(52),
 +
    city character varying(30),
 +
    state_or_province character varying(15),
 +
    postal_code character varying(25),
 +
    country character varying(100),
 +
    notes text,
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
CREATE TABLE "defaults" (
 +
    id serial NOT NULL,
 +
    name character varying(100),
 +
    value character varying(100),
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp without time zone,
 +
    created_at timestamp without time zone,
 +
    created_by integer DEFAULT 1 NOT NULL,
 +
    updated_by integer DEFAULT 1 NOT NULL
 +
);
 +
 +
CREATE TABLE discount_schedules (
 +
    id serial NOT NULL,
 +
    name character varying(25),
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
COMMENT ON TABLE discount_schedules IS 'discount schedules';
 +
 +
CREATE TABLE discount_schedules_gizmo_types (
 +
    id serial NOT NULL,
 +
    gizmo_type_id integer NOT NULL,
 +
    discount_schedule_id integer NOT NULL,
 +
    multiplier numeric(10,3),
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
CREATE TABLE dispersement_types (
 +
    id serial NOT NULL,
 +
    description character varying(100),
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp without time zone,
 +
    created_at timestamp without time zone,
 +
    created_by integer DEFAULT 1 NOT NULL,
 +
    updated_by integer DEFAULT 1 NOT NULL
 +
);
 +
 +
CREATE TABLE dispersements (
 +
    id serial NOT NULL,
 +
    comments text,
 +
    contact_id integer NOT NULL,
 +
    dispersement_type_id integer NOT NULL,
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp without time zone,
 +
    created_at timestamp without time zone,
 +
    created_by integer DEFAULT 1 NOT NULL,
 +
    updated_by integer DEFAULT 1 NOT NULL,
 +
    dispersed_at timestamp without time zone NOT NULL
 +
);
 +
 +
CREATE TABLE donations (
 +
    id serial NOT NULL,
 +
    contact_id integer,
 +
    postal_code character varying(25),
 +
    reported_required_fee numeric(10,2) DEFAULT 0.0,
 +
    reported_suggested_fee numeric(10,2) DEFAULT 0.0,
 +
    txn_complete boolean DEFAULT true,
 +
    txn_completed_at timestamp with time zone DEFAULT now(),
 +
    comments text,
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
CREATE TABLE engine_schema_info (
 +
    engine_name character varying(255),
 +
    version integer
 +
);
 +
 +
CREATE TABLE gizmo_attrs (
 +
    id serial NOT NULL,
 +
    name character varying(100),
 +
    datatype character varying(10),
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
CREATE TABLE gizmo_contexts (
 +
    id serial NOT NULL,
 +
    name character varying(100),
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
CREATE TABLE gizmo_contexts_gizmo_typeattrs (
 +
    gizmo_context_id integer NOT NULL,
 +
    gizmo_typeattr_id integer NOT NULL,
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
CREATE TABLE gizmo_contexts_gizmo_types (
 +
    gizmo_context_id integer NOT NULL,
 +
    gizmo_type_id integer NOT NULL,
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
CREATE TABLE gizmo_events (
 +
    id serial NOT NULL,
 +
    donation_id integer,
 +
    sale_id integer,
 +
    dispersement_id integer,
 +
    recycling_id integer,
 +
    gizmo_type_id integer NOT NULL,
 +
    gizmo_context_id integer NOT NULL,
 +
    gizmo_count integer NOT NULL,
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
CREATE TABLE gizmo_events_gizmo_typeattrs (
 +
    id serial NOT NULL,
 +
    gizmo_event_id integer NOT NULL,
 +
    gizmo_typeattr_id integer NOT NULL,
 +
    attr_val_text text,
 +
    attr_val_boolean boolean,
 +
    attr_val_integer integer,
 +
    attr_val_monetary numeric(10,2),
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
CREATE TABLE gizmo_typeattrs (
 +
    id serial NOT NULL,
 +
    gizmo_type_id integer NOT NULL,
 +
    gizmo_attr_id integer NOT NULL,
 +
    is_required boolean DEFAULT true NOT NULL,
 +
    validation_callback text,
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
CREATE TABLE gizmo_types (
 +
    id serial NOT NULL,
 +
    description character varying(100),
 +
    parent_id integer,
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL,
 +
    required_fee numeric(10,2) DEFAULT 0.0,
 +
    suggested_fee numeric(10,2) DEFAULT 0.0
 +
);
 +
 +
CREATE TABLE payment_methods (
 +
    id serial NOT NULL,
 +
    description character varying(100),
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
 +
CREATE TABLE payments (
 +
    id serial NOT NULL,
 +
    donation_id integer,
 +
    sale_id integer,
 +
    amount numeric(10,2) DEFAULT 0.0 NOT NULL,
 +
    payment_method_id integer NOT NULL,
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
CREATE TABLE recyclings (
 +
    id serial NOT NULL,
 +
    comments text,
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp without time zone,
 +
    created_at timestamp without time zone,
 +
    created_by integer DEFAULT 1 NOT NULL,
 +
    updated_by integer DEFAULT 1 NOT NULL,
 +
    recycled_at timestamp without time zone NOT NULL
 +
);
 +
 +
CREATE TABLE sales (
 +
    id serial NOT NULL,
 +
    contact_id integer,
 +
    postal_code character varying(25),
 +
    reported_discount_amount numeric(10,2) DEFAULT 0.0,
 +
    reported_amount_due numeric(10,2) DEFAULT 0.0 NOT NULL,
 +
    txn_complete boolean DEFAULT true,
 +
    txn_completed_at timestamp with time zone DEFAULT now(),
 +
    discount_schedule_id integer,
 +
    comments text,
 +
    bulk boolean,
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
COMMENT ON TABLE sales IS 'each record represents one sales transaction';
 +
 +
CREATE TABLE schema_info (
 +
    version integer
 +
);
 +
 +
CREATE TABLE volunteer_hours_worked (
 +
    contact_id integer,
 +
    hours_worked numeric
 +
);
 +
 +
CREATE TABLE volunteer_task_types (
 +
    id serial NOT NULL,
 +
    description character varying(100),
 +
    parent_id integer,
 +
    hours_multiplier numeric(10,3) DEFAULT 1.0 NOT NULL,
 +
    instantiable boolean DEFAULT true NOT NULL,
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL,
 +
    required boolean DEFAULT true NOT NULL
 +
);
 +
 +
CREATE TABLE volunteer_task_types_volunteer_tasks (
 +
    volunteer_task_id integer NOT NULL,
 +
    volunteer_task_type_id integer NOT NULL
 +
);
 +
 +
CREATE TABLE volunteer_tasks (
 +
    id serial NOT NULL,
 +
    contact_id integer,
 +
    date_performed date DEFAULT now(),
 +
    duration numeric(5,2) DEFAULT 0.00 NOT NULL,
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 
 +
== Other tables ==
 +
Some tables exist that could be dropped. Short lived tables include those that start with rfs_
 +
 
 +
Also, I am wondering about this:
 +
 
 +
CREATE TABLE relationship_types (
 +
    id serial NOT NULL,
 +
    description character varying(100),
 +
    direction_matters boolean,
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
COMMENT ON TABLE relationship_types IS 'abstract type of relationships that can exist between contacts, i.e. sibling, parent-child, spouse, employer-employee';
 +
 +
CREATE TABLE relationships (
 +
    id serial NOT NULL,
 +
    source_id integer,
 +
    sink_id integer,
 +
    flow integer,
 +
    relationship_type_id integer,
 +
    lock_version integer DEFAULT 0 NOT NULL,
 +
    updated_at timestamp with time zone DEFAULT now(),
 +
    created_at timestamp with time zone DEFAULT now(),
 +
    created_by bigint DEFAULT 1 NOT NULL,
 +
    updated_by bigint DEFAULT 1 NOT NULL
 +
);
 +
 +
COMMENT ON TABLE relationships IS 'actual relationship between two contacts';
 +
 +
 
 +
Probably should be dropped unless we intend to use them.
 +
 
 +
[[User:Rfs|RfS]] 17:34, 23 June 2007 (PDT)

Latest revision as of 09:58, 27 June 2007

Useful SQL snippets

Here is where to put things that might be useful to cut and paste into scripts that could be modified as needed for ad hoc reporting.

Count of volunteers worked over time

SELECT
   EXTRACT( YEAR FROM date_performed ) AS year,
   EXTRACT( MONTH FROM date_performed ) AS month,
   COUNT( DISTINCT volunteer_tasks.contact_id ) AS count
 FROM volunteer_tasks
 WHERE
   volunteer_tasks.duration > 0
 GROUP BY 1, 2
 ORDER BY 1, 2 ;
  • same but for a given period of time
SELECT
   COUNT( DISTINCT volunteer_tasks.contact_id ) AS count
 FROM volunteer_tasks
 WHERE
   volunteer_tasks.duration > 0 AND
   volunteer_tasks.date_performed BETWEEN '2007-01-01' AND now()::date ;


Amount of volunteer hours worked over time

SELECT
   EXTRACT( YEAR FROM date_performed ) AS year,
   EXTRACT( MONTH FROM date_performed ) AS month,
   SUM(volunteer_tasks.duration) AS hours_worked
 FROM volunteer_tasks
 WHERE
   volunteer_tasks.duration > 0
 GROUP BY 1, 2
 ORDER BY 1, 2 ;

All volunteers that have accumulated more than X hours

SELECT
   volunteer_tasks.contact_id,
   contacts.first_name,
   contacts.surname,
   SUM(volunteer_tasks.duration) AS hours_worked,
   MAX( volunteer_tasks.date_performed )
 FROM volunteer_tasks
 LEFT JOIN contacts ON volunteer_tasks.contact_id = contacts.id
 WHERE
   volunteer_tasks.duration > 0
 GROUP BY
   volunteer_tasks.contact_id,
   contacts.first_name,
   contacts.surname
 HAVING SUM(volunteer_tasks.duration) > 1000
 ORDER BY 4 DESC ;

Still needed

  • Number of computer received in a given month (or another specific time period)
    • same but over time, each month or each year
  • Amount of volunteer hours worked by job in a given month (or another specific time period)
    • same but over time, each month or each year

Basic SQL for table creation

CREATE TABLE contact_method_types (
    id serial NOT NULL,
    description character varying(100),
    parent_id integer,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

COMMENT ON TABLE contact_method_types IS 'types of ways we can contact someone, i.e. phone, email website, online chat name -- not physical address';

CREATE TABLE contact_methods (
    id serial NOT NULL,
    contact_method_type_id integer,
    description character varying(100),
    ok boolean,
    contact_id integer,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

COMMENT ON TABLE contact_methods IS 'actual ways a specific contact can be contacted (i.e. phone, email, etc.) note: not physical address';

CREATE TABLE contact_types (
    id serial NOT NULL,
    description character varying(100),
    for_who character varying(3) DEFAULT 'any'::character varying,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

COMMENT ON TABLE contact_types IS 'types of contacts we track, for instance media contact, member, organization';

CREATE TABLE contact_types_contacts (
    contact_id integer DEFAULT 0 NOT NULL,
    contact_type_id integer DEFAULT 0 NOT NULL
);

CREATE TABLE contacts (
    id serial NOT NULL,
    is_organization boolean DEFAULT false,
    sort_name character varying(25),
    first_name character varying(25),
    middle_name character varying(25),
    surname character varying(50),
    organization character varying(100),
    extra_address character varying(52),
    address character varying(52),
    city character varying(30),
    state_or_province character varying(15),
    postal_code character varying(25),
    country character varying(100),
    notes text,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

CREATE TABLE "defaults" (
    id serial NOT NULL,
    name character varying(100),
    value character varying(100),
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp without time zone,
    created_at timestamp without time zone,
    created_by integer DEFAULT 1 NOT NULL,
    updated_by integer DEFAULT 1 NOT NULL
);

CREATE TABLE discount_schedules (
    id serial NOT NULL,
    name character varying(25),
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

COMMENT ON TABLE discount_schedules IS 'discount schedules';

CREATE TABLE discount_schedules_gizmo_types (
    id serial NOT NULL,
    gizmo_type_id integer NOT NULL,
    discount_schedule_id integer NOT NULL,
    multiplier numeric(10,3),
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

CREATE TABLE dispersement_types (
    id serial NOT NULL,
    description character varying(100),
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp without time zone,
    created_at timestamp without time zone,
    created_by integer DEFAULT 1 NOT NULL,
    updated_by integer DEFAULT 1 NOT NULL
);

CREATE TABLE dispersements (
    id serial NOT NULL,
    comments text,
    contact_id integer NOT NULL,
    dispersement_type_id integer NOT NULL,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp without time zone,
    created_at timestamp without time zone,
    created_by integer DEFAULT 1 NOT NULL,
    updated_by integer DEFAULT 1 NOT NULL,
    dispersed_at timestamp without time zone NOT NULL
);

CREATE TABLE donations (
    id serial NOT NULL,
    contact_id integer,
    postal_code character varying(25),
    reported_required_fee numeric(10,2) DEFAULT 0.0,
    reported_suggested_fee numeric(10,2) DEFAULT 0.0,
    txn_complete boolean DEFAULT true,
    txn_completed_at timestamp with time zone DEFAULT now(),
    comments text,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

CREATE TABLE engine_schema_info (
    engine_name character varying(255),
    version integer
);

CREATE TABLE gizmo_attrs (
    id serial NOT NULL,
    name character varying(100),
    datatype character varying(10),
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

CREATE TABLE gizmo_contexts (
    id serial NOT NULL,
    name character varying(100),
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

CREATE TABLE gizmo_contexts_gizmo_typeattrs (
    gizmo_context_id integer NOT NULL,
    gizmo_typeattr_id integer NOT NULL,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

CREATE TABLE gizmo_contexts_gizmo_types (
    gizmo_context_id integer NOT NULL,
    gizmo_type_id integer NOT NULL,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

CREATE TABLE gizmo_events (
    id serial NOT NULL,
    donation_id integer,
    sale_id integer,
    dispersement_id integer,
    recycling_id integer,
    gizmo_type_id integer NOT NULL,
    gizmo_context_id integer NOT NULL,
    gizmo_count integer NOT NULL,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

CREATE TABLE gizmo_events_gizmo_typeattrs (
    id serial NOT NULL,
    gizmo_event_id integer NOT NULL,
    gizmo_typeattr_id integer NOT NULL,
    attr_val_text text,
    attr_val_boolean boolean,
    attr_val_integer integer,
    attr_val_monetary numeric(10,2),
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

CREATE TABLE gizmo_typeattrs (
    id serial NOT NULL,
    gizmo_type_id integer NOT NULL,
    gizmo_attr_id integer NOT NULL,
    is_required boolean DEFAULT true NOT NULL,
    validation_callback text,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

CREATE TABLE gizmo_types (
    id serial NOT NULL,
    description character varying(100),
    parent_id integer,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL,
    required_fee numeric(10,2) DEFAULT 0.0,
    suggested_fee numeric(10,2) DEFAULT 0.0
);

CREATE TABLE payment_methods (
    id serial NOT NULL,
    description character varying(100),
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);


CREATE TABLE payments (
    id serial NOT NULL,
    donation_id integer,
    sale_id integer,
    amount numeric(10,2) DEFAULT 0.0 NOT NULL,
    payment_method_id integer NOT NULL,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

CREATE TABLE recyclings (
    id serial NOT NULL,
    comments text,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp without time zone,
    created_at timestamp without time zone,
    created_by integer DEFAULT 1 NOT NULL,
    updated_by integer DEFAULT 1 NOT NULL,
    recycled_at timestamp without time zone NOT NULL
);

CREATE TABLE sales (
    id serial NOT NULL,
    contact_id integer,
    postal_code character varying(25),
    reported_discount_amount numeric(10,2) DEFAULT 0.0,
    reported_amount_due numeric(10,2) DEFAULT 0.0 NOT NULL,
    txn_complete boolean DEFAULT true,
    txn_completed_at timestamp with time zone DEFAULT now(),
    discount_schedule_id integer,
    comments text,
    bulk boolean,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

COMMENT ON TABLE sales IS 'each record represents one sales transaction';

CREATE TABLE schema_info (
    version integer
);

CREATE TABLE volunteer_hours_worked (
    contact_id integer,
    hours_worked numeric
);

CREATE TABLE volunteer_task_types (
    id serial NOT NULL,
    description character varying(100),
    parent_id integer,
    hours_multiplier numeric(10,3) DEFAULT 1.0 NOT NULL,
    instantiable boolean DEFAULT true NOT NULL,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL,
    required boolean DEFAULT true NOT NULL
);

CREATE TABLE volunteer_task_types_volunteer_tasks (
    volunteer_task_id integer NOT NULL,
    volunteer_task_type_id integer NOT NULL
);

CREATE TABLE volunteer_tasks (
    id serial NOT NULL,
    contact_id integer,
    date_performed date DEFAULT now(),
    duration numeric(5,2) DEFAULT 0.00 NOT NULL,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

Other tables

Some tables exist that could be dropped. Short lived tables include those that start with rfs_

Also, I am wondering about this:

CREATE TABLE relationship_types (
    id serial NOT NULL,
    description character varying(100),
    direction_matters boolean,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

COMMENT ON TABLE relationship_types IS 'abstract type of relationships that can exist between contacts, i.e. sibling, parent-child, spouse, employer-employee';

CREATE TABLE relationships (
    id serial NOT NULL,
    source_id integer,
    sink_id integer,
    flow integer,
    relationship_type_id integer,
    lock_version integer DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    created_by bigint DEFAULT 1 NOT NULL,
    updated_by bigint DEFAULT 1 NOT NULL
);

COMMENT ON TABLE relationships IS 'actual relationship between two contacts';

Probably should be dropped unless we intend to use them.

RfS 17:34, 23 June 2007 (PDT)