Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New funcs #52

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

New funcs #52

wants to merge 2 commits into from

Conversation

polsala
Copy link
Member

@polsala polsala commented Aug 24, 2024

Add New Database Utility Functions

Description

This pull request introduces several new utility functions aimed at enhancing database management. The added functionalities cover a range of database operations, such as backing up tables, restoring tables, managing indexes, checking data integrity, and migrating data between tables.

Summary of Changes

  1. Backup and Restore Functions:

    • backup_table(cursor, table_name): Creates a backup of a specific table by copying its content to a temporary table.
    • restore_table(cursor, table_name): Restores a table from its backup by renaming the backup table to the original name.
  2. Index Management:

    • add_index(cursor, table_name, columns, unique=False): Adds an index to one or more columns in a table. Supports both unique and non-unique indexes, and can handle multiple columns.
    • drop_index(cursor, index_name): Drops an existing index from the database.
  3. Automated Data Integrity Checks:

    • check_data_integrity(cursor, table_name): Automatically checks for common data integrity issues, including:
      • Null values in non-nullable columns.
      • Foreign key violations.
    • This function is particularly useful for ensuring that data remains consistent after migrations or other database modifications.
  4. Data Migration Between Tables:

    • migrate_data_between_tables(cursor, source_table, destination_table, column_mapping): Migrates data from one table to another while mapping source columns to destination columns. This is helpful during schema migrations or when consolidating data.

How to Use

  1. Backup and Restore a Table:

    cursor = ...  # Get your database cursor
    backup_table(cursor, 'your_table_name')
    restore_table(cursor, 'your_table_name')
  2. Add a Unique Index on Multiple Columns:

    cursor = ...  # Get your database cursor
    add_index(cursor, 'your_table_name', ['column1', 'column2'], unique=True)
  3. Check Data Integrity Automatically:

    cursor = ...  # Get your database cursor
    is_valid = check_data_integrity(cursor, 'your_table_name')
    if not is_valid:
        print("Data integrity issues found!")
  4. Migrate Data Between Tables:

    cursor = ...  # Get your database cursor
    column_mapping = {'source_col1': 'dest_col1', 'source_col2': 'dest_col2'}
    migrate_data_between_tables(cursor, 'source_table', 'destination_table', column_mapping)

@polsala polsala marked this pull request as draft August 24, 2024 18:04
@polsala polsala self-assigned this Aug 24, 2024
@polsala polsala added the major label Aug 24, 2024
@polsala polsala modified the milestone: v0.17.2 Aug 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant