Skip to content

Conversation

@endersoncosta
Copy link
Contributor

With this we can write the relationship migrations based on our domain entities.

<%if (props.ref) {
props.ref.forEach(function(link){
if (link.relationship === 'One-to-One') { %>
// table.integer('<%-link.columnName %>').unsigned().nullable()
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment is necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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 ...

@jhomarolo
Copy link
Contributor

@endersoncosta
Copy link
Contributor Author

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
Copy link

codecov bot commented Jul 7, 2022

Codecov Report

Merging #144 (fb0b418) into main (56e99b7) will decrease coverage by 0.15%.
The diff coverage is 78.57%.

❗ Current head fb0b418 differs from pull request most recent head d8a12c6. Consider uploading reports for the commit d8a12c6 to get more accurate results

@@            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     
Impacted Files Coverage Δ
...rators/src/infra/database/migrations/migrations.js 88.46% <73.91%> (-3.65%) ⬇️
src/generators/utils.js 83.33% <100.00%> (+1.85%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us.

@dalssoft
Copy link
Member

dalssoft commented Jul 8, 2022

@endersoncosta about this generated code:

// table.integer('userId').unsigned().nullable()
// table.foreign('userId').references('id').inTable('users')

(1) why .unsigned()?
(2) why .nullable()?
(3) why commented code? the dev would have other options for this operation?
(4) just table.integer('userId').references('id').inTable('users') works?

about this one:

knex.schema.table('markets', function (table) {
    table.integer('companyId').unsigned().index().references('id').inTable('companys')
})

(5) why .index()? FKs are normally already indexed.

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 }
Copy link
Member

Choose a reason for hiding this comment

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

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' })
Copy link
Member

Choose a reason for hiding this comment

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

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' })
Copy link
Member

Choose a reason for hiding this comment

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

Both table and columns name should be snake_cases for DB to keep with the current standard.

ex: userId -> user_id

@endersoncosta
Copy link
Contributor Author

@endersoncosta about this generated code:

// table.integer('userId').unsigned().nullable()
// table.foreign('userId').references('id').inTable('users')

(1) why .unsigned()?
(2) why .nullable()?
(3) why commented code? the dev would have other options for this operation?
(4) just table.integer('userId').references('id').inTable('users') works?

about this one:

knex.schema.table('markets', function (table) {
    table.integer('companyId').unsigned().index().references('id').inTable('companys')
})

(5) why .index()? FKs are normally already indexed.

1. unsigned() indeed this is used in MySQL to define the column to has just positive numbers, I think it is a good practice, but doesn't make any sense in postgres.

  1. To the initial setup this really doesn't has reason, but when we change the structure of an existing table, this can be necessary...

  2. We can't garantee the execution order, if the dev run all the migrations at the same time, this will break the execution with errors.

  3. Indeed, it is better in that simpler way.

  4. Indeed.

@dalssoft
Copy link
Member

dalssoft commented Jul 27, 2022

the CLI created the default entity user.js:

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 user_id should be .string() to follow the same type as the entity.

the same is true for 1:many:

    .table('users', function (table) {
        table.integer('customer_id').references('id').inTable('customers')
    })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants