See rates for the top Laravel developers in Latin America

Using Database Comments to Track Columns With Sensitive Data

Last updated on by

Using Database Comments to Track Columns With Sensitive Data image

Recently, I was exploring a Rails application when I noticed that the developers had marked database columns as having sensitive data via a table's column definition. Various databases support comments for columns, but here's MySQL's explanation of a column comment:

A comment for a column can be specified with the COMMENT option, up to 1024 characters long. The comment is displayed by the SHOW CREATE TABLE and SHOW FULL COLUMNS statements. It is also shown in the COLUMN_COMMENT column of the Information Schema COLUMNS table.

In MySQL, we can add a comment to a column when creating or modifying a table using the following syntax:

CREATE TABLE `users` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'Example comment for name.',
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
-- ...
)

In Laravel, we can easily add comments to table columns using the comment() method in a database migration on an existing or new column:

$table->string('email')
->unique()
->comment('The user\'s email address they use the log in');

You can inspect database column comments via the table's structure; here's an example of the migrations table in a fresh Laravel application:

Not all databases support table or column comments, but databases like MySQL and PostgreSQL allow you to add comments. If your database supports comments, they can be helpful descriptors of a column and used in various ways to help developers understand a schema.

Using Comments to Mark Columns with Sensitive Data

Comment usage could range from user-friendly comments about a column or other contextual information. But what if we used something like sensitive_data=true to mark fields with sensitive user or system information?

Even if your Laravel application is mature, you could create a migration to mark columns containing sensitive information to audit your data and help in removing sensitive information during exports, database cloning, or user information requests:

php artisan make:migration mark_columns_with_sensitive_data

Carefully defining the columns as they were, we can add a comment in the migration for any table and columns we want to mark as sensitive:

public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('email')->comment('sensitive_data=true')->change();
$table->string('name')->comment('sensitive_data=true')->change();
$table->string('password')->comment('sensitive_data=true')->change();
});
 
Schema::table('profiles', function (Blueprint $table) {
// ...
});
}

After running the migration, you will see that the database now has comments on the marked fields:

Finding all Tables and Columns Marked as Sensitive

We now have useful comments throughout our application that developers can use for context, but our new comments allow us to go beyond documentation! We can find all columns marked as sensitive across our application and use this information for privacy, database cloning, and more.

While each database is different, MySQL can query the information schema columns table to find all fields marked as sensitive. My sensitive_data=true is a convention, you can do whatever you want, but here's how you could find all columns across the database:

SELECT TABLE_NAME, COLUMN_NAME, COLUMN_COMMENT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_COMMENT LIKE '%sensitive_data=true%';

Here's the output from our above example:

Taking it Further

What if we wanted an easy way to get all the sensitive column names for a given model? There are probably various ways to achieve this, but what if we built an application trait our models could use to find sensitive columns:

namespace App\Models\Concerns;
 
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
 
/**
* @mixin \Illuminate\Database\Eloquent\Model
*/
trait InteractWithSensitiveColumns
{
/**
* @return Collection<string>
*/
public function getSensitiveColumns(): Collection
{
return $this->getConnection()
->table('INFORMATION_SCHEMA.COLUMNS')
->where('TABLE_NAME', $this->getTable())
->where('COLUMN_COMMENT', 'like', '%sensitive_data=true%')
->pluck('COLUMN_NAME');
}
}

Let's take this trait for a spin in a User model instance, and see how it works:

Note that using the above trait locks you into using MySQL for models using this method. I am unsure if there is a database-agnostic way to get this information, but be aware that the above trait is MySQL-specific.

Learn More

I hope you've seen a few ways that database comments could be useful for maintaining your application database! Whether you are creating a new application or working on an existing app, you can start using comments right away! You can learn more about managing your database through Laravel's migrations and Eloquent models in the Database documentation.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Curotec

Curotec helps software teams hire the best Laravel developers in Latin America. Click for rates and to learn more about how it works.

Visit Curotec
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Join the Mastering Laravel community logo

Join the Mastering Laravel community

Connect with experienced developers in a friendly, noise-free environment. Get insights, share ideas, and find support for your coding challenges. Join us today and elevate your Laravel skills!

Join the Mastering Laravel community
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Auto-translate Application Strings with Laratext image

Auto-translate Application Strings with Laratext

Read article
Simplify Factory Associations with Laravel's UseFactory Attribute image

Simplify Factory Associations with Laravel's UseFactory Attribute

Read article
Improved Installation and Frontend Hooks in Laravel Echo 2.1 image

Improved Installation and Frontend Hooks in Laravel Echo 2.1

Read article
Filter Model Attributes with Laravel's New except() Method image

Filter Model Attributes with Laravel's New except() Method

Read article
Arr::from() Method in Laravel 12.14 image

Arr::from() Method in Laravel 12.14

Read article
Streamline API Resources with Laravel's Fluent Methods image

Streamline API Resources with Laravel's Fluent Methods

Read article