Skip to content

Commit

Permalink
fix(schema): improve diffing of nullable columns (#508)
Browse files Browse the repository at this point in the history
Closes #507
  • Loading branch information
B4nan committed Apr 23, 2020
1 parent 8bad732 commit 166a29f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/schema/SchemaHelper.ts
Expand Up @@ -210,7 +210,7 @@ export abstract class SchemaHelper {
}

if (info.defaultValue === null || info.defaultValue.toLowerCase() === 'null' || info.defaultValue.toString().startsWith('nextval(')) {
return prop.default === undefined;
return !Utils.isDefined(prop.default, true);
}

if (prop.type === 'boolean') {
Expand Down
12 changes: 6 additions & 6 deletions tests/__snapshots__/SchemaGenerator.test.ts.snap
Expand Up @@ -4,7 +4,7 @@ exports[`SchemaGenerator generate schema from metadata [mysql]: mysql-create-sch
"set names utf8;
set foreign_key_checks = 0;
create table \`author2\` (\`id\` int unsigned not null auto_increment primary key, \`created_at\` datetime(3) not null default current_timestamp(3), \`updated_at\` datetime(3) not null default current_timestamp(3), \`name\` varchar(255) not null, \`email\` varchar(255) not null, \`age\` int(11) null, \`terms_accepted\` tinyint(1) not null default false, \`optional\` tinyint(1) null, \`identities\` json null, \`born\` date null, \`born_time\` time null, \`favourite_book_uuid_pk\` varchar(36) null, \`favourite_author_id\` int(11) unsigned null) default character set utf8 engine = InnoDB;
create table \`author2\` (\`id\` int unsigned not null auto_increment primary key, \`created_at\` datetime(3) not null default current_timestamp(3), \`updated_at\` datetime(3) not null default current_timestamp(3), \`name\` varchar(255) not null, \`email\` varchar(255) not null, \`age\` int(11) null default null, \`terms_accepted\` tinyint(1) not null default false, \`optional\` tinyint(1) null, \`identities\` json null, \`born\` date null, \`born_time\` time null, \`favourite_book_uuid_pk\` varchar(36) null, \`favourite_author_id\` int(11) unsigned null) default character set utf8 engine = InnoDB;
alter table \`author2\` add index \`custom_email_index_name\`(\`email\`);
alter table \`author2\` add unique \`custom_email_unique_name\`(\`email\`);
alter table \`author2\` add index \`author2_terms_accepted_index\`(\`terms_accepted\`);
Expand Down Expand Up @@ -190,7 +190,7 @@ drop table if exists \`book_to_tag_unordered\`;
drop table if exists \`publisher2_to_test2\`;
drop table if exists \`user2_to_car2\`;
create table \`author2\` (\`id\` int unsigned not null auto_increment primary key, \`created_at\` datetime(3) not null default current_timestamp(3), \`updated_at\` datetime(3) not null default current_timestamp(3), \`name\` varchar(255) not null, \`email\` varchar(255) not null, \`age\` int(11) null, \`terms_accepted\` tinyint(1) not null default false, \`optional\` tinyint(1) null, \`identities\` json null, \`born\` date null, \`born_time\` time null, \`favourite_book_uuid_pk\` varchar(36) null, \`favourite_author_id\` int(11) unsigned null) default character set utf8 engine = InnoDB;
create table \`author2\` (\`id\` int unsigned not null auto_increment primary key, \`created_at\` datetime(3) not null default current_timestamp(3), \`updated_at\` datetime(3) not null default current_timestamp(3), \`name\` varchar(255) not null, \`email\` varchar(255) not null, \`age\` int(11) null default null, \`terms_accepted\` tinyint(1) not null default false, \`optional\` tinyint(1) null, \`identities\` json null, \`born\` date null, \`born_time\` time null, \`favourite_book_uuid_pk\` varchar(36) null, \`favourite_author_id\` int(11) unsigned null) default character set utf8 engine = InnoDB;
alter table \`author2\` add index \`custom_email_index_name\`(\`email\`);
alter table \`author2\` add unique \`custom_email_unique_name\`(\`email\`);
alter table \`author2\` add index \`author2_terms_accepted_index\`(\`terms_accepted\`);
Expand Down Expand Up @@ -344,7 +344,7 @@ exports[`SchemaGenerator generate schema from metadata [postgres]: postgres-crea
"set names 'utf8';
set session_replication_role = 'replica';
create table \\"author2\\" (\\"id\\" serial primary key, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"updated_at\\" timestamptz(3) not null default current_timestamp(3), \\"name\\" varchar(255) not null, \\"email\\" varchar(255) not null, \\"age\\" int4 null, \\"terms_accepted\\" bool not null default false, \\"optional\\" bool null, \\"identities\\" json null, \\"born\\" date null, \\"born_time\\" time(0) null, \\"favourite_book_uuid_pk\\" varchar(36) null, \\"favourite_author_id\\" int4 null);
create table \\"author2\\" (\\"id\\" serial primary key, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"updated_at\\" timestamptz(3) not null default current_timestamp(3), \\"name\\" varchar(255) not null, \\"email\\" varchar(255) not null, \\"age\\" int4 null default null, \\"terms_accepted\\" bool not null default false, \\"optional\\" bool null, \\"identities\\" json null, \\"born\\" date null, \\"born_time\\" time(0) null, \\"favourite_book_uuid_pk\\" varchar(36) null, \\"favourite_author_id\\" int4 null);
create index \\"custom_email_index_name\\" on \\"author2\\" (\\"email\\");
alter table \\"author2\\" add constraint \\"custom_email_unique_name\\" unique (\\"email\\");
create index \\"author2_terms_accepted_index\\" on \\"author2\\" (\\"terms_accepted\\");
Expand Down Expand Up @@ -483,7 +483,7 @@ drop table if exists \\"book2_to_book_tag2\\" cascade;
drop table if exists \\"book_to_tag_unordered\\" cascade;
drop table if exists \\"publisher2_to_test2\\" cascade;
create table \\"author2\\" (\\"id\\" serial primary key, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"updated_at\\" timestamptz(3) not null default current_timestamp(3), \\"name\\" varchar(255) not null, \\"email\\" varchar(255) not null, \\"age\\" int4 null, \\"terms_accepted\\" bool not null default false, \\"optional\\" bool null, \\"identities\\" json null, \\"born\\" date null, \\"born_time\\" time(0) null, \\"favourite_book_uuid_pk\\" varchar(36) null, \\"favourite_author_id\\" int4 null);
create table \\"author2\\" (\\"id\\" serial primary key, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"updated_at\\" timestamptz(3) not null default current_timestamp(3), \\"name\\" varchar(255) not null, \\"email\\" varchar(255) not null, \\"age\\" int4 null default null, \\"terms_accepted\\" bool not null default false, \\"optional\\" bool null, \\"identities\\" json null, \\"born\\" date null, \\"born_time\\" time(0) null, \\"favourite_book_uuid_pk\\" varchar(36) null, \\"favourite_author_id\\" int4 null);
create index \\"custom_email_index_name\\" on \\"author2\\" (\\"email\\");
alter table \\"author2\\" add constraint \\"custom_email_unique_name\\" unique (\\"email\\");
create index \\"author2_terms_accepted_index\\" on \\"author2\\" (\\"terms_accepted\\");
Expand Down Expand Up @@ -853,7 +853,7 @@ exports[`SchemaGenerator update empty schema from metadata [mysql]: mysql-update
"set names utf8;
set foreign_key_checks = 0;
create table \`author2\` (\`id\` int unsigned not null auto_increment primary key, \`created_at\` datetime(3) not null default current_timestamp(3), \`updated_at\` datetime(3) not null default current_timestamp(3), \`name\` varchar(255) not null, \`email\` varchar(255) not null, \`age\` int(11) null, \`terms_accepted\` tinyint(1) not null default false, \`optional\` tinyint(1) null, \`identities\` json null, \`born\` date null, \`born_time\` time null, \`favourite_book_uuid_pk\` varchar(36) null, \`favourite_author_id\` int(11) unsigned null) default character set utf8 engine = InnoDB;
create table \`author2\` (\`id\` int unsigned not null auto_increment primary key, \`created_at\` datetime(3) not null default current_timestamp(3), \`updated_at\` datetime(3) not null default current_timestamp(3), \`name\` varchar(255) not null, \`email\` varchar(255) not null, \`age\` int(11) null default null, \`terms_accepted\` tinyint(1) not null default false, \`optional\` tinyint(1) null, \`identities\` json null, \`born\` date null, \`born_time\` time null, \`favourite_book_uuid_pk\` varchar(36) null, \`favourite_author_id\` int(11) unsigned null) default character set utf8 engine = InnoDB;
alter table \`author2\` add index \`custom_email_index_name\`(\`email\`);
alter table \`author2\` add unique \`custom_email_unique_name\`(\`email\`);
alter table \`author2\` add index \`author2_terms_accepted_index\`(\`terms_accepted\`);
Expand Down Expand Up @@ -991,7 +991,7 @@ exports[`SchemaGenerator update empty schema from metadata [postgres]: postgres-
"set names 'utf8';
set session_replication_role = 'replica';
create table \\"author2\\" (\\"id\\" serial primary key, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"updated_at\\" timestamptz(3) not null default current_timestamp(3), \\"name\\" varchar(255) not null, \\"email\\" varchar(255) not null, \\"age\\" int4 null, \\"terms_accepted\\" bool not null default false, \\"optional\\" bool null, \\"identities\\" json null, \\"born\\" date null, \\"born_time\\" time(0) null, \\"favourite_book_uuid_pk\\" varchar(36) null, \\"favourite_author_id\\" int4 null);
create table \\"author2\\" (\\"id\\" serial primary key, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"updated_at\\" timestamptz(3) not null default current_timestamp(3), \\"name\\" varchar(255) not null, \\"email\\" varchar(255) not null, \\"age\\" int4 null default null, \\"terms_accepted\\" bool not null default false, \\"optional\\" bool null, \\"identities\\" json null, \\"born\\" date null, \\"born_time\\" time(0) null, \\"favourite_book_uuid_pk\\" varchar(36) null, \\"favourite_author_id\\" int4 null);
create index \\"custom_email_index_name\\" on \\"author2\\" (\\"email\\");
alter table \\"author2\\" add constraint \\"custom_email_unique_name\\" unique (\\"email\\");
create index \\"author2_terms_accepted_index\\" on \\"author2\\" (\\"terms_accepted\\");
Expand Down
2 changes: 1 addition & 1 deletion tests/entities-sql/Author2.ts
Expand Up @@ -29,7 +29,7 @@ export class Author2 extends BaseEntity2 {
@Index({ name: 'custom_email_index_name' })
email: string;

@Property({ nullable: true })
@Property({ nullable: true, default: null })
age?: number;

@Index()
Expand Down

0 comments on commit 166a29f

Please sign in to comment.