Database Integration (MySQL & MongoDB)

Chapter 13
Testing, Migration, Deployment, and Operations

Diploma in Full Stack Programming
Database Integration (MySQL & MongoDB)   •   Diploma

Testing, Migration, Deployment, and Operations

Introduction

A database application is not complete when its tables, collections, queries, and application code work on a developer’s computer. It must also be tested, repeatably constructed, safely migrated, deployed, monitored, backed up, and recovered. These activities form the operational life cycle of database integration.

For a full-stack developer, database operations are not exclusively the responsibility of a database administrator. Application developers write migration scripts, design seed data, configure connections, interpret logs, test failure conditions, and contribute to deployment pipelines. Poor operational practice can cause data loss, security breaches, service interruption, or inconsistent environments even when the application code itself appears correct.

This chapter examines these practices for MySQL and MongoDB, with emphasis on professional application development, version control, CI/CD, containers, cloud deployment, and recovery.

Learning Objectives

By the end of this chapter, the learner should be able to:

  1. Design meaningful unit, integration, migration, and performance tests for database applications.
  2. Create safe seed data and repeatable database initialization processes.
  3. Explain schema migration, rollback, compatibility, and version control.
  4. Configure backups, restoration procedures, and failure-recovery plans.
  5. Use logging, monitoring, health checks, and operational metrics.
  6. Deploy MySQL and MongoDB applications using containers, cloud services, and CI/CD pipelines.
  7. Evaluate operational, ethical, security, and governance risks.

1. The Database Operations Life Cycle

A production database passes through a continuous cycle:

design → test → migrate → deploy → observe → back up → recover → improve

Each stage depends on the others. A migration that has not been tested may fail during deployment. A backup that has never been restored cannot be considered reliable. A system that is not monitored may lose data or performance gradually without detection.

A useful operational distinction is:

  • Development environment: optimized for rapid experimentation.
  • Test or staging environment: designed to resemble production and validate changes.
  • Production environment: contains live data and requires strict controls.

Configuration, credentials, data volume, and access permissions should differ between these environments. Production data should not normally be copied into development without masking or anonymization.

2. Database Testing

Database testing verifies both data correctness and the behavior of the application–database boundary. It should test successful operations, invalid input, concurrency, failure, security, and performance.

2.1 Testing levels

Testing levelPurposeExample
Unit testTests a small unit in isolationValidate a function that builds a query
Integration testTests application code with a real databaseInsert an order and read it back
Migration testVerifies schema changes and compatibilityApply migrations to an empty and existing database
Contract testChecks assumptions between servicesVerify returned fields and data types
Performance testMeasures response under expected loadExecute 500 concurrent read requests
Recovery testTests restoration and failure proceduresRestore a backup to a temporary server
Security testTests authorization and injection resistanceAttempt unauthorized record access

Unit tests are fast but may not detect SQL syntax, index, transaction, or driver problems. Integration tests use a real MySQL or MongoDB instance and therefore provide stronger evidence about database behavior.

2.2 Testing transactions

A transaction groups operations so that they satisfy the intended atomic behavior. In MySQL, transaction behavior depends on the storage engine; InnoDB is normally used because it supports transactions and foreign keys. A typical relational transaction is:

START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

The application must roll back if validation or any operation fails:

START TRANSACTION;
-- database operations
ROLLBACK;

MongoDB supports transactions for replica sets and sharded clusters. They should be used when several document operations must succeed together, but they should not be used automatically for every operation because transactions add coordination and resource costs.

Tests should verify that:

  • all intended changes occur after commit;
  • no partial changes remain after rollback;
  • duplicate or invalid requests are handled safely;
  • retry behavior does not create duplicate records;
  • concurrent updates follow the intended consistency rule.

2.3 Test isolation and repeatability

A test should not depend on the accidental order of other tests. Common approaches include:

  • creating a temporary database for a test suite;
  • wrapping each test in a transaction and rolling it back;
  • loading a known fixture before each test;
  • using containers that are discarded after testing.

Rollback-based isolation is fast, but it may not work for every operation, such as database-definition changes or code that opens a separate connection. A disposable database is slower but often more realistic.

2.4 Testing constraints and validation

Relational constraints should be tested explicitly. Examples include primary keys, unique constraints, foreign keys, NOT NULL, and CHECK constraints. Application validation is useful for user feedback, but database constraints remain important because data may enter through imports, scripts, administrative tools, or other services.

MongoDB validation can be configured with a JSON Schema validator. However, document databases often permit controlled variation in document structure. The design decision is not simply “schema” versus “no schema”; it is the choice of where and how strictly structure is enforced.

2.5 Performance testing

Performance testing should use realistic data volume, indexes, query patterns, and concurrency. A query that is fast on 100 rows may be unsuitable for 10 million rows.

Important measurements include:

  • latency, such as median, 95th percentile, and 99th percentile response time;
  • throughput, such as requests or transactions per second;
  • error rate;
  • CPU, memory, disk, and network utilization;
  • connection-pool usage;
  • lock waits and slow queries;
  • replication lag.

Averages alone can hide poor user experiences. For example, a low average latency may coexist with unacceptable 99th-percentile latency during traffic spikes.

3. Seeding and Test Data

Seeding is the controlled insertion of initial or representative data. Seed data may include reference values, development users, test products, or demonstration orders.

A good seed process is:

  • repeatable;
  • deterministic where possible;
  • safe to run more than once, or explicitly resettable;
  • separate from production secrets;
  • documented and version-controlled;
  • appropriate for the environment.

Reference data, such as country codes or application roles, may be inserted with an upsert. In MySQL, an application may use INSERT ... ON DUPLICATE KEY UPDATE; in MongoDB, it may use updateOne() with { upsert: true }.

Random test data should use a recorded seed or controlled generator when reproducibility matters. Production-like data should be anonymized. Removing only names is insufficient if email addresses, dates, identifiers, combinations of attributes, or free text can re-identify individuals.

A production deployment should not automatically insert development accounts or sample passwords. Seed scripts should identify their target environment and fail safely when used incorrectly.

4. Schema and Data Migration

A migration is a versioned change to database structure or data. In MySQL, it may add a column, create an index, alter a constraint, or transform values. In MongoDB, it may modify document fields, split one field into several, or create an index.

A migration system commonly records applied versions in a metadata table or collection. A deployment tool then applies pending migrations in order.

4.1 Forward migration and rollback

A forward migration moves a database from version v to v + 1. A rollback attempts to return to an earlier state. Rollbacks are not always perfectly reversible. For example, dropping a column destroys information unless it has been preserved elsewhere.

Therefore, teams should distinguish:

  • reversible structural changes, such as adding an unused nullable column;
  • irreversible destructive changes, such as dropping data;
  • data transformations, which may require a carefully designed reverse transformation or backup restoration.

Destructive migrations require explicit approval, a tested backup, and a recovery plan.

4.2 Expand-and-contract migration

A safe approach for applications deployed without downtime is the expand-and-contract pattern.

  1. Expand: add the new column or field while retaining the old one.
  2. Deploy compatible code: write new data in both forms or read either form.
  3. Backfill: convert existing records in controlled batches.
  4. Switch reads: use the new representation.
  5. Contract: remove the old representation only after all services no longer depend on it.

For example, changing full_name into first_name and last_name should not begin by deleting full_name. The application can first add the new fields, backfill them, deploy compatible code, verify completeness, and only then remove the old field.

4.3 Migration design principles

A professional migration should:

  • have a unique, ordered identifier;
  • be committed to version control;
  • be tested on an empty database and a realistic existing database;
  • define expected preconditions;
  • avoid unnecessary table locks or long-running transactions;
  • be observable through logs and progress information;
  • be compatible with the application version being deployed;
  • document whether it is safe to retry.

Large data migrations should usually be batched. A batch operation limits transaction size, reduces lock duration, and allows progress monitoring. The migration must still be designed to be idempotent or restartable.

4.4 MySQL and MongoDB considerations

MySQL schema changes may acquire metadata locks or rebuild tables, depending on the operation, server version, storage engine, and algorithm. Developers should inspect the planned operation and test it with production-like data.

MongoDB schema changes are frequently implemented as application-level transformations because documents may have different shapes. Index creation also requires planning: indexes improve selected reads but consume memory and increase write cost. Query plans should be inspected rather than assuming that an index is beneficial.

5. Version Control and Configuration

Source control should contain:

  • application source code;
  • migration files;
  • seed scripts, excluding secrets;
  • database configuration templates;
  • deployment manifests;
  • automated test definitions;
  • operational documentation.

Secrets must not be committed to a repository. Use environment variables, a secrets manager, or a cloud secret service. Configuration should identify the database host, port, database name, credentials, TLS settings, pool limits, and migration policy.

A migration is code. It should be reviewed, tested, and associated with the application release that requires it. Branching and release strategies may differ, but the essential principle is that every deployed database state must be reproducible from version-controlled artifacts.

6. Backup, Restoration, and Failure Recovery

A backup is a copy from which data can be restored. A snapshot, export, replica, or point-in-time log may serve different recovery purposes; none should be assumed to replace all others.

Two key objectives are:

  • Recovery Point Objective (RPO): the maximum acceptable amount of data loss measured in time.
  • Recovery Time Objective (RTO): the maximum acceptable time to restore service.

If an organization has an RPO of 15 minutes, its backup and replication design must limit potential loss to approximately 15 minutes or less under the defined failure scenario. If its RTO is one hour, restoration procedures, infrastructure provisioning, validation, and application restart must fit within that period.

Backups should follow the principle of multiple copies on different media or systems, including an off-site or separately secured copy. They should be encrypted in transit and at rest, access-controlled, retained according to policy, and protected against accidental deletion or ransomware where feasible.

6.1 MySQL backup approaches

Common approaches include:

  • logical dumps, which export SQL statements and are portable but can be slow for large databases;
  • physical backups, which copy database files and can support faster restoration but are engine- and configuration-dependent;
  • binary logs, which can support point-in-time recovery when correctly retained and archived;
  • managed-service snapshots and automated backups.

A logical dump might be created with a tool such as mysqldump, but the exact options must match the server version, privileges, character set, transaction requirements, and size of the database.

6.2 MongoDB backup approaches

MongoDB may use logical exports such as mongodump, filesystem or volume snapshots, managed-service backups, and operation-log-based point-in-time recovery. The appropriate method depends on deployment topology, storage system, consistency requirements, and service provider.

6.3 Restoration testing

A backup is not proven until it has been restored and validated. A restoration test should:

  1. provision an isolated target;
  2. restore the backup;
  3. apply required logs or migrations;
  4. verify record counts, constraints, indexes, and representative business queries;
  5. measure restoration time;
  6. record defects and improve the procedure.

Recovery documentation should identify who declares an incident, who has authority to fail over, how communication occurs, and how data integrity is confirmed afterward.

7. Deployment with Containers and Cloud Services

Containers package an application and its runtime dependencies. A typical full-stack deployment may contain an application container, a MySQL or MongoDB service, a reverse proxy, and monitoring agents. In development, a Compose file can define these services consistently.

A simplified conceptual configuration is:

services:
  api:
    build: .
    environment:
      DATABASE_URL: ${DATABASE_URL}
  mysql:
    image: mysql:8
    environment:
      MYSQL_DATABASE: app
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}

This example is illustrative, not production-ready. Production systems require persistent volumes, secret management, resource limits, backups, TLS, network restrictions, health checks, and controlled upgrades. A database container without a persistent volume may lose data when the container is removed.

For many production systems, a managed database is preferable to self-hosting because the provider may supply automated backups, patching, monitoring, replication, and failover. Managed services do not eliminate responsibility: the application owner must still configure access, validate backups, control costs, review logs, and test recovery.

A deployment should use:

  • private networking where possible;
  • least-privilege database accounts;
  • TLS connections;
  • separate credentials per environment;
  • connection pooling and bounded pool size;
  • migration execution as a controlled release step;
  • health and readiness checks;
  • resource and cost monitoring.

A liveness check asks whether a process is running. A readiness check asks whether it can serve traffic, including whether required dependencies are available. Confusing the two can cause a failing dependency to trigger unnecessary restarts.

8. CI/CD and DevOps Practices

Continuous Integration (CI) validates changes frequently. Continuous Delivery or Deployment (CD) moves validated changes toward or into production.

A database-aware pipeline commonly performs the following:

  1. install dependencies and run static checks;
  2. run unit tests;
  3. start disposable MySQL and MongoDB services;
  4. run integration and migration tests;
  5. build and scan the application image;
  6. deploy to staging;
  7. run smoke tests and performance checks;
  8. obtain required approval;
  9. apply production-compatible migrations;
  10. deploy the application;
  11. verify health, logs, and key metrics;
  12. provide rollback or forward-fix procedures.

The pipeline should fail when migrations fail, tests fail, secrets are missing, or security policies are violated. Database migrations should not be hidden in application startup if multiple application instances may start simultaneously. A dedicated migration job or migration lock is safer.

Blue-green deployment maintains two environments and shifts traffic after validation. Rolling deployment replaces instances gradually. Canary deployment exposes a small proportion of traffic to the new release before expansion. Each strategy requires database compatibility; an application rollback is unsafe if the database has already undergone an incompatible destructive change.

9. Monitoring, Logging, and Observability

Observability helps operators understand system behavior from outputs. Its principal signals are:

  • metrics: numerical measurements over time;
  • logs: timestamped event records;
  • traces: activity across services and database calls.

Useful database metrics include connection utilization, query latency, slow-query count, transaction failures, deadlocks, lock waits, cache efficiency, disk growth, replication lag, and backup status. MongoDB deployments may additionally monitor replication health, elections, operation rates, and storage engine behavior.

Logs should be structured, searchable, and correlated with a request or trace identifier. They should record event type, severity, service, environment, duration, and relevant non-sensitive identifiers. Passwords, access tokens, full payment details, and unnecessary personal data must not be logged.

An alert should be actionable. Alerting on every transient error causes alert fatigue; alerting only after total failure causes late response. Thresholds and error-budget policies should reflect service requirements.

A useful operational distinction is:

  • symptom: API latency has increased;
  • cause: a missing index causes a query scan;
  • impact: users cannot complete checkout;
  • remediation: add or adjust the index, validate the query plan, and monitor the result.

10. Failure Recovery and Incident Response

Failures may result from hardware faults, software defects, incorrect migrations, expired certificates, exhausted connections, network partitions, human error, malicious activity, or provider outages.

A practical incident process is:

  1. detect and acknowledge the event;
  2. assess scope and user impact;
  3. stabilize the service, possibly by disabling a feature or reducing traffic;
  4. preserve evidence and logs;
  5. recover using failover, restore, or a forward fix;
  6. validate data correctness;
  7. communicate status;
  8. conduct a blameless post-incident review.

Retries require care. A retry of a read is often safer than a retry of a non-idempotent write. Idempotency keys can allow a payment or order request to be processed once even if the client retries. Exponential backoff with a maximum retry limit can reduce overload, but retries should not conceal persistent failures.

Failure recovery is both technical and organizational. Runbooks should state commands, permissions, decision points, validation queries, escalation contacts, and rollback conditions. They should be tested during drills rather than discovered during an emergency.

11. Security, Governance, and Ethics

Database operations involve significant governance responsibilities. Important controls include:

  • least-privilege roles;
  • separation of development, test, and production access;
  • multi-factor authentication for administrative access;
  • encryption in transit and at rest;
  • audit trails for privileged actions;
  • retention and deletion policies;
  • data classification;
  • vulnerability and patch management;
  • tested incident notification procedures.

Legal requirements vary by jurisdiction and sector. Personal data should be collected for legitimate purposes, retained only as necessary, protected against unauthorized access, and deleted or anonymized according to applicable policy and law.

Developers should not use real customer data casually in test environments. Synthetic data, masking, tokenization, and controlled access reduce exposure. Artificial intelligence tools can assist with query explanation, test generation, log classification, and migration review, but generated code must be checked for data leakage, unsafe destructive statements, inaccurate assumptions, and licensing or confidentiality concerns.

12. Illustrative Case: Migration of an Online Bookstore

Background

An online bookstore stores orders in MySQL and product catalog documents in MongoDB. The order service currently stores a customer’s full name in one field. The business requires separate given and family names for invoices and search.

Problem

The service has three application instances, continuous deployment, and several million existing orders. A direct column replacement could break older application instances and make rollback impossible.

Analysis

The team identifies the following risks:

  • old and new application versions may run simultaneously;
  • the transformation may be incomplete;
  • long transactions may lock the orders table;
  • an incorrect split may damage names containing multiple words;
  • rollback cannot reconstruct every original value reliably.

Approach

The team adds nullable given_name and family_name columns while retaining full_name. The application is deployed in a compatibility mode that writes all fields and reads the new fields when available. A version-controlled batch migration processes records in primary-key ranges, records progress, and avoids already completed rows.

The team tests the migration on an anonymized production-sized copy, checks representative names, measures lock duration, and verifies invoices. After the new fields are complete and monitored, the application stops depending on full_name. A later migration archives rather than immediately deletes the old field.

MongoDB product documents receive a similar version marker and are transformed by an idempotent batch job. An index supporting the new search pattern is created and its query plan is inspected.

Outcome

The deployment produces no service interruption. The batch job is restarted once after a temporary database failure and continues without duplicating changes. Monitoring confirms acceptable latency, and the old representation is retained for an agreed retention period.

Learning Points

The case demonstrates that migration is an application and operational concern, not merely a database command. Compatibility, backfill strategy, observability, retry behavior, and data governance are all required for safe change.

13. Best-Practice Checklist

Before production deployment, confirm that:

  • tests cover success, failure, concurrency, and authorization;
  • migrations are version-controlled and tested on existing data;
  • destructive changes have approval and a recovery plan;
  • seeds cannot introduce development credentials into production;
  • secrets are outside source control;
  • backups are automated, encrypted, retained, and regularly restored in tests;
  • RPO and RTO are defined;
  • health checks distinguish liveness from readiness;
  • logs exclude sensitive information and include correlation identifiers;
  • metrics and actionable alerts are configured;
  • deployment supports compatible application and database versions;
  • least privilege and TLS are enabled;
  • an incident runbook and rollback or forward-fix plan exist.

Chapter Summary

Reliable database integration continues beyond schema design and CRUD programming. Testing must cover database behavior, transactions, constraints, performance, security, migration, and recovery. Seed data should be repeatable, safe, and separated from secrets and personal information.

Schema migrations are version-controlled software changes. Expand-and-contract migration reduces incompatibility during rolling deployments, while destructive operations require special approval and tested recovery. Backups must be encrypted, retained, and restored periodically; RPO and RTO express data-loss and service-restoration requirements.

Containers and managed cloud databases can improve repeatability and operational efficiency, but they do not remove responsibility for persistence, security, cost, monitoring, and recovery. CI/CD pipelines should test migrations and database interactions before deployment. Metrics, structured logs, traces, alerts, runbooks, and incident reviews support dependable operations. Finally, least privilege, privacy protection, auditability, and responsible use of automation are essential professional obligations.

Key Terms

Backup: A recoverable copy of database data.

Canary deployment: Releasing a new version to a small portion of traffic before wider release.

CI/CD: Automated continuous integration, delivery, or deployment practices.

Contract test: A test verifying an agreed interface between components or services.

Expand-and-contract: A migration pattern that adds a compatible representation before removing the old one.

Idempotent operation: An operation that can be repeated without producing an unintended additional effect.

Liveness check: A test indicating whether a process is running.

Migration: A versioned change to database structure or data.

Observability: The ability to understand system behavior through outputs such as metrics, logs, and traces.

Point-in-time recovery: Restoration to a selected moment using a backup and subsequent change records.

Readiness check: A test indicating whether a service is prepared to receive traffic.

Recovery Point Objective (RPO): The maximum acceptable period of data loss after a failure.

Recovery Time Objective (RTO): The maximum acceptable time to restore service.

Rollback: Reversing a release or change to an earlier state.

Runbook: Documented operational instructions for routine or incident procedures.

Seed data: Initial or representative data inserted by a controlled script.

Structured logging: Logging in a consistent machine-readable format.

Transaction: A group of database operations treated according to defined atomicity and consistency rules.

Review Questions

Short Answer Questions

  1. What is the difference between an integration test and a unit test in database development?
  2. Why should a backup be restored during testing?
  3. Define RPO and RTO.
  4. What is the purpose of a readiness check?
  5. Why should production secrets not be stored in source control?

Long Answer Questions

  1. Explain the expand-and-contract migration pattern and describe when it is useful.
  2. Compare logical and physical backup approaches for MySQL and identify their operational trade-offs.
  3. Discuss how CI/CD pipelines can safely apply database migrations.
  4. Explain the roles of metrics, logs, and traces in database observability.
  5. Describe a complete incident-response process for a failed production migration.

Analytical / Application Questions

  1. A service must continue operating while a column is renamed, and three application versions may run simultaneously. Design a migration sequence and justify each stage.
  2. A team reports that average query latency is acceptable but users experience occasional very slow requests. Which measurements and investigations should be performed?
  3. A company requires an RPO of 15 minutes and an RTO of one hour. Propose a backup, restoration-test, monitoring, and incident-response plan that could reasonably meet these objectives.

Multiple Choice Questions with Answers

Question 1

Which testing level most directly verifies that application code works correctly with a real MySQL or MongoDB instance?

A. Unit testing

B. Integration testing

C. Static analysis

D. Syntax highlighting

Correct Answer: B. Integration testing

Question 2

What is the main purpose of the expand-and-contract migration pattern?

A. To remove all old data immediately

B. To avoid using version control

C. To maintain compatibility while changing data representation

D. To replace database testing with application testing

Correct Answer: C. To maintain compatibility while changing data representation

Question 3

Which statement best describes RPO?

A. The maximum acceptable time to restore service

B. The maximum acceptable amount of data loss measured in time

C. The average query response time

D. The number of replicas required

Correct Answer: B. The maximum acceptable amount of data loss measured in time

Question 4

Why must backups be restored periodically in a test environment?

A. To increase application latency

B. To prove that the backup and restoration procedure works

C. To delete obsolete records

D. To replace access control

Correct Answer: B. To prove that the backup and restoration procedure works

Question 5

Which practice is safest for application secrets?

A. Commit them to a private repository

B. Place them in source-code comments

C. Use a secrets manager or protected environment configuration

D. Include them in seed data

Correct Answer: C. Use a secrets manager or protected environment configuration

Question 6

What does a readiness check determine?

A. Whether a process exists in memory

B. Whether a service is prepared to receive traffic

C. Whether a database has no indexes

D. Whether a backup is encrypted

Correct Answer: B. Whether a service is prepared to receive traffic

Question 7

Which operation is most likely to be unsafe to retry without an idempotency mechanism?

A. Reading a product by its identifier

B. Checking database health

C. Creating an order through a non-idempotent request

D. Retrieving a cached configuration value

Correct Answer: C. Creating an order through a non-idempotent request

Question 8

Which item should normally be excluded from structured application logs?

A. Request correlation identifier

B. Operation duration

C. Password or access token

D. Severity level

Correct Answer: C. Password or access token

Question 9

What is a major risk of applying a destructive migration before all application instances are compatible?

A. The database will necessarily gain more storage

B. Older instances may fail when accessing the changed structure

C. Indexes will always become unnecessary

D. Backups will automatically improve

Correct Answer: B. Older instances may fail when accessing the changed structure

Question 10

Which deployment approach exposes a new release to a small amount of traffic before expanding it?

A. Canary deployment

B. Full restore

C. Logical export

D. Schema validation

Correct Answer: A. Canary deployment

Database Integration (MySQL & MongoDB)
Chapter 13 of 14