-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy path04-dump-restore.php
More file actions
199 lines (165 loc) · 6.3 KB
/
04-dump-restore.php
File metadata and controls
199 lines (165 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
declare(strict_types=1);
/**
* Example: Database Dump and Restore
*
* This example demonstrates how to use the CLI dump and restore commands
* to export and import database schema and data.
*/
use tommyknocker\pdodb\cli\Application;
use tommyknocker\pdodb\PdoDb;
if (PHP_SAPI !== 'cli') {
die('This script can only be run from the command line.');
}
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../helpers.php';
// Setup database connection
// For SQLite, use a temporary file instead of :memory: so CLI commands can access the same database
$driver = mb_strtolower(getenv('PDODB_DRIVER') ?: 'sqlite', 'UTF-8');
$dbPath = null;
if ($driver === 'sqlite') {
// Use a temporary file for SQLite so CLI commands can access the same database
$dbPath = sys_get_temp_dir() . '/pdodb_dump_example_' . uniqid() . '.sqlite';
$db = new PdoDb('sqlite', ['path' => $dbPath]);
// Ensure CLI commands use the same SQLite database file
putenv('PDODB_DRIVER=sqlite');
putenv('PDODB_PATH=' . $dbPath);
} else {
$db = createExampleDb();
// Set environment variables from config for CLI commands
// This ensures BaseCliCommand can access the same database configuration
$config = getExampleConfig();
setEnvFromConfig($config);
}
$driver = $db->connection?->getDriverName() ?? 'sqlite';
$schema = $db->schema();
putenv('PDODB_NON_INTERACTIVE=1');
// Create CLI application
$app = new Application();
$run = static function (array $argv) use ($app): string {
ob_start();
$app->run($argv);
return ob_get_clean();
};
// Create sample tables and data
echo "Creating sample database...\n";
// Drop tables if they exist using DDL API
$schema->dropTableIfExists('posts');
$schema->dropTableIfExists('users');
// Create users table using Schema Builder
$schema->createTable('users', [
'id' => $schema->primaryKey(),
'name' => $schema->string(100)->notNull(),
'email' => $schema->string(255)->unique(),
'created_at' => $schema->timestamp()->defaultExpression('CURRENT_TIMESTAMP'),
]);
// Create posts table using Schema Builder with foreign key
// For SQLite, foreign key must be in CREATE TABLE, so we use a workaround
if ($driver === 'sqlite') {
// SQLite requires foreign key in CREATE TABLE
$schema->createTable('posts', [
'id' => $schema->primaryKey(),
'user_id' => $schema->integer()->notNull(),
'title' => $schema->string(255)->notNull(),
'content' => $schema->text(),
], [
'foreignKeys' => [
[
'name' => 'fk_posts_user_id',
'columns' => ['user_id'],
'refTable' => 'users',
'refColumns' => ['id'],
'onDelete' => 'CASCADE',
'onUpdate' => 'CASCADE',
],
],
]);
} else {
// For other databases, create table first, then add foreign key
$schema->createTable('posts', [
'id' => $schema->primaryKey(),
'user_id' => $schema->integer()->notNull(),
'title' => $schema->string(255)->notNull(),
'content' => $schema->text(),
]);
$schema->addForeignKey('fk_posts_user_id', 'posts', 'user_id', 'users', 'id', 'CASCADE', 'CASCADE');
}
// Add index
$schema->createIndex('idx_user_id', 'posts', 'user_id');
// Insert sample data using Query Builder
$db->find()->table('users')->insert([
'name' => 'John Doe',
'email' => '[email protected]',
]);
$db->find()->table('users')->insert([
'name' => 'Jane Smith',
'email' => '[email protected]',
]);
$db->find()->table('posts')->insert([
'user_id' => 1,
'title' => 'First Post',
'content' => 'Content of first post',
]);
$db->find()->table('posts')->insert([
'user_id' => 1,
'title' => 'Second Post',
'content' => 'Content of second post',
]);
$db->find()->table('posts')->insert([
'user_id' => 2,
'title' => 'Third Post',
'content' => 'Content of third post',
]);
echo "Sample database created with tables and data.\n\n";
// Full dump of our test tables using CLI command
$usersDumpFile = sys_get_temp_dir() . '/users_dump_' . uniqid() . '.sql';
$postsDumpFile = sys_get_temp_dir() . '/posts_dump_' . uniqid() . '.sql';
echo "=== Full Database Dump (test tables) ===\n";
echo ">> pdodb dump users --output={$usersDumpFile}\n";
$usersDump = $run(['pdodb', 'dump', 'users', '--output=' . $usersDumpFile]);
echo $usersDump;
echo ">> pdodb dump posts --output={$postsDumpFile}\n";
$postsDump = $run(['pdodb', 'dump', 'posts', '--output=' . $postsDumpFile]);
echo $postsDump;
// Combine dumps
$fullDump = file_get_contents($usersDumpFile) . "\n" . file_get_contents($postsDumpFile);
echo substr($fullDump, 0, 500) . "...\n\n";
// Schema only dump
echo "=== Schema Only Dump ===\n";
echo ">> pdodb dump users --schema-only\n";
$schemaDump = $run(['pdodb', 'dump', 'users', '--schema-only']);
echo substr($schemaDump, 0, 300) . "...\n\n";
// Data only dump
echo "=== Data Only Dump ===\n";
echo ">> pdodb dump users --data-only\n";
$dataDump = $run(['pdodb', 'dump', 'users', '--data-only']);
echo substr($dataDump, 0, 300) . "...\n\n";
// Dump specific table
echo "=== Dump Single Table (users) ===\n";
echo ">> pdodb dump users\n";
$tableDump = $run(['pdodb', 'dump', 'users']);
echo substr($tableDump, 0, 200) . "...\n\n";
// Restore from dump using CLI command
echo "=== Restore from Dump ===\n";
$dumpFile = sys_get_temp_dir() . '/pdodb_restore_example_' . uniqid() . '.sql';
file_put_contents($dumpFile, $fullDump);
// Drop tables before restore using CLI commands to ensure same connection
// Drop in correct order (posts first due to foreign key, then users)
$run(['pdodb', 'table', 'drop', 'posts', '--if-exists', '--force']);
$run(['pdodb', 'table', 'drop', 'users', '--if-exists', '--force']);
echo ">> pdodb dump restore {$dumpFile} --force\n";
$restoreOutput = $run(['pdodb', 'dump', 'restore', $dumpFile, '--force']);
echo $restoreOutput;
// Verify restored data using Query Builder
$users = $db->find()->from('users')->get();
echo "Restored users: " . count($users) . "\n";
$posts = $db->find()->from('posts')->get();
echo "Restored posts: " . count($posts) . "\n\n";
// Cleanup
if ($driver === 'sqlite' && $dbPath && $dbPath !== ':memory:' && file_exists($dbPath)) {
@unlink($dbPath);
}
@unlink($dumpFile);
@unlink($usersDumpFile);
@unlink($postsDumpFile);
echo "Example completed successfully!\n";