38
Kristian Ducharme BADCamp 2016 BFFs 4eva: Drupal 8 and Custom Databases

BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

  • Upload
    others

  • View
    38

  • Download
    0

Embed Size (px)

Citation preview

Page 1: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Kristian Ducharme

BADCamp 2016

BFFs 4eva: Drupal 8 and Custom Databases

Page 2: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Introduction

■ Kristian Ducharme - Senior Engineer / Technical Lead @ CivicActions

■ Past Projects: City of Los Angeles, Acquia Projects

■ Development / Drupal Background

Page 3: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Project Background

https://www.digitaldemocracy.org

■ California Legislative Hearings, ML Transcription, Data Aggregation

■ Custom MySQL Database - 100+ tables (DDDB)

■ DB Team supporting schema, data import, transcription

■ Growth Plans - Multi-state, Tiered offerings, Sites for Advocacy Groups + Organizations, Campaigns, Advanced Search

Page 4: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Drupal / Data Architecture

Page 5: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Current D7 Drupal Architecture

■ DDDB is located on same server, connected via read-only MySQL user

■ Nightly import through migration scripts

■ Data imported into fielded nodes with its own structure, incorporating some similarities to DDDB schema

■ Drupal content types are loosely connected via ID fields, not utilizing entity references

Page 6: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Current D7 Issues

■ Synchronization of data between DDDB and Drupal has to be maintained

■ Duplication of data

■ Keeping DDDB an independent DB important

■ Drupal features limited due to architecture (Views / Content Admin / Search)

■ Schema changes in DDDB require 2-3 layers of Drupal changes / testing

Page 7: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

=

DDDB Schema

Page 8: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

D7 or D8?

Page 9: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Use Cases for D8 External Custom Entities

■ Complex External Relational Databases

■ Too much changing data for periodic migration

■ DB Schema with intricate relationship, foreign key constraints

■ Read-only data sources, need to tie with Drupal content types

Page 10: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Advantages Disadvantages

Page 11: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Advantages

■ Gain all Drupal 8 functionality on dataset

■ Fieldable Entities■ Options for headless

Drupal/dataset APIs■ Search API / Facets■ No import/migration process■ Real-time data■ No Data Duplication■ External DB Replication■ High level of

customization/overrides

Disadvantages

Page 12: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Advantages

■ Gain all Drupal 8 functionality on dataset

■ Fieldable Entities■ Options for headless

Drupal/dataset APIs■ Search API / Facets■ No import/migration process■ Real-time data■ No Data Duplication■ External DB Replication■ High level of

customization/overrides

Disadvantages

■ Complexity of Views■ Configuring Relationships■ Heavy initial development to

establish Custom Entities■ More Performance Tuning

Required■ Requires External DB to be on

same server as Drupal DB■ For local dev, requires copy

of external DB

Page 13: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

D8 Custom Entities In Action

Page 14: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Addressing Drupal DB/Table Needs

Possible Issues with External DB■ Multiple Primary Keys, Non-Integer Primary Keys■ Relationship tables (all become entities?)■ DB Not On Same Server as Drupal

Solutions■ Adding new unique ID column to tables■ Generated Columns (MySQL 5.7.6+)■ Computed Fields■ SQL Views, Custom Entity built on views■ Database Replication to sync w/Drupal server

Page 15: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Custom Entity Creation Process

■ Creating Custom Modules To Hold Entities

■ Mapping Tables/Columns to Entities/Fields

■ Drupal Console drupal generate:entity:content

■ Adjusting Annotations

■ Getters/Setters for all fields

■ Adding fields to baseFieldDefinitions

■ Adding Table Prefix to settings.php

Page 16: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Custom Entities Annotation

Page 17: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Custom Entities Services

■ Overriding Uninstall Services

Page 18: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

ContentUninstallValidator Override

Page 19: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Custom Entities Class Inheritance

■ DB Tables mapped to entities w/common fields (state, status, location, etc)

■ Extending “base” entity class(es) to provide common functionality to many entities

■ Use of custom SqlContentEntityStorage

Page 20: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

“State” Filtering Example on Entity

/ Views

Page 21: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Use of “State” field in

DdBaseStateField Class

Page 22: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Hearing Entity extending

DdBaseStateField Class

Page 23: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Use Of Custom SqlContentEntityStorage Handlers

Page 24: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

dd_base.views_execution.inc - hook_views_query_alter() to add State filtering

Page 25: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Custom Entities w/Views

■ Post Drupal Console generation, ready w/only relationships defined in Entity baseFieldDefinitions

■ Modifying xxxxViewsData.php(Reference: views.api.php hook_views_data)

■ Ability to add relationships to tables that have no custom entities

■ Support for table joins w/multiple conditions

Page 26: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Drupal\dd_hearing\Entity\DdHearing

Page 27: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Views “Add Relationships” for Hearing

Page 28: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

DdPersonViewsData - “Add Relationships” for Person

Base Table Name

Unique ID For Relationship Column

Table to join

Field to join on

Field on Person Table to join

Resulting SQL Query:SELECT Person.pid AS pid, Legislator_Person.pid AS legislator_person_pid FROM {Person} Person INNER JOIN {Legislator} Legislator_Person ON Person.pid = Legislator_Person.pid

Page 29: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Views “Add Relationships” for Person

Page 30: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Limitation Of Views (w/o Search API)

■ Handling sitewide search

■ Performance

■ No Facets

■ No Big Data Tools

Page 31: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Custom Entities w/Search API

■ Entity Reference fields automatically available

■ Computed Fields allow data to be added to Search Index during entity load

■ Patch to allow computed fields to be visible: https://www.drupal.org/node/2814925

■ Faceted Search on Views built on Search Indexes

Page 32: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

dd_hearing.moduleSet Computed Field On Entity Storage Load

Page 33: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Search API Index Fields

Page 34: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

The End Result

Page 35: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API
Page 36: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API
Page 37: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Q & A

Page 38: BFFs 4eva: Drupal 8 and Custom Databases - BADCamp 4eva- Drupal 8 and...BFFs 4eva: Drupal 8 and Custom Databases. Introduction ... Options for headless Drupal/dataset APIs Search API

Contact Information

Kristian [email protected]

Drupal.org: kducharm GitHub: kducharm

BFFs 4eva: Drupal 8 and Custom Databases

http://www.civicactions.com