Skip to the content
Documentation
Help Start
Reading in:
  • German: 9,220 of 9,220 pages
About the help in other languages
All help pages

The newsletter tables on every database

cjw_newsletter runs on the seven databases that carry a whole Exponential site: SQLite, PostgreSQL, MongoDB, MariaDB, MySQL, Oracle (through the extension ezoracle and PHP's oci8) and Microsoft SQL Server / Azure SQL…

Download as PDF

Address: https://doc.exponential.earth/learninminutes/exphelp/viewer/ext/cjw_newsletter/databases.md

On this page

The newsletter tables on every database

cjw_newsletter runs on the seven databases that carry a whole Exponential site: SQLite, PostgreSQL, MongoDB, MariaDB, MySQL, Oracle (through the extension ezoracle and PHP's oci8) and Microsoft SQL Server / Azure SQL (sqlsrv, pdo_sqlsrv or PDO_ODBC). This page says where the schema of each engine lives, how a new installation and an upgrade get it, what a release that changes the schema must add, and how the CI proves it.

1. One definition: share/db_schema.dba

share/db_schema.dba is the schema, in the kernel's engine-neutral format. Everything else is made from it or checked against it.

EngineDatabaseImplementationSchema fileWritten by
SQLitesqlite3sql/sqlite/schema.sqlhand (since 1.x)
PostgreSQLezpostgresqlsql/postgresql/schema.sqlhand
MariaDB, MySQLezmysqlisql/mysql/schema.sqlhand
Oracleezoraclesql/oracle/schema.sqlbin/php/dbschema.php --write-files
SQL Server, Azure SQLsqlserversql/sqlserver/schema.sqlbin/php/dbschema.php --write-files
MongoDBmongodbsql/mongodb/schema.json (collections and indexes)bin/php/dbschema.php --write-files

The three generated files are written by the kernel's own schema handler of each engine (expKernelTableSchema, eZOracleSchema of ezoracle, expSQLServerSchema, and for MongoDB the same index mapping as expMongoSchema), so they say exactly what an installation creates. --check-files fails when one of them no longer matches share/db_schema.dba; the CI runs it.

2. A new installation

Nothing needs to be run by hand. The kernel creates an extension's tables from its share/db_schema.dba with the handler of the site's engine:

  • exp:install and the setup wizard create the tables of every extension that is active when the site is made.
  • An existing site that switches the newsletter on gets the missing tables from ./console exp:upgrade:database --apply (step 6.0.15-active-extension-tables), or from the package:
php extension/cjw_newsletter/bin/php/dbschema.php -s <admin siteaccess> --install

Both create only missing tables; nothing is dropped or changed. The sql/<engine>/schema.* files are for a database administrator who prefers to create the tables by hand (sqlplus, sqlcmd, mongosh).

Engine notes:

  • Oracle: two columns are reserved words and are created quoted: cjwnl_approval."COMMENT" and cjwnl_schedule."MODE". Oracle stores an empty string as NULL, so ezoracle writes text columns with the default '' without NOT NULL. Use ezoracle 2.3.16 or later: earlier versions write a text of more than 4000 bytes as one literal, which Oracle refuses (ORA-01704), so an edition's output could not be stored and its sends failed.
  • SQL Server: create the database with a UTF-8 collation and READ_COMMITTED_SNAPSHOT ON (see the kernel's doc/eng-us/DATABASES.md).
  • MongoDB: there are no columns; each collection gets its indexes (unique for a primary or unique index). A field exists once a document has it. Known limit: the edition filter of the content fetches (extendedattributefilter/cjwnewslettereditionfilter.php: editions by state, e.g. the archive) nests a sub-select that joins cjwnl_edition_send with ezcontentobject; the kernel's MongoDB adapter finds nothing for it, so those lists stay empty on MongoDB. Conditions of one-table queries name their columns without the table: the adapter matches no document for table.column there.

3. Upgrades: update/database/<engine>/

An existing database is brought from one release to a newer one by the update files of its engine. The layout is the kernel's:

update/database/<engine>/<x.y>/dbupdate-<from>-to-<to>.sql     mysql, postgresql, sqlite, oracle, sqlserver
update/database/mongodb/<x.y>/dbupdate-<from>-to-<to>.json     MongoDB
  • <engine> is one of mysql (MariaDB uses MySQL's files), postgresql, sqlite, oracle, sqlserver, mongodb. <x.y> is the line of the target release <to>. unstable/ subdirectories hold pre-releases and are never run.
  • A file covers every release from <from> up to <to>. A database at any version below <to> runs it; the files run in the order of their targets. A file is run once: it has no IF NOT EXISTS.
  • Each engine's files start at its baseline, the oldest release they carry a database from: MySQL/MariaDB, PostgreSQL and SQLite 4.1.20; Oracle, SQL Server and MongoDB 4.3.3. The history before 4.3.3 is not written for the three newer engines: a site on them that runs an older release installs the newsletter tables fresh (section 2) and keeps no newsletter data of that release.

The package lists and runs them:

php extension/cjw_newsletter/bin/php/dbschema.php -s <admin siteaccess> --baseline            # engine and baseline
php extension/cjw_newsletter/bin/php/dbschema.php -s <admin siteaccess> --update --from=4.3.3 # what would run
php extension/cjw_newsletter/bin/php/dbschema.php -s <admin siteaccess> --update --from=4.3.3 --apply

Take a backup first (exp:upgrade and xne do that for the kernel; they do not run an extension's update files). A MySQL, PostgreSQL or SQLite file can also be run with the database's own client, as before.

File formats

  • SQL (all but MongoDB): statements end with ; at the end of a line; -- and /* */ comments are allowed. Oracle: a PL/SQL block (BEGIN, DECLARE, CREATE OR REPLACE TRIGGER ...) ends at a line holding only /, as in SQL*Plus. SQL Server: a line GO ends a batch as well.
  • MongoDB (JSON):
{
    "from": "4.3.3",
    "to": "5.0.0",
    "comment": "What changes and why",
    "collections": [ { "name": "cjwnl_new_table", "indexes": [ { "keys": { "id": 1 }, "options": { "name": "PRIMARY", "unique": true } } ] } ],
    "fields": [ { "collection": "cjwnl_edition_send", "field": "new_column", "default": 0 } ],
    "drop_fields": [ { "collection": "cjwnl_edition_send", "field": "old_column" } ],
    "drop_indexes": [ { "collection": "cjwnl_user", "name": "cjwnl_user_old" } ],
    "drop_collections": [ "cjwnl_old_table" ]
}

collections creates the collection where missing and its indexes; fields sets the default on every document that lacks the field (the adapter's WHERE new_column = 0 finds no document without it).

4. What a release that changes the schema adds

For a new column or table, in one change:

  1. share/db_schema.dba: the table or column.

  2. sql/mysql/schema.sql, sql/postgresql/schema.sql, sql/sqlite/schema.sql: the same, by hand.

  3. php extension/cjw_newsletter/bin/php/dbschema.php --write-files: rewrites sql/oracle/schema.sql, sql/sqlserver/schema.sql and sql/mongodb/schema.json.

  4. One update file per engine, all with the same <from> and <to> (the previous release and the new one):

    • update/database/mysql/<x.y>/dbupdate-<from>-to-<to>.sql
    • update/database/postgresql/<x.y>/dbupdate-<from>-to-<to>.sql
    • update/database/sqlite/<x.y>/dbupdate-<from>-to-<to>.sql
    • update/database/oracle/<x.y>/dbupdate-<from>-to-<to>.sql
    • update/database/sqlserver/<x.y>/dbupdate-<from>-to-<to>.sql
    • update/database/mongodb/<x.y>/dbupdate-<from>-to-<to>.json

    The statements of each engine come from its handler, so they match a new installation:

php extension/cjw_newsletter/bin/php/dbschema.php --print=oracle --table=cjwnl_new_table           # CREATE TABLE ...
php extension/cjw_newsletter/bin/php/dbschema.php --print=sqlserver --table=cjwnl_user --column=x   # ALTER TABLE ... ADD
php extension/cjw_newsletter/bin/php/dbschema.php --print=mongodb --table=cjwnl_user --column=x     # the "fields" entry

If the previous release is not out yet, its statements go into that release's still unreleased file instead.

The package test tests/cjwNewsletterDbSchemaTest.php fails when an update file has no counterpart for an engine whose baseline is older than its target, when a file is in the wrong line directory or has the wrong extension, when a MongoDB file holds an unknown key, and when a generated file lacks a table or column of share/db_schema.dba.

5. The checks

# the newsletter tables of the site's database against share/db_schema.dba
php extension/cjw_newsletter/bin/php/dbschema.php -s <admin siteaccess> --check [--level=tables|columns|definitions]

# an older release's schema plus the update files equals a new installation
git -C extension/cjw_newsletter show 4.3.3:share/db_schema.dba > var/tmp/cjwnl-4.3.3.dba
php extension/cjw_newsletter/bin/php/dbschema.php -s <admin siteaccess> --upgrade-check --from=4.3.3 \
    --from-schema=var/tmp/cjwnl-4.3.3.dba --upgrade-db=cjwnl_upgrade --fresh-db=cjwnl_fresh

--check compares as the kernel's database consistency check does (Setup > Upgrade check): the expected tables in the engine's own format, both sides normalized by the handler, then eZDbSchemaChecker. The default level fails on a missing or extra table or column, a different type, default or NOT NULL, and a missing, extra or different index; --level=columns fails only on tables and columns and reports the rest as notes. MongoDB compares collections and their indexes.

--upgrade-check needs two empty databases on the site's server and engine (SQLite: two file names in var/storage/sqlite3; Oracle: two users, with the password in CJWNL_SCRATCH_DB_PASSWORD). It drops their newsletter tables first, creates the old schema in one and runs the update files, creates the current schema in the other with the handler, and compares the two exactly (the current one is also compared with share/db_schema.dba; its definition differences there are notes, --check judges them).

The old schema comes from the release's share/db_schema.dba (--from-schema), or, with --from-sql=<that release's sql/<engine>/schema.sql>, from the file its sites were really made with. The hand-written MySQL, PostgreSQL and SQLite files name some indexes and write some defaults otherwise than the installer (PostgreSQL: index names, (1)::smallint defaults, and cjwnl_subscription.newsletter_user_id and cjwnl_user.external_user_id NOT NULL DEFAULT 0 where share/db_schema.dba has them nullable). An upgrade check from such a file therefore runs with --level=columns: the tables and columns must match, the rest is listed as notes. PostgreSQL needs the file: from 4.1.20 the 4.2.0 update renames the sequences <table>_s that only the 4.1 file created.

6. The CI: .github/workflows/engines.yml

On every push to master, every pull request and by hand (workflow_dispatch), one job per engine:

LegServerPHP extensions
SQLitenonesqlite3
PostgreSQL 17postgres:17pgsql
MongoDB 7mongo:7mongodb and the library mongodb/mongodb
MariaDB 11.8mariadb:11.8mysqli
MySQL 8.0mysql:8.0mysqli
Oracle Free 23gvenzl/oracle-free:23-slimoci8, extension ezoracle (from the kernel's Composer)
SQL Server 2022mcr.microsoft.com/mssql/server:2022-latestsqlsrv, pdo_sqlsrv, pdo_odbc, ODBC Driver 18

Each leg checks out the kernel (se7enxweb/exponential, main), installs it with Composer, puts this checkout at extension/cjw_newsletter, installs a site with exp:install --apply --db=<engine> on a database named nl_test (the kernel's live tests write only to a database whose name has the word test; Oracle's database setting is its service name, so there they run as the kernel allows without one), switches the newsletter on and creates its tables with exp:upgrade:database, creates a newsletter list (tests/ci/newsletter-list.php), and runs:

  1. the generated schema files check (--check-files),
  2. the schema check (--check --level=columns: tables and columns must equal share/db_schema.dba; the definitions are listed as notes, as Oracle reads its shortened index names back and other handlers word some types otherwise),
  3. the upgrade check from the engine's baseline and from the newest release tag (from the release's own sql/<engine>/schema.sql where it shipped one, at the level columns; otherwise from its share/db_schema.dba, exactly),
  4. the package tests (vendor/bin/phpunit extension/cjw_newsletter/tests),
  5. the kernel's live suite (vendor/bin/phpunit --testsuite cjw_newsletter, with EXP_TEST_SITEACCESS=admin and CJWNL_TEST_LIST_OBJECT_ID).

Every leg runs on the project's self-hosted runner (runs-on: [self-hosted, 7x-db]) and its steps in a throwaway job container (ubuntu:24.04) with the database as a service container on the job's own network, reached by the name db, up to three legs side by side. Nothing is installed on the runner, no port of the runner is published and no container name can collide, so the workflow is safe on a self-hosted runner that is a shared machine as well. The databases of a leg are made by tests/ci/prepare-database.php with PHP's own drivers.

When the site cannot be installed on a leg, the leg writes the database settings itself and still runs the schema install (--install), the schema check, the upgrade check and the package tests; the live suite needs a site and is skipped. Every part that fails or does not run fails the leg; the job summary shows one row per engine and the logs are uploaded as engine-<engine>.

A release that changes the schema needs no change to the workflow: its update files (section 4) are found by the upgrade check, and the new release tag becomes a starting point of the next run.

Something wrong on this page? Report an error

Documentation license

This page is part of the documentation of the extension cjw_newsletter and is distributed under the license of that extension.