

-- /*******************************************************
-- *
-- * Clean up the exisiting tables
-- *
-- *******************************************************/
DROP TABLE IF EXISTS civicrm_contribution_product;
DROP TABLE IF EXISTS civicrm_contribution;
DROP TABLE IF EXISTS civicrm_mailing_event_unsubscribe;
DROP TABLE IF EXISTS civicrm_mailing_event_trackable_url_open;
DROP TABLE IF EXISTS civicrm_mailing_event_reply;
DROP TABLE IF EXISTS civicrm_mailing_event_opened;
DROP TABLE IF EXISTS civicrm_mailing_event_forward;
DROP TABLE IF EXISTS civicrm_mailing_event_delivered;
DROP TABLE IF EXISTS civicrm_mailing_event_bounce;
DROP TABLE IF EXISTS civicrm_mailing_event_queue;
DROP TABLE IF EXISTS civicrm_group_contact;
DROP TABLE IF EXISTS civicrm_uf_join;
DROP TABLE IF EXISTS civicrm_uf_field;
DROP TABLE IF EXISTS civicrm_uf_group;
DROP TABLE IF EXISTS civicrm_im;
DROP TABLE IF EXISTS civicrm_email;
DROP TABLE IF EXISTS civicrm_address;
DROP TABLE IF EXISTS civicrm_activity_history;
DROP TABLE IF EXISTS civicrm_phonecall;
DROP TABLE IF EXISTS civicrm_membership_log;
DROP TABLE IF EXISTS civicrm_membership_block;
DROP TABLE IF EXISTS civicrm_membership_payment;
DROP TABLE IF EXISTS civicrm_membership;
DROP TABLE IF EXISTS civicrm_membership_type;
DROP TABLE IF EXISTS civicrm_contribution_page;
DROP TABLE IF EXISTS civicrm_mailing_event_confirm;
DROP TABLE IF EXISTS civicrm_mailing_event_subscribe;
DROP TABLE IF EXISTS civicrm_relationship;
DROP TABLE IF EXISTS civicrm_subscription_history;
DROP TABLE IF EXISTS civicrm_group;
DROP TABLE IF EXISTS civicrm_acl_cache;
DROP TABLE IF EXISTS civicrm_log;
DROP TABLE IF EXISTS civicrm_uf_match;
DROP TABLE IF EXISTS civicrm_phone;
DROP TABLE IF EXISTS civicrm_note;
DROP TABLE IF EXISTS civicrm_mapping_field;
DROP TABLE IF EXISTS civicrm_location;
DROP TABLE IF EXISTS civicrm_entity_tag;
DROP TABLE IF EXISTS civicrm_email_history;
DROP TABLE IF EXISTS civicrm_custom_value;
DROP TABLE IF EXISTS civicrm_custom_field;
DROP TABLE IF EXISTS civicrm_custom_group;
DROP TABLE IF EXISTS civicrm_county;
DROP TABLE IF EXISTS civicrm_participant_payment;
DROP TABLE IF EXISTS civicrm_participant;
DROP TABLE IF EXISTS civicrm_event_page;
DROP TABLE IF EXISTS civicrm_event;
DROP TABLE IF EXISTS civicrm_meeting;
DROP TABLE IF EXISTS civicrm_activity;
DROP TABLE IF EXISTS civicrm_msg_template;
DROP TABLE IF EXISTS civicrm_membership_status;
DROP TABLE IF EXISTS civicrm_task_status;
DROP TABLE IF EXISTS civicrm_task;
DROP TABLE IF EXISTS civicrm_project;
DROP TABLE IF EXISTS civicrm_sms_history;
DROP TABLE IF EXISTS civicrm_premiums_product;
DROP TABLE IF EXISTS civicrm_product;
DROP TABLE IF EXISTS civicrm_premiums;
DROP TABLE IF EXISTS civicrm_financial_trxn;
DROP TABLE IF EXISTS civicrm_contribution_type;
DROP TABLE IF EXISTS civicrm_contribution_recur;
DROP TABLE IF EXISTS civicrm_mailing_bounce_pattern;
DROP TABLE IF EXISTS civicrm_mailing_bounce_type;
DROP TABLE IF EXISTS civicrm_mailing_job;
DROP TABLE IF EXISTS civicrm_mailing_trackable_url;
DROP TABLE IF EXISTS civicrm_mailing_group;
DROP TABLE IF EXISTS civicrm_mailing;
DROP TABLE IF EXISTS civicrm_mailing_component;
DROP TABLE IF EXISTS civicrm_saved_search;
DROP TABLE IF EXISTS civicrm_relationship_type;
DROP TABLE IF EXISTS civicrm_organization;
DROP TABLE IF EXISTS civicrm_individual;
DROP TABLE IF EXISTS civicrm_household;
DROP TABLE IF EXISTS civicrm_contact;
DROP TABLE IF EXISTS civicrm_acl_entity_role;
DROP TABLE IF EXISTS civicrm_acl;
DROP TABLE IF EXISTS civicrm_currency;
DROP TABLE IF EXISTS civicrm_entity_file;
DROP TABLE IF EXISTS civicrm_file;
DROP TABLE IF EXISTS civicrm_option_value;
DROP TABLE IF EXISTS civicrm_option_group;
DROP TABLE IF EXISTS civicrm_dupe_match;
DROP TABLE IF EXISTS civicrm_validation;
DROP TABLE IF EXISTS civicrm_tag;
DROP TABLE IF EXISTS civicrm_state_province;
DROP TABLE IF EXISTS civicrm_mapping;
DROP TABLE IF EXISTS civicrm_location_type;
DROP TABLE IF EXISTS civicrm_geo_coord;
DROP TABLE IF EXISTS civicrm_domain;
DROP TABLE IF EXISTS civicrm_custom_option;
DROP TABLE IF EXISTS civicrm_country;

-- /*******************************************************
-- *
-- * Create new tables
-- *
-- *******************************************************/

-- /*******************************************************
-- *
-- * civicrm_country
-- *
-- *******************************************************/
CREATE TABLE civicrm_country (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Country Id',
     name varchar(64)    COMMENT 'Country Name',
     iso_code char(2)    COMMENT 'ISO Code',
     country_code varchar(4)    COMMENT 'National prefix to be used when dialing TO this country.',
     idd_prefix varchar(4)    COMMENT 'International direct dialing prefix from within the country TO another country',
     ndd_prefix varchar(4)    COMMENT 'Access prefix to call within a country to a different area' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_name_iso_code(
        name
      , iso_code
  )
  
 
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_custom_option
-- *
-- * Stores info about options for custom fields of type 'Select' and 'Radio'
-- *
-- *******************************************************/
CREATE TABLE civicrm_custom_option (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique Custom Option ID',
     entity_table varchar(64) NOT NULL   COMMENT 'Name of table where item being referenced is stored.',
     entity_id int unsigned NOT NULL   COMMENT 'Foreign key to the referenced item.',
     label varchar(64)    COMMENT 'Label for option',
     value varchar(64)    COMMENT 'Value of the option (when form is submitted)',
     weight int NOT NULL  DEFAULT 1 COMMENT 'Order in which the options are displayed',
     is_active tinyint    COMMENT 'Is this option active ?' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_entity(
        entity_table
      , entity_id
  )
  
 
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_domain
-- *
-- * Top-level hierarchy to support multi-org/domain installations. Define domains for multi-org installs, else all contacts belong to one domain.
-- *
-- *******************************************************/
CREATE TABLE civicrm_domain (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Domain ID',
     name varchar(64)    COMMENT 'Name of Domain / Organization',
     description varchar(255)    COMMENT 'Description of Domain.',
     contact_name varchar(64)    COMMENT 'Name of the person responsible for this domain',
     email_domain varchar(64)    COMMENT 'The domain from which outgoing email for this domain will appear to originate',
     email_return_path varchar(64)    COMMENT 'The domain from which outgoing email for this domain will appear to originate',
     config_backend text    COMMENT 'Backend configuration.',
     config_frontend text    COMMENT 'Frontend configuration.' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_name(
        name
  )
  
 
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_geo_coord
-- *
-- * Geo code coordinate system info.
-- *
-- *******************************************************/
CREATE TABLE civicrm_geo_coord (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Geo Coord ID',
     coord_type enum('LatLong', 'Projected')    COMMENT 'Projected or unprojected coordinates - projected coordinates (e.g. UTM) may be treated as cartesian by some modules.',
     coord_units enum('Degree', 'Grad', 'Radian', 'Foot', 'Meter')    COMMENT 'If the coord_type is LATLONG, indicate the unit of angular measure: Degree|Grad|Radian. If the coord_type is Projected, indicate unit of distance measure: Foot|Meter.',
     coord_ogc_wkt_string text    COMMENT 'Coordinate sys description in Open GIS Consortium WKT (well known text) format - see http://www.opengeospatial.org/docs/01-009.pdf this is provided for the convenience of the user or third party modules.' 
,
    PRIMARY KEY ( id )
 
 
 
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_location_type
-- *
-- *******************************************************/
CREATE TABLE civicrm_location_type (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Location Type ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this location type.',
     name varchar(64)    COMMENT 'Location Type Name.',
     vcard_name varchar(64)    COMMENT 'vCard Location Type Name.',
     description varchar(255)    COMMENT 'Location Type Description.',
     is_reserved tinyint    COMMENT 'Is this location type a predefined system location?',
     is_active tinyint    COMMENT 'Is this property active?',
     is_default tinyint    COMMENT 'Is this location type the default?' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_name_domain_id(
        name
      , domain_id
  )
  
,      
     CONSTRAINT FK_civicrm_location_type_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mapping
-- *
-- * Store field mappings in import or export for reuse
-- *
-- *******************************************************/
CREATE TABLE civicrm_mapping (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Mapping ID',
     domain_id int unsigned NOT NULL   COMMENT 'Domain to which this mapping belongs',
     name varchar(64)    COMMENT 'Name of Mapping',
     description varchar(255)    COMMENT 'Description of Mapping.',
     mapping_type enum('Export', 'Import', 'Export Contributions', 'Import Contributions', 'Import Activity History', 'Search Builder', 'Import Memberships', 'Import Participants')    COMMENT 'Type of Mapping.' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX UI_name(
        name
  )
  
,      
     CONSTRAINT FK_civicrm_mapping_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_state_province
-- *
-- *******************************************************/
CREATE TABLE civicrm_state_province (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'State / Province ID',
     name varchar(64)    COMMENT 'Name of State / Province',
     abbreviation varchar(4)    COMMENT '2-4 Character Abbreviation of State / Province',
     country_id int unsigned NOT NULL   COMMENT 'ID of Country that State / Province belong' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_name_country_id(
        name
      , country_id
  )
  
,      
     CONSTRAINT FK_civicrm_state_province_country_id FOREIGN KEY (country_id) REFERENCES civicrm_country(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_tag
-- *
-- * Provides support for flat or hierarchical classification of various types of entities (contacts, groups, actions...).
-- *
-- *******************************************************/
CREATE TABLE civicrm_tag (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Tag ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this tag',
     name varchar(64)    COMMENT 'Name of Tag.',
     description varchar(255)    COMMENT 'Optional verbose description of the tag.',
     parent_id int unsigned   DEFAULT NULL COMMENT 'Optional parent id for this tag.' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_name_domain_id(
        name
      , domain_id
  )
  
,      
     CONSTRAINT FK_civicrm_tag_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
,      
     CONSTRAINT FK_civicrm_tag_parent_id FOREIGN KEY (parent_id) REFERENCES civicrm_tag(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_validation
-- *
-- * Stores core info about an extended (custom) property. Input form-related info is kept separately (in contact_form_field), so a property may be 'presented' in multiple form fields.
-- *
-- *******************************************************/
CREATE TABLE civicrm_validation (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique Validation ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this contact',
     type enum('Email', 'Money', 'URL', 'Phone', 'Positive Integer', 'Variable Name', 'Range', 'Regular Expression Match', 'Regular Expression No Match')    COMMENT 'List of rule built-in rule types. custom types may be added to ENUM via directory scan.',
     parameters varchar(255)    COMMENT 'optional value(s) passed to validation function, e.g. a regular expression, min and max for Range, operator + number for Comparison type, etc.',
     function_name varchar(128)    COMMENT 'custom validation function name. Class methods should be invoked using php syntax array(CLASS_NAME, FN_NAME)',
     description varchar(255)    COMMENT 'Rule Description.' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_validation_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_dupe_match
-- *
-- * DupeMatch specific fields
-- *
-- *******************************************************/
CREATE TABLE civicrm_dupe_match (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique DupeMatch ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this contact',
     entity_table varchar(64) NOT NULL   COMMENT 'Name Of Entity Table',
     rule varchar(255)    COMMENT 'String that can Contains valid civicrm core or custom field name,parenthesis,,AND,OR ' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_dupe_match_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_option_group
-- *
-- *******************************************************/
CREATE TABLE civicrm_option_group (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Option Group ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which domain owns this group of options.',
     name varchar(64) NOT NULL   COMMENT 'Option group name. Used as selection key by class properties which lookup options in civicrm_option_value.',
     description varchar(255)    COMMENT 'Option group description.',
     is_reserved tinyint    COMMENT 'Is this a predefined system option group (i.e. it can not be deleted)?',
     is_active tinyint    COMMENT 'Is this option group active?' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX option_group_name_domain_id(
        name
      , domain_id
  )
  
,      
     CONSTRAINT FK_civicrm_option_group_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_option_value
-- *
-- *******************************************************/
CREATE TABLE civicrm_option_value (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Option ID',
     option_group_id int unsigned NOT NULL   COMMENT 'Group which this option belongs to.',
     label varchar(64) NOT NULL   COMMENT 'Option string as displayed to users - e.g. the label in an HTML OPTION tag.',
     value int unsigned NOT NULL   COMMENT 'The actual value stored (as a foreign key) in the data record. Functions which need lookup option_value.title should use civicrm_option_value.option_group_id plus civicrm_option_value.value as the key.',
     name varchar(64)    COMMENT 'May be used to store an option string that is different from the display title. One use case is when a non-translated value needs to be set / sent to another application (e.g. for Credit Card type).',
     grouping varchar(64)    COMMENT 'Use to sort and/or set display properties for sub-set(s) of options within an option group. EXAMPLE: Use for college_interest field, to differentiate partners from non-partners.',
     filter int unsigned NOT NULL   COMMENT 'Bitwise logic can be used to create subsets of options within an option_group for different uses.',
     is_default tinyint   DEFAULT 0 COMMENT 'Is this the default option for the group?',
     weight int unsigned NOT NULL   COMMENT 'Controls display sort order.',
     description varchar(255)    COMMENT 'Optional description.',
     is_optgroup tinyint   DEFAULT 0 COMMENT 'Is this row simply a display header? Expected usage is to render these as OPTGROUP tags within a SELECT field list of options?',
     is_reserved tinyint   DEFAULT 0 COMMENT 'Is this a predefined system object?',
     is_active tinyint   DEFAULT 1 COMMENT 'Is this option active?' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_option_group_id_value(
        value
      , option_group_id
  )
  
,      
     CONSTRAINT FK_civicrm_option_value_option_group_id FOREIGN KEY (option_group_id) REFERENCES civicrm_option_group(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_file
-- *
-- * Data store for uploaded (attached) files (pointer to file on disk OR blob). Maybe be joined to entities via custom_value.file_id or entity_file table.
-- *
-- *******************************************************/
CREATE TABLE civicrm_file (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique ID',
     file_type_id int unsigned    COMMENT 'Type of file (e.g. Transcript, Income Tax Return, etc). FK to civicrm_option_value.',
     mime_type varchar(255)    COMMENT 'mime type of the document',
     uri varchar(255)    COMMENT 'uri of the file on disk',
     document mediumblob    COMMENT 'contents of the document',
     description varchar(255)    COMMENT 'Additional descriptive text regarding this attachment (optional).',
     upload_date datetime    COMMENT 'Date and time that this attachment was uploaded or written to server.' 
,
    PRIMARY KEY ( id )
 
 
 
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_entity_file
-- *
-- * Attaches (joins)  uploaded files (images, documents, etc.) to entities (Contacts, Groups, Actions).
-- *
-- *******************************************************/
CREATE TABLE civicrm_entity_file (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'primary key',
     entity_table varchar(64)    COMMENT 'physical tablename for entity being joined to file, e.g. civicrm_contact',
     entity_id int unsigned NOT NULL   COMMENT 'FK to entity table specified in entity_table column.',
     file_id int unsigned NOT NULL   COMMENT 'FK to civicrm_file' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_entity(
        entity_table
      , entity_id
  )
  ,     INDEX index_entity_file_id(
        entity_table
      , entity_id
      , file_id
  )
  
,      
     CONSTRAINT FK_civicrm_entity_file_file_id FOREIGN KEY (file_id) REFERENCES civicrm_file(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_currency
-- *
-- *******************************************************/
CREATE TABLE civicrm_currency (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Currency Id',
     name varchar(64)    COMMENT 'Currency Name',
     symbol varchar(8)    COMMENT 'Currency Symbol' 
,
    PRIMARY KEY ( id )
 
 
 
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_acl
-- *
-- * Access Control List
-- *
-- *******************************************************/
CREATE TABLE civicrm_acl (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique table ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this contact',
     name varchar(64)    COMMENT 'ACL Name.',
     deny tinyint NOT NULL  DEFAULT 0 COMMENT 'Is this ACL entry Allow  (0) or Deny (1) ?',
     entity_table varchar(64) NOT NULL   COMMENT 'Table of the object possessing this ACL entry (Contact, Group, or ACL Group)',
     entity_id int unsigned    COMMENT 'ID of the object possessing this ACL',
     operation enum('All', 'View', 'Edit', 'Create', 'Delete', 'Grant', 'Revoke') NOT NULL   COMMENT 'What operation does this ACL entry control?',
     object_table varchar(64)    COMMENT 'The table of the object controlled by this ACL entry',
     object_id int unsigned    COMMENT 'The ID of the object controlled by this ACL entry',
     acl_table varchar(64)    COMMENT 'If this is a grant/revoke entry, what table are we granting?',
     acl_id int unsigned    COMMENT 'ID of the ACL or ACL group being granted/revoked',
     is_active tinyint    COMMENT 'Is this property active?' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_acl_id(
        acl_id
  )
  
,      
     CONSTRAINT FK_civicrm_acl_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_acl_entity_role
-- *
-- * Join table for Contacts and Groups to ACL Roles
-- *
-- *******************************************************/
CREATE TABLE civicrm_acl_entity_role (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique table ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this contact',
     acl_role_id int unsigned NOT NULL   COMMENT 'Foreign Key to ACL Role (which is an option value pair and hence an implicit FK)',
     entity_table varchar(64) NOT NULL   COMMENT 'Table of the object joined to the ACL Role (Contact or Group)',
     entity_id int unsigned NOT NULL   COMMENT 'ID of the group/contact object being joined',
     is_active tinyint    COMMENT 'Is this property active?' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_role(
        acl_role_id
  )
  ,     INDEX index_entity(
        entity_table
      , entity_id
  )
  
,      
     CONSTRAINT FK_civicrm_acl_entity_role_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_contact
-- *
-- * Three types of contacts are defined: Individual, Organization and Household. Contact objects are defined by a civicrm_contact record plus a related civicrm_contact_type record.
-- *
-- *******************************************************/
CREATE TABLE civicrm_contact (


     nick_name varchar(128)    COMMENT 'Nick Name.',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this contact',
     contact_type enum('Individual', 'Organization', 'Household')    COMMENT 'Type of Contact.',
     do_not_email tinyint   DEFAULT 0 ,
     do_not_phone tinyint   DEFAULT 0 ,
     do_not_mail tinyint   DEFAULT 0 ,
     contact_sub_type varchar(64)    COMMENT 'May be used to over-ride contact view and edit templates.',
     legal_identifier varchar(32)    COMMENT 'May be used for SSN, EIN/TIN, Household ID (census) or other applicable unique legal/government ID.',
     external_identifier varchar(32)    COMMENT 'Unique trusted external ID (generally from a legacy app/datasource). Particularly useful for deduping operations.',
     sort_name varchar(128)    COMMENT 'Name used for sorting different contact types',
     display_name varchar(128)    COMMENT 'Formatted name representing preferred format for display/print/other output.',
     home_URL varchar(128)    COMMENT 'optional "home page" URL for this contact.',
     image_URL varchar(128)    COMMENT 'optional URL for preferred image (photo, logo, etc.) to display for this contact.',
     preferred_communication_method varchar(255)    COMMENT 'What is the preferred mode of communication.',
     preferred_mail_format enum('Text', 'HTML', 'Both')   DEFAULT "Both" COMMENT 'What is the preferred mode of sending an email.',
     do_not_trade tinyint   DEFAULT 0 ,
     hash varchar(32)    COMMENT 'Key for validating requests related to this contact.',
     is_opt_out tinyint NOT NULL  DEFAULT 0 COMMENT 'Has the contact opted out from receiving all bulk email from the organization or site domain?',
     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique Contact ID',
     source varchar(255)    COMMENT 'where domain_id contact come from, e.g. import, donate module insert...' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_external_identifier(
        external_identifier
  )
  ,     INDEX index_sort_name_domain(
        sort_name
      , domain_id
  )
  ,     INDEX index_contact_type_domain(
        contact_type
      , domain_id
  )
  ,     INDEX index_contact_sub_type_domain(
        contact_sub_type
      , domain_id
  )
  ,     INDEX index_hash_domain(
        hash
      , domain_id
  )
  ,     INDEX index_preferred_communication_method(
        preferred_communication_method
  )
  
,      
     CONSTRAINT FK_civicrm_contact_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_household
-- *
-- * Define household specific properties
-- *
-- *******************************************************/
CREATE TABLE civicrm_household (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique Household ID',
     contact_id int unsigned NOT NULL   COMMENT 'FK to Contact ID',
     household_name varchar(128)    COMMENT 'Household Name.',
     primary_contact_id int unsigned    COMMENT 'Optional FK to Primary Contact for this household.' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_contact(
        contact_id
  )
  
,      
     CONSTRAINT FK_civicrm_household_contact_id FOREIGN KEY (contact_id) REFERENCES civicrm_contact(id)
,      
     CONSTRAINT FK_civicrm_household_primary_contact_id FOREIGN KEY (primary_contact_id) REFERENCES civicrm_contact(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_individual
-- *
-- * Define contact-individual specific properties. Extends civicrm_contact.
-- *
-- *******************************************************/
CREATE TABLE civicrm_individual (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique Individual ID',
     contact_id int unsigned NOT NULL   COMMENT 'FK to Contact ID',
     first_name varchar(64)    COMMENT 'First Name.',
     middle_name varchar(64)    COMMENT 'Middle Name.',
     last_name varchar(64)    COMMENT 'Last Name.',
     prefix_id int unsigned    COMMENT 'Prefix or Title for name (Ms, Mr...). FK to prefix ID',
     suffix_id int unsigned    COMMENT 'Suffix for name (Jr, Sr...). FK to suffix ID',
     greeting_type enum('Formal', 'Informal', 'Honorific', 'Custom', 'Other')    COMMENT 'Preferred greeting format.',
     custom_greeting varchar(128)    COMMENT 'Custom greeting message.',
     job_title varchar(64)    COMMENT 'Job Title',
     gender_id int unsigned    COMMENT 'FK to gender ID',
     birth_date date    COMMENT 'Date of birth',
     is_deceased tinyint   DEFAULT 0 ,
     deceased_date date    COMMENT 'Date of deceased',
     phone_to_household_id int unsigned    COMMENT 'OPTIONAL FK to civicrm_contact_household record. If NOT NULL, direct phone communications to household rather than individual location.',
     email_to_household_id int unsigned    COMMENT 'OPTIONAL FK to civicrm_contact_household record. If NOT NULL, direct phone communications to household rather than individual location.',
     mail_to_household_id int unsigned    COMMENT 'OPTIONAL FK to civicrm_contact_household record. If NOT NULL, direct mail communications to household rather than individual location.' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX UI_prefix(
        prefix_id
  )
  ,     INDEX UI_suffix(
        suffix_id
  )
  ,     INDEX UI_gender(
        gender_id
  )
  ,     UNIQUE INDEX UI_contact(
        contact_id
  )
  
,      
     CONSTRAINT FK_civicrm_individual_contact_id FOREIGN KEY (contact_id) REFERENCES civicrm_contact(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_organization
-- *
-- * Define organization specific properties
-- *
-- *******************************************************/
CREATE TABLE civicrm_organization (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique Organization ID',
     contact_id int unsigned NOT NULL   COMMENT 'FK to Contact ID',
     organization_name varchar(128)    COMMENT 'Organization Name.',
     legal_name varchar(128)    COMMENT 'Legal Name.',
     sic_code varchar(8)    COMMENT 'Standard Industry Classification Code.',
     primary_contact_id int unsigned    COMMENT 'Optional FK to Primary Contact for this organization.' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_contact(
        contact_id
  )
  
,      
     CONSTRAINT FK_civicrm_organization_contact_id FOREIGN KEY (contact_id) REFERENCES civicrm_contact(id)
,      
     CONSTRAINT FK_civicrm_organization_primary_contact_id FOREIGN KEY (primary_contact_id) REFERENCES civicrm_contact(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_relationship_type
-- *
-- * Relationship types s/b structured with contact_a as the 'subject/child' contact and contact_b as the 'object/parent' contact (e.g. Individual A is Employee of Org B).
-- *
-- *******************************************************/
CREATE TABLE civicrm_relationship_type (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Primary key',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this contact',
     name_a_b varchar(64)    COMMENT 'name/label for relationship of contact_a to contact_b.',
     name_b_a varchar(64)    COMMENT 'Optional name/label for relationship of contact_b to contact_a.',
     description varchar(255)    COMMENT 'Optional verbose description of the relationship type.',
     contact_type_a enum('Individual', 'Organization', 'Household')    COMMENT 'If defined, contact_a in a relationship of this type must be a specific contact_type.',
     contact_type_b enum('Individual', 'Organization', 'Household')    COMMENT 'If defined, contact_b in a relationship of this type must be a specific contact_type.',
     is_reserved tinyint    COMMENT 'Is this relationship type a predefined system type (can not be changed or de-activated)?',
     is_active tinyint   DEFAULT 1 COMMENT 'Is this relationship type currently active (i.e. can be used when creating or editing relationships)?' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_name_a_b_domain_id(
        name_a_b
      , domain_id
  )
  ,     UNIQUE INDEX UI_name_b_a_domain_id(
        name_b_a
      , domain_id
  )
  
,      
     CONSTRAINT FK_civicrm_relationship_type_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_saved_search
-- *
-- * Users can save their complex SQL queries and use them later.
-- *
-- *******************************************************/
CREATE TABLE civicrm_saved_search (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Saved search ID',
     form_values text    COMMENT 'Submitted form values for this search',
     mapping_id int unsigned    COMMENT 'Foreign key to civicrm_mapping used for saved search-builder searches.',
     where_clause text    COMMENT 'the sql where clause if a saved search acl',
     select_tables text    COMMENT 'the tables to be included in a select data',
     where_tables text    COMMENT 'the tables to be included in the count statement' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_saved_search_mapping_id FOREIGN KEY (mapping_id) REFERENCES civicrm_mapping(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing_component
-- *
-- * Stores information about the mailing components (header/footer).
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing_component (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this component',
     name varchar(64)    COMMENT 'The name of this component',
     component_type enum('Header', 'Footer', 'Subscribe', 'Welcome', 'Unsubscribe', 'OptOut', 'Reply')    COMMENT 'Type of Component.',
     subject varchar(255)    ,
     body_html text    COMMENT 'Body of the component in html format.',
     body_text text    COMMENT 'Body of the component in text format.',
     is_default tinyint   DEFAULT 0 COMMENT 'Is this the default component for this component_type?',
     is_active tinyint    COMMENT 'Is this property active?' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mailing_component_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing
-- *
-- * Stores information about a mailing.
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this mailing',
     header_id int unsigned    COMMENT 'FK to the header component.',
     footer_id int unsigned    COMMENT 'FK to the footer component.',
     reply_id int unsigned NOT NULL   COMMENT 'FK to the auto-responder component.',
     unsubscribe_id int unsigned NOT NULL   COMMENT 'FK to the unsubscribe component.',
     optout_id int unsigned NOT NULL   COMMENT 'FK to the opt-out component.',
     name varchar(128)    COMMENT 'Mailing Name.',
     from_name varchar(128)    COMMENT 'From Header of mailing',
     from_email varchar(128)    COMMENT 'From Email of mailing',
     replyto_email varchar(128)    COMMENT 'Reply-To Email of mailing',
     subject varchar(128)    COMMENT 'Subject of mailing',
     body_text text    COMMENT 'Body of the mailing in text format.',
     body_html text    COMMENT 'Body of the mailing in html format.',
     is_template tinyint    COMMENT 'Is this object a mailing template?',
     url_tracking tinyint    COMMENT 'Should we track URL click-throughs for this mailing?',
     forward_replies tinyint    COMMENT 'Should we forward replies back to the author?',
     auto_responder tinyint    COMMENT 'Should we enable the auto-responder?',
     open_tracking tinyint    COMMENT 'Should we track when recipients open/read this mailing?',
     is_completed tinyint    COMMENT 'Has at least one job associated with this mailing finished?' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mailing_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
,      
     CONSTRAINT FK_civicrm_mailing_header_id FOREIGN KEY (header_id) REFERENCES civicrm_mailing_component(id)
,      
     CONSTRAINT FK_civicrm_mailing_footer_id FOREIGN KEY (footer_id) REFERENCES civicrm_mailing_component(id)
,      
     CONSTRAINT FK_civicrm_mailing_reply_id FOREIGN KEY (reply_id) REFERENCES civicrm_mailing_component(id)
,      
     CONSTRAINT FK_civicrm_mailing_unsubscribe_id FOREIGN KEY (unsubscribe_id) REFERENCES civicrm_mailing_component(id)
,      
     CONSTRAINT FK_civicrm_mailing_optout_id FOREIGN KEY (optout_id) REFERENCES civicrm_mailing_component(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing_group
-- *
-- * Stores information about the groups that participate in this mailing..
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing_group (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     mailing_id int unsigned NOT NULL   COMMENT 'The ID of a previous mailing to include/exclude recipients.',
     group_type enum('Include', 'Exclude')    COMMENT 'Are the members of the group included or excluded?.',
     entity_table varchar(64) NOT NULL   COMMENT 'Name of table where item being referenced is stored.',
     entity_id int unsigned NOT NULL   COMMENT 'Foreign key to the referenced item.' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mailing_group_mailing_id FOREIGN KEY (mailing_id) REFERENCES civicrm_mailing(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing_trackable_url
-- *
-- * Stores URLs for which we should track click-throughs from mailings
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing_trackable_url (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     url varchar(255) NOT NULL   COMMENT 'The URL to be tracked.',
     mailing_id int unsigned NOT NULL   COMMENT 'FK to the mailing' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mailing_trackable_url_mailing_id FOREIGN KEY (mailing_id) REFERENCES civicrm_mailing(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing_job
-- *
-- * Stores information about the job that executes this mailing
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing_job (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     mailing_id int unsigned NOT NULL   COMMENT 'The ID of the mailing this Job will send.',
     scheduled_date datetime    COMMENT 'date on which this job was scheduled.',
     start_date datetime    COMMENT 'date on which this job was started.',
     end_date datetime    COMMENT 'date on which this job ended.',
     status enum('Scheduled', 'Running', 'Complete', 'Paused', 'Canceled')    COMMENT 'The state of this job',
     is_retry tinyint    COMMENT 'Is this job a retry?' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mailing_job_mailing_id FOREIGN KEY (mailing_id) REFERENCES civicrm_mailing(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing_bounce_type
-- *
-- * Table to index the various bounce types and their properties
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing_bounce_type (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     name enum('AOL', 'Away', 'DNS', 'Host', 'Inactive', 'Invalid', 'Loop', 'Quota', 'Relay', 'Spam', 'Syntax', 'Unknown') NOT NULL   COMMENT 'Type of bounce',
     description varchar(255)    COMMENT 'A description of this bounce type',
     hold_threshold int unsigned NOT NULL   COMMENT 'Number of bounces of this type required before the email address is put on bounce hold' 
,
    PRIMARY KEY ( id )
 
 
 
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing_bounce_pattern
-- *
-- * Pseudo-constant table of patterns for bounce classification
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing_bounce_pattern (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     bounce_type_id int unsigned NOT NULL   COMMENT 'Type of bounce',
     pattern varchar(255)    COMMENT 'A regexp to match a message to a bounce type' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mailing_bounce_pattern_bounce_type_id FOREIGN KEY (bounce_type_id) REFERENCES civicrm_mailing_bounce_type(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_contribution_recur
-- *
-- *******************************************************/
CREATE TABLE civicrm_contribution_recur (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Contribution Recur ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this contribution class.',
     contact_id int unsigned NOT NULL   COMMENT 'Foreign key to civicrm_contact.id .',
     amount decimal(20,2) NOT NULL   COMMENT 'Amount to be contributed or charged each recurrence.',
     frequency_unit enum('day', 'week', 'month', 'year')   DEFAULT 'month' COMMENT 'Time units for recurrence of payment.',
     frequency_interval int unsigned NOT NULL   COMMENT 'Number of time units for recurrence of payment.',
     installments int unsigned    COMMENT 'Total number of payments to be made. Set this to 0 if this is an open-ended commitment i.e. no set end date.',
     start_date datetime NOT NULL   COMMENT 'The date the first scheduled recurring contribution occurs.',
     create_date datetime NOT NULL   COMMENT 'When this recurring contribution record was created.',
     modified_date datetime NOT NULL   COMMENT 'Last updated date for this record. mostly the last time a payment was received',
     cancel_date datetime NOT NULL   COMMENT 'Date this recurring contribution was cancelled by contributor- if we can get access to it',
     end_date datetime NOT NULL   COMMENT 'Date this recurring contribution finished successfully',
     processor_id varchar(255)    COMMENT 'Possibly needed to store a unique identifier for this recurring payment order - if this is available from the processor??',
     trxn_id varchar(255)    COMMENT 'unique transaction id. may be processor id, bank id + trans id, or account number + check number... depending on payment_method',
     invoice_id varchar(255)    COMMENT 'unique invoice id, system generated or passed in',
     contribution_status_id int unsigned   DEFAULT 1 ,
     is_test tinyint   DEFAULT 0 ,
     cycle_day int unsigned NOT NULL  DEFAULT 1 COMMENT 'Day in the period when the payment should be charged e.g. 1st of month, 15th etc.',
     next_sched_contribution datetime NOT NULL   COMMENT 'At Groundspring this was used by the cron job which triggered payments. If we\'re not doing that but we know about payments, it might still be useful to store for display to org andor contributors.',
     failure_count int unsigned   DEFAULT 0 COMMENT 'Number of failed charge attempts since last success. Business rule could be set to deactivate on more than x failures.',
     failure_retry_date datetime    COMMENT 'At Groundspring we set a business rule to retry failed payments every 7 days - and stored the next scheduled attempt date there.',
     auto_renew tinyint NOT NULL  DEFAULT 0 COMMENT 'Some systems allow contributor to set a number of installments - but then auto-renew the subscription or commitment if they do not cancel.' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_contrib_trxn_id_domain_id(
        trxn_id
      , domain_id
  )
  ,     UNIQUE INDEX UI_contrib_invoice_id_domain_id(
        invoice_id
      , domain_id
  )
  ,     INDEX index_contribution_status(
        contribution_status_id
  )
  
,      
     CONSTRAINT FK_civicrm_contribution_recur_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
,      
     CONSTRAINT FK_civicrm_contribution_recur_contact_id FOREIGN KEY (contact_id) REFERENCES civicrm_contact(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_contribution_type
-- *
-- *******************************************************/
CREATE TABLE civicrm_contribution_type (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Contribution Type ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this contribution type.',
     name varchar(64)    COMMENT 'Contribution Type Name.',
     accounting_code varchar(64)    COMMENT 'Optional value for mapping contributions to accounting system codes for each type/category of contribution.',
     description varchar(255)    COMMENT 'Contribution Type Description.',
     is_deductible tinyint   DEFAULT 1 COMMENT 'Is this contribution type tax-deductible? If true, contributions of this type may be fully OR partially deductible - non-deductible amount is stored in the Contribution record.',
     is_reserved tinyint    COMMENT 'Is this a predefined system object?',
     is_active tinyint    COMMENT 'Is this property active?' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_name_domain_id(
        name
      , domain_id
  )
  
,      
     CONSTRAINT FK_civicrm_contribution_type_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_financial_trxn
-- *
-- *******************************************************/
CREATE TABLE civicrm_financial_trxn (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Gift ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this gift class.',
     entity_table varchar(64)    COMMENT 'physical tablename for entity being extended by this data, e.g. civicrm_contact',
     entity_id int unsigned NOT NULL   COMMENT 'FK to record in the entity table specified by entity_table column.',
     trxn_date datetime NOT NULL   ,
     trxn_type enum('Debit', 'Credit') NOT NULL   ,
     total_amount decimal(20,2) NOT NULL   COMMENT 'amount of transaction',
     fee_amount decimal(20,2)    COMMENT 'actual processor fee if known - may be 0.',
     net_amount decimal(20,2)    COMMENT 'actual funds transfer amount. total less fees. if processor does not report actual fee during transaction, this is set to total_amount.',
     currency varchar(64) NOT NULL   COMMENT '3 character string, value derived from payment processor config setting.',
     payment_processor varchar(64) NOT NULL   COMMENT 'derived from Processor setting in civicrm.settings.php.',
     trxn_id varchar(255) NOT NULL   COMMENT 'unique processor transaction id, bank id + trans id,... depending on payment_method',
     trxn_result_code varchar(255)    COMMENT 'processor result code' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_entity(
        entity_table
      , entity_id
  )
  ,     UNIQUE INDEX UI_ft_trxn_id_domain_id(
        trxn_id
      , domain_id
  )
  
,      
     CONSTRAINT FK_civicrm_financial_trxn_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_premiums
-- *
-- * table - settings for the Premiums features for a given contribution page 
-- *
-- *******************************************************/
CREATE TABLE civicrm_premiums (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     entity_table varchar(64) NOT NULL   COMMENT 'Joins these premium settings to another object. Always civicrm_contribution_page for now.',
     entity_id int unsigned NOT NULL   ,
     premiums_active tinyint NOT NULL  DEFAULT 0 COMMENT 'Is the Premiums feature enabled for this page?',
     premiums_intro_title varchar(255)    COMMENT 'Title for Premiums section.',
     premiums_intro_text text    COMMENT 'Displayed in <div> at top of Premiums section of page. Text and HTML allowed.',
     premiums_contact_email varchar(100)    COMMENT 'This email address is included in receipts if it is populated and a premium has been selected.',
     premiums_contact_phone varchar(50)    COMMENT 'This phone number is included in receipts if it is populated and a premium has been selected.',
     premiums_display_min_contribution tinyint NOT NULL   COMMENT 'Boolean. Should we automatically display minimum contribution amount text after the premium descriptions.' 
,
    PRIMARY KEY ( id )
 
 
 
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_product
-- *
-- * able - stores "product info" for premiums and can be used for non-incentive products 
-- *
-- *******************************************************/
CREATE TABLE civicrm_product (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this product class.',
     name varchar(255) NOT NULL   COMMENT 'Required product/premium name',
     description text    COMMENT 'Optional description of the product/premium.',
     sku varchar(50)    COMMENT 'Optional product sku or code.',
     options text    COMMENT 'Store comma-delimited list of color, size, etc. options for the product.',
     image varchar(255)    COMMENT 'Full or relative URL to uploaded image - fullsize.',
     thumbnail varchar(255)    COMMENT 'Full or relative URL to image thumbnail.',
     price decimal(20,2)    COMMENT 'Sell price or market value for premiums. For tax-deductible contributions, this will be stored as non_deductible_amount in the contribution record.',
     min_contribution decimal(20,2)    COMMENT 'Minimum contribution required to be eligible to select this premium.',
     cost decimal(20,2)    COMMENT 'Actual cost of this product. Useful to determine net return from sale or using this as an incentive.',
     is_active tinyint NOT NULL   COMMENT 'Disabling premium removes it from the premiums_premium join table below.',
     period_type enum('rolling', 'fixed')   DEFAULT 'rolling' COMMENT 'Rolling means we set start/end based on current day, fixed means we set start/end for current year or month
(e.g. 1 year + fixed -> we would set start/end for 1/1/06 thru 12/31/06 for any premium chosen in 2006) ',
     fixed_period_start_day int   DEFAULT 0101 COMMENT 'Month and day (MMDD) that fixed period type subscription or membership starts.',
     duration_unit enum('day', 'month', 'week', 'year')   DEFAULT 'year' ,
     duration_interval int    COMMENT 'Number of units for total duration of subscription, service, membership (e.g. 12 Months).',
     frequency_unit enum('day', 'month', 'week', 'year')   DEFAULT 'month' COMMENT 'Frequency unit and interval allow option to store actual delivery frequency for a subscription or service.',
     frequency_interval int    COMMENT 'Number of units for delivery frequency of subscription, service, membership (e.g. every 3 Months).' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_product_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_premiums_product
-- *
-- * joins premiums (settings) to individual product/premium items - determines which products are available for a given contribution page
-- *
-- *******************************************************/
CREATE TABLE civicrm_premiums_product (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Contribution ID',
     premiums_id int unsigned NOT NULL   COMMENT 'Foreign key to premiums settings record.',
     product_id int unsigned NOT NULL   COMMENT 'Foreign key to each product object.',
     sort_position int unsigned NOT NULL    
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_premiums_product_premiums_id FOREIGN KEY (premiums_id) REFERENCES civicrm_premiums(id)
,      
     CONSTRAINT FK_civicrm_premiums_product_product_id FOREIGN KEY (product_id) REFERENCES civicrm_product(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_sms_history
-- *
-- * SMS History can be linked to any object in the application.
-- *
-- *******************************************************/
CREATE TABLE civicrm_sms_history (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'SMS History ID',
     message text    COMMENT 'Contents of the SMS.',
     contact_id int unsigned NOT NULL   COMMENT 'FK to Contact who is sending this SMS',
     sent_date date    COMMENT 'When was this SMS sent' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_sms_history_contact_id FOREIGN KEY (contact_id) REFERENCES civicrm_contact(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_project
-- *
-- * Instantiate projects, programs, campaigns, etc.
-- *
-- *******************************************************/
CREATE TABLE civicrm_project (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Project ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this record.',
     title varchar(64)    COMMENT 'Project name.',
     description text    COMMENT 'Optional verbose description of the project. May be used for display - HTML allowed.',
     logo varchar(255)    COMMENT 'Full or relative URL to optional uploaded logo image for project.',
     owner_entity_table varchar(64) NOT NULL   COMMENT 'Name of table where project owner being referenced is stored (e.g. civicrm_contact or civicrm_group).',
     owner_entity_id int unsigned NOT NULL   COMMENT 'Foreign key to project owner (contact, group, etc.).',
     start_date datetime    COMMENT 'Project start date.',
     end_date datetime    COMMENT 'Project end date.',
     is_active tinyint    COMMENT 'Is this record active? For Projects: can tasks be created for it, does it appear on project listings, etc.',
     status_id int unsigned    COMMENT 'Configurable status value (e.g. Planned, Active, Closed...). FK to civicrm_option_value.' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_project_owner(
        id
      , owner_entity_table
      , owner_entity_id
  )
  
,      
     CONSTRAINT FK_civicrm_project_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_task
-- *
-- * To-do items. Can be assigned to self or other entities.
-- *
-- *******************************************************/
CREATE TABLE civicrm_task (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Task ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this record.',
     title varchar(64)    COMMENT 'Task name.',
     description varchar(255)    COMMENT 'Optional verbose description of the Task. May be used for display - HTML allowed.',
     task_type_id int unsigned    COMMENT 'Configurable task type values (e.g. App Submit, App Review...). FK to civicrm_option_value.',
     owner_entity_table varchar(64) NOT NULL   COMMENT 'Name of table where Task owner being referenced is stored (e.g. civicrm_contact or civicrm_group).',
     owner_entity_id int unsigned NOT NULL   COMMENT 'Foreign key to Task owner (contact, group, etc.).',
     parent_entity_table varchar(64)    COMMENT 'Name of table where optional Task parent is stored (e.g. civicrm_project, or civicrm_task for sub-tasks).',
     parent_entity_id int unsigned    COMMENT 'Optional foreign key to Task Parent (project, another task, etc.).',
     due_date datetime    COMMENT 'Task due date.',
     priority_id int unsigned    COMMENT 'Configurable priority value (e.g. Critical, High, Medium...). FK to civicrm_option_value.',
     task_class varchar(255)    COMMENT 'Optional key to a process class related to this task (e.g. CRM_Quest_PreApp).',
     is_active tinyint    COMMENT 'Is this record active? For tasks: can it be assigned, does it appear on open task listings, etc.' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_task_parent(
        id
      , parent_entity_table
      , parent_entity_id
  )
  ,     UNIQUE INDEX UI_task_owner(
        id
      , owner_entity_table
      , owner_entity_id
  )
  
,      
     CONSTRAINT FK_civicrm_task_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_task_status
-- *
-- * Tracks the responsible entity, optional target entity and status of a task. Tasks can have multiple task_status entries if assigned to multiple responsible entities and-or there are multiple targets.
-- *
-- *******************************************************/
CREATE TABLE civicrm_task_status (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Task ID',
     task_id int unsigned NOT NULL   COMMENT 'Status is for which task.',
     responsible_entity_table varchar(64) NOT NULL   COMMENT 'Entity responsible for this task_status instance (table where entity is stored e.g. civicrm_contact or civicrm_group).',
     responsible_entity_id int unsigned NOT NULL   COMMENT 'Foreign key to responsible entity (contact, group, etc.).',
     target_entity_table varchar(64) NOT NULL   COMMENT 'Optional target entity for this task_status instance, i.e. review this membership application-prospect member contact record is target (table where entity is stored e.g. civicrm_contact or civicrm_group).',
     target_entity_id int unsigned NOT NULL   COMMENT 'Foreign key to target entity (contact, group, etc.).',
     status_detail text    COMMENT 'Encoded array of status details used for programmatic progress reporting and tracking.',
     status_id int unsigned    COMMENT 'Configurable status value (e.g. Not Started, In Progress, Completed, Deferred...). FK to civicrm_option_value.',
     create_date datetime    COMMENT 'Date this record was created (date work on task started).',
     modified_date datetime    COMMENT 'Date-time of last update to this task_status record.' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_task_status_responsible(
        task_id
      , responsible_entity_table
      , responsible_entity_id
  )
  ,     INDEX index_task_status_target(
        task_id
      , target_entity_table
      , target_entity_id
  )
  
,      
     CONSTRAINT FK_civicrm_task_status_task_id FOREIGN KEY (task_id) REFERENCES civicrm_task(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_membership_status
-- *
-- * Membership Status stores admin configurable rules for assigning status to memberships.
-- *
-- *******************************************************/
CREATE TABLE civicrm_membership_status (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Membership Id',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this contact',
     name varchar(128)    COMMENT 'Name for Membership Status',
     start_event enum('start_date', 'end_date', 'join_date')    COMMENT 'Event when this status starts.',
     start_event_adjust_unit enum('day', 'month', 'year')    COMMENT 'Unit used for adjusting from start_event.',
     start_event_adjust_interval int    COMMENT 'Status range begins this many units from start_event.',
     end_event enum('start_date', 'end_date', 'join_date')    COMMENT 'Event after which this status ends.',
     end_event_adjust_unit enum('day', 'month', 'year')    COMMENT 'Unit used for adjusting from the ending event.',
     end_event_adjust_interval int    COMMENT 'Status range ends this many units from end_event.',
     is_current_member tinyint    COMMENT 'Does this status aggregate to current members (e.g. New, Renewed, Grace might all be TRUE... while Unrenewed, Lapsed, Inactive would be FALSE).',
     is_admin tinyint    COMMENT 'Is this status for admin/manual assignment only.',
     weight int    ,
     is_default tinyint    COMMENT 'Assign this status to a membership record if no other status match is found.',
     is_active tinyint   DEFAULT 1 COMMENT 'Is this membership_status enabled.' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_membership_status_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_msg_template
-- *
-- * Users will need a way to save and retrieve templates with tokens for use in recurring email communication tasks
-- *
-- *******************************************************/
CREATE TABLE civicrm_msg_template (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Message Template ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this contact',
     msg_title varchar(255)    COMMENT 'Descriptive title of message',
     msg_subject varchar(255)    COMMENT 'Subject for email message.',
     msg_text text    COMMENT 'Text formatted message',
     msg_html text    COMMENT 'HTML formatted message',
     is_active tinyint   DEFAULT 1  
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_msg_template_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_activity
-- *
-- * Other Activity  details stored here include contact, location, details.
-- *
-- *******************************************************/
CREATE TABLE civicrm_activity (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique  Other Activity ID',
     source_contact_id int unsigned NOT NULL   COMMENT 'Contact ID of person scheduling or logging this Activity. This will generally an authenticated user.',
     activity_type_id int unsigned NOT NULL   COMMENT 'Foreign key to the referenced item.',
     target_entity_table varchar(64) NOT NULL   COMMENT 'Name of table where item being referenced is stored.',
     target_entity_id int unsigned NOT NULL   COMMENT 'Foreign key to the referenced item.',
     subject varchar(64)    COMMENT 'The subject/purpose of this meeting.',
     scheduled_date_time datetime    COMMENT 'Date and time meeting is scheduled to occur.',
     duration_hours int unsigned    COMMENT 'Planned or actual duration of meeting - hours.',
     duration_minutes int unsigned    COMMENT 'Planned or actual duration of meeting - minutes.',
     location varchar(255)    COMMENT 'Where will the meeting be held ?',
     details text    COMMENT 'Details about the meeting (agenda, notes, etc).',
     status enum('Scheduled', 'Completed')    COMMENT 'What is the status of this meeting? Completed meeting status results in activity history entry.',
     parent_id int unsigned    COMMENT 'Parent meeting ID (if this is a follow-up item). This is not currently implemented' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX UI_activity_type_id(
        activity_type_id
  )
  
,      
     CONSTRAINT FK_civicrm_activity_source_contact_id FOREIGN KEY (source_contact_id) REFERENCES civicrm_contact(id)
,      
     CONSTRAINT FK_civicrm_activity_parent_id FOREIGN KEY (parent_id) REFERENCES civicrm_activity(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_meeting
-- *
-- * Meeting details stored here include contact, location, details.
-- *
-- *******************************************************/
CREATE TABLE civicrm_meeting (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique Meeting ID',
     source_contact_id int unsigned NOT NULL   COMMENT 'Contact ID of person scheduling or logging this meeting. This will generally an authenticated user.',
     target_entity_table varchar(64) NOT NULL   COMMENT 'Name of table where item being referenced is stored.',
     target_entity_id int unsigned NOT NULL   COMMENT 'Foreign key to the referenced item.',
     subject varchar(64)    COMMENT 'The subject/purpose of this meeting.',
     scheduled_date_time datetime    COMMENT 'Date and time meeting is scheduled to occur.',
     duration_hours int unsigned    COMMENT 'Planned or actual duration of meeting - hours.',
     duration_minutes int unsigned    COMMENT 'Planned or actual duration of meeting - minutes.',
     location varchar(255)    COMMENT 'Where will the meeting be held ?',
     details text    COMMENT 'Details about the meeting (agenda, notes, etc).',
     status enum('Scheduled', 'Completed')    COMMENT 'What is the status of this meeting? Completed meeting status results in activity history entry.',
     parent_id int unsigned    COMMENT 'Parent meeting ID (if this is a follow-up item). This is not currently implemented' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_meeting_source_contact_id FOREIGN KEY (source_contact_id) REFERENCES civicrm_contact(id)
,      
     CONSTRAINT FK_civicrm_meeting_parent_id FOREIGN KEY (parent_id) REFERENCES civicrm_meeting(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_event
-- *
-- *******************************************************/
CREATE TABLE civicrm_event (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Event',
     domain_id int unsigned NOT NULL  DEFAULT 0 COMMENT 'Event belongs to which Domain?',
     title varchar(255)    COMMENT 'Event Title (e.g. Fall Fundraiser Dinner)',
     summary text    COMMENT 'Brief summary of event. Text and html allowed. Displayed on Event Registration form and can be used on other CMS pages which need an event summary.',
     description text    COMMENT 'Full description of event. Text and html allowed. Displayed on built-in Event Information screens.',
     event_type_id int unsigned   DEFAULT 0 COMMENT 'Event Type ID.Implicit FK to civicrm_option_value where option_group = event_type.',
     is_public tinyint   DEFAULT 1 COMMENT 'Public events will be included in the iCal feeds. Access to private event information may be limited using ACLs.',
     start_date datetime    COMMENT 'Date and time that event starts.',
     end_date datetime    COMMENT 'Date and time that event ends. May be NULL if no defined end date/time',
     is_online_registration tinyint   DEFAULT 0 COMMENT 'If true, include registration link on Event Info page.',
     registration_link_text varchar(255)    COMMENT 'Text for link to Event Registration form which is displayed on Event Information screen when is_online_registration is true.',
     max_participants int unsigned   DEFAULT 0 COMMENT 'Maximum number of registered participants to allow. After max is reached, a custom Event Full message is displayed. If NULL, allow unlimited number of participants.',
     event_full_text text    COMMENT 'Message to display on Event Information page and INSTEAD OF Event Registration form if maximum participants are signed up. Can include email address/info about getting on a waiting list, etc. Text and html allowed.',
     is_monetary tinyint   DEFAULT 0 COMMENT 'Is this a PAID event? If true, one or more fee amounts must be set and a Payment Processor must be configured for Online Event Registration.',
     contribution_type_id int unsigned   DEFAULT 0 COMMENT 'Contribution type assigned to paid event registrations for this event. Required if is_monetary is true.',
     is_map tinyint   DEFAULT 0 COMMENT 'Include a map block on the Event Information page when geocode info is available and a mapping provider has been specified?',
     is_active tinyint   DEFAULT 0 COMMENT 'Is this Event enabled or disabled/cancelled?' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_event_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_event_page
-- *
-- *******************************************************/
CREATE TABLE civicrm_event_page (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Event Page ID',
     event_id int unsigned NOT NULL   COMMENT 'Event which this page belongs to.',
     intro_text text    COMMENT 'Introductory message for Event Registration page. Text and html allowed. Displayed at the top of Event Registration form.',
     footer_text text    COMMENT 'Footer message for Event Registration page. Text and html allowed. Displayed at the bottom of Event Registration form.',
     confirm_title varchar(255)   DEFAULT NULL COMMENT 'Title for Confirmation page.',
     confirm_text text    COMMENT 'Introductory message for Event Registration page. Text and html allowed. Displayed at the top of Event Registration form.',
     confirm_footer_text text    COMMENT 'Footer message for Event Registration page. Text and html allowed. Displayed at the bottom of Event Registration form.',
     is_email_confirm tinyint   DEFAULT 1 COMMENT 'If true, confirmation is automatically emailed to contact on successful registration.',
     confirm_email_text text    COMMENT 'text to include above standard event info on confirmation email. emails are text-only, so do not allow html for now',
     confirm_from_name varchar(255)    COMMENT 'FROM email name used for confirmation emails.',
     confirm_from_email varchar(255)    COMMENT 'FROM email address used for confirmation emails.',
     cc_confirm varchar(255)    COMMENT 'comma-separated list of email addresses to cc each time a confirmation is sent',
     bcc_confirm varchar(255)    COMMENT 'comma-separated list of email addresses to bcc each time a confirmation is sent',
     default_fee_id int unsigned NOT NULL   COMMENT 'FK to civicrm_custom_option.',
     thankyou_title varchar(255)   DEFAULT NULL COMMENT 'Title for ThankYou page.',
     thankyou_text text    COMMENT 'ThankYou Text.',
     thankyou_footer_text text    COMMENT 'Footer message.' 
,
    PRIMARY KEY ( id )
 
 
 
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_participant
-- *
-- *******************************************************/
CREATE TABLE civicrm_participant (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Participant Id',
     contact_id int unsigned   DEFAULT 0 COMMENT 'FK to Contact ID',
     event_id int unsigned   DEFAULT 0 COMMENT 'FK to Event ID',
     status_id int unsigned   DEFAULT 1 COMMENT 'Participant status ID. Implicit FK to civicrm_option_value where option_group = participant_status. Default of 1 should map to status = Registered.',
     role_id int unsigned   DEFAULT NULL COMMENT 'Participant role ID. Implicit FK to civicrm_option_value where option_group = participant_role.',
     register_date datetime    COMMENT 'When did contact register for event?',
     source varchar(128)    COMMENT 'Source of this event registration.',
     event_level varchar(255)    COMMENT 'Populate with the label (text) associated with a fee level for paid events with multiple levels. Note that we store the label value and not the key',
     is_test tinyint   DEFAULT 0  
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_participant_contact_id FOREIGN KEY (contact_id) REFERENCES civicrm_contact(id)
,      
     CONSTRAINT FK_civicrm_participant_event_id FOREIGN KEY (event_id) REFERENCES civicrm_event(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_participant_payment
-- *
-- *******************************************************/
CREATE TABLE civicrm_participant_payment (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Participant Payment Id',
     participant_id int unsigned NOT NULL   COMMENT 'Participant Id (FK)',
     payment_entity_table varchar(128)    COMMENT 'Table being referenced for payment entity (expected usage is civicrm_contribution).',
     payment_entity_id int unsigned NOT NULL   COMMENT 'FK to table with payment record (e.g. civicrm_contribution.id).' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_participant_payment_participant_id FOREIGN KEY (participant_id) REFERENCES civicrm_participant(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_county
-- *
-- *******************************************************/
CREATE TABLE civicrm_county (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'County ID',
     name varchar(64)    COMMENT 'Name of County',
     abbreviation varchar(4)    COMMENT '2-4 Character Abbreviation of County',
     state_province_id int unsigned NOT NULL   COMMENT 'ID of State / Province that County belongs' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_name_state_id(
        name
      , state_province_id
  )
  
,      
     CONSTRAINT FK_civicrm_county_state_province_id FOREIGN KEY (state_province_id) REFERENCES civicrm_state_province(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_custom_group
-- *
-- * All extended (custom) properties are associated with a group. These are logical sets of related data.
-- *
-- *******************************************************/
CREATE TABLE civicrm_custom_group (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique Custom Group ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this contact',
     name varchar(64)    COMMENT 'Variable name/programmatic handle for this group.',
     title varchar(64)    COMMENT 'Friendly Name.',
     extends enum('Contact', 'Individual', 'Household', 'Organization', 'Location', 'Address', 'Contribution', 'Activity', 'Relationship', 'Phonecall', 'Meeting', 'Group', 'Membership', 'Participant', 'Event')   DEFAULT 'Contact' COMMENT 'Type of object this group extends (can add other options later e.g. contact_address, etc.).',
     extends_entity_column_name varchar(64)    COMMENT 'linking custom group for dynamic object',
     extends_entity_column_value varchar(64)    COMMENT 'linking custom group for dynamic object',
     style enum('Tab', 'Inline')    COMMENT 'Visual relationship between this form and its parent.',
     collapse_display int unsigned   DEFAULT 0 COMMENT 'Will this group be in collapsed or expanded mode on initial display ?',
     help_pre text    COMMENT 'Description and/or help text to display before fields in form.',
     help_post text    COMMENT 'Description and/or help text to display after fields in form.',
     weight int NOT NULL  DEFAULT 1 COMMENT 'Controls display order when multiple extended property groups are setup for the same class.',
     is_active tinyint    COMMENT 'Is this property active?' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_title_domain_id(
        title
      , domain_id
  )
  ,     UNIQUE INDEX UI_name_domain_id(
        name
      , domain_id
  )
  
,      
     CONSTRAINT FK_civicrm_custom_group_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_custom_field
-- *
-- * Stores info about an extended (custom) property (data and form field info).
-- *
-- *******************************************************/
CREATE TABLE civicrm_custom_field (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique Custom Field ID',
     custom_group_id int unsigned NOT NULL   COMMENT 'FK to civicrm_custom_group.',
     name varchar(64)    COMMENT 'Variable name/programmatic handle for this property.',
     label varchar(64)    COMMENT 'Text for form field label (also friendly name for administering this custom property).',
     data_type enum('String', 'Int', 'Float', 'Money', 'Memo', 'Date', 'Boolean', 'StateProvince', 'Country', 'File')    COMMENT 'Controls location of data storage in extended_data table.',
     html_type enum('Text', 'TextArea', 'Select', 'Multi-Select', 'Radio', 'CheckBox', 'Select Date', 'Select State/Province', 'Select Country', 'File')    COMMENT 'HTML types plus several built-in extended types.',
     default_value varchar(255)    COMMENT 'Use form_options.is_default for field_types which use options.',
     is_required tinyint    COMMENT 'Is a value required for this property.',
     is_searchable tinyint    COMMENT 'Is this property searchable.',
     is_search_range tinyint   DEFAULT 0 COMMENT 'Is this property range searchable.',
     weight int NOT NULL  DEFAULT 1 COMMENT 'Controls field display order within an extended property group.',
     validation_id int unsigned    COMMENT 'FK to civicrm_validation. Will be used for custom validation functions.',
     help_pre text    COMMENT 'Description and/or help text to display before this field.',
     help_post text    COMMENT 'Description and/or help text to display after this field.',
     mask varchar(64)    COMMENT 'Optional format instructions for specific field types, like date types.',
     attributes varchar(255)    COMMENT 'Store collection of type-appropriate attributes, e.g. textarea  needs rows/cols attributes',
     javascript varchar(255)    COMMENT 'Optional scripting attributes for field.',
     is_active tinyint    COMMENT 'Is this property active?',
     options_per_line int unsigned    COMMENT 'number of options per line for checkbox and radio',
     start_date_years int unsigned    COMMENT 'Date may be up to start_date_years years prior to tcurrent date  ',
     end_date_years int unsigned    COMMENT 'Date may be up to end_date_years years after to tcurrent date ',
     date_parts varchar(255)    COMMENT 'which date part included in display ',
     note_columns int unsigned    COMMENT ' Number of columns in Note Field ',
     note_rows int unsigned    COMMENT ' Number of rows in Note Field ' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_label_custom_group_id(
        label
      , custom_group_id
  )
  
,      
     CONSTRAINT FK_civicrm_custom_field_custom_group_id FOREIGN KEY (custom_group_id) REFERENCES civicrm_custom_group(id)
,      
     CONSTRAINT FK_civicrm_custom_field_validation_id FOREIGN KEY (validation_id) REFERENCES civicrm_validation(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_custom_value
-- *
-- * Data store for each extended properties.
-- *
-- *******************************************************/
CREATE TABLE civicrm_custom_value (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique ID',
     custom_field_id int unsigned NOT NULL   COMMENT 'Foreign key to civicrm_ext_property.',
     entity_table varchar(64)    COMMENT 'physical tablename for entity being extended by this data, e.g. civicrm_contact',
     entity_id int unsigned NOT NULL   COMMENT 'FK to record in the entity table specified by entity_table column.',
     int_data int    COMMENT 'stores data for ext property data_type = integer. This col supports signed integers.',
     float_data double    COMMENT 'stores data for ext property data_type = float.',
     decimal_data decimal(20,2)    COMMENT 'stores data for ext property data_type = money.',
     char_data varchar(255)    COMMENT 'data for ext property data_type = text.',
     date_data date    COMMENT 'data for ext property data_type = date.',
     memo_data text    COMMENT 'data for ext property data_type = memo.',
     file_id int unsigned    COMMENT 'FK to civicrm_file' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_entity(
        entity_table
      , entity_id
  )
  ,     INDEX index_int_field(
        int_data
      , custom_field_id
  )
  ,     INDEX index_float_field(
        float_data
      , custom_field_id
  )
  ,     INDEX index_decimal_field(
        decimal_data
      , custom_field_id
  )
  ,     INDEX index_char_field(
        char_data
      , custom_field_id
  )
  ,     INDEX index_date_field(
        date_data
      , custom_field_id
  )
  
,      
     CONSTRAINT FK_civicrm_custom_value_custom_field_id FOREIGN KEY (custom_field_id) REFERENCES civicrm_custom_field(id)
,      
     CONSTRAINT FK_civicrm_custom_value_file_id FOREIGN KEY (file_id) REFERENCES civicrm_file(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_email_history
-- *
-- * Email History can be linked to any object in the application.
-- *
-- *******************************************************/
CREATE TABLE civicrm_email_history (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Email History ID',
     subject varchar(255)    COMMENT 'Subject of the email.',
     message text    COMMENT 'Contents of the email.',
     contact_id int unsigned NOT NULL   COMMENT 'FK to Contact who is sending this email',
     sent_date date    COMMENT 'When was this email sent' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_email_history_contact_id FOREIGN KEY (contact_id) REFERENCES civicrm_contact(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_entity_tag
-- *
-- * Tag entities (Contacts, Groups, Actions) to categories.
-- *
-- *******************************************************/
CREATE TABLE civicrm_entity_tag (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'primary key',
     entity_table varchar(64)    COMMENT 'physical tablename for entity being tagged, e.g. civicrm_contact',
     entity_id int unsigned NOT NULL   COMMENT 'FK to entity table specified in entity_table column.',
     tag_id int unsigned NOT NULL   COMMENT 'FK to civicrm_tag' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_entity(
        entity_table
      , entity_id
  )
  
,      
     CONSTRAINT FK_civicrm_entity_tag_tag_id FOREIGN KEY (tag_id) REFERENCES civicrm_tag(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_location
-- *
-- * Define location specific properties
-- *
-- *******************************************************/
CREATE TABLE civicrm_location (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique Location ID',
     entity_table varchar(64) NOT NULL   COMMENT 'Name of table where item being referenced is stored.',
     entity_id int unsigned NOT NULL   COMMENT 'Foreign key to the referenced item.',
     location_type_id int unsigned NOT NULL   COMMENT 'FK to Location Type ID',
     is_primary tinyint   DEFAULT 0 COMMENT 'Is this the primary location for the contact. (allow only ONE primary location / contact.)',
     name varchar(255)     
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_entity(
        entity_table
      , entity_id
  )
  ,     UNIQUE INDEX index_entity_location_type(
        entity_table
      , entity_id
      , location_type_id
  )
  
,      
     CONSTRAINT FK_civicrm_location_location_type_id FOREIGN KEY (location_type_id) REFERENCES civicrm_location_type(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mapping_field
-- *
-- * Individual field mappings for Mapping
-- *
-- *******************************************************/
CREATE TABLE civicrm_mapping_field (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Mapping Field ID',
     mapping_id int unsigned NOT NULL   COMMENT 'Mapping to which this field belongs',
     name varchar(64)    COMMENT 'Mapping field key',
     contact_type varchar(64)    COMMENT 'Contact Type in mapping',
     column_number int unsigned NOT NULL   COMMENT 'Column number for mapping set',
     location_type_id int unsigned    COMMENT 'Location type of this mapping, if required',
     phone_type varchar(64)    COMMENT 'Phone type, if required',
     relationship_type_id int unsigned    COMMENT 'Relationship type, if required',
     relationship_direction varchar(6)    ,
     grouping int unsigned   DEFAULT 1 COMMENT 'Used to group mapping_field records into related sets (e.g. for criteria sets in search builder mappings).',
     operator enum('=', '!=', '>', '<', '>=', '<=', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE')    COMMENT 'SQL WHERE operator for search-builder mapping fields (search criteria).',
     value varchar(255)    COMMENT 'SQL WHERE value for search-builder mapping fields.' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mapping_field_mapping_id FOREIGN KEY (mapping_id) REFERENCES civicrm_mapping(id)
,      
     CONSTRAINT FK_civicrm_mapping_field_location_type_id FOREIGN KEY (location_type_id) REFERENCES civicrm_location_type(id)
,      
     CONSTRAINT FK_civicrm_mapping_field_relationship_type_id FOREIGN KEY (relationship_type_id) REFERENCES civicrm_relationship_type(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_note
-- *
-- * Notes can be linked to any object in the application.
-- *
-- *******************************************************/
CREATE TABLE civicrm_note (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Note ID',
     entity_table varchar(64) NOT NULL   COMMENT 'Name of table where item being referenced is stored.',
     entity_id int unsigned NOT NULL   COMMENT 'Foreign key to the referenced item.',
     note text    COMMENT 'Note and/or Comment.',
     contact_id int unsigned NOT NULL   COMMENT 'FK to Contact ID creator',
     modified_date date    COMMENT 'When was this note last modified/edited',
     subject varchar(255)    COMMENT 'subject of note description' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_entity(
        entity_table
      , entity_id
  )
  
,      
     CONSTRAINT FK_civicrm_note_contact_id FOREIGN KEY (contact_id) REFERENCES civicrm_contact(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_phone
-- *
-- * Phone information for a specific location.
-- *
-- *******************************************************/
CREATE TABLE civicrm_phone (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique Phone ID',
     location_id int unsigned NOT NULL   COMMENT 'Which Location does this phone belong to.',
     is_primary tinyint   DEFAULT 0 COMMENT 'Is this the primary phone for this contact and location.',
     mobile_provider_id int unsigned    COMMENT 'Which Mobile Provider does this phone belong to.',
     phone varchar(32)    COMMENT 'Complete phone number.',
     phone_type enum('Phone', 'Mobile', 'Fax', 'Pager')    COMMENT 'What type of telecom device is this.' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX UI_mobile_provider_id(
        mobile_provider_id
  )
  
,      
     CONSTRAINT FK_civicrm_phone_location_id FOREIGN KEY (location_id) REFERENCES civicrm_location(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_uf_match
-- *
-- * The mapping from an user framework (UF) object to a CRM object.
-- *
-- *******************************************************/
CREATE TABLE civicrm_uf_match (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'System generated ID.',
     uf_id int unsigned NOT NULL   COMMENT 'UF ID',
     contact_id int unsigned NOT NULL   COMMENT 'FK to Contact ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this contact (cached here for ease of use reasons)',
     email varchar(64)    COMMENT 'Email address' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_uf_domain_id(
        uf_id
      , domain_id
  )
  ,     UNIQUE INDEX UI_contact(
        contact_id
  )
  
,      
     CONSTRAINT FK_civicrm_uf_match_contact_id FOREIGN KEY (contact_id) REFERENCES civicrm_contact(id)
,      
     CONSTRAINT FK_civicrm_uf_match_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_log
-- *
-- * Log can be linked to any object in the application.
-- *
-- *******************************************************/
CREATE TABLE civicrm_log (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Log ID',
     entity_table varchar(64) NOT NULL   COMMENT 'Name of table where item being referenced is stored.',
     entity_id int unsigned NOT NULL   COMMENT 'Foreign key to the referenced item.',
     data text    COMMENT 'Updates does to this object if any.',
     modified_id int unsigned    COMMENT 'FK to Contact ID of person under whose credentials this data modification was made.',
     modified_date datetime    COMMENT 'When was the referenced entity created or modified or deleted.' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_entity(
        entity_table
      , entity_id
  )
  
,      
     CONSTRAINT FK_civicrm_log_modified_id FOREIGN KEY (modified_id) REFERENCES civicrm_contact(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_acl_cache
-- *
-- * Cache for acls and contacts
-- *
-- *******************************************************/
CREATE TABLE civicrm_acl_cache (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique table ID',
     contact_id int unsigned    COMMENT 'Foreign Key to Contact',
     acl_id int unsigned NOT NULL   COMMENT 'Foreign Key to ACL',
     modified_date date    COMMENT 'When was this cache entry last modified' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_acl_id(
        acl_id
  )
  
,      
     CONSTRAINT FK_civicrm_acl_cache_contact_id FOREIGN KEY (contact_id) REFERENCES civicrm_contact(id)
,      
     CONSTRAINT FK_civicrm_acl_cache_acl_id FOREIGN KEY (acl_id) REFERENCES civicrm_acl(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_group
-- *
-- * Provide grouping of related contacts
-- *
-- *******************************************************/
CREATE TABLE civicrm_group (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Group ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this group',
     name varchar(64)    COMMENT 'Internal name of Group.',
     title varchar(64)    COMMENT 'Name of Group.',
     description varchar(255)    COMMENT 'Optional verbose description of the group.',
     source varchar(64)    COMMENT 'Module or process which created this group.',
     saved_search_id int unsigned    COMMENT 'FK to saved search table.',
     is_active tinyint    COMMENT 'Is this entry active?',
     visibility enum('User and User Admin Only', 'Public User Pages', 'Public User Pages and Listings')   DEFAULT 'User and User Admin Only' COMMENT 'In what context(s) is this field visible.',
     where_clause text    COMMENT 'the sql where clause if a saved search acl',
     select_tables text    COMMENT 'the tables to be included in a select data',
     where_tables text    COMMENT 'the tables to be included in the count statement' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_title_domain_id(
        title
      , domain_id
  )
  ,     UNIQUE INDEX UI_name_domain_id(
        name
      , domain_id
  )
  
,      
     CONSTRAINT FK_civicrm_group_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
,      
     CONSTRAINT FK_civicrm_group_saved_search_id FOREIGN KEY (saved_search_id) REFERENCES civicrm_saved_search(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_subscription_history
-- *
-- * History information of subscribe/unsubscribe actions
-- *
-- *******************************************************/
CREATE TABLE civicrm_subscription_history (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Internal Id',
     contact_id int unsigned NOT NULL   COMMENT 'Contact Id',
     group_id int unsigned    COMMENT 'Group Id',
     date datetime NOT NULL   COMMENT 'Date of the (un)subscription',
     method enum('Admin', 'Email', 'Web', 'API')    COMMENT 'How the (un)subscription was triggered',
     status enum('Added', 'Removed', 'Pending')    COMMENT 'The state of the contact within the group',
     tracking varchar(255)    COMMENT 'IP address or other tracking info' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_subscription_history_contact_id FOREIGN KEY (contact_id) REFERENCES civicrm_contact(id)
,      
     CONSTRAINT FK_civicrm_subscription_history_group_id FOREIGN KEY (group_id) REFERENCES civicrm_group(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_relationship
-- *
-- * Relationship between any 2 types of contacts.
-- *
-- *******************************************************/
CREATE TABLE civicrm_relationship (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Relationship ID',
     contact_id_a int unsigned NOT NULL   COMMENT 'id of the first contact',
     contact_id_b int unsigned NOT NULL   COMMENT 'id of the second contact',
     relationship_type_id int unsigned NOT NULL   COMMENT 'id of the relationship',
     start_date date    COMMENT 'date when the relationship started',
     end_date date    COMMENT 'date when the relationship ended',
     is_active tinyint   DEFAULT 1 COMMENT 'is the relationship active ?',
     description varchar(255)    COMMENT 'Optional verbose description for the relationship.' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_relationship_contact_id_a FOREIGN KEY (contact_id_a) REFERENCES civicrm_contact(id)
,      
     CONSTRAINT FK_civicrm_relationship_contact_id_b FOREIGN KEY (contact_id_b) REFERENCES civicrm_contact(id)
,      
     CONSTRAINT FK_civicrm_relationship_relationship_type_id FOREIGN KEY (relationship_type_id) REFERENCES civicrm_relationship_type(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing_event_subscribe
-- *
-- * Tracks when a (new) contact subscribes to a group by email
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing_event_subscribe (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     group_id int unsigned NOT NULL   COMMENT 'FK to Group',
     contact_id int unsigned NOT NULL   COMMENT 'FK to Contact',
     hash varchar(255) NOT NULL   COMMENT 'Security hash',
     time_stamp datetime NOT NULL   COMMENT 'When this subscription event occurred.' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mailing_event_subscribe_group_id FOREIGN KEY (group_id) REFERENCES civicrm_group(id)
,      
     CONSTRAINT FK_civicrm_mailing_event_subscribe_contact_id FOREIGN KEY (contact_id) REFERENCES civicrm_contact(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing_event_confirm
-- *
-- * Tracks when a subscription event is confirmed by email
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing_event_confirm (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     event_subscribe_id int unsigned NOT NULL   COMMENT 'FK to civicrm_mailing_event_subscribe',
     time_stamp datetime NOT NULL   COMMENT 'When this confirmation event occurred.' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mailing_event_confirm_event_subscribe_id FOREIGN KEY (event_subscribe_id) REFERENCES civicrm_mailing_event_subscribe(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_contribution_page
-- *
-- * A Contribution object store meta information about a single customized contribution page
-- *
-- *******************************************************/
CREATE TABLE civicrm_contribution_page (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Contribution Id',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this page',
     title varchar(255)    COMMENT 'Contribution Page title. For top of page display',
     intro_text text    COMMENT 'Text and html allowed. Displayed below title.',
     contribution_type_id int unsigned NOT NULL   COMMENT 'default Contribution type assigned to contributions submitted via this page, e.g. Contribution, Campaign Contribution',
     is_credit_card_only tinyint   DEFAULT 0 COMMENT 'if true - processing logic must reject transaction at confirmation stage if pay method != credit card',
     is_monetary tinyint   DEFAULT 1 COMMENT 'if true - allows real-time monetary transactions otherwise non-monetary transactions',
     is_recur tinyint   DEFAULT 0 COMMENT 'if true - allows recurring contributions, valid only for PayPal_Standard',
     is_allow_other_amount tinyint   DEFAULT 0 COMMENT 'if true, page will include an input text field where user can enter their own amount',
     default_amount_id int unsigned NOT NULL   COMMENT 'FK to civicrm_custom_option.',
     min_amount decimal(20,2)    COMMENT 'if other amounts allowed, user can configure minimum allowed.',
     max_amount decimal(20,2)    COMMENT 'if other amounts allowed, user can configure maximum allowed.',
     goal_amount decimal(20,2)    COMMENT 'The target goal for this page, allows people to build a goal meter',
     thankyou_title varchar(255)    COMMENT 'Title for Thank-you page (header title tag, and display at the top of the page).',
     thankyou_text text    COMMENT 'text and html allowed. displayed above result on success page',
     thankyou_footer text    COMMENT 'Text and html allowed. displayed at the bottom of the success page. Common usage is to include link(s) to other pages such as tell-a-friend, etc.',
     is_email_receipt tinyint   DEFAULT 1 COMMENT 'if true, receipt is automatically emailed to contact on success',
     receipt_from_name varchar(255)    COMMENT 'FROM email name used for receipts generated by contributions to this contribution page.',
     receipt_from_email varchar(255)    COMMENT 'FROM email address used for receipts generated by contributions to this contribution page.',
     cc_receipt varchar(255)    COMMENT 'comma-separated list of email addresses to cc each time a receipt is sent',
     bcc_receipt varchar(255)    COMMENT 'comma-separated list of email addresses to bcc each time a receipt is sent',
     receipt_text text    COMMENT 'text to include above standard receipt info on receipt email. emails are text-only, so do not allow html for now',
     is_active tinyint    COMMENT 'Is this property active?',
     footer_text text    COMMENT 'Text and html allowed. Displayed at the bottom of the first page of the contribution wizard.',
     amount_block_is_active tinyint   DEFAULT 1 COMMENT 'Is this property active?',
     is_thermometer tinyint    COMMENT 'Should this contribution have the thermometer block enabled?',
     thermometer_title varchar(255)    COMMENT 'Title for thermometer block.',
     honor_block_is_active tinyint    COMMENT 'Should this contribution have the honor  block enabled?',
     honor_block_title varchar(255)    COMMENT 'Title for honor block.',
     honor_block_text text    COMMENT 'text for honor block.' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_contribution_page_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
,      
     CONSTRAINT FK_civicrm_contribution_page_contribution_type_id FOREIGN KEY (contribution_type_id) REFERENCES civicrm_contribution_type(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_membership_type
-- *
-- * Sites can configure multiple types of memberships. They encode the owner organization, fee, and the rules needed to set start and end (expire) dates when a member signs up for that type.
-- *
-- *******************************************************/
CREATE TABLE civicrm_membership_type (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Membership Id',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this contact',
     name varchar(128)    COMMENT 'Name of Membership Type',
     description varchar(255)    COMMENT 'Description of Membership Type',
     member_of_contact_id int unsigned NOT NULL   COMMENT 'Owner organization for this membership type. FK to Contact ID',
     contribution_type_id int unsigned NOT NULL   COMMENT 'If membership is paid by a contribution - what contribution type should be used. FK to Contribution Type ID',
     minimum_fee decimal(20,2)   DEFAULT 0 COMMENT 'Minimum fee for this membership (0 for free/complimentary memberships).',
     duration_unit enum('day', 'month', 'year', 'lifetime')    COMMENT 'Unit in which membership period is expressed.',
     duration_interval int    COMMENT 'Number of duration units in membership period (e.g. 1 year, 12 months).',
     period_type enum('rolling', 'fixed')    COMMENT 'Rolling membership period starts on signup date. Fixed membership periods start on fixed_period_start_day.',
     fixed_period_start_day int   DEFAULT 0101 COMMENT 'For fixed period memberships, month and day (mmdd) on which subscription/membership will start. Period start is back-dated unless after rollover day.',
     fixed_period_rollover_day int   DEFAULT 1231 COMMENT 'For fixed period memberships, signups after this day (mmdd) rollover to next period.',
     relationship_type_id int unsigned    COMMENT 'FK to Relationship Type ID',
     relationship_direction varchar(6)    ,
     visibility varchar(64)    ,
     weight int    ,
     renewal_msg_id int unsigned    COMMENT 'FK to civicrm_msg_template.id',
     renewal_reminder_day int    COMMENT 'Number of days prior to expiration to send renewal reminder',
     is_active tinyint   DEFAULT 1 COMMENT 'Is this membership_type enabled' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_membership_type_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
,      
     CONSTRAINT FK_civicrm_membership_type_member_of_contact_id FOREIGN KEY (member_of_contact_id) REFERENCES civicrm_contact(id)
,      
     CONSTRAINT FK_civicrm_membership_type_contribution_type_id FOREIGN KEY (contribution_type_id) REFERENCES civicrm_contribution_type(id)
,      
     CONSTRAINT FK_civicrm_membership_type_relationship_type_id FOREIGN KEY (relationship_type_id) REFERENCES civicrm_relationship_type(id)
,      
     CONSTRAINT FK_civicrm_membership_type_renewal_msg_id FOREIGN KEY (renewal_msg_id) REFERENCES civicrm_msg_template(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_membership
-- *
-- * Contact Membership records.
-- *
-- *******************************************************/
CREATE TABLE civicrm_membership (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Membership Id',
     contact_id int unsigned NOT NULL   COMMENT 'FK to Contact ID',
     membership_type_id int unsigned NOT NULL   COMMENT 'FK to Membership Type',
     join_date date    COMMENT 'Beginning of initial membership period (member since...).',
     start_date date    COMMENT 'Beginning of current uninterrupted membership period.',
     end_date date    COMMENT 'Current membership period expire date.',
     source varchar(128)    ,
     status_id int unsigned NOT NULL   COMMENT 'FK to Membership Status',
     is_override tinyint    COMMENT 'Admin users may set a manual status which overrides the calculated status. When this flag is true, automated status update scripts should NOT modify status for the record.',
     reminder_date date    COMMENT 'When should a reminder be sent.',
     owner_membership_id int unsigned    COMMENT 'Optional FK to Parent Membership.' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_owner_membership_id(
        owner_membership_id
  )
  
,      
     CONSTRAINT FK_civicrm_membership_contact_id FOREIGN KEY (contact_id) REFERENCES civicrm_contact(id)
,      
     CONSTRAINT FK_civicrm_membership_membership_type_id FOREIGN KEY (membership_type_id) REFERENCES civicrm_membership_type(id)
,      
     CONSTRAINT FK_civicrm_membership_status_id FOREIGN KEY (status_id) REFERENCES civicrm_membership_status(id)
,      
     CONSTRAINT FK_civicrm_membership_owner_membership_id FOREIGN KEY (owner_membership_id) REFERENCES civicrm_membership(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_membership_payment
-- *
-- * Membership Payment
-- *
-- *******************************************************/
CREATE TABLE civicrm_membership_payment (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     membership_id int unsigned NOT NULL   COMMENT 'FK to Membership table',
     payment_entity_table varchar(128)    COMMENT 'Table being referenced for payment entity.',
     payment_entity_id int unsigned    COMMENT 'FK to table with payment record (expected usage is civicrm_contribution).' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_payment_entity(
        payment_entity_table
      , payment_entity_id
  )
  
,      
     CONSTRAINT FK_civicrm_membership_payment_membership_id FOREIGN KEY (membership_id) REFERENCES civicrm_membership(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_membership_block
-- *
-- * A Membership Block stores admin configurable status options and rules
-- *
-- *******************************************************/
CREATE TABLE civicrm_membership_block (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Membership Id',
     entity_table varchar(64)    COMMENT 'Name for Membership Status',
     entity_id int unsigned NOT NULL   COMMENT 'FK to civicrm_contribution_page.id',
     membership_types varchar(255)    COMMENT 'Membership types to be exposed by this block',
     membership_type_default int unsigned    COMMENT 'Optional foreign key to membership_type',
     display_min_fee tinyint   DEFAULT 1 COMMENT 'Display minimum membership fee',
     is_separate_payment tinyint   DEFAULT 1 COMMENT 'Should membership transactions be processed separately',
     new_title varchar(255)    COMMENT 'Title to display at top of block',
     new_text text    COMMENT 'Text to display below title',
     renewal_title varchar(255)    COMMENT 'Title for renewal',
     renewal_text text    COMMENT 'Text to display for member renewal',
     is_required tinyint   DEFAULT 0 COMMENT 'Is membership sign up optional',
     is_active tinyint   DEFAULT 1 COMMENT 'Is this membership_block enabled' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_membership_block_entity_id FOREIGN KEY (entity_id) REFERENCES civicrm_contribution_page(id)
,      
     CONSTRAINT FK_civicrm_membership_block_membership_type_default FOREIGN KEY (membership_type_default) REFERENCES civicrm_membership_type(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_membership_log
-- *
-- * Logs actions which affect a Membership record (signup, status override, renewal, etc.)
-- *
-- *******************************************************/
CREATE TABLE civicrm_membership_log (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     membership_id int unsigned NOT NULL   COMMENT 'FK to Membership table',
     status_id int unsigned NOT NULL   COMMENT 'New status assigned to membership by this action. FK to Membership Status',
     start_date date    COMMENT 'New membership period start date',
     end_date date    COMMENT 'New membership period expiration date.',
     modified_id int unsigned    COMMENT 'FK to Contact ID of person under whose credentials this data modification was made.',
     modified_date date    COMMENT 'Date this membership modification action was logged.',
     renewal_reminder_date date    COMMENT 'The day we sent a renewal reminder' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_membership_log_membership_id FOREIGN KEY (membership_id) REFERENCES civicrm_membership(id)
,      
     CONSTRAINT FK_civicrm_membership_log_status_id FOREIGN KEY (status_id) REFERENCES civicrm_membership_status(id)
,      
     CONSTRAINT FK_civicrm_membership_log_modified_id FOREIGN KEY (modified_id) REFERENCES civicrm_contact(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_phonecall
-- *
-- * Record details regarding phone call to a contact
-- *
-- *******************************************************/
CREATE TABLE civicrm_phonecall (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique Phone call ID',
     source_contact_id int unsigned NOT NULL   COMMENT 'Contact ID of person making the call. This will generally an authenticated user.',
     target_entity_table varchar(64) NOT NULL   COMMENT 'Name of table where item being referenced is stored.',
     target_entity_id int unsigned NOT NULL   COMMENT 'Foreign key to the referenced item.',
     subject varchar(64)    COMMENT 'Short description of the subject of this call.',
     scheduled_date_time datetime    COMMENT 'Date and time phonecall is scheduled to be made.',
     duration_hours int unsigned    COMMENT 'Planned or actual duration of call - hours.',
     duration_minutes int unsigned    COMMENT 'Planned or actual duration of call - minutes.',
     phone_id int unsigned    COMMENT 'Phone ID of the number called (optional - used if an existing phone number is selected).',
     phone_number varchar(64)    COMMENT 'Phone number in case the number does not exist in the civicrm_phone table.',
     details text    COMMENT 'Details about the call.',
     status enum('Scheduled', 'Left Message', 'Unreachable', 'Completed')    COMMENT 'What is the status of this phone call? Completed calls result in activity history entry.',
     parent_id int unsigned    COMMENT 'Parent phone call ID (if this is a follow-up item). This is not currently implemented' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_phonecall_source_contact_id FOREIGN KEY (source_contact_id) REFERENCES civicrm_contact(id)
,      
     CONSTRAINT FK_civicrm_phonecall_phone_id FOREIGN KEY (phone_id) REFERENCES civicrm_phone(id)
,      
     CONSTRAINT FK_civicrm_phonecall_parent_id FOREIGN KEY (parent_id) REFERENCES civicrm_phonecall(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_activity_history
-- *
-- * Record history for an entity in the crm module
-- *
-- *******************************************************/
CREATE TABLE civicrm_activity_history (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'table record id',
     entity_table varchar(64)    COMMENT 'physical tablename for entity being tagged, e.g. civicrm_contact',
     entity_id int unsigned NOT NULL   COMMENT 'FK to entity table specified in entity_table column',
     activity_type varchar(64)    COMMENT 'sortable label for this activity assigned be registering module or user (e.g. Phone Call)',
     module varchar(64)    COMMENT 'Display name of module which registered this activity',
     callback varchar(64)    COMMENT 'Function to call which will return URL for viewing details',
     activity_id int unsigned NOT NULL   COMMENT 'FK to details item - passed to callback',
     activity_summary varchar(255)    COMMENT 'brief description of activity for summary display - as populated by registering module',
     activity_date datetime   DEFAULT 0 COMMENT 'when did this activity occur',
     relationship_id int unsigned    COMMENT 'OPTIONAL FK to civicrm_relationship.id. Which relationship (of this contact) potentially triggered this activity, i.e. he donated because he was a Board Member of Org X / Employee of Org Y',
     group_id int unsigned    COMMENT 'OPTIONAL FK to civicrm_group.id. Was this part of a group communication that triggered this activity?' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_entity(
        entity_table
      , entity_id
  )
  ,     INDEX index_activity(
        activity_id
  )
  
,      
     CONSTRAINT FK_civicrm_activity_history_relationship_id FOREIGN KEY (relationship_id) REFERENCES civicrm_relationship(id)
,      
     CONSTRAINT FK_civicrm_activity_history_group_id FOREIGN KEY (group_id) REFERENCES civicrm_group(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_address
-- *
-- * Stores the physical street / mailing address. This format should be capable of storing ALL international addresses.
-- *
-- *******************************************************/
CREATE TABLE civicrm_address (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique Address ID',
     location_id int unsigned NOT NULL   COMMENT 'Which Location does this address belong to.',
     street_address varchar(96)    COMMENT 'Concatenation of all routable street address components (prefix, street number, street name, suffix, unit number OR P.O. Box). Apps should be able to determine physical location with this data (for mapping, mail delivery, etc.).',
     street_number int    COMMENT 'Numeric portion of address number on the street, e.g. For 112A Main St, the street_number = 112.',
     street_number_suffix varchar(8)    COMMENT 'Non-numeric portion of address number on the street, e.g. For 112A Main St, the street_number_suffix = A',
     street_number_predirectional varchar(8)    COMMENT 'Directional prefix, e.g. SE Main St, SE is the prefix.',
     street_name varchar(64)    COMMENT 'Actual street name, excluding St, Dr, Rd, Ave, e.g. For 112 Main St, the street_name = Main.',
     street_type varchar(8)    COMMENT 'St, Rd, Dr, etc.',
     street_number_postdirectional varchar(8)    COMMENT 'Directional prefix, e.g. Main St S, S is the suffix.',
     street_unit varchar(16)    COMMENT 'Secondary unit designator, e.g. Apt 3 or Unit # 14, or Bldg 1200',
     supplemental_address_1 varchar(96)    COMMENT 'Supplemental Address Information, Line 1',
     supplemental_address_2 varchar(96)    COMMENT 'Supplemental Address Information, Line 2',
     supplemental_address_3 varchar(96)    COMMENT 'Supplemental Address Information, Line 3',
     city varchar(64)    COMMENT 'City, Town or Village Name.',
     county_id int unsigned    COMMENT 'Which County does this address belong to.',
     state_province_id int unsigned    COMMENT 'Which State_Province does this address belong to.',
     postal_code_suffix varchar(12)    COMMENT 'Store the suffix, like the +4 part in the USPS system.',
     postal_code varchar(12)    COMMENT 'Store both US (zip5) AND international postal codes. App is responsible for country/region appropriate validation.',
     usps_adc varchar(32)    COMMENT 'USPS Bulk mailing code.',
     country_id int unsigned    COMMENT 'Which Country does this address belong to.',
     geo_coord_id int unsigned    COMMENT 'Which Geo_Coord does this address belong to.',
     geo_code_1 double    COMMENT 'Latitude or UTM (Universal Transverse Mercator Grid) Northing.',
     geo_code_2 double    COMMENT 'Longitude or UTM (Universal Transverse Mercator Grid) Easting.',
     timezone varchar(8)    COMMENT 'Timezone expressed as a UTC offset - e.g. United States CST would be written as "UTC-6".',
     note varchar(255)    COMMENT 'Optional misc info (e.g. delivery instructions) for this address.' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_location(
        location_id
  )
  ,     INDEX index_street_name(
        street_name
  )
  ,     INDEX index_city(
        city
  )
  
,      
     CONSTRAINT FK_civicrm_address_location_id FOREIGN KEY (location_id) REFERENCES civicrm_location(id)
,      
     CONSTRAINT FK_civicrm_address_county_id FOREIGN KEY (county_id) REFERENCES civicrm_county(id)
,      
     CONSTRAINT FK_civicrm_address_state_province_id FOREIGN KEY (state_province_id) REFERENCES civicrm_state_province(id)
,      
     CONSTRAINT FK_civicrm_address_country_id FOREIGN KEY (country_id) REFERENCES civicrm_country(id)
,      
     CONSTRAINT FK_civicrm_address_geo_coord_id FOREIGN KEY (geo_coord_id) REFERENCES civicrm_geo_coord(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_email
-- *
-- * Email information for a specific location.
-- *
-- *******************************************************/
CREATE TABLE civicrm_email (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique Email ID',
     location_id int unsigned NOT NULL   COMMENT 'Which Location does this email belong to.',
     email varchar(64)    COMMENT 'Email address',
     is_primary tinyint   DEFAULT 0 COMMENT 'Is this the primary email for this contact and location.',
     on_hold tinyint NOT NULL  DEFAULT 0 COMMENT 'Is this address on bounce hold?',
     hold_date datetime    COMMENT 'When the address went on bounce hold',
     reset_date datetime    COMMENT 'When the address bounce status was last reset' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX UI_email(
        email
  )
  
,      
     CONSTRAINT FK_civicrm_email_location_id FOREIGN KEY (location_id) REFERENCES civicrm_location(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_im
-- *
-- * IM information for a specific location.
-- *
-- *******************************************************/
CREATE TABLE civicrm_im (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique IM ID',
     location_id int unsigned NOT NULL   COMMENT 'Which Location does this IM identifier belong to.',
     name varchar(64)    COMMENT 'IM screen name',
     provider_id int unsigned    COMMENT 'Which IM Provider does this screen name belong to.',
     is_primary tinyint   DEFAULT 0 COMMENT 'Is this the primary IM for this contact and location.' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX UI_provider_id(
        provider_id
  )
  
,      
     CONSTRAINT FK_civicrm_im_location_id FOREIGN KEY (location_id) REFERENCES civicrm_location(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_uf_group
-- *
-- * User framework groups. Each group represents a form which encompasses a set of fields defined in civicrm_uf_fields table. Initially will be used for CiviCRM Profile form(s). Subsequently we anticipate using this to define other public facing forms (e.g. online donation solicitation forms, mailing list preferences, etc.).
-- *
-- *******************************************************/
CREATE TABLE civicrm_uf_group (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique table ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this form.',
     is_active tinyint   DEFAULT 1 COMMENT 'Is this form currently active? If false, hide all related fields for all sharing contexts.',
     form_type enum('CiviCRM Profile')    COMMENT 'Type of form.',
     title varchar(64)    COMMENT 'Form title.',
     collapse_display int unsigned   DEFAULT 0 COMMENT 'Will this group be in collapsed or expanded mode on initial display ?',
     help_pre text    COMMENT 'Description and/or help text to display before fields in form.',
     help_post text    COMMENT 'Description and/or help text to display after fields in form.',
     limit_listings_group_id int unsigned    COMMENT 'Group id, foriegn key from civicrm_group',
     post_URL varchar(255)    COMMENT 'Redirect to URL.',
     add_to_group_id int unsigned    COMMENT 'foreign key to civicrm_group_id',
     add_captcha tinyint   DEFAULT 0 COMMENT 'Should a CAPTCHA widget be included this Profile form.',
     is_map tinyint   DEFAULT 0 COMMENT 'Do we want to map results from this profile.',
     is_edit_link tinyint   DEFAULT 0 COMMENT 'Should edit link display in profile selector',
     is_uf_link tinyint   DEFAULT 0 COMMENT 'Should we display a link to the website profile in profile selector',
     is_update_dupe tinyint   DEFAULT 0 COMMENT 'Should we update the contact record if we find a duplicate',
     cancel_URL varchar(255)    COMMENT 'Redirect to URL when Cancle button clik .' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_uf_group_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
,      
     CONSTRAINT FK_civicrm_uf_group_limit_listings_group_id FOREIGN KEY (limit_listings_group_id) REFERENCES civicrm_group(id)
,      
     CONSTRAINT FK_civicrm_uf_group_add_to_group_id FOREIGN KEY (add_to_group_id) REFERENCES civicrm_group(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_uf_field
-- *
-- * User Framework fields and their properties.
-- *
-- *******************************************************/
CREATE TABLE civicrm_uf_field (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique table ID',
     uf_group_id int unsigned NOT NULL   COMMENT 'Which form does this field belong to.',
     field_name varchar(64)    COMMENT 'Name for CiviCRM field which is being exposed for sharing.',
     is_active tinyint   DEFAULT 1 COMMENT 'Is this field currently shareable? If false, hide the field for all sharing contexts.',
     is_view tinyint   DEFAULT 0 COMMENT 'the field is view only and not editable in user forms.',
     is_required tinyint   DEFAULT 0 COMMENT 'Is this field required when included in a user or registration form?',
     weight int NOT NULL  DEFAULT 1 COMMENT 'Controls field display order when user framework fields are displayed in registration and account editing forms.',
     help_post text    COMMENT 'Description and/or help text to display after this field.',
     visibility enum('User and User Admin Only', 'Public User Pages', 'Public User Pages and Listings')   DEFAULT 'User and User Admin Only' COMMENT 'In what context(s) is this field visible.',
     in_selector tinyint   DEFAULT 0 COMMENT 'Is this field included as a column in the selector table?',
     is_searchable tinyint   DEFAULT 0 COMMENT 'Is this field included search form of profile?',
     location_type_id int unsigned    COMMENT 'Location type of this mapping, if required',
     phone_type varchar(64)    COMMENT 'Phone type, if required',
     label varchar(255)    COMMENT 'To save label for fields.',
     field_type varchar(255)    COMMENT 'This field saves field type (ie individual,household.. field etc).' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_uf_field_uf_group_id FOREIGN KEY (uf_group_id) REFERENCES civicrm_uf_group(id)
,      
     CONSTRAINT FK_civicrm_uf_field_location_type_id FOREIGN KEY (location_type_id) REFERENCES civicrm_location_type(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_uf_join
-- *
-- * User framework join table. This links various internal civicrm object with a profile. Initial use cases are the donation object and the user module
-- *
-- *******************************************************/
CREATE TABLE civicrm_uf_join (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Unique table ID',
     is_active tinyint   DEFAULT 1 COMMENT 'Is this join currently active?',
     module varchar(64) NOT NULL   COMMENT 'Module which owns this uf_join instance, e.g. User Registration, CiviDonate, etc.',
     entity_table varchar(64)    COMMENT 'Name of table where item being referenced is stored. Modules which only need a single collection of uf_join instances may choose not to populate entity_table and entity_id.',
     entity_id int unsigned    COMMENT 'Foreign key to the referenced item.',
     weight int NOT NULL  DEFAULT 1 COMMENT 'Controls display order when multiple user framework groups are setup for concurrent display.',
     uf_group_id int unsigned NOT NULL   COMMENT 'Which form does this field belong to.' 
,
    PRIMARY KEY ( id )
 
    ,     INDEX index_entity(
        entity_table
      , entity_id
  )
  
,      
     CONSTRAINT FK_civicrm_uf_join_uf_group_id FOREIGN KEY (uf_group_id) REFERENCES civicrm_uf_group(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_group_contact
-- *
-- * Join table sets membership for 'static' groups. Also used to store 'opt-out' entries for 'query' type groups (status = 'OUT')
-- *
-- *******************************************************/
CREATE TABLE civicrm_group_contact (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'primary key',
     group_id int unsigned NOT NULL   COMMENT 'FK to civicrm_group',
     contact_id int unsigned NOT NULL   COMMENT 'FK to civicrm_contact',
     status enum('Added', 'Removed', 'Pending')    COMMENT 'status of contact relative to membership in group',
     location_id int unsigned    COMMENT 'Optional location to associate with this membership',
     email_id int unsigned    COMMENT 'Optional email to associate with this membership' 
,
    PRIMARY KEY ( id )
 
    ,     UNIQUE INDEX UI_contact_group(
        contact_id
      , group_id
  )
  
,      
     CONSTRAINT FK_civicrm_group_contact_group_id FOREIGN KEY (group_id) REFERENCES civicrm_group(id)
,      
     CONSTRAINT FK_civicrm_group_contact_contact_id FOREIGN KEY (contact_id) REFERENCES civicrm_contact(id)
,      
     CONSTRAINT FK_civicrm_group_contact_location_id FOREIGN KEY (location_id) REFERENCES civicrm_location(id)
,      
     CONSTRAINT FK_civicrm_group_contact_email_id FOREIGN KEY (email_id) REFERENCES civicrm_email(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing_event_queue
-- *
-- * A collection of all intended recipients of a job
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing_event_queue (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     job_id int unsigned NOT NULL   COMMENT 'FK to Job',
     email_id int unsigned NOT NULL   COMMENT 'FK to Email',
     contact_id int unsigned NOT NULL   COMMENT 'FK to Contact',
     hash varchar(255) NOT NULL   COMMENT 'Security hash' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mailing_event_queue_job_id FOREIGN KEY (job_id) REFERENCES civicrm_mailing_job(id)
,      
     CONSTRAINT FK_civicrm_mailing_event_queue_email_id FOREIGN KEY (email_id) REFERENCES civicrm_email(id)
,      
     CONSTRAINT FK_civicrm_mailing_event_queue_contact_id FOREIGN KEY (contact_id) REFERENCES civicrm_contact(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing_event_bounce
-- *
-- * Tracks when and why an email bounced.
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing_event_bounce (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     event_queue_id int unsigned NOT NULL   COMMENT 'FK to EventQueue',
     bounce_type_id int unsigned    COMMENT 'What type of bounce was it?',
     bounce_reason varchar(255)    COMMENT 'The reason the email bounced.',
     time_stamp datetime NOT NULL   COMMENT 'When this bounce event occurred.' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mailing_event_bounce_event_queue_id FOREIGN KEY (event_queue_id) REFERENCES civicrm_mailing_event_queue(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing_event_delivered
-- *
-- * Tracks when a queued email is actually delivered to the MTA
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing_event_delivered (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     event_queue_id int unsigned NOT NULL   COMMENT 'FK to EventQueue',
     time_stamp datetime NOT NULL   COMMENT 'When this delivery event occurred.' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mailing_event_delivered_event_queue_id FOREIGN KEY (event_queue_id) REFERENCES civicrm_mailing_event_queue(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing_event_forward
-- *
-- * Tracks when a contact forwards a mailing to a (new) contact
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing_event_forward (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     event_queue_id int unsigned NOT NULL   COMMENT 'FK to EventQueue',
     dest_queue_id int unsigned NOT NULL   COMMENT 'FK to EventQueue for destination',
     time_stamp datetime NOT NULL   COMMENT 'When this forward event occurred.' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mailing_event_forward_event_queue_id FOREIGN KEY (event_queue_id) REFERENCES civicrm_mailing_event_queue(id)
,      
     CONSTRAINT FK_civicrm_mailing_event_forward_dest_queue_id FOREIGN KEY (dest_queue_id) REFERENCES civicrm_mailing_event_queue(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing_event_opened
-- *
-- * Tracks when a delivered email is opened by the recipient
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing_event_opened (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     event_queue_id int unsigned NOT NULL   COMMENT 'FK to EventQueue',
     time_stamp datetime NOT NULL   COMMENT 'When this open event occurred.' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mailing_event_opened_event_queue_id FOREIGN KEY (event_queue_id) REFERENCES civicrm_mailing_event_queue(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing_event_reply
-- *
-- * Tracks when a contact replies to a mailing
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing_event_reply (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     event_queue_id int unsigned NOT NULL   COMMENT 'FK to EventQueue',
     time_stamp datetime NOT NULL   COMMENT 'When this reply event occurred.' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mailing_event_reply_event_queue_id FOREIGN KEY (event_queue_id) REFERENCES civicrm_mailing_event_queue(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing_event_trackable_url_open
-- *
-- * Tracks when a TrackableURL is clicked by a recipient.
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing_event_trackable_url_open (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     event_queue_id int unsigned NOT NULL   COMMENT 'FK to EventQueue',
     trackable_url_id int unsigned NOT NULL   COMMENT 'FK to TrackableURL',
     time_stamp datetime NOT NULL   COMMENT 'When this trackable URL open occurred.' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mailing_event_trackable_url_open_event_queue_id FOREIGN KEY (event_queue_id) REFERENCES civicrm_mailing_event_queue(id)
,      
     CONSTRAINT FK_civicrm_mailing_event_trackable_url_open_trackable_url_id FOREIGN KEY (trackable_url_id) REFERENCES civicrm_mailing_trackable_url(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_mailing_event_unsubscribe
-- *
-- * Tracks when a recipient unsubscribes from a group/domain
-- *
-- *******************************************************/
CREATE TABLE civicrm_mailing_event_unsubscribe (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     event_queue_id int unsigned NOT NULL   COMMENT 'FK to EventQueue',
     org_unsubscribe tinyint NOT NULL   COMMENT 'Unsubscribe at org- or group-level',
     time_stamp datetime NOT NULL   COMMENT 'When this delivery event occurred.' 
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_mailing_event_unsubscribe_event_queue_id FOREIGN KEY (event_queue_id) REFERENCES civicrm_mailing_event_queue(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_contribution
-- *
-- *******************************************************/
CREATE TABLE civicrm_contribution (


     id int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Contribution ID',
     domain_id int unsigned NOT NULL   COMMENT 'Which Domain owns this contribution class.',
     contact_id int unsigned NOT NULL   COMMENT 'FK to Contact ID',
     solicitor_id int unsigned    COMMENT 'FK to Solicitor ID',
     contribution_type_id int unsigned    COMMENT 'FK to Contribution Type',
     contribution_page_id int unsigned    COMMENT 'The Contribution Page which triggered this contribution',
     payment_instrument_id int unsigned    COMMENT 'FK to Payment Instrument',
     receive_date datetime    COMMENT 'when was gift received',
     non_deductible_amount decimal(20,2)   DEFAULT 0 COMMENT 'Portion of total amount which is NOT tax deductible. Equal to total_amount for non-deductible contribution types.',
     total_amount decimal(20,2) NOT NULL   COMMENT 'Total amount of this contribution. Use market value for non-monetary gifts.',
     fee_amount decimal(20,2)    COMMENT 'actual processor fee if known - may be 0.',
     net_amount decimal(20,2)    COMMENT 'actual funds transfer amount. total less fees. if processor does not report actual fee during transaction, this is set to total_amount.',
     trxn_id varchar(255)    COMMENT 'unique transaction id. may be processor id, bank id + trans id, or account number + check number... depending on payment_method',
     invoice_id varchar(255)    COMMENT 'unique invoice id, system generated or passed in',
     currency varchar(64) NOT NULL   COMMENT '3 character string, value derived from payment processor config setting.',
     cancel_date datetime    COMMENT 'when was gift cancelled',
     cancel_reason text    ,
     receipt_date datetime    COMMENT 'when (if) receipt was sent. populated automatically for online donations w/ automatic receipting',
     thankyou_date datetime    COMMENT 'when (if) was donor thanked',
     source varchar(255)    COMMENT 'Origin of this Contribution.',
     amount_level varchar(255)    ,
     contribution_recur_id int unsigned    COMMENT 'Conditional foreign key to civicrm_contribution_recur id. Each contribution made in connection with a recurring contribution carries a foreign key to the recurring contribution record. This assumes we can track these processor initiated events.',
     honor_contact_id int unsigned    COMMENT 'FK to contact ID',
     is_test tinyint   DEFAULT 0 ,
     contribution_status_id int unsigned   DEFAULT 1  
,
    PRIMARY KEY ( id )
 
    ,     INDEX UI_contrib_payment_instrument_id(
        payment_instrument_id
  )
  ,     UNIQUE INDEX UI_contrib_trxn_id_domain_id(
        trxn_id
      , domain_id
  )
  ,     UNIQUE INDEX UI_contrib_invoice_id_domain_id(
        invoice_id
      , domain_id
  )
  ,     INDEX index_contribution_status(
        contribution_status_id
  )
  ,     INDEX received_date(
        receive_date
  )
  
,      
     CONSTRAINT FK_civicrm_contribution_domain_id FOREIGN KEY (domain_id) REFERENCES civicrm_domain(id)
,      
     CONSTRAINT FK_civicrm_contribution_contact_id FOREIGN KEY (contact_id) REFERENCES civicrm_contact(id)
,      
     CONSTRAINT FK_civicrm_contribution_solicitor_id FOREIGN KEY (solicitor_id) REFERENCES civicrm_contact(id)
,      
     CONSTRAINT FK_civicrm_contribution_contribution_type_id FOREIGN KEY (contribution_type_id) REFERENCES civicrm_contribution_type(id)
,      
     CONSTRAINT FK_civicrm_contribution_contribution_page_id FOREIGN KEY (contribution_page_id) REFERENCES civicrm_contribution_page(id)
,      
     CONSTRAINT FK_civicrm_contribution_contribution_recur_id FOREIGN KEY (contribution_recur_id) REFERENCES civicrm_contribution_recur(id)
,      
     CONSTRAINT FK_civicrm_contribution_honor_contact_id FOREIGN KEY (honor_contact_id) REFERENCES civicrm_contact(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

-- /*******************************************************
-- *
-- * civicrm_contribution_product
-- *
-- *******************************************************/
CREATE TABLE civicrm_contribution_product (


     id int unsigned NOT NULL AUTO_INCREMENT  ,
     product_id int unsigned NOT NULL   ,
     contribution_id int unsigned NOT NULL   ,
     product_option varchar(255)    COMMENT 'Option value selected if applicable - e.g. color, size etc.',
     quantity int    ,
     fulfilled_date date    COMMENT 'Optional. Can be used to record the date this product was fulfilled or shipped.',
     start_date date    COMMENT 'Actual start date for a time-delimited premium (subscription, service or membership)',
     end_date date    COMMENT 'Actual end date for a time-delimited premium (subscription, service or membership)',
     comment text     
,
    PRIMARY KEY ( id )
 
 
,      
     CONSTRAINT FK_civicrm_contribution_product_contribution_id FOREIGN KEY (contribution_id) REFERENCES civicrm_contribution(id)
  
)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;

 