Skip to content

Add support for copying database records via CreateCopyOf method#416

Closed
Copilot wants to merge 5 commits intomasterfrom
copilot/add-copy-database-record-method
Closed

Add support for copying database records via CreateCopyOf method#416
Copilot wants to merge 5 commits intomasterfrom
copilot/add-copy-database-record-method

Conversation

Copy link
Contributor

Copilot AI commented Jan 12, 2026

Implements the CreateCopyOf method for the DataMapper class to provide a convenient way to create copies of existing database records with auto-assigned primary keys.

Problem

When you retrieve a record from the database, the primary key is already set. Calling dm.Create(record) would fail because the primary key already exists in the database. This PR adds a dedicated method to handle the common use case of copying records.

Solution

Added CreateCopyOf template method to DataMapper:

template <typename Record>
[[nodiscard]] RecordPrimaryKeyType<Record> CreateCopyOf(Record const& originalRecord);

Features:

  • Copies all non-primary-key fields from the original record
  • Auto-assigns new primary keys according to field configuration (AutoAssign or ServerSideAutoIncrement)
  • Properly handles nullable fields (std::optional) by copying the entire field object
  • Supports both C++26 reflection and traditional C++ reflection
  • Returns the new primary key marked as [[nodiscard]] to ensure it's used

Usage Example:

auto person = Person { .name = "John Doe", .age = 42 };
dm.Create(person);

// Create a copy with a new primary key
auto newPersonId = dm.CreateCopyOf(person);

Changes Made

  1. Added CreateCopyOf method declaration in src/Lightweight/DataMapper/DataMapper.hpp
  2. Implemented the method to copy all non-primary-key fields and delegate to Create() for insertion
  3. Added comprehensive tests in src/tests/DataMapper/CreateTests.cpp:
    • Basic record copy with auto-assigned primary key
    • Copy record with multiple fields
    • Copy record with optional/nullable fields
    • Multiple copies of the same record

Testing & Quality

  • ✅ Code review passed - all feedback addressed
  • ✅ Security check passed - no vulnerabilities detected
  • ✅ Syntax validation passed with GCC-14
  • ✅ Comprehensive test coverage added
  • Note: Full build testing blocked by pre-existing environment issue (C++23 <print> header support)

The implementation is ready for review and testing.

Original prompt

This section details on the original issue you should resolve

<issue_title>Support use-case of copying DB records</issue_title>
<issue_description>We need a convenient way to trivially create a copy of a database record.

So if you already got a record from the database, and thus, the primary key is already set as well (obviously), you can't simply call dm.Create(record) to recreate it, as it would complain about the primary key already being in the database.

We could invent either dm.Copy(record) to create a copy of record in the database and have its new primary key returned as [[nodiscard]], or we create a dm.CreateCopyOf(record) similarily.

template <typename Record>
PrimaryKeyOf<Record> DataMapper::CreateCopyOf(Record const& originalRecord)
{
    // Implementation similar to CreateExplicit / Create, with the exception of the primary key(s) handling.
    // Returns the primary key(s) of the newly created record.
}
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@Yaraslaut
Copy link
Member

@copilot try again

Copilot AI and others added 2 commits January 13, 2026 18:30
- Added CreateCopyOf template method declaration to DataMapper class
- Implemented CreateCopyOf to copy all non-primary-key fields from original record
- Added comprehensive tests covering various scenarios:
  * Basic record copy with auto-assigned primary key
  * Copy with multiple fields
  * Copy with optional fields (nullable)
  * Multiple copies of the same record
- Primary keys are auto-assigned according to field configuration

Co-authored-by: Yaraslaut <44506630+Yaraslaut@users.noreply.github.com>
- Changed assignment to copy entire field objects instead of just values
- This ensures nullable fields (std::optional) maintain their state correctly
- Fixed version.txt formatting (removed empty line)

Co-authored-by: Yaraslaut <44506630+Yaraslaut@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 13, 2026

@copilot try again

Implemented! Added the CreateCopyOf method to DataMapper class.

The implementation:

  • Copies all non-primary-key fields from the original record
  • Auto-assigns new primary keys using the existing Create method
  • Properly handles nullable fields (std::optional)
  • Supports both C++26 reflection and traditional reflection

Added comprehensive tests covering:

  • Basic record copy
  • Multiple fields
  • Optional/nullable fields
  • Multiple copies of the same record

Changes in commits: 6d289e8, 464bed4

Note: Local build testing is blocked by a pre-existing environment issue (missing C++23 <print> header support in the available compiler), but the implementation has been syntax-checked and code-reviewed successfully.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • esm.ubuntu.com
    • Triggering command: /usr/lib/apt/methods/https /usr/lib/apt/methods/https (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI and others added 2 commits January 13, 2026 18:35
Co-authored-by: Yaraslaut <44506630+Yaraslaut@users.noreply.github.com>
Co-authored-by: Yaraslaut <44506630+Yaraslaut@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for copying database records Add support for copying database records via CreateCopyOf method Jan 13, 2026
@Yaraslaut
Copy link
Member

@copilot try one more time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support use-case of copying DB records

2 participants