-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: create default tenant, anonymous user and instance config migr…
…ation (#33304) ## Description Adds migration for generating instance id, adding default tenant and creating anonymous user Fixes #`Issue Number` _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="" ### 🔍 Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!CAUTION] > If you modify the content in this section, you are likely to disrupt the CI result for your PR. <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [ ] No --------- Co-authored-by: Abhijeet <[email protected]>
- Loading branch information
Showing
6 changed files
with
134 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/JsonHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.appsmith.server.migrations; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class JsonHelper { | ||
private static ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
public static String convertToString(Object object) throws JsonProcessingException { | ||
if (object == null) { | ||
return null; | ||
} | ||
return objectMapper.writeValueAsString(object); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...appsmith-server/src/main/java/com/appsmith/server/migrations/RepositoryHelperMethods.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.appsmith.server.migrations; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
@RequiredArgsConstructor | ||
public class RepositoryHelperMethods { | ||
private final JdbcTemplate jdbcTemplate; | ||
|
||
public String getDefaultTenantId() { | ||
try { | ||
return jdbcTemplate.queryForObject("SELECT id FROM tenant WHERE slug = 'default' LIMIT 1", String.class); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...smith-server/src/main/java/com/appsmith/server/migrations/ce/V4__configureInstanceId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.appsmith.server.migrations.ce; | ||
|
||
import com.appsmith.server.domains.Config; | ||
import com.appsmith.server.migrations.AppsmithJavaMigration; | ||
import com.appsmith.server.migrations.JsonHelper; | ||
import net.minidev.json.JSONObject; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
public class V4__configureInstanceId extends AppsmithJavaMigration { | ||
|
||
public static final String INSTANCE_ID = "instance-id"; | ||
|
||
@Override | ||
public void migrate(JdbcTemplate jdbcTemplate) throws Exception { | ||
String sql = "SELECT COUNT(*) FROM config WHERE name = ?"; | ||
Integer count = jdbcTemplate.queryForObject(sql, Integer.class, INSTANCE_ID); | ||
if (count != null && count != 0) { | ||
return; | ||
} | ||
final String valueStr = UUID.randomUUID().toString(); | ||
|
||
Config instanceIdConfig = new Config(new JSONObject(Map.of("value", valueStr)), INSTANCE_ID); | ||
String jsonConfig = JsonHelper.convertToString(instanceIdConfig.getConfig()); | ||
String insertInstanceConfigurationQuery = | ||
"INSERT INTO config (id, name, config, created_at, updated_at) VALUES (gen_random_uuid(), ?, cast(? as jsonb), now(), now())"; | ||
jdbcTemplate.update(insertInstanceConfigurationQuery, INSTANCE_ID, jsonConfig); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...smith-server/src/main/java/com/appsmith/server/migrations/ce/V5__createDefaultTenant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.appsmith.server.migrations.ce; | ||
|
||
import com.appsmith.server.migrations.AppsmithJavaMigration; | ||
import com.appsmith.server.migrations.RepositoryHelperMethods; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
public class V5__createDefaultTenant extends AppsmithJavaMigration { | ||
|
||
private RepositoryHelperMethods helperMethods; | ||
|
||
@Override | ||
public void migrate(JdbcTemplate jdbcTemplate) throws Exception { | ||
RepositoryHelperMethods helperMethods = new RepositoryHelperMethods(jdbcTemplate); | ||
if (!StringUtils.isBlank(helperMethods.getDefaultTenantId())) { | ||
return; | ||
} | ||
jdbcTemplate.execute( | ||
"INSERT INTO tenant (id, slug, display_name, pricing_plan, created_at, updated_at) VALUES (gen_random_uuid(), 'default', 'Default', 'FREE', now(), now())"); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...smith-server/src/main/java/com/appsmith/server/migrations/ce/V6__createAnonymousUser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.appsmith.server.migrations.ce; | ||
|
||
import com.appsmith.server.constants.FieldName; | ||
import com.appsmith.server.migrations.AppsmithJavaMigration; | ||
import com.appsmith.server.migrations.JsonHelper; | ||
import com.appsmith.server.migrations.RepositoryHelperMethods; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
import java.util.Set; | ||
|
||
public class V6__createAnonymousUser extends AppsmithJavaMigration { | ||
private JdbcTemplate jdbcTemplate; | ||
private RepositoryHelperMethods helperMethods; | ||
|
||
@Override | ||
public void migrate(JdbcTemplate jdbcTemplate) throws Exception { | ||
this.jdbcTemplate = jdbcTemplate; | ||
helperMethods = new RepositoryHelperMethods(jdbcTemplate); | ||
addAnonymousUser(); | ||
} | ||
|
||
private void addAnonymousUser() throws JsonProcessingException { | ||
String defaultTenantId = helperMethods.getDefaultTenantId(); | ||
if (doesAnonymousUserExist(defaultTenantId)) { | ||
return; | ||
} | ||
String insertUserQuery = | ||
"INSERT INTO \"user\" (id, email, name, current_workspace_id, workspace_ids, is_anonymous, tenant_id, created_at, updated_at) VALUES (gen_random_uuid(), ?, ?, ?, cast(? as jsonb), ?, ?, now() ,now())"; | ||
jdbcTemplate.update( | ||
insertUserQuery, | ||
FieldName.ANONYMOUS_USER, | ||
FieldName.ANONYMOUS_USER, | ||
"", | ||
JsonHelper.convertToString(Set.of()), | ||
true, | ||
defaultTenantId); | ||
} | ||
|
||
private boolean doesAnonymousUserExist(String tenantId) { | ||
String sql = "SELECT COUNT(*) FROM \"user\" WHERE email = ? and tenant_id = ?"; | ||
Integer count = jdbcTemplate.queryForObject(sql, Integer.class, FieldName.ANONYMOUS_USER, tenantId); | ||
return count != null && count != 0; | ||
} | ||
} |