|
| 1 | +from .pglite import check_cluster, init_cluster, reset_cluster, start_cluster, stop_cluster, cluster_params, is_started, create_db, drop_db, list_db, export_db, import_db, die |
| 2 | +import sys |
| 3 | + |
| 4 | +usage = """ |
| 5 | + usage pglite <command> [arguments] |
| 6 | +
|
| 7 | + PGLite management tool. |
| 8 | + |
| 9 | + Commands:\n |
| 10 | + |
| 11 | + init [path_to_pg_ctl] Initialize the cluster |
| 12 | +
|
| 13 | + reset Reset the cluster |
| 14 | +
|
| 15 | + status Print the status of the cluster |
| 16 | +
|
| 17 | + start Start the cluster |
| 18 | +
|
| 19 | + stop [mode] Stop the cluster (mode=smart|fast|immediate) |
| 20 | +
|
| 21 | + create db_name Create a database |
| 22 | +
|
| 23 | + drop db_name Drop a database |
| 24 | +
|
| 25 | + list List databases |
| 26 | +
|
| 27 | + export db_name file Export a database to a dump file |
| 28 | +
|
| 29 | + import file db_name Create a new database and import data from the dump file |
| 30 | +
|
| 31 | + psql Launch a psql shell on the cluster |
| 32 | + """ |
| 33 | + |
| 34 | +if len(sys.argv) < 2 or sys.argv[1] == '--help' or sys.argv[1] == 'help' or sys.argv[1] == '-h': |
| 35 | + print(usage) |
| 36 | + exit(0) |
| 37 | + |
| 38 | +if sys.argv[1] == "init": |
| 39 | + pg_ctl_path = None |
| 40 | + if len(sys.argv) > 2: |
| 41 | + pg_ctl_path = sys.argv[2] |
| 42 | + init_cluster(pg_ctl_path) |
| 43 | +elif sys.argv[1] == "reset": |
| 44 | + reset_cluster() |
| 45 | +elif sys.argv[1] == "status": |
| 46 | + print_cluster_status() |
| 47 | +elif sys.argv[1] == "start": |
| 48 | + check_cluster() or die("Cluster not present") |
| 49 | + start_cluster() |
| 50 | +elif sys.argv[1] == "stop": |
| 51 | + shutdown_mode = "fast" |
| 52 | + if len(sys.argv) > 2: |
| 53 | + shutdown_mode = sys.argv[2] |
| 54 | + check_cluster() or die("Cluster not present") |
| 55 | + stop_cluster(shutdown_mode) |
| 56 | +elif sys.argv[1] == "list": |
| 57 | + check_cluster() or die("Cluster not present") |
| 58 | + print("\n".join(list_db())) |
| 59 | +elif sys.argv[1] == "create": |
| 60 | + check_cluster() or die("Cluster not present") |
| 61 | + if len(sys.argv) < 3: |
| 62 | + raise RuntimeError("Needed argument: db name") |
| 63 | + create_db(sys.argv[2]) |
| 64 | +elif sys.argv[1] == "drop": |
| 65 | + check_cluster() or die("Cluster not present") |
| 66 | + if len(sys.argv) < 3: |
| 67 | + raise RuntimeError("Needed argument: db name") |
| 68 | + drop_db(sys.argv[2]) |
| 69 | +elif sys.argv[1] == "export": |
| 70 | + check_cluster() or die("Cluster not present") |
| 71 | + if len(sys.argv) < 4: |
| 72 | + raise RuntimeError("Needed arguments: db_name dump_file") |
| 73 | + export_db(sys.argv[2], sys.argv[3]) |
| 74 | +elif sys.argv[1] == "import": |
| 75 | + check_cluster() or die("Cluster not present") |
| 76 | + if len(sys.argv) < 4: |
| 77 | + raise RuntimeError("Needed arguments: dump_file db_name") |
| 78 | + import_db(sys.argv[2], sys.argv[3]) |
| 79 | +elif sys.argv[1] == "psql": |
| 80 | + check_cluster() or die("Cluster not present") |
| 81 | + psql(sys.argv[2:]) |
| 82 | +else: |
| 83 | + print(usage) |
| 84 | + exit(1) |
| 85 | + |
0 commit comments