forked from ShahinSorkh/laravel-cassandra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_db.php
68 lines (58 loc) · 3.16 KB
/
prepare_db.php
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
<?php
require './vendor/autoload.php';
echo 'creating keyspace...'.PHP_EOL;
`cqlsh -e "CREATE KEYSPACE IF NOT EXISTS testing WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;"`;
$session = Cassandra::cluster()->build()->connect('testing');
echo 'creating table users...'.PHP_EOL;
$session->execute('CREATE TABLE IF NOT EXISTS testing.users ( id uuid primary key, username text, email text, password text, birthdate date )');
$session->executeAsync('CREATE MATERIALIZED VIEW IF NOT EXISTS testing.users_by_username AS SELECT * FROM testing.users WHERE username IS NOT NULL AND id IS NOT NULL PRIMARY KEY (username, id)');
$session->executeAsync('CREATE MATERIALIZED VIEW IF NOT EXISTS testing.users_by_email AS SELECT * FROM testing.users WHERE email IS NOT NULL AND id IS NOT NULL PRIMARY KEY (email, id)');
echo 'creating table posts...'.PHP_EOL;
$session->execute('CREATE TABLE IF NOT EXISTS testing.posts ( user uuid, id uuid, title text, body text, published_at timestamp, published_month text, primary key (user, id) )');
$session->executeAsync('CREATE MATERIALIZED VIEW IF NOT EXISTS testing.posts_by_month AS SELECT * FROM testing.posts WHERE user IS NOT NULL AND id IS NOT NULL AND published_month IS NOT NULL PRIMARY KEY (published_month, user, id)');
$faker = Faker\Factory::create();
echo 'inserting users...'.PHP_EOL;
$p = $session->prepare('insert into users(id, username, email, password, birthdate) values (?,?,?,?,?)');
$users_passwords = [];
$count_users = 0;
foreach (range(0, 200) as $i) {
$new_id = new Cassandra\Uuid();
$password = $faker->password;
$users_passwords[$new_id->uuid()] = $password;
$session->executeAsync($p, ['arguments' => [
$new_id,
$faker->username,
$faker->email,
password_hash($password, PASSWORD_DEFAULT),
new Cassandra\Date(strtotime($faker->date)),
]]);
$count_users++;
}
echo 'dumping users...'.PHP_EOL;
$users_file = 'tests/data/users.json';
$old_users = file_exists($users_file) ? json_decode(file_get_contents($users_file), true) : [];
file_put_contents($users_file, json_encode(array_merge($old_users, $users_passwords)));
echo 'inserting posts...'.PHP_EOL;
$p = $session->prepare('insert into posts(user, id, title, body, published_at, published_month) values (?,?,?,?,?,?)');
$count_posts = 0;
foreach (range(0, 20000) as $i) {
$published_at = $faker->unixTime;
$session->executeAsync($p, ['arguments' => [
new Cassandra\Uuid(array_rand($users_passwords)),
new Cassandra\Uuid(),
$faker->sentence(3),
$faker->realText(20000),
new Cassandra\Timestamp($published_at),
date('Y-m', $published_at),
]]);
$count_posts++;
}
echo 'dumping stats...'.PHP_EOL;
$data_file = 'tests/data/data.json';
$old_data = file_exists($data_file) ? json_decode(file_get_contents($data_file), true) : ['users' => 0, 'posts' => 0];
$data = [
'users' => $old_data['users'] + $count_users,
'posts' => $old_data['posts'] + $count_posts,
];
file_put_contents($data_file, json_encode(array_merge($old_data, $data)));
echo 'all users ids and their respective passwords are saved to tests/data/users.json'.PHP_EOL;