feat: including relationships in migrations#144
feat: including relationships in migrations#144endersoncosta wants to merge 9 commits intoherbsjs:mainfrom
Conversation
| <%if (props.ref) { | ||
| props.ref.forEach(function(link){ | ||
| if (link.relationship === 'One-to-One') { %> | ||
| // table.integer('<%-link.columnName %>').unsigned().nullable() |
There was a problem hiding this comment.
this comment is necessary?
There was a problem hiding this comment.
the idea are to not create a migration that does not work, due to not has an order of migrations when we write the files. These relationships throw errors due the dependence dont exist yet ...
|
Locally the tests are passing through. And other PRs apparently has the same problem from time to time. Is possible to re-run the CI workflow? |
Codecov Report
@@ Coverage Diff @@
## main #144 +/- ##
==========================================
- Coverage 86.28% 86.13% -0.16%
==========================================
Files 25 25
Lines 423 440 +17
==========================================
+ Hits 365 379 +14
- Misses 58 61 +3
Help us with your feedback. Take ten seconds to tell us how you rate us. |
|
@endersoncosta about this generated code: // table.integer('userId').unsigned().nullable()
// table.foreign('userId').references('id').inTable('users')(1) why about this one: knex.schema.table('markets', function (table) {
table.integer('companyId').unsigned().index().references('id').inTable('companys')
})(5) why |
| template: `infra/data/database/${db.toLowerCase()}/migration.ejs`, | ||
| target: migrationFullPath, | ||
| props: { table: `${camelCase(name)}s`, columns: columns } | ||
| props: { table: `${camelCase(name)}s`, externalColumnName: `${camelCase(name)}Id`, columns, ref, idColumn } |
There was a problem hiding this comment.
Both table and columns name should be snake_cases for DB to keep with the current standard.
ex: userId -> user_id
| } | ||
|
|
||
| if (isArrayWithType(type) && herbs.entity.isEntity(type[0])) | ||
| refs.push({ table: `${camelCase(type[0].name)}s`, relationship: 'One-to-Many' }) |
There was a problem hiding this comment.
Both table and columns name should be snake_cases for DB to keep with the current standard.
ex: userId -> user_id
| if (herbs.entity.isEntity(type)) { | ||
| const typeSchema = type.prototype.meta.schema | ||
| const idRef = Object.values(typeSchema).find(column => column.options.isId)?.name | ||
| refs.push({ id: idRef, columnName: `${camelCase(name)}Id`, table: `${camelCase(type.name)}s`, relationship: 'One-to-One' }) |
There was a problem hiding this comment.
Both table and columns name should be snake_cases for DB to keep with the current standard.
ex: userId -> user_id
1.
|
pull main
|
the CLI created the default entity const User =
entity('User', {
id: id(String),
...
})when adding an entity with a reference: const Project =
entity('Project', {
id: id(String),
user: field(User),
})the generated migration: .createTable('projects', function (table) {
table.string('id').primary()
table.integer('user_id').references('id').inTable('users')here the the same is true for 1:many: .table('users', function (table) {
table.integer('customer_id').references('id').inTable('customers')
}) |
With this we can write the relationship migrations based on our domain entities.