Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add automatic id to each row #2767

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.molgenis.emx2.sql;

import static org.jooq.impl.DSL.name;
import static org.molgenis.emx2.Constants.MG_ID;
import static org.molgenis.emx2.Privileges.*;
import static org.molgenis.emx2.sql.SqlTableMetadataExecutor.executeDropTable;

Expand Down Expand Up @@ -71,6 +72,13 @@ static void executeCreateSchema(SqlDatabase db, SchemaMetadata schema) {
.execute("GRANT USAGE ON SCHEMA {0} TO {1}", name(schema.getName()), name(aggregator));
db.getJooq().execute("GRANT ALL ON SCHEMA {0} TO {1}", name(schema.getName()), name(manager));

// create autoid sequence
db.getJooq()
.execute(
"CREATE SEQUENCE IF NOT EXISTS {0} AS bigint INCREMENT BY 9 START 59049 CACHE 1000 NO CYCLE",
name(schemaName, MG_ID));
db.getJooq().execute("GRANT USAGE ON {0} TO {1}", name(schemaName, MG_ID), name(editor));

MetadataUtils.saveSchemaMetadata(db.getJooq(), schema);
}

Expand Down Expand Up @@ -218,6 +226,9 @@ static void executeDropSchema(SqlDatabase db, String schemaName) {
Collections.reverse(tables);
tables.forEach(table -> executeDropTable(db.getJooq(), table.getMetadata()));

// drop sequence
db.getJooq().dropSequence(name(schemaName, MG_ID));

// drop schema
db.getJooq().dropSchema(name(schemaName)).execute();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ static void executeCreateTable(DSLContext jooq, SqlTableMetadata table) {

// create the table
Table jooqTable = table.getJooqTable();
jooq.execute("CREATE TABLE {0}()", jooqTable);
jooq.execute(
"CREATE TABLE {0}(mg_id BIGINT DEFAULT nextval('\""
+ table.getSchemaName()
+ "\".mg_id') PRIMARY KEY)",
jooqTable);
MetadataUtils.saveTableMetadata(jooq, table);

// grant rights to schema manager, editor and viewer role
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public class Constants {

public static final String CONTACT_RECIPIENTS_QUERY_SETTING_KEY = "contactRecipientsQuery";
public static final String CONTACT_BCC_ADDRESS = "contactBccAddress";
public static final String MG_ID = "mg_id";

private Constants() {
// hide constructor
Expand Down