Testing Migrations
This commit is contained in:
1 parent
81b4162117
commit
d9abeb7fb1
7 files changed
+105
-78
No files matched your search
@@ -10,7 +10,7 @@
|
||||
//
|
||||
// The restricted account is correct - the server ingests untrusted URLs and
|
||||
// should not be able to reshape its own tables. So DDL moved out of the
|
||||
// application entirely: an admin runs ./applyScrapeSchema.sh once, and this
|
||||
// application entirely: an admin runs ./migrateDatabase.sh once, and this
|
||||
// module only checks that the result is present and usable.
|
||||
//
|
||||
// Note on why the DDL is REMOVED rather than guarded by an existence check:
|
||||
@@ -27,6 +27,17 @@ const REQUIRED_TABLE = 'scrape_cache'
|
||||
const REQUIRED_COLUMNS = ['scrape_state', 'scrape_attempts', 'enqueued_at']
|
||||
const REQUIRED_INDEX = 'attachment_scrape_state'
|
||||
|
||||
//
|
||||
// The FULLTEXT index backing Attachment.textSearch's MATCH..AGAINST.
|
||||
//
|
||||
// Checked by TYPE, not just existence: Prisma's `@@fulltext` is a preview
|
||||
// feature and MariaDB is not among its documented databases, so a future
|
||||
// migration could plausibly emit a plain BTREE index of the same name. That
|
||||
// would make attachment search return nothing at all, with no error anywhere -
|
||||
// so it is worth asserting the type explicitly on every boot.
|
||||
//
|
||||
const REQUIRED_FULLTEXT = { table: 'attachment', index: 'attachment_text_index' }
|
||||
|
||||
// The scraper writes and deletes, not just reads. DELETE is genuinely needed:
|
||||
// terminal failures drop the shell attachment row (ScrapeQueue), and
|
||||
// ScrapeCache.purgeExpired removes expired rows.
|
||||
@@ -62,6 +73,19 @@ const columnExists = async (table, column) => {
|
||||
return rows.length > 0
|
||||
}
|
||||
|
||||
//
|
||||
// Does this index exist AND is it actually FULLTEXT?
|
||||
//
|
||||
const fulltextIndexExists = async (table, index) => {
|
||||
const [rows] = await db.promise().query(
|
||||
`SELECT 1 FROM information_schema.STATISTICS
|
||||
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND INDEX_NAME = ?
|
||||
AND INDEX_TYPE = 'FULLTEXT' LIMIT 1`,
|
||||
[table, index]
|
||||
)
|
||||
return rows.length > 0
|
||||
}
|
||||
|
||||
const indexExists = async (table, index) => {
|
||||
const [rows] = await db.promise().query(
|
||||
`SELECT 1 FROM information_schema.STATISTICS
|
||||
@@ -74,6 +98,7 @@ const indexExists = async (table, index) => {
|
||||
Schema.__tableExists = tableExists
|
||||
Schema.__columnExists = columnExists
|
||||
Schema.__indexExists = indexExists
|
||||
Schema.__fulltextIndexExists = fulltextIndexExists
|
||||
|
||||
//
|
||||
// Parse SHOW GRANTS output into the set of privileges that apply to this
|
||||
@@ -182,12 +207,16 @@ Schema.verifyScrapeSchema = async () => {
|
||||
missingObjects.push(`attachment index ${REQUIRED_INDEX}`)
|
||||
}
|
||||
|
||||
if (!await fulltextIndexExists(REQUIRED_FULLTEXT.table, REQUIRED_FULLTEXT.index)) {
|
||||
missingObjects.push(`FULLTEXT index ${REQUIRED_FULLTEXT.table}.${REQUIRED_FULLTEXT.index}`)
|
||||
}
|
||||
|
||||
const privileges = await checkPrivileges()
|
||||
const missingPrivileges = privileges.missing || []
|
||||
|
||||
const remedy = []
|
||||
if (missingObjects.length) {
|
||||
remedy.push('run ./applyScrapeSchema.sh as a database admin')
|
||||
remedy.push('run ./migrateDatabase.sh as a database admin')
|
||||
}
|
||||
if (missingPrivileges.length) {
|
||||
// Name the real database and user so this can be pasted straight into
|
||||
|
||||
+1
-1
@@ -350,7 +350,7 @@ app.use('/api/metric-tracking', metricTracking)
|
||||
//
|
||||
// The application deliberately holds no DDL privileges, so it does not create
|
||||
// its own schema - verifyScrapeSchema() only checks that an admin has already
|
||||
// run ./applyScrapeSchema.sh and that the required DML grants are present.
|
||||
// run ./migrateDatabase.sh and that the required DML grants are present.
|
||||
//
|
||||
// When anything is missing the scraper stays off rather than failing an INSERT
|
||||
// on every note save. The listener binds either way: the API must not depend on
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
--
|
||||
-- Scrape cache + per-attachment scrape lifecycle.
|
||||
--
|
||||
-- HOW THIS IS APPLIED
|
||||
--
|
||||
-- ./applyScrapeSchema.sh (from the repo root, as an admin)
|
||||
--
|
||||
-- Run it once per environment. The application does NOT apply this itself:
|
||||
-- production connects as a least-privilege `app` user with DML rights only, and
|
||||
-- deliberately holds no CREATE/ALTER. server/helpers/Schema.js only *verifies*
|
||||
-- that these objects exist and that the required privileges are held; if they
|
||||
-- are missing it disables link previews and logs the remedy.
|
||||
--
|
||||
-- Safe to re-run: every statement is conditional.
|
||||
--
|
||||
-- Portability: dev and production are both MariaDB 11.4, so ADD COLUMN IF NOT
|
||||
-- EXISTS and CREATE INDEX IF NOT EXISTS are available. If this project ever
|
||||
-- moves to MySQL 8 these become invalid and need information_schema guards
|
||||
-- instead.
|
||||
--
|
||||
|
||||
-- One row per URL, shared across users and notes.
|
||||
--
|
||||
-- url_hash carries the UNIQUE index because a UNIQUE on varchar(1000) utf8mb4
|
||||
-- is 4000 bytes and exceeds InnoDB's 3072-byte key limit. `url` is kept
|
||||
-- human-readable for debugging only.
|
||||
--
|
||||
-- COLLATE is stated explicitly to match the existing tables (utf8mb4_general_ci)
|
||||
-- rather than inheriting whatever the server default happens to be.
|
||||
CREATE TABLE IF NOT EXISTS `scrape_cache` (
|
||||
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`url_hash` char(64) NOT NULL,
|
||||
`url` varchar(1000) NOT NULL DEFAULT '',
|
||||
`canonical_url` varchar(1000) DEFAULT NULL,
|
||||
`status` smallint(6) NOT NULL DEFAULT 0,
|
||||
`title` varchar(512) DEFAULT NULL,
|
||||
`site_name` varchar(255) DEFAULT NULL,
|
||||
`description` mediumtext DEFAULT NULL,
|
||||
`keywords` varchar(512) DEFAULT NULL,
|
||||
`search_text` mediumtext DEFAULT NULL,
|
||||
`image_url` varchar(1000) DEFAULT NULL,
|
||||
`file_location` varchar(500) DEFAULT NULL,
|
||||
`etag` varchar(255) DEFAULT NULL,
|
||||
`last_modified` varchar(128) DEFAULT NULL,
|
||||
`content_type` varchar(128) DEFAULT NULL,
|
||||
`fetch_state` varchar(20) NOT NULL DEFAULT 'pending',
|
||||
`failure_count` int(11) NOT NULL DEFAULT 0,
|
||||
`last_error` varchar(255) DEFAULT NULL,
|
||||
`fetched_at` int(11) DEFAULT NULL,
|
||||
`expires_at` int(11) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `scrape_cache_url_hash` (`url_hash`),
|
||||
KEY `scrape_cache_expires_at` (`expires_at`),
|
||||
KEY `scrape_cache_fetch_state` (`fetch_state`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
|
||||
-- Per-attachment lifecycle, so the in-memory scrape queue survives a restart.
|
||||
--
|
||||
-- scrape_state: 0 = pending, 1 = fetching, 2 = done, 3 = failed.
|
||||
--
|
||||
-- DEFAULT 2 is load-bearing: it keeps every pre-existing attachment row
|
||||
-- (~2200 in production) out of the boot recovery sweep.
|
||||
ALTER TABLE `attachment` ADD COLUMN IF NOT EXISTS `scrape_state` tinyint(4) NOT NULL DEFAULT 2;
|
||||
ALTER TABLE `attachment` ADD COLUMN IF NOT EXISTS `scrape_attempts` tinyint(4) NOT NULL DEFAULT 0;
|
||||
ALTER TABLE `attachment` ADD COLUMN IF NOT EXISTS `enqueued_at` int(11) DEFAULT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS `attachment_scrape_state` ON `attachment` (`scrape_state`, `enqueued_at`);
|
||||
@@ -39,7 +39,7 @@ beforeAll(async () => {
|
||||
// The server normally does this at boot; tests have no boot phase.
|
||||
const verification = await Schema.verifyScrapeSchema()
|
||||
if (!verification.ok) {
|
||||
throw new Error(`dev scrape schema not applied - run ./applyScrapeSchema.sh (${verification.remedy})`)
|
||||
throw new Error(`dev scrape schema not applied - run ./migrateDatabase.sh (${verification.remedy})`)
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
@@ -432,7 +432,7 @@ ScrapeQueue.recoverPending = async () => {
|
||||
if (!settings.enabled) { return { requeued: 0, retired: 0 } }
|
||||
|
||||
// Every query below references scrape_state, which only exists once an
|
||||
// admin has run ./applyScrapeSchema.sh.
|
||||
// admin has run ./migrateDatabase.sh.
|
||||
if (!Schema.isScrapeSchemaReady()) { return { requeued: 0, retired: 0 } }
|
||||
|
||||
// Anything left at FETCHING died with the previous process.
|
||||
|
||||
Reference in new issue
Block a user