-
Notifications
You must be signed in to change notification settings - Fork 19
Refactor default tenant/domain migration with safety improvements #821
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 0 additions & 46 deletions
46
src/database/migrations/20250502091425-create-deafult-tenants-and-domains.js
This file was deleted.
Oops, something went wrong.
184 changes: 184 additions & 0 deletions
184
src/database/migrations/20250506091446-create-deafult-tenants-and-domains.js
This file contains hidden or 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,184 @@ | ||
| 'use strict' | ||
|
|
||
| module.exports = { | ||
| up: async (queryInterface, Sequelize) => { | ||
| const now = new Date() | ||
| const t = await queryInterface.sequelize.transaction() | ||
|
|
||
| // safe env fallbacks | ||
| const DEFAULT_TENANT_CODE = process.env.DEFAULT_TENANT_CODE | ||
| const DEFAULT_ORG_CODE = process.env.DEFAULT_ORGANISATION_CODE | ||
| const DEFAULT_ORG_NAME = process.env.DEFAULT_ORGANISATION_NAME || 'Default Organization' | ||
|
|
||
| try { | ||
| // Insert tenant (if not exists) | ||
| const existingTenant = await queryInterface.rawSelect( | ||
| 'tenants', | ||
| { where: { code: DEFAULT_TENANT_CODE }, transaction: t }, | ||
| 'id' | ||
| ) | ||
|
|
||
| if (!existingTenant) { | ||
| await queryInterface.bulkInsert( | ||
| 'tenants', | ||
| [ | ||
| { | ||
| code: DEFAULT_TENANT_CODE, | ||
| name: 'Default Tenant', | ||
| status: 'ACTIVE', | ||
| description: 'This is the default tenant.', | ||
| logo: 'https://www.logo.dev', | ||
| theming: JSON.stringify({ | ||
| primaryColor: '#4F46E5', | ||
| secondaryColor: '#F97316', | ||
| }), | ||
| meta: null, | ||
| created_by: null, | ||
| updated_by: null, | ||
| created_at: now, | ||
| updated_at: now, | ||
| deleted_at: null, | ||
| }, | ||
| ], | ||
| { transaction: t } | ||
| ) | ||
| } | ||
|
|
||
| // Insert tenant_domain (if not exists) | ||
| const existingDomain = await queryInterface.rawSelect( | ||
| 'tenant_domains', | ||
| { where: { tenant_code: DEFAULT_TENANT_CODE, domain: 'localhost' }, transaction: t }, | ||
| 'id' | ||
| ) | ||
|
|
||
| if (!existingDomain) { | ||
| await queryInterface.bulkInsert( | ||
| 'tenant_domains', | ||
| [ | ||
| { | ||
| tenant_code: DEFAULT_TENANT_CODE, | ||
| domain: 'localhost', | ||
| verified: true, | ||
| created_at: now, | ||
| updated_at: now, | ||
| deleted_at: null, | ||
| }, | ||
| ], | ||
| { transaction: t } | ||
| ) | ||
| } | ||
|
|
||
| // Insert organization if not exists | ||
| const existingOrgId = await queryInterface.rawSelect( | ||
| 'organizations', | ||
| { where: { code: DEFAULT_ORG_CODE, tenant_code: DEFAULT_TENANT_CODE }, transaction: t }, | ||
| 'id' | ||
| ) | ||
|
|
||
| let insertedOrgCode = DEFAULT_ORG_CODE | ||
|
|
||
| if (!existingOrgId) { | ||
| await queryInterface.bulkInsert( | ||
| 'organizations', | ||
| [ | ||
| { | ||
| name: DEFAULT_ORG_NAME, | ||
| code: DEFAULT_ORG_CODE, | ||
| description: 'Default Organisation', | ||
| status: 'ACTIVE', | ||
| tenant_code: DEFAULT_TENANT_CODE, | ||
| created_at: now, | ||
| updated_at: now, | ||
| }, | ||
| ], | ||
| { transaction: t } | ||
| ) | ||
|
|
||
| // get the id just inserted (DB may have returned id via serial; rawSelect by code ensures we get it) | ||
| } | ||
|
|
||
| // Fetch all features to seed organization_features | ||
| const [features] = await queryInterface.sequelize.query('SELECT code, label, icon FROM features;', { | ||
| transaction: t, | ||
| }) | ||
|
|
||
| if (features && features.length > 0) { | ||
| // prepare rows but skip duplicates: check existing by org + feature | ||
| const orgFeatureRows = [] | ||
|
|
||
| for (const feature of features) { | ||
| // check if already exists | ||
| const exists = await queryInterface.rawSelect( | ||
| 'organization_features', | ||
| { | ||
| where: { | ||
| organization_code: DEFAULT_ORG_CODE, | ||
| feature_code: feature.code, | ||
| tenant_code: DEFAULT_TENANT_CODE, | ||
| }, | ||
| transaction: t, | ||
| }, | ||
| 'id' | ||
| ) | ||
|
|
||
| if (!exists) { | ||
| orgFeatureRows.push({ | ||
| organization_code: DEFAULT_ORG_CODE, | ||
| feature_code: feature.code, | ||
| enabled: true, | ||
| feature_name: feature.label, | ||
| icon: feature.icon || null, | ||
| tenant_code: DEFAULT_TENANT_CODE, | ||
| created_at: now, | ||
| updated_at: now, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| if (orgFeatureRows.length > 0) { | ||
| await queryInterface.bulkInsert('organization_features', orgFeatureRows, { transaction: t }) | ||
| } | ||
| } | ||
|
|
||
| await t.commit() | ||
| } catch (err) { | ||
| await t.rollback() | ||
| console.error('Migration failed:', err) | ||
| throw err | ||
| } | ||
| }, | ||
|
|
||
| down: async (queryInterface, Sequelize) => { | ||
| const t = await queryInterface.sequelize.transaction() | ||
| const DEFAULT_TENANT_CODE = process.env.DEFAULT_TENANT_CODE | ||
| const DEFAULT_ORG_CODE = process.env.DEFAULT_ORGANISATION_CODE | ||
|
|
||
| try { | ||
| // Delete seeded organization_features for this org & tenant | ||
| await queryInterface.bulkDelete( | ||
| 'organization_features', | ||
| { organization_code: DEFAULT_ORG_CODE, tenant_code: DEFAULT_TENANT_CODE }, | ||
| { transaction: t } | ||
| ) | ||
|
|
||
| // Delete organization | ||
| await queryInterface.bulkDelete( | ||
| 'organizations', | ||
| { code: DEFAULT_ORG_CODE, tenant_code: DEFAULT_TENANT_CODE }, | ||
| { transaction: t } | ||
| ) | ||
|
|
||
| // Delete tenant_domain(s) | ||
| await queryInterface.bulkDelete('tenant_domains', { tenant_code: DEFAULT_TENANT_CODE }, { transaction: t }) | ||
|
|
||
| // Delete tenant | ||
| await queryInterface.bulkDelete('tenants', { code: DEFAULT_TENANT_CODE }, { transaction: t }) | ||
|
|
||
| await t.commit() | ||
| } catch (err) { | ||
| await t.rollback() | ||
| console.error('Rollback failed:', err) | ||
| throw err | ||
| } | ||
| }, | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.