Rollback method and device of database, electronic equipment and storage medium

By acquiring and generating change statements that reveal differences in the database table structure, and by using a rollback strategy to restore the database state when execution fails, the problem of inconsistent database table structures was resolved, ensuring normal business operations.

CN121277761BActive Publication Date: 2026-02-24XINHUA SAN IND INTERNET CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202511823456.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-12-05
Publication Date
2026-02-24
Estimated Expiration
2045-12-05

AI Technical Summary

Technical Problem

During database table structure changes, the lack of transaction guarantees in many data definition language statements leads to some statements executing successfully while others fail, resulting in inconsistencies between the table structures of the configuration database and the business database. Traditional rollback solutions cannot guarantee the normal operation of the business.

Method used

By obtaining the table structure differences between the first and second databases, a change statement in the form of a data definition language is generated. If the execution fails, the rollback strategy of the successfully executed change statement is used to restore the second database to its original state before the change.

Benefits of technology

Ensure that the table structure and business data of the second database are restored to their original state before the change, so as to avoid affecting the normal operation of related businesses.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121277761B_ABST
    Figure CN121277761B_ABST
Patent Text Reader

Abstract

The present application relates to the technical field of database, and discloses a database rollback method and device, electronic equipment and storage medium, the method comprises the following steps: obtaining a first table structure corresponding to a first database and a second table structure corresponding to a second database; when the first table structure is different from the second table structure, determining the table structure difference information between the first table structure and the second table structure; generating at least one change statement in the form of data definition language based on the difference attribute corresponding to the table structure difference information; if a first change statement fails during the execution of the table structure change process, obtaining a second change statement that has been successfully executed; based on the rollback strategy corresponding to the second change statement, restoring the second database to the original state before the change. By implementing the method, the table structure and business data of the second database can be restored to the original state before the change, thereby not affecting the normal operation of related businesses.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of database technology, and more specifically to database rollback methods, apparatus, electronic devices, and storage media. Background Technology

[0002] In modern information systems and applications, a collaborative model between configuration databases and business databases is commonly used. By storing the table structure information of the business database in the configuration database, changes can be made to the table structure of the business database to ensure that the table structure of the business database strictly follows the dynamic updates of the configuration database.

[0003] During the process of changing the table structure of the business database, due to the lack of transaction guarantees for multiple Data Definition Language (DDL) statements that have interdependencies, some DDL statements may execute successfully while the rest fail. For example, if a field is added to a table in the business database and then an index is created for that field, and the DDL statement for adding the field executes successfully but the DDL statement for creating the index fails, the change operation for that table will be in a "partially completed" state, resulting in a situation where the table structures of the configuration database and the business database are inconsistent.

[0004] Given the aforementioned issues, DDL statements are typically rolled back to restore the table structure of the business database to its state before the changes. However, traditional rollback solutions cannot adequately guarantee the normal operation of the business. Summary of the Invention

[0005] This invention provides a database rollback method, apparatus, electronic device, and storage medium to solve the problem that traditional rollback schemes cannot adequately guarantee the normal operation of business.

[0006] In a first aspect, the present invention provides a database rollback method, the method comprising: obtaining a first table structure corresponding to a first database and a second table structure corresponding to a second database, wherein the first database is used to store table structures and the second database is used to store business data; when the first table structure and the second table structure are different, determining table structure difference information between the first table structure and the second table structure; generating at least one change statement in the form of a data definition language based on the difference attributes corresponding to the table structure difference information, wherein the change statement is used to change the second table structure to the first table structure; if a first change statement fails to execute during the execution of the table structure change, obtaining a second change statement that has been successfully executed; and restoring the second database to its original state before the change based on the rollback strategy corresponding to the second change statement.

[0007] Secondly, the present invention provides a database rollback device, comprising: a first acquisition module, configured to acquire a first table structure corresponding to a first database and a second table structure corresponding to a second database, wherein the first database is used to store table structures and the second database is used to store business data; a first determination module, configured to determine table structure difference information between the first table structure and the second table structure when the first table structure and the second table structure are different; a first generation module, configured to generate at least one change statement represented in the form of a data definition language based on the difference attributes corresponding to the table structure difference information, wherein the change statement is used to change the second table structure to the first table structure; a second acquisition module, configured to acquire a second change statement that has been successfully executed if a first change statement fails to execute during the execution of the table structure change; and a recovery module, configured to restore the second database to its original state before the change based on the rollback strategy corresponding to the second change statement.

[0008] Thirdly, the present invention provides an electronic device, comprising: a memory and a processor, wherein the memory and the processor are communicatively connected to each other, the memory stores computer instructions, and the processor executes the computer instructions to perform the database rollback method of the first aspect or any corresponding embodiment described above.

[0009] Fourthly, the present invention provides a computer-readable storage medium storing computer instructions for causing a computer to execute the database rollback method of the first aspect or any corresponding embodiment described above.

[0010] Fifthly, the present invention provides a computer program product, including computer instructions for causing a computer to execute the database rollback method of the first aspect or any corresponding embodiment described above.

[0011] The database rollback method of this application embodiment can accurately determine the table structure differences between the first table structure and the second table structure by comparing the first table structure of the first database with the second table structure of the second database. Then, based on the table structure difference information, at least one change statement represented in the form of a data definition language can be accurately generated. During the process of changing the second table structure to the first table structure through the change statement, if a first change statement fails to execute, the second database is restored to its original state before the change by using the rollback strategy corresponding to the currently successfully executed second change statement. This ensures that the table structure and business data of the second database can be restored to their original state before the change, thereby not affecting the normal operation of related businesses. Attached Figure Description

[0012] To more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.

[0013] Figure 1 This is a schematic diagram of an application scenario according to an embodiment of the present invention;

[0014] Figure 2 This is a schematic diagram of the first type of database rollback method according to an embodiment of the present invention;

[0015] Figure 3 This is a schematic diagram of a second process for a database rollback method according to an embodiment of the present invention;

[0016] Figure 4 This is a schematic diagram of the third process of the database rollback method according to an embodiment of the present invention;

[0017] Figure 5 This is a structural block diagram of a database rollback device according to an embodiment of the present invention;

[0018] Figure 6 This is a schematic diagram of the hardware structure of an electronic device according to an embodiment of the present invention. Detailed Implementation

[0019] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0020] It is understood that before using the technical solutions disclosed in the various embodiments of the present invention, users should be informed of the types, scope of use, and usage scenarios of the personal information involved in the present invention and their authorization should be obtained in accordance with relevant laws and regulations through appropriate means.

[0021] The terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of this invention, "a plurality of" means two or more, unless otherwise explicitly specified.

[0022] In modern information systems and applications, database management and restructuring are dynamic and continuous processes. Especially for large enterprises, databases are no longer fixed storage modules; they require frequent structural adjustments based on business needs and application changes. In this context, the importance of batch Data Definition Language (DDL) operations is increasingly evident.

[0023] In mainstream enterprise architectures, a collaborative model between configuration databases and business databases is commonly adopted. The configuration database stores the table structure, change history, and version information of the business database, acting as the "decision center" for changes to the business database's table structure. The business database stores the actual business data, and its table structure must strictly follow the dynamic updates of the configuration database. This collaborative model can automate the entire "design-deployment-verification" process, avoiding structural inconsistencies caused by manual changes to the business database's table structure. However, in the implementation of batch DDL operations, existing technologies face several core challenges, such as:

[0024] 1. The native mechanisms of databases have certain limitations. For example, the transaction mechanisms of mainstream databases only support the atomicity of Data Manipulation Language (DML), which has a critical defect for DDL operations. Traditional DDL statements (such as ALTER TABLE and CREATE INDEX) have implicit commit characteristics, which force the current transaction to be committed after execution and cannot be rolled back. Even if a single DDL statement supports atomic operations on multiple objects, it still lacks transaction guarantees for independent DDL statement sequences (such as adding a field first and then creating an index). For example, if the DDL statement "ALTER TABLE users ADD COLUMN mobileVARCHAR (20)" is executed successfully, and the subsequent "CREATE UNIQUE INDEX idx_mobile ON users(mobile)" fails due to a unique key conflict, the changes to the users table will be in a "semi-complete" state (the added field is retained but the constraint is missing), resulting in inconsistencies between the table structure of the business database and the configuration database.

[0025] 2. Increased complexity of business scenarios. For example, large enterprise business databases are generally characterized by "large data volume (TB level), tight table relationships (many foreign key dependencies), and cross-database deployment (mixed use of MySQL / PostgreSQL / Oracle)". The backup and recovery of TB-level large tables can take up to several hours, and traditional backup and rollback solutions cannot meet business continuity requirements. In multi-table foreign key relationship scenarios, incorrect execution order of DDL statements can easily cause dependency conflicts (such as foreign key addition in the child table before the parent table field has been created). When deployed across databases, the syntax rules and snapshot mechanisms of different databases are significantly different, making it difficult for a single solution to be compatible.

[0026] In view of this, this application proposes a database rollback method. This method compares the first table structure of the first database with the second table structure of the second database to accurately determine the table structure differences between the first and second table structures. Then, based on these differences, at least one change statement represented in Data Definition Language (DDL) can be accurately generated. During the process of changing the second table structure to the first table structure using these change statements, if a first change statement fails to execute, the second database is restored to its original state before the change using the rollback strategy corresponding to the currently successfully executed second change statement. This ensures that the table structure and business data of the second database can be restored to their original state before the change, thus preventing any impact on the normal operation of related business processes.

[0027] As one possible application scenario for this application, such as Figure 1 As shown, the database system includes a first database, namely the configuration database, and a second database, namely the business database. The database can be deployed on electronic devices such as personal computers, servers, and cloud platforms; this application does not specifically limit its deployment.

[0028] Configure the database to store the table structure of various versions of the business database (e.g.) Figure 1 The table structure shown includes a first table structure with version number N, a first historical table structure with version number N-1, and a second historical table structure with version number N-2. Table structures can include table information such as table name and table comments; column structures such as column names, data types, data length, data precision, default values, and whether NULL values ​​are allowed; index structures such as whether it is a unique index, index name, index fields, index length, and index ascending / descending order; and foreign key information such as foreign key name, foreign key column, foreign key related tables, and column names of the foreign key related tables. It should be understood that the higher the version number of the table structure stored in the configuration database, the newer the table structure corresponding to that version number.

[0029] like Figure 1As shown, the business database stores business data through multiple tables, such as Business Table 1, Business Table 2, and Business Table 3. The business database has no independent structure definition; all changes to the table structure are driven by the configuration database, and the table structure of the business database is consistent with the latest version of the configuration database's table structure (e.g., ...). Figure 1 The first table structure (shown as version number N) must be strictly consistent. It should be understood that... Figure 1 The business table 1, business table 2 and business table 3 shown all store the corresponding business data.

[0030] If the configuration database contains the latest version of the first table structure, and the business database's second table structure is the first historical table structure with version number N-1 in the configuration database, then the first table structure in the configuration database will be different from the second table structure in the business database. Therefore, at least one change statement can be generated based on the table structure difference information between the first and second table structures to change the second table structure in the business database to the first table structure, ensuring that the table structure in the business database is strictly consistent with the table structure in the configuration database.

[0031] If a first change statement fails during the execution of a change statement, the currently successfully executed change statement is designated as the second change statement. The business database is then restored to its original state before the change by using the rollback strategy corresponding to the second change statement. This ensures that the table structure and business data of the business database can be restored to their original state before the change, thereby guaranteeing that the business that depends on the business database can operate normally.

[0032] According to an embodiment of the present invention, a method for rolling back a database is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.

[0033] This embodiment provides a database rollback method that can be used in electronic devices. Figure 2 This is a flowchart of a database rollback method according to an embodiment of the present invention, such as... Figure 2 As shown, the process includes the following steps:

[0034] Step S201: Obtain the first table structure corresponding to the first database and the second table structure corresponding to the second database. The first database is used to store table structures, and the second database is used to store business data.

[0035] The first database can be the configuration database shown above. The first database is used to store the table structures of various versions of the business database (such as...). Figure 1As shown, there is a first table structure with version number N, a first history table structure with version number N-1, and a second history table structure with version number N-2. The table structures are as described above and will not be repeated here.

[0036] The second database can be the business database shown above, and it can include multiple data tables, such as... Figure 1 The table shows business table 1, business table 2, and business table 3, and the business data is stored through multiple data tables.

[0037] The first and second table structures can be used to define the filenames of each data table in the second database (i.e., the business database), as well as the fields contained in each data table in the second database (i.e., the business database), the field names, field types, widths, and other attribute information, but do not contain the specific business data.

[0038] The first table structure can be the table structure of the latest version N in the first database, i.e. the configuration database. The second table structure can be the current table structure in the second database, i.e. the business database. In other words, the second table structure can be the table structure with version number N-1 in the first database.

[0039] In a specific example, the structure of a first table can be obtained by accessing a first database, and the structure of a second table can be obtained by accessing a second database. For example, a first connection pool can be configured for the first database, and first connection parameters (such as URL, account, maximum number of connections, etc.) can be configured for the first connection pool. Similarly, a second connection pool can be configured for the second database, and second connection parameters (such as URL, account, maximum number of connections, etc.) can be configured for the second connection pool. Then, the full definition information of tables, columns, indexes, and foreign keys in the first database can be accessed and read through the connection relationships in the first connection pool to obtain the structure of the first table. Likewise, the full definition information of tables, columns, indexes, and foreign keys in the second database can be accessed and read through the connection relationships in the second connection pool to obtain the structure of the second table.

[0040] Step S202: When the structure of the first table is different from that of the second table, determine the table structure difference information between the first table structure and the second table structure.

[0041] As business operations change, the tables in the business database (second database) will also change accordingly. This can be achieved by configuring a new table structure (first table structure) in the configuration database (first database), resulting in a different table structure between the configuration database (first database) and the business database (second database). This table structure difference information is used to characterize the differences between the first and second table structures at the table, column, index, foreign key, and other levels.

[0042] As a concrete example, the differences in table structure can be determined by comparing the first and second table structures one by one, following the hierarchical order of the table structure, such as tables, columns, indexes, and foreign keys. Alternatively, the differences in table structure can be stored in the form of an array.

[0043] Step S203: Based on the difference attributes corresponding to the table structure difference information, generate at least one change statement in the form of a data definition language. The change statement is used to change the second table structure to the first table structure.

[0044] Difference attributes are used to characterize the difference type corresponding to table structure difference information. For example, when the table structure difference information indicates the deletion of a data table, the corresponding difference attribute is "deleted data table". In this way, the difference attribute determines the type of change operation, such as delete, add, or modify, and also determines the object of the change, such as table, field, index, and foreign key, etc.; then, at least one change statement represented in a data definition language can be generated based on the determined change type and the object of the change.

[0045] Step S204: If a first change statement fails to execute during the table structure change process, then obtain the second change statement that has been successfully executed.

[0046] The second table structure of the second database (the business database) is changed to the first table structure of the first database (the configuration database) by executing each change statement sequentially. During the execution of each change statement, if a single change statement fails (e.g., a timeout or error code such as 1062 primary key conflict), a global execution abort mechanism is immediately triggered, terminating the execution of all subsequent change statements. Thus, all change statements preceding the first failed change statement are considered successful second change statements. Therefore, the currently successfully executed second change statements can be obtained by querying the operation log.

[0047] Step S205: Based on the rollback strategy corresponding to the second change statement, restore the second database to its original state before the change.

[0048] The rollback strategy is used to characterize how to reverse the operation corresponding to the second change statement to undo the change operation, thereby restoring the second database to its original state before the change.

[0049] Since changing the table structure of the second database usually involves changing the business data of the second database, a rollback strategy can be used to restore the second database to its original state before the change by first restoring the table structure and then restoring the business data of the second database. This ensures that the second database can be accurately restored to its original state before the change, both in terms of table structure and business data, thereby avoiding impact on the operation of normal business.

[0050] The database rollback method provided in this embodiment accurately determines the table structure differences between the first and second table structures by comparing the first table structure of the first database with the second table structure of the second database. Based on these differences, at least one change statement, represented in Data Definition Language (DDL), can be accurately generated. During the process of changing the second table structure to the first table structure using the change statement, if a first change statement fails to execute, the rollback strategy corresponding to the successfully executed second change statement restores the second database to its original state before the change. This ensures that the table structure and business data of the second database can be restored to their original state before the change, thus preventing any impact on the normal operation of related businesses.

[0051] This embodiment provides a database rollback method that can be used in electronic devices. Figure 3 This is a flowchart of a database rollback method according to an embodiment of the present invention, such as... Figure 3 As shown, the process includes the following steps:

[0052] Step S301: Obtain the first table structure corresponding to the first database and the second table structure corresponding to the second database. The first database is used to store table structures, and the second database is used to store business data. For details, please refer to [link to relevant documentation]. Figure 2 Step S201 of the illustrated embodiment will not be described again here.

[0053] Step S302: When the structure of the first table is different from that of the second table, determine the table structure difference information between the structure of the first table and the structure of the second table.

[0054] Specifically, step S302 includes:

[0055] Step S3021: When the structure of the first table is different from that of the second table, perform hash processing on the first table structure to generate a first hash value, and perform hash processing on the second table structure to generate a second hash value.

[0056] As shown above, both the first and second table structures can include table information such as table name and table comments; column structure such as column name, data type, data length, data precision, default value, and whether NULL values ​​are allowed; index structure such as whether it is a unique index, index name, index field, index length, index ascending / descending order; and foreign key information such as foreign key name, foreign key column, foreign key associated table, and column names of the foreign key associated table, etc.

[0057] The following section uses the first table structure as an example to describe the generation process of the first hash value. First, Message-Digest Algorithm Version 5 (MD5) can be used to hash the table information, column structure, index structure, and foreign key structure, respectively, to obtain the table information hash value, column structure hash value, index structure hash value, and foreign key structure hash value. Then, the first hash value is obtained by concatenating these hash values. The generation process of the second hash value is similar to that of the first hash value and will not be repeated here.

[0058] It should be understood that the hash values ​​of table information, column structure, index structure, and foreign key structure can be directly concatenated or merged; this application does not impose any specific limitations.

[0059] Step S3022: Based on the first comparison result between the first hash value and the second hash value, determine the table structure difference information.

[0060] Here, the first hash value and the second hash value can be compared. If the first hash value and the second hash value are the same, it means that the structure of the first table and the structure of the second table are the same, and there is no need to change the structure of the second table.

[0061] Alternatively, the hash values ​​of the table information corresponding to the first table structure can be directly compared with those of the table information corresponding to the second table structure to determine if there are any table-level differences between them. This process can be repeated to determine if there are any differences between the first and second table structures. If no differences exist, then no changes to the second table structure are necessary.

[0062] In some optional implementations, step S3023 above includes:

[0063] Step a1: If the first comparison result indicates that the first hash value and the second hash value are different, then obtain the first-level information of the first table structure and the second-level information of the second table structure.

[0064] Step a2: According to the correspondence between the first-level information and the second-level information, compare the first table structure and the second table structure in turn to determine the differences in table structure information.

[0065] The first-level information corresponding to the first table structure and the second-level information corresponding to the second table structure can be the same. For example, both the first-level information and the second-level information can be information about tables, columns, indexes, and foreign keys.

[0066] The correspondence between the first-level and second-level information can be achieved by matching the tables in the first-level information with the tables in the second-level information. This allows for a comparison of the table information in the first-level information with the table information in the second-level information. Then, the correspondence between the columns in the first-level information and the columns in the second-level information can be achieved by comparing the column structures in the first-level information with the column structures in the second-level information. This process of comparison is repeated to determine the differences in table structure information.

[0067] For example, at the table level, the table names and / or table comments of the first table structure are compared with the table names and / or table comments of the second table structure. If a difference exists, the difference is identified as a MODIFY TABLE.

[0068] For example, at the column level, the column names, data types (including length / precision), default values, NOT NULL constraints, etc. of the first table structure are compared with those of the second table structure. If a new column is added, the difference is determined as ADD COLUMN; if a column is deleted, the difference is determined as DROP COLUMN; if the attribute changes, the difference is determined as MODIFY COLUMN.

[0069] For example, at the index level, the index name, type (primary key / normal / unique), and included fields (including order) of the first table structure are compared with the index name, type, and included fields of the second table structure. If a new index is added, the difference is determined as CREATE INDEX; if an index is deleted, the difference is determined as DROP INDEX; if an attribute is changed, the difference is determined as MODIFY INDEX.

[0070] For example, at the foreign key level, the foreign key names, related tables, related columns, deletion rules (RESTRICT / CASCADE, etc.), update rules, etc. of the first table structure are compared with the foreign key names, related tables, related columns, deletion rules (RESTRICT / CASCADE, etc.), update rules, etc. of the second table structure. If a new foreign key is added, the difference is determined as ADD FOREIGN KEY; if a foreign key is deleted, the difference is determined as DROP FOREIGN KEY; if the attribute is changed, the difference is determined as MODIFY FOREIGN KEY.

[0071] As a concrete example, table structure differences can be recorded in JSON array format. The array fields can include change operation type (ADD / MODIFY / DROP), object type (TABLE / COLUMN / INDEX / FOREIGN_KEY), table name (table_name), object name (object name), old_value (old attribute JSON), and new_value (new attribute JSON).

[0072] By relying on the correspondence between the first-level information and the second-level information, the structure of the first table and the structure of the second table are compared layer by layer, thus accurately identifying the differences in table structure information.

[0073] By comparing the first hash value corresponding to the first table structure with the second hash value corresponding to the second table structure, the first comparison result can be quickly determined, and then the differences in table structure information can be determined relatively quickly.

[0074] Step S303: Based on the difference attributes corresponding to the table structure difference information, generate at least one change statement in the form of a data definition language. The change statement is used to change the second table structure to the first table structure.

[0075] Specifically, step S303 includes:

[0076] Step S3031: Based on the difference attributes corresponding to the table structure difference information, determine the change operation type for modifying the second table structure.

[0077] The change operation type can be an operation type that changes the structure of the second table. For example, it can be an add type such as adding a column (ADD COLUMN) or a modify type such as modifying an index (MODIFY INDEX).

[0078] Step S3032: Obtain the predefined template statement corresponding to the change operation type.

[0079] Predefined template statements are "statement frames" pre-defined for each type of change operation (such as add or modify). They contain a fixed format and placeholders for variables to be filled, and are used to quickly convert table structure differences into executable change statements, i.e., DDL statements as shown above. Furthermore, different database types correspond to different predefined template statements. For example, MySQL and PostgreSQL have different predefined template statements. In a specific example, one can query the template library by change operation type to obtain the corresponding predefined template statement.

[0080] Step S3033: Invoke the predefined template statement to convert the table structure difference information into at least one change statement.

[0081] For example, in MySQL, if the change operation type is ADD COLUMN, a change statement such as ALTER TABLE ${table_name} ADD COLUMN ${column_definition} ALGORITHM=INPLACE can be generated.

[0082] For example, in PostgreSQL, if the change operation type is CREATE INDEX, a change statement such as CREATEINDEX CONCURRENTLY ${index_name} ON ${table_name} (${columns}) can be generated.

[0083] By using the change operation type, you can quickly identify the predefined template statement corresponding to the change operation type. Then, by calling the predefined template statement, you can quickly and accurately convert the table structure difference information into one or more change statements.

[0084] In some optional implementations, step S3033 above further includes:

[0085] Step b1: Invoke the predefined template statement to convert the table structure difference information into multiple initial change statements.

[0086] Step b2: Sort the initial change statements according to their dependencies to obtain sorted initial change statements.

[0087] Step b3: According to the data tables of the second database corresponding to each sorted initial change statement, merge the sorted initial change statements to obtain at least one change statement.

[0088] Dependencies can be used to characterize the execution order constraints between multiple initial change statements. For example, a column must be added before an index can be created for that column; otherwise, syntax errors, data inconsistencies, or change failures will occur. Thus, the essence of dependencies is determined by the logical relationships in the table structure (such as the dependencies between fields, constraints, and indexes).

[0089] When sorting multiple initial change statements based on dependencies, change statements related to the deletion operation can be placed last, and then the initial change statements can be sorted according to dependencies. This way, if any change statements fail to execute and a rollback is needed, the success rate of the rollback can be improved, and the impact of the deletion operation can be reduced.

[0090] After sorting multiple initial change statements, if two or more initial change statements correspond to the same data table and there is no dependency between these initial change statements, these initial change statements can be merged into one change statement, thereby reducing the table locking time of the data table corresponding to the initial change statement.

[0091] For example, there are three change statements: adding column 1 to a data table, creating an index for column 1, and adding column 2 to the same data table. Since there is no dependency between adding column 1 and adding column 2 and they correspond to the same data table, the change statements for adding column 1 and adding column 2 can be merged into one change statement.

[0092] First, the initial change statements are sorted according to their dependencies. Then, the sorted initial change statements are merged to obtain at least one change statement. This ensures that errors are avoided when executing at least one change statement and reduces the table locking time, thereby reducing the impact on normal business operations.

[0093] Step S304: If a first change statement fails to execute during the table structure modification process, then retrieve the second change statement that has already been successfully executed. For details, please refer to [link to relevant documentation]. Figure 2 Step S204 of the illustrated embodiment will not be described again here.

[0094] Step S305: Based on the rollback strategy corresponding to the second change statement, restore the second database to its original state before the change. For details, please refer to [link to details]. Figure 2 Step S205 of the illustrated embodiment will not be described again here.

[0095] The database rollback method provided in this embodiment can accurately determine the table structure differences between the first table structure and the second table structure according to the correspondence between the first-level information corresponding to the first table structure and the second-level information corresponding to the second table structure. Furthermore, by using the change operation type corresponding to the table structure difference information, the table structure difference information can be accurately converted into one or more change statements.

[0096] This embodiment provides a database rollback method that can be used in electronic devices. Figure 4 This is a flowchart of a database rollback method according to an embodiment of the present invention, such as... Figure 4 As shown, the process includes the following steps:

[0097] Step S401: Obtain the first table structure corresponding to the first database and the second table structure corresponding to the second database. The first database is used to store table structures, and the second database is used to store business data. For details, please refer to [link to relevant documentation]. Figure 2Step S201 of the illustrated embodiment will not be described again here.

[0098] Step S402: When the structure of the first table differs from that of the second table, determine the table structure difference information between the first table structure and the second table structure. For details, please refer to [link to relevant documentation]. Figure 2 Step S202 of the illustrated embodiment will not be described again here.

[0099] Step S403: Based on the difference attributes corresponding to the table structure difference information, generate at least one change statement represented in Data Definition Language (DDL) format. This change statement is used to change the second table structure to the first table structure. For details, please refer to [link to details]. Figure 2 Step S203 of the illustrated embodiment will not be described again here.

[0100] Step S404: If a first change statement fails to execute during the table structure modification process, then retrieve the second change statement that has already been successfully executed. For details, please refer to [link to relevant documentation]. Figure 2 Step S204 of the illustrated embodiment will not be described again here.

[0101] Step S405: Based on the rollback strategy corresponding to the second change statement, restore the second database to its original state before the change.

[0102] Specifically, step S405 includes:

[0103] A rollback strategy is a method to restore the database to its original state before the change when the execution of a change statement fails during the process of modifying the table structure of a second database. By understanding the database type and change operation type corresponding to the change statement, a rollback strategy can be determined relatively quickly and effectively. This rollback strategy can include rollback statements and rollback resources. It should be understood that different database types require different methods for restoring the second database to its original business data using rollback resources.

[0104] Step S4051: Execute the rollback statement corresponding to the second change statement to restore the table structure of the second database to the original table structure before the change.

[0105] For example, if the change operation type corresponding to the second change statement is an insert operation, you can first check if the new object exists. If it exists, you can execute the rollback statement corresponding to the second change statement, such as DROP COLUMN, DROP TABLE, or DROP INDEX, to restore the table structure of the second database to its original structure before the change.

[0106] For example, if the change operation type corresponding to the second change statement is a modification operation, then the rollback statement corresponding to the second change statement can be executed, such as restoring the new attributes of the column to the old attributes, so as to restore the table structure of the second database to the original table structure before the change.

[0107] For example, if the change operation type corresponding to the second change statement is a delete operation, a rollback statement that is the opposite of the delete operation can be executed, such as ALTER TABLE table_A ADD COLUMN col_old INT DEFAULT 0, to restore the table structure of the second database to the original table structure before the change.

[0108] Step S4052: Based on the rollback resources corresponding to the second change statement, restore the business data of the second database to the original business data before the change.

[0109] For example, if the change operation type corresponding to the second change statement is a modification operation and the database type is PostgreSQL, then the rollback resources can be read by SELECT * FROM ${table_name}, and the original data can be written back by generating an UPDATE statement (with the WHERE clause built based on the primary key) to restore the business data of the second database to the original business data before the change.

[0110] For example, if the change operation type corresponding to the second change statement is a modification operation and the database type is MySQL, then by generating a statement such as INSERT ... ON DUPLICATE KEY UPDATE, the original data in the rollback resource can be used to overwrite the modified data, so as to restore the business data of the second database to the original business data before the change.

[0111] For example, if the change operation type corresponding to the second change statement is a deletion operation, the data in the rollback resource can be fed back to the second database by generating a statement such as INSERT INTO table_A (col_old) SELECT col_old FROM table_A_backup, so as to restore the business data in the second database to the original business data before the change.

[0112] In some alternative implementations, the method further includes:

[0113] Step c1: For any change statement, determine the generation strategy for the rollback resources corresponding to the change statement based on the database type corresponding to the change statement.

[0114] Step c2: Generate rollback resources corresponding to each change statement according to the generation strategy corresponding to each change statement.

[0115] Step c3: After generating the rollback resources for each change statement, execute multiple change statements. For each successfully executed change statement, generate the corresponding rollback statement.

[0116] After obtaining at least one change statement, it can be treated as a whole and a transaction can be generated for it. For at least one change statement within this transaction, each change statement can be loaded into memory in the previously ordered sequence, and its syntax validity can be verified using the database's PREPARE statement. If verification passes, the rollback resource generation strategy for that change statement is determined; if verification fails, error details (including error location and incompatibility reason) are immediately output, and the entire transaction process is terminated. After the rollback resources for each change statement are prepared, each change statement can be executed sequentially.

[0117] After a transaction is started, information such as the transaction start time, table names involved, and execution nodes can be recorded. Furthermore, the snapshot storage path (such as a local temporary directory, path format / tmp / ddl_rollback / ${txn_id} / ) can be initialized for modification-type change statements, and the backup table naming rules can be initialized for deletion-type change statements.

[0118] For example, if the change operation type corresponding to the change statement is modification type, the rollback resource generation strategy can be: start an isolation level transaction such as REPEATABLE READ to ensure snapshot data consistency; create a transaction snapshot based on the database type, such as PostgreSQL calling pg_export_snapshot() to obtain the snapshot identifier, and MySQL parsing the current binary log (binlog) position (record file name and offset) to generate an equivalent snapshot; commit the snapshot transaction, only retain the snapshot reference, and do not modify the business data.

[0119] For example, if the change operation type corresponding to the change statement is deletion, the rollback resource generation strategy can be to create a backup table. For instance, before executing DROP TABLE, a full backup table can be created using CREATE TABLE ${bak_table} AS SELECT * FROM ${src_table} WITH DATA. Before executing DROP COLUMN, an incremental backup table can be created using CREATE TABLE ${bak_table} AS SELECT primary key field, ${deleted column name} FROM ${src_table} WITH DATA (preserving the data associated with the deleted object).

[0120] As a concrete example, after acquiring rollback resources, these resources can be temporarily stored in memory. After a change statement is successfully executed, it can be associated with the corresponding rollback resource. For instance, information such as snapshot identifiers, binlog locations, and backup table names can be temporarily stored in memory.

[0121] During the execution of each change statement, the execution status of each change statement can be recorded in real time, such as pending execution, executing, successful, and failed. Furthermore, a rollback statement is generated for each successfully executed change statement. For example, the rollback statement for `ADD COLUMN col1` is `ALTER TABLE t DROP COLUMN col1`; the rollback statement for `MODIFY COLUMN col1 INT` is `ALTER TABLE t MODIFY COLUMN col1 VARCHAR (20)` (generated based on `old_value`). The rollback statement is stored in a data storage structure with a first-in, first-out (LIFO) characteristic according to the execution order of the change statements and is associated with the transaction identifier of the transaction.

[0122] Meanwhile, during the execution of change statements, if the change operation type of the change statement is an add operation, the dependency relationship between the added object (such as a table, column, index, foreign key) and subsequent change operations can be recorded (such as which indexes reference the added column, and the table / column associated with the added foreign key), providing a basis for dependency handling during rollback.

[0123] Executing the change statement after generating the rollback resources ensures that if the change statement fails, the data in the second database can be restored to the original business data using the rollback resources.

[0124] In some alternative implementations, the method further includes:

[0125] Step d1: Treat multiple change statements as a whole, generate transactions corresponding to the multiple change statements, and determine the transaction identifier corresponding to the transactions.

[0126] Step d2: When a change statement is successfully executed, the rollback resource corresponding to the change statement is associated with the transaction identifier to generate an association result.

[0127] Step d3: Obtain the execution information corresponding to the change statement and the resource name corresponding to the rollback resource.

[0128] Step d4: Write the transaction identifier, associated results, execution information, and resource name into the operation log.

[0129] For example, if the change operation type of a successfully executed change statement is a modification type such as modifying a column or modifying an index, the transaction snapshot information created before execution (PostgreSQL records the snapshot identifier, and MySQL records the binlog file name and position) can be associated with the transaction identifier corresponding to the successfully executed change statement and written to the operation log.

[0130] For example, if the change operation type of a successfully executed change statement is a deletion type, such as deleting a table, deleting a column, or deleting a foreign key, then the backup table information created before execution (backup table name, creation time, total number of rows) can be associated with the transaction identifier corresponding to the successfully executed change statement, and the mapping relationship between the backup table and the original data table can be recorded.

[0131] Each time a change statement is successfully executed, the transaction identifier corresponding to the change statement can be associated with the rollback resource, and the association result, transaction identifier, execution information and resource name can be written to the operation log. The change statement can be traced through the operation log later.

[0132] In some alternative implementations, the method further includes:

[0133] Step e1: Obtain the exception information of the first change statement.

[0134] Step e2: Obtain the execution result of the rollback statement corresponding to the second change statement.

[0135] Step e2: Write the execution results and exception information into the operation log.

[0136] The exception information for the first change statement here may include, but is not limited to, database error codes and error descriptions. Furthermore, the execution result may include, but is not limited to, whether the rollback statement was executed successfully, rollback statements that were not executed successfully, and the rollback strategies corresponding to each of the second change statements.

[0137] As a specific example, the transaction identifier, change statement, execution time of the change statement, number of rows affected by the change statement, rollback statement ID corresponding to the change statement, database object type, snapshot identifier, backup table name, snapshot creation time, backup table verification result, etc. can be written to the operation log.

[0138] In addition, using structured storage (such as MySQL tables) to store operation logs allows for querying operation logs by transaction identifier, table name, and status index.

[0139] The execution results and exception information are written to the operation log. The operation log can be used to trace the rollback process and facilitate subsequent problem location and repair.

[0140] In some alternative implementations, the method further includes:

[0141] Step f1: Obtain the target data table corresponding to the second change statement and the primary key corresponding to the target data table.

[0142] Step f2: Sample data is obtained by sampling the target data table based on the primary key.

[0143] Step f3: Compare the sampled data with the rollback resources corresponding to the second change statement to obtain a second comparison result. The second comparison result is used to characterize the consistency between the sampled data and the rollback resources.

[0144] For example, after executing the rollback statement corresponding to the second change statement, the primary key data in the target data table can be sampled according to the sampling rate, and the sampled data can be compared with the rollback resources corresponding to the second change statement to verify whether the data in the second database has been restored to the original business data after the rollback is executed.

[0145] Comparing the sampled data with the rollback resources can improve the efficiency of verifying whether the data in the second database has been restored to the original business data after the rollback is performed, and further ensure that the data in the second database has been restored to the original business data.

[0146] As a concrete example, after executing the rollback statement corresponding to the second change statement, the third hash value corresponding to the current table structure of the second database can be determined. This third hash value is then compared with the second hash value to determine if the current table structure of the second database has been restored to its pre-change state. If the third hash value and the second hash value are different, the current table structure and the second table structure of the second database can be compared layer by layer according to the hierarchical comparison method described above to determine the differences between them. Rollback is then performed again based on these differences until the current table structure of the second database is restored to its original state, i.e., the second table structure.

[0147] As a concrete example, if all change statements are executed successfully and verified, the current version number of the table structure in the configuration database (i.e., the first database) will be configured to match the version number of the first table structure, such as upgrading the current version number to version=N, and marking the table structure with version=N-1 as archived. Simultaneously, the current table structure of the business database will be synchronized to the configuration database, overwriting the snapshot content of version=N. Additionally, historical version table structures in the configuration database that have exceeded the retention period (configurable, default 1 year) will be archived to cold storage.

[0148] As a specific example, after all change statements have been successfully executed and verified, or after executing the rollback statement corresponding to the second change statement, idle connections in the first and second connection pools are closed; and snapshot files, backup tables (after rollback is complete and verification is successful), metadata caches in memory, and execution queues in the local temporary directory are deleted. Simultaneously, successfully executed operation logs are archived and compressed (in GZIP format) on a daily basis, while failure logs and rollback logs are permanently stored (retrieving by transaction identifier is supported).

[0149] The database rollback method provided in this embodiment can accurately roll back the table structure of the second database based on rollback statements, and can accurately restore the business data of the second database to the original business data before the change based on rollback resources. This can improve the accuracy and success rate of rollback and further avoid affecting the normal operation of business.

[0150] This embodiment also provides a database rollback device for implementing the above embodiments and preferred embodiments; details already described will not be repeated. As used below, the term "module" can refer to a combination of software and / or hardware that performs a predetermined function. Although the device described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.

[0151] This embodiment provides a database rollback device, such as... Figure 5 As shown, it includes:

[0152] The first acquisition module 501 is used to acquire the first table structure corresponding to the first database and the second table structure corresponding to the second database. The first database is used to store the table structure and the second database is used to store business data.

[0153] The first determining module 502 is used to determine the table structure difference information between the first table structure and the second table structure when the first table structure is different from the second table structure.

[0154] The first generation module 503 is used to generate at least one change statement in the form of a data definition language based on the difference attributes corresponding to the table structure difference information. The change statement is used to change the second table structure to the first table structure.

[0155] The second acquisition module 504 is used to acquire the currently successfully executed second change statement if a first change statement fails to execute during the execution of a table structure change.

[0156] Recovery module 505 is used to restore the second database to its original state before the change, based on the rollback strategy corresponding to the second change statement.

[0157] In some optional implementations, the first determining module 502 is further configured to perform hash processing on the first table structure to generate a first hash value, and perform hash processing on the second table structure to generate a second hash value; and determine the table structure difference information based on the first comparison result between the first hash value and the second hash value.

[0158] In some optional implementations, the first determining module 502 is further configured to, if the first comparison result indicates that the first hash value and the second hash value are different, obtain the first level information of the first table structure and the second level information of the second table structure; and, according to the correspondence between the first level information and the second level information, sequentially compare the first table structure and the second table structure to determine the table structure difference information.

[0159] In some optional implementations, the first generation module 503 is further configured to determine the change operation type for modifying the second table structure based on the difference attributes corresponding to the table structure difference information; obtain the predefined template statement corresponding to the change operation type; and call the predefined template statement to convert the table structure difference information into at least one change statement.

[0160] In some optional implementations, the first generation module 503 is further configured to call a predefined template statement to convert the table structure difference information into multiple initial change statements; sort the multiple initial change statements according to the dependencies between them to obtain multiple sorted initial change statements; and merge the sorted multiple initial change statements according to the data tables of the second database corresponding to each sorted initial change statement to obtain at least one change statement.

[0161] In some optional implementations, the recovery module 505 is also used to execute the rollback statement corresponding to the second change statement to restore the table structure of the second database to the original table structure before the change; and to restore the business data of the second database to the original business data before the change based on the rollback resources corresponding to the second change statement.

[0162] In some alternative embodiments, the device further includes:

[0163] The second determination module is used to determine the generation strategy of rollback resources corresponding to any change statement based on the database type corresponding to the change statement.

[0164] The second generation module is used to generate rollback resources corresponding to each change statement according to the generation strategy corresponding to each change statement.

[0165] The execution module is used to execute multiple change statements after generating rollback resources for each change statement. For each successfully executed change statement, a rollback statement corresponding to the successfully executed change statement is generated.

[0166] In some alternative embodiments, the device further includes:

[0167] The third determination module is used to treat multiple change statements as a whole, generate transactions corresponding to the multiple change statements, and determine the transaction identifiers corresponding to the transactions.

[0168] The association module is used to associate the rollback resources corresponding to the change statement with the transaction identifier to generate an association result when a change statement is successfully executed.

[0169] The third acquisition module is used to acquire the execution information corresponding to the change statement and the resource name corresponding to the rollback resource.

[0170] The first writing module is used to write the transaction identifier, associated results, execution information, and resource name into the operation log.

[0171] In some alternative embodiments, the device further includes:

[0172] The fourth acquisition module is used to acquire exception information of the first change statement.

[0173] The fifth acquisition module is used to acquire the execution result of the rollback statement corresponding to the second change statement.

[0174] The second writing module is used to write the execution results and exception information to the operation log.

[0175] In some alternative embodiments, the device further includes:

[0176] The sixth acquisition module is used to acquire the target data table corresponding to the second change statement and the primary key corresponding to the target data table after restoring the second database to its original state.

[0177] The sampling module is used to sample data from the target data table based on the primary key.

[0178] The comparison module is used to compare the sampled data with the rollback resources corresponding to the second change statement to obtain a second comparison result. The second comparison result is used to characterize the consistency between the sampled data and the rollback resources.

[0179] The database rollback apparatus provided in this embodiment of the invention can execute the database rollback method provided in any embodiment of the invention, and has the corresponding functional modules and beneficial effects for executing the method. Further functional descriptions of the various modules and units described above are the same as in the corresponding embodiments described above, and will not be repeated here.

[0180] Figure 6 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present invention.

[0181] The following is a detailed reference. Figure 6This diagram illustrates a suitable structural design for implementing an electronic device according to embodiments of the present invention. The electronic device may include a processor (e.g., a central processing unit, graphics processor, etc.) 601, which can perform various appropriate actions and processes based on a program stored in read-only memory (ROM) 602 or a program loaded from memory 608 into random access memory (RAM) 603. RAM 603 also stores various programs and data required for the operation of the electronic device. The processor 601, ROM 602, and RAM 603 are interconnected via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.

[0182] Typically, the following devices can be connected to I / O interface 605: input devices 606 including, for example, touchscreens, touchpads, keyboards, mice, cameras, microphones, accelerometers, gyroscopes, etc.; output devices 607 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; memory devices 608 including, for example, magnetic tapes, hard disks, etc.; and communication devices 609. Communication device 609 allows electronic devices to communicate wirelessly or wiredly with other devices to exchange data. Although Figure 6 Electronic devices with various devices are shown, but it should be understood that it is not required to implement or have all of the devices shown, and more or fewer devices may be implemented or have instead.

[0183] In particular, according to embodiments of the present invention, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of the present invention include a computer program product comprising a computer program carried on a non-transitory computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device 609, or installed from a memory 608, or installed from a ROM 602. When the computer program is executed by the processor 601, it performs the functions defined in the database rollback method of the embodiments of the present invention.

[0184] Figure 6 The electronic device shown is merely an example and should not be construed as limiting the functionality and scope of use of the embodiments of the present invention.

[0185] This invention also provides a computer-readable storage medium. The methods described above according to embodiments of the invention can be implemented in hardware or firmware, or implemented as recordable on a storage medium, or implemented as computer code originally stored on a remote storage medium or a non-transitory machine-readable storage medium and subsequently stored on a local storage medium after being downloaded via a network. Thus, the methods described herein can be processed by software stored on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. The storage medium can be a magnetic disk, optical disk, read-only memory, random access memory, flash memory, hard disk, or solid-state drive, etc.; further, the storage medium can also include combinations of the above types of memory. It is understood that computers, processors, microprocessor controllers, or programmable hardware include storage components capable of storing or receiving software or computer code. When the software or computer code is accessed and executed by the computer, processor, or hardware, the database rollback method shown in the above embodiments is implemented.

[0186] A portion of this invention can be applied as a computer program product, such as computer program instructions, which, when executed by a computer, can invoke or provide the methods and / or technical solutions according to the invention through the operation of the computer. Those skilled in the art will understand that the forms in which computer program instructions exist in a computer-readable medium include, but are not limited to, source files, executable files, installation package files, etc. Correspondingly, the ways in which computer program instructions are executed by a computer include, but are not limited to: the computer directly executing the instructions, or the computer compiling the instructions and then executing the corresponding compiled program, or the computer reading and executing the instructions, or the computer reading and installing the instructions and then executing the corresponding installed program. Here, the computer-readable medium can be any available computer-readable storage medium or communication medium accessible to a computer.

[0187] Although embodiments of the invention have been described in conjunction with the accompanying drawings, those skilled in the art can make various modifications and variations without departing from the spirit and scope of the invention, and such modifications and variations all fall within the scope defined by the appended claims.

Claims

1. A database rollback method, characterized in that, The method includes: Obtain the first table structure corresponding to the first database and the second table structure corresponding to the second database. The first database is used to store table structures, and the second database is used to store business data. When the first table structure is different from the second table structure, determine the table structure difference information between the first table structure and the second table structure; Based on the difference attributes corresponding to the table structure difference information, at least one change statement represented in the form of a data definition language is generated, and the change statement is used to change the second table structure to the first table structure. If the first change statement fails to execute during the table structure change process, then retrieve the second change statement that has been successfully executed. Based on the rollback strategy corresponding to the second change statement, the second database is restored to its original state before the change; The step of generating at least one change statement in the form of a data definition language based on the difference attributes corresponding to the table structure difference information includes: Based on the difference attributes corresponding to the table structure difference information, determine the change operation type for modifying the second table structure; Retrieve the predefined template statement corresponding to the change operation type; The predefined template statement is invoked to convert the table structure difference information into at least one change statement; The rollback strategy includes rollback statements and rollback resources; restoring the second database to its original state before the change based on the rollback strategy corresponding to the second change statement includes: Execute the rollback statement corresponding to the second change statement to restore the table structure of the second database to the original table structure before the change; Based on the rollback resource corresponding to the second change statement, the business data of the second database is restored to the original business data before the change.

2. The method according to claim 1, characterized in that, The step of determining the table structure difference information between the first table structure and the second table structure includes: The first table structure is hashed to generate a first hash value, and the second table structure is hashed to generate a second hash value. Based on the first comparison result between the first hash value and the second hash value, the table structure difference information is determined.

3. The method according to claim 2, characterized in that, The step of determining the table structure difference information based on the first comparison result between the first hash value and the second hash value includes: If the first comparison result indicates that the first hash value and the second hash value are different, then the first level information of the first table structure and the second level information of the second table structure are obtained. Based on the correspondence between the first-level information and the second-level information, the first table structure and the second table structure are compared sequentially to determine the differences in the table structure.

4. The method according to claim 1, characterized in that, The step of calling the predefined template statement to convert the table structure difference information into at least one change statement includes: The predefined template statement is invoked to convert the table structure difference information into multiple initial change statements; According to the dependencies between the multiple initial change statements, the multiple initial change statements are sorted to obtain the sorted multiple initial change statements; According to the data tables of the second database corresponding to each of the sorted initial change statements, the sorted initial change statements are merged to obtain at least one change statement.

5. The method according to claim 1, characterized in that, The method further includes: For any of the change statements, based on the database type corresponding to the change statement, determine the generation strategy for the rollback resources corresponding to the change statement; Generate the rollback resources corresponding to each change statement according to the generation strategy corresponding to each change statement; After generating the rollback resources for each of the change statements, multiple change statements are executed. For each successfully executed change statement, a rollback statement corresponding to the successfully executed change statement is generated.

6. The method according to any one of claims 1 or 5, characterized in that, The method further includes: Treat multiple change statements as a whole, generate multiple transactions corresponding to the change statements, and determine the transaction identifier corresponding to the transactions; When a change statement is successfully executed, the rollback resource corresponding to the change statement is associated with the transaction identifier to generate an association result; Obtain the execution information corresponding to the change statement and the resource name corresponding to the rollback resource; Write the transaction identifier, the association result, the execution information, and the resource name into the operation log.

7. The method according to claim 6, characterized in that, The method further includes: Obtain the exception information of the first change statement; Obtain the execution result of the rollback statement corresponding to the second change statement; The execution result and the exception information are written into the operation log.

8. The method according to claim 1, characterized in that, After restoring the second database to its original state, the method further includes: Obtain the target data table corresponding to the second change statement and the primary key corresponding to the target data table; Sampling data is obtained by sampling the target data table based on the primary key; The sampled data is compared with the rollback resource corresponding to the second change statement to obtain a second comparison result, which is used to characterize the consistency between the sampled data and the rollback resource.

9. A database rollback device, characterized in that, The device includes: The first acquisition module is used to acquire the first table structure corresponding to the first database and the second table structure corresponding to the second database. The first database is used to store table structures and the second database is used to store business data. The first determining module is used to determine the table structure difference information between the first table structure and the second table structure when the first table structure is different from the second table structure. The first generation module is used to generate at least one change statement in the form of a data definition language based on the difference attributes corresponding to the table structure difference information. The change statement is used to change the second table structure to the first table structure. The second acquisition module is used to acquire the currently successfully executed second change statement if the first change statement fails to execute during the execution of table structure changes. The recovery module is used to restore the second database to its original state before the change, based on the rollback strategy corresponding to the second change statement. The first generation module is further configured to determine the change operation type for modifying the second table structure based on the difference attributes corresponding to the table structure difference information; obtain a predefined template statement corresponding to the change operation type; and call the predefined template statement to convert the table structure difference information into the at least one change statement. The rollback strategy includes rollback statements and rollback resources; the recovery module is further configured to execute the rollback statement corresponding to the second change statement to restore the table structure of the second database to the original table structure before the change; and based on the rollback resources corresponding to the second change statement, restore the business data of the second database to the original business data before the change.

10. An electronic device, characterized in that, include: A memory and a processor are communicatively connected, the memory storing computer instructions, and the processor executing the computer instructions to perform the database rollback method of any one of claims 1 to 8.

11. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions for causing a computer to execute the database rollback method according to any one of claims 1 to 8.

Citation Information

Patent Citations

  • Database table structure changing method and device, equipment and storage medium

    CN115203200A

  • Heterogeneous database structure dynamic synchronization method, equipment and medium

    CN120744008A