-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsqlpopulate.pl
More file actions
73 lines (59 loc) · 2 KB
/
sqlpopulate.pl
File metadata and controls
73 lines (59 loc) · 2 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
#!/usr/bin/perl
=begin
Make sure to replace the placeholders your_database_name , your_database_host , your_database_username ,
and your_database_password with the appropriate values for your database configuration.
This script uses the DBI module to connect to the MySQL database and execute the SQL statements to create tables
and insert sample data. It assumes that you have the necessary Perl modules and a MySQL database already set up.
After running the script, it will populate the database with the provided table schema and sample data.
=cut
use strict;
use warnings;
use DBI;
# Database configuration
my $db_name = $ENV{'DATABASE_NAME'};
my $db_host = $ENV{'DATABASE_HOST'};
my $db_user = $ENV{'DATABASE_USERNAME'};
my $db_pass = $ENV{'DATABASE_PASSWORD'};
# SQL statements to create tables
my $create_users_table = <<'SQL';
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password VARCHAR(100) NOT NULL,
enabled BOOLEAN NOT NULL
)
SQL
my $create_authorities_table = <<'SQL';
CREATE TABLE authorities (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
role VARCHAR(50) NOT NULL
)
SQL
# SQL statements to insert sample data
my $insert_users_data = <<'SQL';
INSERT INTO users (username, password, enabled)
VALUES
('user1', 'password1', 1),
('user2', 'password2', 1),
('user3', 'password3', 0)
SQL
my $insert_authorities_data = <<'SQL';
INSERT INTO authorities (username, role)
VALUES
('user1', 'ROLE_USER'),
('user2', 'ROLE_ADMIN'),
('user3', 'ROLE_USER')
SQL
# Connect to the database
my $dbh = DBI->connect("DBI:mysql:$db_name:$db_host", $db_user, $db_pass)
or die "Cannot connect to the database: $DBI::errstr";
# Create tables
$dbh->do($create_users_table);
$dbh->do($create_authorities_table);
# Insert sample data
$dbh->do($insert_users_data);
$dbh->do($insert_authorities_data);
# Disconnect from the database
$dbh->disconnect;
print "Database population completed successfully.\n";