Issue
I want to replace
@Column("uuid", {
primary: true,
name: "id",
default: () => "uuid_generate_v4()",
})
with
@PrimaryGeneratedColumn
in a file with the following content:
export class SomeClass {
@Column("uuid", {
primary: true,
name: "id",
default: () => "uuid_generate_v4()",
})
id: string;
@Column("timestamp with time zone", {
name: "created_at",
default: () => "now()",
})
created_at: Date;
@Column("timestamp with time zone", {
name: "updated_at",
default: () => "now()",
})
updated_at: Date;
@Column("uuid", { name: "other_id", unique: true })
other_id: string;
}
This is my current solution:
sed -E -i '' -e '/@Column\(\"uuid\", \{/,/\}\)?/c @PrimaryGeneratedColumn\(\"uuid\"\)' fileName
It basically works, but it's too inaccurate and also replaces other lines that shouldn't be replaced. I tried to add uuid_generate_v4()
but couldn't get it to work, probably because of the line breaks.
Solution
This might work for you (GNU sed):
sed '/@Column("uuid"/{:a;N;/^\s*})$/M!ba;/"uuid_generate_v4()/s/.*/@PrimaryGeneratedColum/}' file
Gather up lines between those containing @Column("uuid"
and })
and if those lines also contain uid_generate_v4()
, replace all them by PrimaryGeneratedColum
.
Answered By - potong Answer Checked By - Marilyn (WPSolving Volunteer)