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

Update deployment to run migrations as a separate step #1778

Merged
Merged
25 changes: 23 additions & 2 deletions .github/workflows/manual-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
dockerfile: "packages/ai-bot/Dockerfile"

deploy-ai-bot:
needs: [build-ai-bot]
needs: [build-ai-bot, migrate-db]
name: Deploy ai-bot to AWS ECS
uses: cardstack/gh-actions/.github/workflows/ecs-deploy.yml@main
secrets: inherit
Expand Down Expand Up @@ -77,9 +77,30 @@ jobs:
build-args: |
"realm_server_script=start:${{ inputs.environment }}"
build-pg-migration:
name: Build pg-migration Docker image
uses: cardstack/gh-actions/.github/workflows/docker-ecr.yml@main
secrets: inherit
with:
repository: "boxel-pg-migration-${{ inputs.environment }}"
environment: ${{ inputs.environment }}
dockerfile: "packages/postgres/Dockerfile"

migrate-db:
needs: [build-pg-migration]
name: Deploy and run DB migrations
uses: cardstack/gh-actions/.github/workflows/ecs-deploy.yml@main
secrets: inherit
with:
container-name: "boxel-pg-migration"
environment: ${{ inputs.environment }}
cluster: ${{ inputs.environment }}
service-name: "boxel-pg-migration-${{ inputs.environment }}"
image: ${{ needs.build-pg-migration.outputs.image }}

deploy-realm-server:
name: Deploy realm server
needs: [build-realm-server, deploy-host]
needs: [build-realm-server, deploy-host, migrate-db]
uses: cardstack/gh-actions/.github/workflows/ecs-deploy.yml@main
secrets: inherit
with:
Expand Down
1 change: 1 addition & 0 deletions packages/matrix/helpers/isolated-realm-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export async function startServer() {
`--matrixURL='http://localhost:8008'`,
`--realmsRootPath='${dir.name}'`,
`--seedPath='${seedPath}'`,
`--migrateDB`,
`--useRegistrationSecretFunction`,

`--path='${testRealmDir}'`,
Expand Down
11 changes: 11 additions & 0 deletions packages/postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM node:18.6.0-slim
ARG CI=1
RUN apt-get update && apt-get install -y postgresql
RUN npm install -g [email protected]
WORKDIR /boxel
COPY . .
RUN pnpm install --frozen-lockfile

WORKDIR /boxel/packages/postgres

CMD ./node_modules/.bin/node-pg-migrate --check-order false --migrations-table migrations up && sleep infinity
10 changes: 9 additions & 1 deletion packages/postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@
"dependencies": {
"@cardstack/runtime-common": "workspace:*",
"@sentry/node": "^8.31.0",
"@types/fs-extra": "^9.0.13",
"@types/pg": "^8.11.5",
"fs-extra": "^10.1.0",
"node-pg-migrate": "^6.2.2",
"pg": "^8.11.5"
},
"devDependencies": {
"concurrently": "^8.0.1"
"concurrently": "^8.0.1",
"sql-parser-cst": "^0.28.0"
},
"scripts": {
"start:pg": "./scripts/start-pg.sh",
"stop:pg": "./scripts/stop-pg.sh",
"migrate": "PGDATABASE=boxel ./scripts/ensure-db-exists.sh && PGPORT=5435 PGDATABASE=boxel PGUSER=postgres node-pg-migrate --migrations-table migrations",
"make-schema": "./scripts/schema-dump.sh",
"drop-db": "docker exec boxel-pg dropdb -U postgres -w",
"lint": "concurrently \"pnpm:lint:*(!fix)\" --names \"lint:\"",
"lint:fix": "concurrently \"pnpm:lint:*:fix\" --names \"fix:\"",
"lint:js": "eslint . --cache",
Expand Down
13 changes: 7 additions & 6 deletions packages/postgres/pg-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ type Config = ReturnType<typeof config>;
export class PgAdapter implements DBAdapter {
#isClosed = false;
private pool: Pool;
private started = this.#startClient();
private started: Promise<void>;
private config: Config;

constructor() {
constructor(opts?: { autoMigrate?: true }) {
if (opts?.autoMigrate) {
this.started = this.migrateDb();
} else {
this.started = Promise.resolve();
}
this.config = config();
let { user, host, database, password, port } = this.config;
log.info(`connecting to DB ${this.url}`);
Expand All @@ -52,10 +57,6 @@ export class PgAdapter implements DBAdapter {
return `${user}@${host}:${port}/${database}`;
}

async #startClient() {
await this.migrateDb();
}

async close() {
log.info(`closing ${this.url}`);
this.#isClosed = true;
Expand Down
8 changes: 8 additions & 0 deletions packages/postgres/scripts/start-pg.sh
Copy link
Contributor Author

@habdelra habdelra Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just a rename--the symlink i made to it confused github

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /bin/sh
if [ -z "$(docker ps -f name=boxel-pg --all --format '{{.Names}}')" ]; then
# running postgres on port 5435 so it doesn't collide with native postgres
# that may be running on your system
docker run --name boxel-pg -e POSTGRES_HOST_AUTH_METHOD=trust -p 5435:5432 -d postgres:16.3 >/dev/null
else
docker start boxel-pg >/dev/null
fi
2 changes: 2 additions & 0 deletions packages/postgres/scripts/stop-pg.sh
Copy link
Contributor Author

@habdelra habdelra Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just a rename--the symlink i made to it confused github

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! /bin/sh
docker stop boxel-pg >/dev/null
14 changes: 11 additions & 3 deletions packages/realm-server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ let {
username: usernames,
useRegistrationSecretFunction,
seedPath,
migrateDB,
} = yargs(process.argv.slice(2))
.usage('Start realm server')
.options({
Expand Down Expand Up @@ -118,6 +119,11 @@ let {
demandOption: true,
type: 'array',
},
migrateDB: {
description:
'When this flag is set the database will automatically migrate when server is started',
type: 'boolean',
},
useRegistrationSecretFunction: {
description:
'The flag should be set when running matrix tests where the synapse instance is torn down and restarted multiple times during the life of the realm server.',
Expand Down Expand Up @@ -166,18 +172,19 @@ for (let [from, to] of urlMappings) {
}
let hrefs = urlMappings.map(([from, to]) => [from.href, to.href]);
let dist: URL = new URL(distURL);
let autoMigrate = migrateDB || undefined;

(async () => {
let realms: Realm[] = [];
let dbAdapter = new PgAdapter();
let dbAdapter = new PgAdapter({ autoMigrate });
let queue = new PgQueuePublisher(dbAdapter);
let manager = new RunnerOptionsManager();
let { getIndexHTML } = await makeFastBootIndexRunner(
dist,
manager.getOptions.bind(manager),
);

await startWorker();
await startWorker({ autoMigrate });

for (let [i, path] of paths.entries()) {
let url = hrefs[i][0];
Expand Down Expand Up @@ -298,7 +305,7 @@ let dist: URL = new URL(distURL);
process.exit(-3);
});

async function startWorker() {
async function startWorker(opts?: { autoMigrate?: true }) {
let worker = spawn(
'ts-node',
[
Expand All @@ -307,6 +314,7 @@ async function startWorker() {
`--port=${port}`,
`--matrixURL='${matrixURL}'`,
`--distURL='${distURL}'`,
...(opts?.autoMigrate ? [`--migrateDB`] : []),
...flattenDeep(
urlMappings.map(([from, to]) => [
`--fromUrl='${from}'`,
Expand Down
6 changes: 1 addition & 5 deletions packages/realm-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"qs": "^6.13.0",
"qunit": "^2.20.0",
"sane": "^5.0.1",
"sql-parser-cst": "^0.28.0",
"start-server-and-test": "^1.14.0",
"supertest": "^6.2.4",
"testem": "^3.10.1",
Expand Down Expand Up @@ -92,10 +91,7 @@
"lint:js": "eslint . --cache",
"lint:js:fix": "eslint . --fix",
"lint:glint": "glint",
"migrate": "PGDATABASE=boxel ./scripts/ensure-db-exists.sh && PGPORT=5435 PGDATABASE=boxel PGUSER=postgres node-pg-migrate --migrations-table migrations",
"make-schema": "./scripts/schema-dump.sh",
"drop-db": "docker exec boxel-pg dropdb -U postgres -w",
"drop-all-dbs": "./scripts/drop-all-dbs.sh"
"full-reset": "./scripts/full-reset.sh"
},
"volta": {
"extends": "../../package.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
CURRENT_DIR="$(pwd)"
SCRIPTS_DIR="$(cd "$(dirname "$0")" && pwd)"

cd ${SCRIPTS_DIR}/../../postgres
pnpm run drop-db boxel
pnpm run drop-db boxel_test
pnpm run drop-db boxel_base
Expand Down
1 change: 1 addition & 0 deletions packages/realm-server/scripts/start-base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ NODE_ENV=development \
--port=4201 \
--matrixURL='http://localhost:8008' \
--realmsRootPath='./realms/localhost_4201' \
--migrateDB \
\
--path='../base' \
--username='base_realm' \
Expand Down
1 change: 1 addition & 0 deletions packages/realm-server/scripts/start-development.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ NODE_ENV=development \
--matrixURL='http://localhost:8008' \
--realmsRootPath='./realms/localhost_4201' \
--seedPath='../seed-realm' \
--migrateDB \
\
--path='../base' \
--username='base_realm' \
Expand Down
8 changes: 0 additions & 8 deletions packages/realm-server/scripts/start-pg.sh
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was so convenient having this script here I decided to just sym link it to its new location

This file was deleted.

1 change: 1 addition & 0 deletions packages/realm-server/scripts/start-pg.sh
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was so convenient having this script here I decided to just sym link it to its new location

1 change: 1 addition & 0 deletions packages/realm-server/scripts/start-test-realms.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ NODE_ENV=test \
--matrixURL='http://localhost:8008' \
--realmsRootPath='./realms/localhost_4202' \
--matrixRegistrationSecretFile='../matrix/registration_secret.txt' \
--migrateDB \
\
--path='./tests/cards' \
--username='node-test_realm' \
Expand Down
2 changes: 0 additions & 2 deletions packages/realm-server/scripts/stop-pg.sh

This file was deleted.

1 change: 1 addition & 0 deletions packages/realm-server/scripts/stop-pg.sh
2 changes: 1 addition & 1 deletion packages/realm-server/tests/billing-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ module('billing', function (hooks) {

hooks.beforeEach(async function () {
prepareTestDB();
dbAdapter = new PgAdapter();
dbAdapter = new PgAdapter({ autoMigrate: true });
});

hooks.afterEach(async function () {
Expand Down
2 changes: 1 addition & 1 deletion packages/realm-server/tests/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function setupDB(

const runBeforeHook = async () => {
prepareTestDB();
dbAdapter = new PgAdapter();
dbAdapter = new PgAdapter({ autoMigrate: true });
publisher = new PgQueuePublisher(dbAdapter);
runner = new PgQueueRunner(dbAdapter, 'test-worker');
};
Expand Down
2 changes: 1 addition & 1 deletion packages/realm-server/tests/index-query-engine-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module('query', function (hooks) {
]);
loader = new Loader(fetch, virtualNetwork.resolveImport);

dbAdapter = new PgAdapter();
dbAdapter = new PgAdapter({ autoMigrate: true });
indexQueryEngine = new IndexQueryEngine(dbAdapter);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/realm-server/tests/index-writer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module('index-writer', function (hooks) {

hooks.beforeEach(async function () {
prepareTestDB();
adapter = new PgAdapter();
adapter = new PgAdapter({ autoMigrate: true });
indexWriter = new IndexWriter(adapter);
indexQueryEngine = new IndexQueryEngine(adapter);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/realm-server/tests/queue-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module('queue', function (hooks) {

hooks.beforeEach(async function () {
prepareTestDB();
adapter = new PgAdapter();
adapter = new PgAdapter({ autoMigrate: true });
publisher = new PgQueuePublisher(adapter);
runner = new PgQueueRunner(adapter, 'q1');
await runner.start();
Expand Down Expand Up @@ -50,7 +50,7 @@ module('queue', function (hooks) {
let runner2: QueueRunner;
let adapter2: PgAdapter;
nestedHooks.beforeEach(async function () {
adapter2 = new PgAdapter();
adapter2 = new PgAdapter({ autoMigrate: true });
runner2 = new PgQueueRunner(adapter2, 'q2');
await runner2.start();

Expand Down
9 changes: 8 additions & 1 deletion packages/realm-server/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ let {
distURL = process.env.HOST_URL ?? 'http://localhost:4200',
fromUrl: fromUrls,
toUrl: toUrls,
migrateDB,
} = yargs(process.argv.slice(2))
.usage('Start worker')
.options({
Expand All @@ -61,6 +62,11 @@ let {
'the URL of a deployed host app. (This can be provided instead of the --distPath)',
type: 'string',
},
migrateDB: {
description:
'When this flag is set the database will automatically migrate when server is started',
type: 'boolean',
},
matrixURL: {
description: 'The matrix homeserver for the realm',
demandOption: true,
Expand Down Expand Up @@ -90,9 +96,10 @@ for (let [from, to] of urlMappings) {
virtualNetwork.addURLMapping(from, to);
}
let dist: URL = new URL(distURL);
let autoMigrate = migrateDB || undefined;

(async () => {
let dbAdapter = new PgAdapter();
let dbAdapter = new PgAdapter({ autoMigrate });
let queue = new PgQueueRunner(dbAdapter, workerId);
let manager = new RunnerOptionsManager();
let { getRunner } = await makeFastBootIndexRunner(
Expand Down
13 changes: 9 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading