diff options
author | Martin Schanzenbach <schanzen@gnunet.org> | 2022-09-30 19:18:40 +0900 |
---|---|---|
committer | Martin Schanzenbach <schanzen@gnunet.org> | 2022-09-30 19:18:40 +0900 |
commit | 51b6cf4d3944152ea0c62a5aa41224f6227d62f7 (patch) | |
tree | 17ea52f6830ff8ad90871fbc408c33174e8386b1 | |
parent | c3eca4a0aabe79be51c18fbe56a91b033b1f0549 (diff) | |
download | gnunet-51b6cf4d3944152ea0c62a5aa41224f6227d62f7.tar.gz gnunet-51b6cf4d3944152ea0c62a5aa41224f6227d62f7.zip |
NAMESTORE: Add postgres functions for DB setup utility
-rw-r--r-- | src/namestore/plugin_namestore_postgres.c | 86 | ||||
-rw-r--r-- | src/namestore/test_namestore_api_postgres.conf | 1 |
2 files changed, 83 insertions, 4 deletions
diff --git a/src/namestore/plugin_namestore_postgres.c b/src/namestore/plugin_namestore_postgres.c index 510d24496..e3b8dc745 100644 --- a/src/namestore/plugin_namestore_postgres.c +++ b/src/namestore/plugin_namestore_postgres.c | |||
@@ -60,8 +60,10 @@ struct Plugin | |||
60 | * @return #GNUNET_OK on success | 60 | * @return #GNUNET_OK on success |
61 | */ | 61 | */ |
62 | static int | 62 | static int |
63 | database_setup (struct Plugin *plugin) | 63 | init_database (struct Plugin *plugin, char **emsg, int drop) |
64 | { | 64 | { |
65 | struct GNUNET_PQ_ExecuteStatement es_drop = | ||
66 | GNUNET_PQ_make_execute ("DROP TABLE IF EXISTS ns098records"); | ||
65 | struct GNUNET_PQ_ExecuteStatement es_temporary = | 67 | struct GNUNET_PQ_ExecuteStatement es_temporary = |
66 | GNUNET_PQ_make_execute ( | 68 | GNUNET_PQ_make_execute ( |
67 | "CREATE TEMPORARY TABLE IF NOT EXISTS ns098records (" | 69 | "CREATE TEMPORARY TABLE IF NOT EXISTS ns098records (" |
@@ -87,7 +89,7 @@ database_setup (struct Plugin *plugin) | |||
87 | ")"); | 89 | ")"); |
88 | const struct GNUNET_PQ_ExecuteStatement *cr; | 90 | const struct GNUNET_PQ_ExecuteStatement *cr; |
89 | struct GNUNET_PQ_ExecuteStatement sc = GNUNET_PQ_EXECUTE_STATEMENT_END; | 91 | struct GNUNET_PQ_ExecuteStatement sc = GNUNET_PQ_EXECUTE_STATEMENT_END; |
90 | 92 | struct GNUNET_PQ_ExecuteStatement sc2 = GNUNET_PQ_EXECUTE_STATEMENT_END; | |
91 | if (GNUNET_YES == | 93 | if (GNUNET_YES == |
92 | GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg, | 94 | GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg, |
93 | "namestore-postgres", | 95 | "namestore-postgres", |
@@ -104,7 +106,13 @@ database_setup (struct Plugin *plugin) | |||
104 | GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg, | 106 | GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg, |
105 | "namestore-postgres", | 107 | "namestore-postgres", |
106 | "ASYNC_COMMIT")) | 108 | "ASYNC_COMMIT")) |
109 | { | ||
107 | sc = GNUNET_PQ_make_try_execute ("SET synchronous_commit TO off"); | 110 | sc = GNUNET_PQ_make_try_execute ("SET synchronous_commit TO off"); |
111 | if (GNUNET_YES == drop) | ||
112 | sc2 = es_drop; | ||
113 | } | ||
114 | else if (GNUNET_YES == drop) | ||
115 | sc = es_drop; | ||
108 | 116 | ||
109 | { | 117 | { |
110 | struct GNUNET_PQ_ExecuteStatement es[] = { | 118 | struct GNUNET_PQ_ExecuteStatement es[] = { |
@@ -118,8 +126,59 @@ database_setup (struct Plugin *plugin) | |||
118 | GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS zone_label " | 126 | GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS zone_label " |
119 | "ON ns098records (zone_private_key,label)"), | 127 | "ON ns098records (zone_private_key,label)"), |
120 | sc, | 128 | sc, |
129 | sc2, | ||
121 | GNUNET_PQ_EXECUTE_STATEMENT_END | 130 | GNUNET_PQ_EXECUTE_STATEMENT_END |
122 | }; | 131 | }; |
132 | |||
133 | plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->cfg, | ||
134 | "namestore-postgres", | ||
135 | NULL, | ||
136 | es, | ||
137 | NULL); | ||
138 | } | ||
139 | if (NULL == plugin->dbh) | ||
140 | { | ||
141 | *emsg = GNUNET_strdup ("Failed to connect to PQ database"); | ||
142 | return GNUNET_SYSERR; | ||
143 | } | ||
144 | GNUNET_PQ_disconnect (plugin->dbh); | ||
145 | plugin->dbh = NULL; | ||
146 | return GNUNET_OK; | ||
147 | } | ||
148 | |||
149 | /** | ||
150 | * Initialize the database connections and associated | ||
151 | * data structures (create tables and indices | ||
152 | * as needed as well). | ||
153 | * | ||
154 | * @param plugin the plugin context (state for this module) | ||
155 | * @return #GNUNET_OK on success | ||
156 | */ | ||
157 | static int | ||
158 | database_connect (struct Plugin *plugin) | ||
159 | { | ||
160 | const struct GNUNET_PQ_ExecuteStatement *cr; | ||
161 | char *emsg; | ||
162 | struct GNUNET_PQ_ExecuteStatement sc = GNUNET_PQ_EXECUTE_STATEMENT_END; | ||
163 | |||
164 | if (GNUNET_YES == | ||
165 | GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg, | ||
166 | "namestore-postgres", | ||
167 | "INIT_ON_CONNECT")) | ||
168 | { | ||
169 | /** | ||
170 | * Gracefully fail as this should not be a critical error if the | ||
171 | * database is already created | ||
172 | */ | ||
173 | if (GNUNET_OK != init_database (plugin, &emsg, GNUNET_NO)) | ||
174 | { | ||
175 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
176 | "Failed to initialize database on connect: `%s'\n", | ||
177 | emsg); | ||
178 | GNUNET_free (emsg); | ||
179 | } | ||
180 | } | ||
181 | { | ||
123 | struct GNUNET_PQ_PreparedStatement ps[] = { | 182 | struct GNUNET_PQ_PreparedStatement ps[] = { |
124 | GNUNET_PQ_make_prepare ("store_records", | 183 | GNUNET_PQ_make_prepare ("store_records", |
125 | "INSERT INTO ns098records" | 184 | "INSERT INTO ns098records" |
@@ -161,7 +220,7 @@ database_setup (struct Plugin *plugin) | |||
161 | plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->cfg, | 220 | plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->cfg, |
162 | "namestore-postgres", | 221 | "namestore-postgres", |
163 | NULL, | 222 | NULL, |
164 | es, | 223 | NULL, |
165 | ps); | 224 | ps); |
166 | } | 225 | } |
167 | if (NULL == plugin->dbh) | 226 | if (NULL == plugin->dbh) |
@@ -666,6 +725,23 @@ namestore_postgres_transaction_commit (void *cls, | |||
666 | } | 725 | } |
667 | 726 | ||
668 | 727 | ||
728 | static enum GNUNET_GenericReturnValue | ||
729 | namestore_postgres_initialize_database (void *cls, | ||
730 | char **emsg) | ||
731 | { | ||
732 | return init_database (cls, emsg, GNUNET_NO); | ||
733 | } | ||
734 | |||
735 | |||
736 | static enum GNUNET_GenericReturnValue | ||
737 | namestore_postgres_reset_database (void *cls, | ||
738 | char **emsg) | ||
739 | { | ||
740 | return init_database (cls, emsg, GNUNET_YES); | ||
741 | } | ||
742 | |||
743 | |||
744 | |||
669 | /** | 745 | /** |
670 | * Shutdown database connection and associate data | 746 | * Shutdown database connection and associate data |
671 | * structures. | 747 | * structures. |
@@ -695,7 +771,7 @@ libgnunet_plugin_namestore_postgres_init (void *cls) | |||
695 | 771 | ||
696 | plugin = GNUNET_new (struct Plugin); | 772 | plugin = GNUNET_new (struct Plugin); |
697 | plugin->cfg = cfg; | 773 | plugin->cfg = cfg; |
698 | if (GNUNET_OK != database_setup (plugin)) | 774 | if (GNUNET_OK != database_connect (plugin)) |
699 | { | 775 | { |
700 | database_shutdown (plugin); | 776 | database_shutdown (plugin); |
701 | GNUNET_free (plugin); | 777 | GNUNET_free (plugin); |
@@ -710,6 +786,8 @@ libgnunet_plugin_namestore_postgres_init (void *cls) | |||
710 | api->transaction_begin = &namestore_postgres_transaction_begin; | 786 | api->transaction_begin = &namestore_postgres_transaction_begin; |
711 | api->transaction_commit = &namestore_postgres_transaction_commit; | 787 | api->transaction_commit = &namestore_postgres_transaction_commit; |
712 | api->transaction_rollback = &namestore_postgres_transaction_rollback; | 788 | api->transaction_rollback = &namestore_postgres_transaction_rollback; |
789 | api->initialize_database = &namestore_postgres_initialize_database; | ||
790 | api->reset_database = &namestore_postgres_reset_database; | ||
713 | api->edit_records = &namestore_postgres_edit_records; | 791 | api->edit_records = &namestore_postgres_edit_records; |
714 | LOG (GNUNET_ERROR_TYPE_INFO, | 792 | LOG (GNUNET_ERROR_TYPE_INFO, |
715 | "Postgres namestore plugin running\n"); | 793 | "Postgres namestore plugin running\n"); |
diff --git a/src/namestore/test_namestore_api_postgres.conf b/src/namestore/test_namestore_api_postgres.conf index c648a6ab9..007168280 100644 --- a/src/namestore/test_namestore_api_postgres.conf +++ b/src/namestore/test_namestore_api_postgres.conf | |||
@@ -7,3 +7,4 @@ DATABASE = postgres | |||
7 | [namestore-postgres] | 7 | [namestore-postgres] |
8 | CONFIG = connect_timeout=10 dbname=gnunetcheck | 8 | CONFIG = connect_timeout=10 dbname=gnunetcheck |
9 | TEMPORARY_TABLE = NO | 9 | TEMPORARY_TABLE = NO |
10 | INIT_ON_CONNECT = YES | ||