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

26 db migration, type safety, domain layer and insert project api #32

Closed
wants to merge 108 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
108 commits
Select commit Hold shift + click to select a range
d20c4d0
Add domain models as currently specified in nextCloud
Nov 1, 2024
c3d2800
WIP add app post and put
Oct 31, 2024
620b24c
auto reformat with prettier
Nov 1, 2024
eb4b677
Copy domain models to db models
Nov 5, 2024
1765b2b
Refactor db models and add README.md
Nov 5, 2024
70769ea
Start aligning domain models with db models
Nov 7, 2024
6ce0447
Make table id's and relations uniform
Nov 7, 2024
bca59c1
Rename AppCategory to AppCategoryName to allow AppCategory table
Nov 7, 2024
125ec6d
Add list of files to Version
Nov 7, 2024
9547cb9
Rename relations to indicate the purpose. Eg `ProjectHasVersions`
Nov 7, 2024
617b3e2
Fixup domain project integration
Nov 7, 2024
d90f64e
Simplify Badge Model, remove types as well
Nov 7, 2024
10cfae2
Refactor ProjectStatusOnBadge in order not to form a circular dependency
Nov 7, 2024
befe733
remove dependants from Project to remove circular dependency
Nov 7, 2024
9bd171e
Clean up relations in ...FromUser
Nov 7, 2024
27e1116
Clean up ids in ...FromUser
Nov 7, 2024
6d872c1
Change VersionedDependency in db models to use slug
Nov 7, 2024
4650831
Refactor relations in db models again to reflect relation field names
Nov 7, 2024
4dcf2a0
Add download_count to Version
Nov 7, 2024
52bfe2c
Extend project port
Nov 7, 2024
cd6ad71
Add some more necessary items to ProjectPort.ts
Nov 7, 2024
b55dc17
Add create and detele Project methods to project port
Nov 7, 2024
312feaa
Add sql-template-tag
Nov 7, 2024
da7190f
Implement first private-rest endpoints that use the ProjectPort inter…
Nov 7, 2024
5c8716e
Change getProject in public-rest.ts to use projectAdapter
Nov 7, 2024
bb19e0c
Move RegisterRoutes to index.ts
Nov 7, 2024
5b63a08
Add sequelize-typescript
Nov 7, 2024
8122922
Implement getApps with ProjectPort interface
Nov 8, 2024
be7c8ac
Rename AppCategory to AppCategory name for consistency
Nov 8, 2024
4af7fab
Add Category table to implement getCategories in public-rest.ts
Nov 8, 2024
3097f09
Finnish moving public-rest.ts to ProjectPort interface
Nov 8, 2024
9ad5550
Rename ProjectPort to BadgeHubDataPort and adapter alike
Nov 8, 2024
4582fa7
Clean up old models
Nov 9, 2024
ad00fee
Add zip upload api
Nov 9, 2024
9b2a333
Clean up imports
Nov 10, 2024
c9cb5ed
Optimize all imports
Nov 10, 2024
4b491b6
Remove id's from domain models
Nov 12, 2024
2fa8260
Prefix all DB interfaces with DB for easier importing and less confus…
Nov 12, 2024
3585a1e
WIP (DB tables not ready yet, so not tested yet) implement getProject…
Nov 12, 2024
96b1fb0
automatically optimize imports
Nov 13, 2024
1af7524
Fixup removing id's from BadgeHubDataPort.ts
Nov 13, 2024
63863d2
Fix timestamp parsing and published_at
Nov 13, 2024
30c3998
Clean up unused ProjectRelation
Nov 13, 2024
6714f93
Change sql to lowerstring
Nov 13, 2024
62d0e42
change insertProject to upsertProject in order to allow updating othe…
Nov 13, 2024
300772b
WIP change db relations
Nov 13, 2024
9232d1d
Implement simple crud operations for project
Nov 16, 2024
1c54a7c
Remove google2fa fields
Nov 16, 2024
a5cc4f4
make Version_id optional in DBProject.ts
Nov 16, 2024
8d3768d
Add db-migrations and create projects table in first migration
Nov 16, 2024
7641b57
Rename MetadataFileContents to AppMetadataJSON
Nov 16, 2024
bed11b2
add all required tables for simple api
Nov 16, 2024
fa03dfd
/api/v3/apps not errorring
Nov 17, 2024
927b719
Move populate.ts to __test__/populateDB.ts
Nov 17, 2024
953e557
Extract config stuff into src/config.ts
Nov 17, 2024
b37501b
Fix props type for insertProject
Nov 17, 2024
c2cf7ad
Fix using parameterized keys
Nov 17, 2024
2719dd7
Insert user and project and getting it back via /projects working
Nov 17, 2024
0365c3c
remove aggregates dir and rename domain models to readModels
Nov 17, 2024
31db9cb
implement getBadges in BadgeHubDataPostgresAdapter.ts
Nov 17, 2024
f0e30af
make deleted_at default to null
Nov 17, 2024
1c2678c
Add default null to email_verified and published columns
Nov 17, 2024
273b8ec
Remove redundant default null from timestamps
Nov 17, 2024
6fe8a95
Fix deleted_at in BadgeHubDataPostgresAdapter.ts
Nov 17, 2024
f831fb4
Add writeProjectZip to BadgeHubDataPort.ts
Nov 17, 2024
66b12f4
Implement getProject
Nov 17, 2024
d9d25da
Add DELETE endpoint for project
Nov 17, 2024
2ba52be
Implement getVersions except for files and app_metadata
Nov 17, 2024
6a08759
Implement app_metadata property in getVersion
Nov 17, 2024
a63078f
Implement /categories getter
Nov 17, 2024
968451a
Create draft metadata and revision on creation of project
Nov 17, 2024
0f15859
Add (unimplemented) endpoints for file and zip downloading
Nov 17, 2024
b628c3a
Fixup insertProject, cleanup unused code
Nov 17, 2024
99d3f82
Prevent write api being used outside of development environment since…
Nov 17, 2024
cf5dba6
WIP updateDraftMetadata
Nov 18, 2024
7718d5a
WIP Add TODO for this ticket
Nov 18, 2024
63b0fb0
update
Nov 18, 2024
f1e8990
Add NODE_ENV=development to .env.example
Nov 18, 2024
43ce8c4
Improve Type namings in swagger
Nov 18, 2024
421bb3f
Add running db-migrate:up on start of server
Nov 19, 2024
9018efd
Update TODO-UPLOAD-API.md
Nov 19, 2024
132870b
undo servers change in swagger an
Nov 20, 2024
0f11f4a
Update TODO-UPLOAD-API.md
Nov 20, 2024
968b49e
Make operationId's match rest again
Nov 22, 2024
4072736
Make operationId's similar on private rest
Nov 22, 2024
2c0cc94
Change Project properties to match what we are expecting in the frontend
Nov 24, 2024
6947930
Add category slug back
Nov 24, 2024
0268ee5
WIP Add data migration to initialize-up.sql scripts
Nov 24, 2024
c995180
Fixup categorie relation
Nov 24, 2024
47d4271
Convert table data from old to new format
Nov 24, 2024
72ccbe4
Update TODO-UPLOAD-API.md regarding status cross table
Nov 25, 2024
82d4383
Fix bad version creation in migration
Nov 25, 2024
9bb846b
Fix category and name it as just category for the name in project and…
Nov 25, 2024
5c0492c
Add todo's for missing relation tables
Nov 25, 2024
ba1b783
Add versioned_dependencies table with migration
Nov 25, 2024
d7bf279
Add project_statuses_on_badges table
Nov 25, 2024
498ea55
Update mock data for new tables including migrations
Nov 25, 2024
045b4d3
Update TODO-UPLOAD-API.md
Nov 25, 2024
1d784f1
Fix getCategories
Nov 26, 2024
86dfeb4
Fix filtering on category and device and update TODO doc
Nov 26, 2024
fdb7a08
Implement adding badges to project requests
Nov 26, 2024
59ce14f
Clean up TODO doc
Nov 26, 2024
a916f0a
Add setupPopulateDBApi in case of development mode
Nov 27, 2024
b68281d
move setupPopulateDBApi.ts out of __test__
Nov 27, 2024
29b147b
Change user id back to number like other id's
Nov 27, 2024
b56c9eb
Extract objectToSQL.ts
Nov 27, 2024
ef416f2
fixup move setupPopulateDBApi
Nov 27, 2024
2b5643f
Fix populateDB
Nov 27, 2024
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
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ POSTGRES_PASSWORD=badgehub
PGADMIN_DEFAULT_PASSWORD=badgehub
[email protected]

JWT_SIGNING_KEY="!!HackThePlanet!!"
NODE_ENV=development

JWT_SIGNING_KEY="!!HackThePlanet!!"
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,56 @@

## - Development -

### Database Migrations

In order to make sure that we can keep track of the database schema, we use [db-migrate](https://db-migrate.readthedocs.io/en/latest/).
This allows us to track the database changes along with git and provide a framework for migrating data and rolling back changes.
So when a change to the database schema is needed, a new migration should be created instead of manually changing the database.
To create a new migration, follow the steps below.

#### Create a new migration

```bash
npm run db-migrate:create -- <migration-name>
```

This will create a new migration file in the `migrations` directory with the name `<timestamp>-<migration-name>.js` as well as 2 sql files, one for the up migration and one for the down migration.

#### Fill in the down and up migration sql files with the necessary changes to the database schema.

These sql commands should take care of changing the database schema as well as migrating the data if necessary.

#### Run the migration to test it.

```bash
npm run db-migrate:up
```

#### Create updated mock.sql

For the mock data, we always want up to date tables, so after you have done the migration on the mock data, you should re-export the database.
You can do this with the pg_dump command in the postgres container:

```bash
npm run overwrite-mockup-data
```

Note: you might have to change the db container name in the script. Eg on mac with podman its with underscores instead of dashes which makes the command:

```bash
docker exec -it badgehub-api_db_1 /usr/bin/pg_dump --username badgehub --schema badgehub badgehub > mockup-data.sql
```

#### Run the down migration to test it.

```bash
npm run db-migrate:down
```

#### Commit the migration files to git.

When the code is deployed, the up migrations will be run automatically before starting the server.

## Install

Make sure [Docker](https://www.docker.com/get-started/) is installed and running.
Expand Down Expand Up @@ -99,5 +149,7 @@ docker compose --file docker-compose-production.yml down

- [Express](https://expressjs.com/), a framework for Node.js
- [tsoa](https://tsoa-community.github.io/docs/) for generating a swagger file from code
- [sql-template-tag](https://github.com/blakeembrey/sql-template-tag) for more easily writing SQL queries
- [db-migrate](https://db-migrate.readthedocs.io/en/latest/) for database migrations
- [tsx](https://tsx.is/) for watching TypeScript files in Node.js
- [PM2](https://pm2.keymetrics.io/) for managing Node.js processes
12 changes: 12 additions & 0 deletions database.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"defaultEnv": "pg-from-env",
"pg-from-env": {
"driver": "pg",
"host": { "ENV": "POSTGRES_HOST" },
"database": { "ENV": "POSTGRES_DB" },
"user": { "ENV": "POSTGRES_USER" },
"password": { "ENV": "POSTGRES_PASSWORD" },
"port": "5432",
"schema": "badgehub"
}
}
59 changes: 59 additions & 0 deletions migrations/20241116085102-initialize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"use strict";

var dbm;
var type;
var seed;
var fs = require("fs");
var path = require("path");
var Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function (options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function (db) {
var filePath = path.join(
__dirname,
"sqls",
"20241116085102-initialize-up.sql"
);
return new Promise(function (resolve, reject) {
fs.readFile(filePath, { encoding: "utf-8" }, function (err, data) {
if (err) return reject(err);
console.log("received data: " + data);

resolve(data);
});
}).then(function (data) {
return db.runSql(data);
});
};

exports.down = function (db) {
var filePath = path.join(
__dirname,
"sqls",
"20241116085102-initialize-down.sql"
);
return new Promise(function (resolve, reject) {
fs.readFile(filePath, { encoding: "utf-8" }, function (err, data) {
if (err) return reject(err);
console.log("received data: " + data);

resolve(data);
});
}).then(function (data) {
return db.runSql(data);
});
};

exports._meta = {
version: 1,
};
3 changes: 3 additions & 0 deletions migrations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
4 changes: 4 additions & 0 deletions migrations/sqls/20241116085102-initialize-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- restore old schema
insert into badgehub_old.migrations select * from badgehub.migrations;
drop schema badgehub cascade;
alter schema badgehub_old rename to badgehub;
Loading
Loading