-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed-database.php
More file actions
executable file
·147 lines (121 loc) · 4.45 KB
/
Copy pathseed-database.php
File metadata and controls
executable file
·147 lines (121 loc) · 4.45 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
#!/usr/bin/env php
<?php
declare(strict_types=1);
/**
* Claimable Postgres (Instagres): database seeding example
*
* This example demonstrates how to:
* 1. Create a claimable Neon database
* 2. Connect to it using PDO
* 3. Execute SQL from a file to seed initial data
*
* Usage:
* php examples/seed-database.php
* php examples/seed-database.php path/to/custom-schema.sql
*/
require_once __DIR__ . '/../vendor/autoload.php';
use Philip\Instagres\Client;
use Philip\Instagres\Exception\InstagresException;
// Get SQL file path from command line or use default
$sqlFile = $argv[1] ?? __DIR__ . '/sample-schema.sql';
// Verify SQL file exists
if (!file_exists($sqlFile)) {
echo "✗ Error: SQL file not found: {$sqlFile}\n";
exit(1);
}
echo "\n";
echo "==========================================\n";
echo " Neon Instagres - Seeding Example\n";
echo "==========================================\n";
echo "\n";
try {
// Step 1: Create the database
echo "Step 1: Creating database...\n";
$startTime = microtime(true);
$database = Client::createClaimableDatabase('seeding-example');
$createTime = round((microtime(true) - $startTime) * 1000);
echo " ✓ Database created in {$createTime}ms\n";
echo "\n";
// Step 2: Parse connection string and connect to the database
echo "Step 2: Parsing connection string and connecting...\n";
// Claimable Postgres returns a pooled connection string; that is fine for typical app queries and seeding.
$parsed = Client::parseConnectionString($database['connection_string']);
try {
$pdo = new PDO($parsed['dsn'], $parsed['user'], $parsed['password'], [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
echo " ✓ Connected successfully\n";
echo "\n";
} catch (PDOException $e) {
throw new Exception("Failed to connect to database: {$e->getMessage()}");
}
// Step 3: Read and execute SQL file
echo "Step 3: Executing SQL from {$sqlFile}...\n";
$sql = file_get_contents($sqlFile);
if ($sql === false) {
throw new Exception("Failed to read SQL file");
}
$seedStart = microtime(true);
try {
// Execute the SQL
$pdo->exec($sql);
$seedTime = round((microtime(true) - $seedStart) * 1000);
echo " ✓ SQL executed in {$seedTime}ms\n";
echo "\n";
} catch (PDOException $e) {
throw new Exception("Failed to execute SQL: {$e->getMessage()}");
}
// Step 4: Verify seeding by querying data
echo "Step 4: Verifying data...\n";
try {
// Check users table
$stmt = $pdo->query("SELECT COUNT(*) as count FROM users");
$userCount = $stmt->fetch()['count'];
echo " ✓ Users table: {$userCount} rows\n";
// Check posts table
$stmt = $pdo->query("SELECT COUNT(*) as count FROM posts");
$postCount = $stmt->fetch()['count'];
echo " ✓ Posts table: {$postCount} rows\n";
// Show sample data
$stmt = $pdo->query("SELECT username, email FROM users LIMIT 3");
$users = $stmt->fetchAll();
echo "\n";
echo " Sample users:\n";
foreach ($users as $user) {
echo " - {$user['username']} ({$user['email']})\n";
}
} catch (PDOException $e) {
throw new Exception("Failed to verify data: {$e->getMessage()}");
}
echo "\n";
echo "==========================================\n";
echo "\n";
echo "✓ SUCCESS! Database seeded successfully\n";
echo "\n";
echo "Connection String:\n";
echo " {$database['connection_string']}\n";
echo "\n";
echo "Claim URL:\n";
echo " {$database['claim_url']}\n";
echo "\n";
echo "Expires At:\n";
echo " {$database['expires_at']}\n";
echo "\n";
echo "==========================================\n";
echo "\n";
echo "💡 Next Steps:\n";
echo " - Use the connection string to connect with your application\n";
echo " - Visit the claim URL to make this database permanent\n";
echo " - Query the database: psql \"{$database['connection_string']}\"\n";
echo "\n";
exit(0);
} catch (InstagresException $e) {
echo "✗ Instagres Error: {$e->getMessage()}\n";
echo "\n";
exit(1);
} catch (Exception $e) {
echo "✗ Error: {$e->getMessage()}\n";
echo "\n";
exit(1);
}