forked from kamba4/sunders
moved files into subdirs
This commit is contained in:
parent
bf6ccf5094
commit
10fcf55b10
242 changed files with 23 additions and 0 deletions
97
data_handler/utils/init_db.php
Normal file
97
data_handler/utils/init_db.php
Normal file
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
// Admin credentials (must have privileges to create DB/users)
|
||||
$dbHost = getenv('MYSQL_HOST');
|
||||
$dbAdmin = getenv('MYSQL_USER');
|
||||
$dbPassword = getenv('MYSQL_PASSWORD');
|
||||
$dbName = getenv('MYSQL_DB');
|
||||
|
||||
// Variables for new users
|
||||
$cameraUser = getenv('CAMERA_USER');
|
||||
$cameraPassword = getenv('CAMERA_USER_PASSWORD');
|
||||
|
||||
$camSelectUser = getenv('CAMERA_SELECT_USER');
|
||||
$camSelectPassword = getenv('CAMERA_SELECT_USER_PASSWORD');
|
||||
|
||||
// Connect to MySQL
|
||||
$conn = new mysqli($dbHost, $dbAdmin, $dbPassword);
|
||||
|
||||
// Check connection
|
||||
if ($conn->connect_error) {
|
||||
die("Connection failed: " . $conn->connect_error);
|
||||
}
|
||||
|
||||
|
||||
if (!$conn->query("CREATE DATABASE IF NOT EXISTS $dbName")) {
|
||||
echo "Error creating database: " . $conn->error . "\n";
|
||||
}
|
||||
if (!$conn->select_db($dbName)) {
|
||||
die("Error selecting database: " . $conn->error);
|
||||
}
|
||||
|
||||
|
||||
// Create camera user
|
||||
$conn->query("CREATE USER IF NOT EXISTS '$cameraUser'@'%' IDENTIFIED BY '$cameraPassword'");
|
||||
$conn->query("GRANT ALL PRIVILEGES ON $dbName.* TO '$cameraUser'@'%'");
|
||||
|
||||
// Create camselect user
|
||||
$conn->query("CREATE USER IF NOT EXISTS '$camSelectUser'@'%' IDENTIFIED BY '$camSelectPassword'");
|
||||
$conn->query("GRANT SELECT ON $dbName.* TO '$camSelectUser'@'%'");
|
||||
|
||||
|
||||
// position table
|
||||
$conn->query("
|
||||
CREATE TABLE IF NOT EXISTS position (
|
||||
id BIGINT PRIMARY KEY,
|
||||
latitude INT,
|
||||
longitude INT
|
||||
)
|
||||
");
|
||||
|
||||
// tag table
|
||||
$conn->query("
|
||||
CREATE TABLE IF NOT EXISTS tag (
|
||||
id BIGINT,
|
||||
k VARCHAR(100),
|
||||
v VARCHAR(10000),
|
||||
PRIMARY KEY (id, k),
|
||||
CONSTRAINT fk_position FOREIGN KEY (id) REFERENCES `position`(id) ON DELETE CASCADE
|
||||
)
|
||||
");
|
||||
|
||||
$conn->query("CREATE INDEX IF NOT EXISTS LatLon ON position (latitude, longitude)");
|
||||
|
||||
|
||||
// sync_state table
|
||||
$conn->query("
|
||||
CREATE TABLE IF NOT EXISTS sync_state (
|
||||
k VARCHAR(100),
|
||||
v VARCHAR(100)
|
||||
)
|
||||
");
|
||||
|
||||
$result = $conn -> query("SELECT * FROM sync_state WHERE k='sequenceNumber'");
|
||||
|
||||
$table_count = $result -> num_rows;
|
||||
|
||||
if ($table_count == 0){
|
||||
$conn->query("INSERT INTO sync_state (k, v) VALUES ('sequenceNumber', '0');");
|
||||
}
|
||||
|
||||
echo "Database, users, and tables created successfully.\n";
|
||||
|
||||
$result = $conn -> query("SELECT * FROM position limit 10");
|
||||
|
||||
$table_count = $result -> num_rows;
|
||||
|
||||
printf("Result set has %d rows.\n", $table_count);
|
||||
if ($table_count == 0){
|
||||
$location = "/opt/init/init.sql";
|
||||
|
||||
$commands = file_get_contents($location);
|
||||
$conn->multi_query($commands);
|
||||
echo "Inserted data.\n";
|
||||
}
|
||||
|
||||
|
||||
$conn->close();
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue