Skip to content

Commit 26ce7ff

Browse files
authored
Merge pull request #143 from Planetbiru/feature/version-3.18.0
Add parameter to callback function database migration
2 parents 3499168 + 2c954cc commit 26ce7ff

File tree

2 files changed

+48
-32
lines changed

2 files changed

+48
-32
lines changed

manual/includes/_database-migration.md

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Users simply create an import data configuration file dan import script as follo
1313
File `import.yml`
1414

1515
```yml
16-
database_target:
16+
databaseTarget:
1717
driver: mysql
1818
host: server1.domain.tld
1919
port: 3306
@@ -22,7 +22,7 @@ database_target:
2222
database_name: sipro
2323
databaseSchema: public
2424
timeZone: Asia/Jakarta
25-
database_source:
25+
databaseSource:
2626
driver: mysql
2727
host: server1.domain.tld
2828
port: 3306
@@ -31,36 +31,36 @@ database_source:
3131
database_name: sipro_ori
3232
databaseSchema: public
3333
timeZone: Asia/Jakarta
34-
maximum_record: 100
34+
maximumRecord: 100
3535
table:
3636
- source: modul
3737
target: modul
3838
map:
3939
- 'default_data : default'
4040
- 'sort_order : order'
41-
pre_import_script:
41+
preImportScript:
4242
- "truncate modul"
43-
maximum_record: 2000
43+
maximumRecord: 2000
4444
- source: hak_akses
4545
target: hak_akses
4646
map:
4747
- 'allowed_detail : view'
4848
- 'allowed_create : insert'
4949
- 'allowed_update : update'
5050
- 'allowed_delete : delete'
51-
pre_import_script:
51+
preImportScript:
5252
- "truncate hak_akses"
53-
post_import_script:
53+
postImportScript:
5454
- "update hak_akses set allowed_list = true, allowed_approve = true, allowed_sort_order = true"
55-
maximum_record: 50
55+
maximumRecord: 50
5656
```
5757
5858
**Explanation**
5959
60-
- `database_source` is the source database configuration
61-
- `database_target` is the target database configuration
60+
- `databaseSource` is the source database configuration
61+
- `databaseTarget` is the target database configuration
6262
- `table` is an array containing all the tables to be imported. Tables not listed in `table` will not be imported.
63-
- `maximum_record` is the maximum number of records in a single insert query. Note that MagicObject does not care about the size of the data in bytes. If you need to adjust the maximum records per table, specify `maximum_record` on the table you want to set.
63+
- `maximumRecord` is the maximum number of records in a single insert query. Note that MagicObject does not care about the size of the data in bytes. If you need to adjust the maximum records per table, specify `maximumRecord` on the table you want to set.
6464

6565
1. `source` (required)
6666

@@ -70,7 +70,7 @@ Table name of the source database
7070

7171
Table name of the target database
7272

73-
3. `maximum_record` (optional)
73+
3. `maximumRecord` (optional)
7474

7575
`maximum records` on a table is used to reset the number of records per `insert` query on a table for that table. This setting will override the global setting.
7676

@@ -80,13 +80,14 @@ Table name of the target database
8080

8181
`map` is an array of text separated by colons. On the left side of the colon are the column names in the target table and database while on the right side of the colon are the column names in the source table and database.
8282

83-
5. `pre_import_script` (optional)
83+
5. `preImportScript` (optional)
8484

85-
`pre_import_script` is an array of queries that will be executed before the data import begins. `pre_import_script` is usually used to clear data from a table and reset all sequence or auto increment values ​​from the target table.
85+
`preImportScript` is an array of queries that will be executed before the data import begins. `preImportScript` is usually used to clear data from a table and reset all sequence or auto increment values ​​from the target table.
8686

87-
6. `post_import_script` (optional)
87+
6. `postImportScript` (optional)
88+
89+
`postImportScript` is an array of queries that will be executed after the data import is complete. `postImportScript` can be used for various purposes such as fixing some data on the target table including taking values ​​from other tables. Therefore post_script must be run after all tables have been successfully imported.
8890

89-
`post_import_script` is an array of queries that will be executed after the data import is complete. `post_import_script` can be used for various purposes such as fixing some data on the target table including taking values ​​from other tables. Therefore post_script must be run after all tables have been successfully imported.
9091

9192
**Import Script**
9293

@@ -105,7 +106,7 @@ $config->loadYamlFile('import.yml', true, true, true);
105106
106107
$fp = fopen(__DIR__.'/db.sql', 'w');
107108
fclose($fp);
108-
$sql = (new PicoDatabaseUtilMySql())->importData($config, function($sql, $source, $target){
109+
$sql = (new PicoDatabaseUtilMySql())->importData($config, function($sql, $tableNameSource, $tableNameTarget, $databaseSource, $databaseTarget){
109110
$fp = fopen(__DIR__.'/db.sql', 'a');
110111
fwrite($fp, $sql.";\r\n\r\n");
111112
fclose($fp);
@@ -118,9 +119,13 @@ $sql = (new PicoDatabaseUtilMySql())->importData($config, function($sql, $source
118119
php import.php
119120
```
120121

121-
MagicObject will create a database query that is saved into a file named `db.sql`. The data is taken from the `database_source` but the table and column names have been adjusted to the `database_target`. This query can be run in the `database_target`. If you want to empty a table before importing data, you can add a pre_import_script to each table. Keep in mind that all pre_import_scripts will be executed before MagicObject starts importing data.
122+
MagicObject will generate SQL queries based on the configuration in `import.yml`. These queries are written into `db.sql`. The data is read from the `databaseSource`, while table and column names are automatically mapped to the `databaseTarget`. The resulting `db.sql` file can then be executed on the `databaseTarget`.
123+
124+
If you want to clear a table before importing data, you can define a `preImportScript` for that table. All preImportScripts will be executed before MagicObject starts the data import process.
125+
126+
For more complex databases, you can use the method `PicoDatabaseUtilMySql::autoConfigureImportData()` to generate a configuration template. This method compares the source and target databases and automatically maps tables and columns. If a table exists in the target but not in the source, MagicObject will mark its source as `???`. Likewise, if a column exists in the target table but not in the source, its source will be marked as `???`. You can then manually adjust these placeholders in the configuration file.
122127

123-
If the database is too complex, users can use the PicoDatabaseUtilMySql::autoConfigureImportData() method to create a configuration template to avoid missing table and column names. Users simply specify the source database and the target database. MagicObject will check the tables and columns in both databases. If a table exists in the target database but not in the source database, MagicObject will write ??? as its source name. Users can manually change the name of this table. In the same table, if a column exists in the target database but not in the source database, MagicObject will write ??? as its source name. Users can manually change the name of this column.
128+
In addition to generating an SQL file, users can also choose to **execute the generated queries directly on the target database**. This allows the import process to be performed automatically without the need to run the resulting `db.sql` file manually.
124129

125130
Here is an example of how to create a database import configuration template.
126131

@@ -129,7 +134,7 @@ Here is an example of how to create a database import configuration template.
129134
File `import.yml`
130135

131136
```yml
132-
database_target:
137+
databaseTarget:
133138
driver: mysql
134139
host: server1.domain.tld
135140
port: 3306
@@ -139,7 +144,7 @@ database_target:
139144
databaseSchema: public
140145
timeZone: Asia/Jakarta
141146
charset: utf8
142-
database_source:
147+
databaseSource:
143148
driver: mysql
144149
host: server1.domain.tld
145150
port: 3306
@@ -149,7 +154,7 @@ database_source:
149154
databaseSchema: public
150155
timeZone: Asia/Jakarta
151156
charset: utf8
152-
maximum_record: 100
157+
maximumRecord: 100
153158
```
154159

155160
**Import Template Script**

src/Util/Database/PicoDatabaseUtilBase.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,25 @@ public function importDataTable($databaseSource, $databaseTarget, $tableNameSour
404404
}
405405

406406
/**
407-
* Imports data from the source database to the target database.
408-
*
409-
* This method connects to the source and target databases, executes any pre-import scripts,
410-
* transfers data from the source tables to the target tables, and executes any post-import scripts.
411-
*
412-
* @param SecretObject $config Configuration object containing database and table details.
413-
* @param callable $callbackFunction Callback function to execute SQL scripts.
414-
* @return bool Returns true on successful import, false on failure.
407+
* Executes a full data import process from a source database to a target database.
408+
*
409+
* This method performs the following steps:
410+
* - Establishes connections to both source and target databases using configuration details.
411+
* - Executes optional pre-import SQL scripts for each table.
412+
* - Transfers data from source tables to corresponding target tables, respecting record limits.
413+
* - Executes optional post-import SQL scripts for each table.
414+
*
415+
* The import process is table-driven, with each table configuration specifying source/target names,
416+
* pre/post import scripts, and other metadata. SQL execution is delegated to a user-defined callback.
417+
*
418+
* @param SecretObject $config Configuration object containing:
419+
* - Source and target database connection parameters.
420+
* - Table mappings with source/target names and optional scripts.
421+
* - Maximum record limits for import operations.
422+
* @param callable $callbackFunction A user-defined function with the signature:
423+
* function(string $sql, string $sourceTable, string $targetTable, PicoDatabase $sourceDb, PicoDatabase $targetDb): void
424+
* Used to execute pre/post import SQL scripts.
425+
* @return bool True if all operations succeed; false if any exception occurs during the process.
415426
*/
416427
public function importData($config, $callbackFunction)
417428
{
@@ -437,7 +448,7 @@ public function importData($config, $callbackFunction)
437448
{
438449
foreach($preImportScript as $sql)
439450
{
440-
call_user_func($callbackFunction, $sql, $tableNameSource, $tableNameTarget);
451+
call_user_func($callbackFunction, $sql, $tableNameSource, $tableNameTarget, $databaseSource, $databaseTarget);
441452
}
442453
}
443454
}
@@ -460,7 +471,7 @@ public function importData($config, $callbackFunction)
460471
{
461472
foreach($postImportScript as $sql)
462473
{
463-
call_user_func($callbackFunction, $sql, $tableNameSource, $tableNameTarget);
474+
call_user_func($callbackFunction, $sql, $tableNameSource, $tableNameTarget, $databaseSource, $databaseTarget);
464475
}
465476
}
466477
}

0 commit comments

Comments
 (0)