-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMigrateCommand.java
More file actions
76 lines (62 loc) · 2.41 KB
/
MigrateCommand.java
File metadata and controls
76 lines (62 loc) · 2.41 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
package dev.dbos.transact.cli;
import dev.dbos.transact.migrations.MigrationManager;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.concurrent.Callable;
import picocli.CommandLine.Command;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.Spec;
@Command(name = "migrate", description = "Create DBOS system tables")
public class MigrateCommand implements Callable<Integer> {
@Option(
names = {"-r", "--app-role"},
description = "The role with which you will run your DBOS application")
String appRole;
@Mixin DatabaseOptions dbOptions;
@Option(
names = {"-h", "--help"},
usageHelp = true,
description = "Display this help message")
boolean help;
@Spec CommandSpec spec;
@Override
public Integer call() throws Exception {
var out = spec.commandLine().getOut();
out.println("Starting DBOS migrations");
out.format(" System Database: %s\n", dbOptions.url());
out.format(" System Database User: %s\n", dbOptions.user());
// TODO: add option for useListenNotify
MigrationManager.runMigrations(
dbOptions.url(), dbOptions.user(), dbOptions.password(), dbOptions.schema(), true);
grantDBOSSchemaPermissions(out, dbOptions.schema());
return 0;
}
void grantDBOSSchemaPermissions(PrintWriter out, String schema) throws SQLException {
if (appRole == null || appRole.isEmpty()) {
return;
}
out.format(
"Granting permissions for the %s schema to %s in database %s\n",
schema, appRole, dbOptions.url());
String[] queries = {
"GRANT USAGE ON SCHEMA %s TO %s",
"GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA %s TO %s",
"GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA %s TO %s",
"GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA %s TO %s",
"ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT ALL ON TABLES TO %s",
"ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT ALL ON SEQUENCES TO %s",
"ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT EXECUTE ON FUNCTIONS TO %s"
};
try (var conn =
DriverManager.getConnection(dbOptions.url(), dbOptions.user(), dbOptions.password());
var stmt = conn.createStatement()) {
for (var query : queries) {
query = query.formatted(schema, appRole);
stmt.execute(query);
}
}
}
}