-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.groovy
152 lines (128 loc) · 4.34 KB
/
db.groovy
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
// Run using:
//
// $ java -jar zefaker-all.jar -f=db.groovy -output=files -sql -x
//
/**
* db.groovy
*
* An example of how to use Zefaker to generate a whole test database
*
* Creates "migration" files that can be used to create tables and populate
* a database about "Businesses".
*
* Creates the following files in the current directory:
*
* * V000_schema.sql - Base migration that creates the tables
* * V001_create_businesses.sql - Inserts businesses data
* * V002_create_users.sql - Inserts Users data
* * V003_create_businesses_services.sql - Inserts Businesses' Services data
*
*/
import java.nio.file.Files
import java.nio.file.Paths
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicLong
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.text.SimpleDateFormat
ymd = new SimpleDateFormat("yyyy-MM-dd")
dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
timestampFaker = { faker ->
return dateFormatter.format(faker.date().past(365, TimeUnit.DAYS))
}
def addTimestampColumns(theData) {
modifiedData = theData
def lastID = 0
theData.each {
if (it.getKey().index > lastID) {
lastID = it.getKey().index
}
}
modifiedData.put(column(index=++lastID,name="created_at"), timestampFaker)
modifiedData.put(column(index=++lastID,name="modified_at"), timestampFaker)
modifiedData.put(column(index=++lastID,name="deleted_at"), { faker -> null })
return modifiedData
}
businessIDGenerator = new AtomicLong(0)
idGen = new AtomicLong(0)
businessData = [
(column(index=0,name="id")): { faker -> businessIDGenerator.incrementAndGet() },
(column(index=1,name="business_name")): { faker -> faker.name().fullName() },
(column(index=2,name="address")): { faker -> faker.name().fullName() },
(column(index=3,name="registration_date")): { faker -> ymd.format(faker.date().birthday()) },
(column(index=4,name="email")): { faker -> faker.internet().emailAddress() },
]
usersData = [
(column(index=0,name="id")): { faker -> idGen.incrementAndGet() },
(column(index=1,name="business_id")): { faker ->
faker.number().numberBetween(1, businessIDGenerator.get())
},
(column(index=2,name="username")): { faker ->
faker.name().fullName().replace("\\s*","").toLowerCase()
},
(column(index=3,name="subscription_type")): { faker ->
faker.options().option('premium', 'team', 'free')
},
(column(index=4,name="last_login_at")): timestampFaker
]
businessServicesData = [
(column(index=0,name="id")): { faker -> idGen.incrementAndGet() },
(column(index=1,name="business_id")): { faker ->
faker.number().numberBetween(1, businessIDGenerator.get())
},
(column(index=2,name="service_name")): { faker ->
faker.options().option('Electronics', 'Retail', 'Transportation')
}
]
def sqlSchema = """
DROP TABLE businesses;
DROP TABLE users;
DROP TABLE businesses_services;
CREATE TABLE businesses (
id int not null primary key,
business_name varchar(255) not null,
address varchar(255),
registration_date date,
email text,
created_at timestamp,
modified_at timestamp,
deleted_at timestamp
);
CREATE TABLE users (
id int not null primary key,
business_id int,
username varchar(255),
subscription_type varchar(50),
last_login_at timestamp,
created_at timestamp,
modified_at timestamp,
deleted_at timestamp
);
CREATE TABLE businesses_services (
id int not null primary key,
business_id int,
service_name varchar(255),
created_at timestamp,
modified_at timestamp,
deleted_at timestamp
);
"""
Files.write(Paths.get("V000__schema.sql"), sqlSchema.getBytes())
businessData = addTimestampColumns(businessData)
usersData = addTimestampColumns(usersData)
businessServicesData = addTimestampColumns(businessServicesData)
// Businesses
maxRows = 400
tableName = "businesses"
outputFile = "V001__create_businesses.sql"
generateFrom(businessData) // Generates the file
maxRows = 50
tableName = "users"
outputFile = "V002__create_users.sql"
generateFrom(usersData) // Generates the file
maxRows = 10
tableName = "businesses_services"
outputFile = "V003__create_businesses_services.sql"
idGen.getAndSet(0)
generateFrom(businessServicesData) // Generates the file