-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop creating default organization and refactor ui for org creation
fixes lint fix remove unnecessary tests fixes fix case where we don't know at all what to do, but we do have orgs added migration to delete everything related to personal orgs, and also delete unused orgs remove everything related to personal org fix migration delete oopsi added migrations testing fix better migration
- Loading branch information
1 parent
94360c5
commit 8432143
Showing
35 changed files
with
292 additions
and
265 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
on: | ||
workflow_call: | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-22.04 | ||
|
||
services: | ||
postgres: | ||
image: postgres:13.10-alpine | ||
ports: | ||
- 5432:5432 | ||
env: | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_USER: postgres | ||
POSTGRES_DB: registry | ||
options: >- | ||
--health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 | ||
env: | ||
POSTGRES_HOST: localhost | ||
POSTGRES_PORT: 5432 | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_USER: postgres | ||
POSTGRES_DB: registry | ||
|
||
steps: | ||
- name: checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: setup environment | ||
uses: ./.github/actions/setup | ||
with: | ||
codegen: false # no need to run codegen in this case, we can skip | ||
|
||
- name: migrations tests | ||
run: pnpm test | ||
working-directory: packages/migrations |
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
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
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
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
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
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
13 changes: 13 additions & 0 deletions
13
packages/migrations/src/actions/2023.02.22T09.27.02.delete-personal-org.sql
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,13 @@ | ||
-- Find and delete all organizations of type PERSONAL that have no projects | ||
DELETE FROM public.organizations as o | ||
WHERE | ||
o.type = 'PERSONAL' | ||
AND NOT EXISTS ( | ||
SELECT id from public.projects as p WHERE p.org_id = o.id LIMIT 1 | ||
); | ||
|
||
-- Delete the "type" column from organizations | ||
ALTER TABLE public.organizations DROP COLUMN type; | ||
|
||
-- Delete the "organization_type" enum, as it's unused now | ||
DROP TYPE organization_type; |
1 change: 1 addition & 0 deletions
1
packages/migrations/src/actions/down/2023.02.22T09.27.02.delete-personal-org.sql
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 @@ | ||
raise 'down migration not implemented' |
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,11 @@ | ||
export function createConnectionString(config: { | ||
host: string; | ||
port: number; | ||
password: string; | ||
user: string; | ||
db: string; | ||
ssl: boolean; | ||
}) { | ||
// prettier-ignore | ||
return `postgres://${config.user}:${config.password}@${config.host}:${config.port}/${config.db}${config.ssl ? '?sslmode=require' : '?sslmode=disable'}`; | ||
} |
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
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,69 @@ | ||
import assert from 'node:assert'; | ||
import { describe, test } from 'node:test'; | ||
import { sql } from 'slonik'; | ||
import { initMigrationTestingEnvironment } from './utils/testkit'; | ||
|
||
describe('migration: drop-personal-org', async () => { | ||
await test('should remove all existing personal orgs that does not have projects', async () => { | ||
const { db, runTo, complete, done, seed } = await initMigrationTestingEnvironment(); | ||
|
||
try { | ||
// Run migrations all the way to the point before the one we are testing | ||
await runTo('2023.02.21T14.32.24.supertokens-4.0.0.sql'); | ||
|
||
// Seed the DB with orgs | ||
const user = await seed.user(); | ||
const emptyOrgs = await Promise.all([ | ||
db.one( | ||
sql`INSERT INTO public.organizations (clean_id, name, user_id, type) VALUES ('personal-empty', 'personal-empty', ${user.id}, 'PERSONAL') RETURNING *;`, | ||
), | ||
db.one( | ||
sql`INSERT INTO public.organizations (clean_id, name, user_id, type) VALUES ('regular-empty', 'regular-empty', ${user.id}, 'REGULAR') RETURNING *;`, | ||
), | ||
]); | ||
const orgsWithProjects = await Promise.all([ | ||
await db.one( | ||
sql`INSERT INTO public.organizations (clean_id, name, user_id, type) VALUES ('personal-project', 'personal-project', ${user.id}, 'PERSONAL') RETURNING *;`, | ||
), | ||
await db.one( | ||
sql`INSERT INTO public.organizations (clean_id, name, user_id, type) VALUES ('regular-project', 'regular-project', ${user.id}, 'PERSONAL') RETURNING *;`, | ||
), | ||
]); | ||
|
||
// Seed with projects | ||
await db.one( | ||
sql`INSERT INTO public.projects (clean_id, name, type, org_id) VALUES ('proj-1', 'proj-1', 'SINGLE', ${orgsWithProjects[0].id}) RETURNING *;`, | ||
); | ||
await db.one( | ||
sql`INSERT INTO public.projects (clean_id, name, type, org_id) VALUES ('proj-2', 'proj-2', 'SINGLE', ${orgsWithProjects[1].id}) RETURNING *;`, | ||
); | ||
|
||
// Run the additional remaining migrations | ||
await complete(); | ||
|
||
// Only this one should be deleted, the rest should still exists | ||
assert.equal( | ||
await db.maybeOne(sql`SELECT * FROM public.organizations WHERE id = ${emptyOrgs[0].id}`), | ||
null, | ||
); | ||
assert.notEqual( | ||
await db.maybeOne(sql`SELECT * FROM public.organizations WHERE id = ${emptyOrgs[1].id}`), | ||
null, | ||
); | ||
assert.notEqual( | ||
await db.maybeOne( | ||
sql`SELECT * FROM public.organizations WHERE id = ${orgsWithProjects[0].id}`, | ||
), | ||
null, | ||
); | ||
assert.notEqual( | ||
await db.maybeOne( | ||
sql`SELECT * FROM public.organizations WHERE id = ${orgsWithProjects[1].id}`, | ||
), | ||
null, | ||
); | ||
} finally { | ||
await done(); | ||
} | ||
}); | ||
}); |
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,71 @@ | ||
/* eslint-disable import/first */ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import { dirname, resolve } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { config } from 'dotenv'; | ||
import pgpFactory from 'pg-promise'; | ||
import { createPool, sql } from 'slonik'; | ||
import { SlonikMigrator } from '@slonik/migrator'; | ||
import type * as DbTypes from '../../../services/storage/src/db/types'; | ||
import { createConnectionString } from '../../src/connection-string'; | ||
|
||
export { DbTypes }; | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
|
||
config({ | ||
path: resolve(__dirname, '../../.env'), | ||
}); | ||
|
||
import { env } from '../../src/environment'; | ||
|
||
export async function initMigrationTestingEnvironment() { | ||
const pgp = pgpFactory(); | ||
const db = pgp( | ||
createConnectionString({ | ||
...env.postgres, | ||
db: 'postgres', | ||
}), | ||
); | ||
|
||
const dbName = 'migration_test_' + Date.now(); | ||
await db.query(`CREATE DATABASE ${dbName};`); | ||
|
||
const slonik = await createPool( | ||
createConnectionString({ | ||
...env.postgres, | ||
db: dbName, | ||
}), | ||
); | ||
|
||
const actionsDirectory = resolve(__dirname + '/../../src/actions/'); | ||
console.log('actionsDirectory', actionsDirectory); | ||
|
||
const migrator = new SlonikMigrator({ | ||
migrationsPath: actionsDirectory, | ||
slonik, | ||
migrationTableName: 'migration', | ||
logger: console, | ||
}); | ||
|
||
return { | ||
db: slonik, | ||
async runTo(name: string) { | ||
await migrator.up({ to: name }); | ||
}, | ||
seed: { | ||
async user() { | ||
return await slonik.one<DbTypes.users>( | ||
sql`INSERT INTO public.users (email, display_name, full_name, supertoken_user_id) VALUES ('test@mail.com', 'test1' , 'test1', '1') RETURNING *;`, | ||
); | ||
}, | ||
}, | ||
async complete() { | ||
await migrator.up(); | ||
}, | ||
async done(deleteDb = true) { | ||
deleteDb ?? (await db.query(`DROP DATABASE ${dbName};`)); | ||
await db.$pool.end().catch(); | ||
}, | ||
}; | ||
} |
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
Oops, something went wrong.