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

Feat: Add created_at, created_at to workspace_schedule and workspaces_user #418

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 33 additions & 0 deletions apps/workspaces/migrations/0041_auto_20241223_1102.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 3.2.14 on 2024-12-23 11:02

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('workspaces', '0040_workspacegeneralsettings_memo_structure'),
]

operations = [
migrations.AddField(
model_name='workspaceschedule',
name='created_at',
field=models.DateTimeField(auto_now_add=True, help_text='Created at datetime', null=True),
),
migrations.AddField(
model_name='workspaceschedule',
name='updated_at',
field=models.DateTimeField(auto_now=True, help_text='Updated at datetime', null=True),
),
migrations.RunSQL(
sql="""
ALTER TABLE workspaces_user
ADD COLUMN created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW();
""",
reverse_sql="""
ALTER TABLE workspaces_user
DROP COLUMN created_at;
""",
),
]
2 changes: 2 additions & 0 deletions apps/workspaces/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ class WorkspaceSchedule(models.Model):
null=True,
help_text="Emails that has to be send mail",
)
created_at = models.DateTimeField(auto_now_add=True, null=True, help_text='Created at datetime')
updated_at = models.DateTimeField(auto_now=True, null=True, help_text='Updated at datetime')

class Meta:
db_table = "workspace_schedules"
Expand Down
32 changes: 19 additions & 13 deletions tests/sql_fixtures/reset_db_fixtures/reset_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,9 @@ CREATE TABLE public.workspace_schedules (
workspace_id integer NOT NULL,
additional_email_options jsonb,
emails_selected character varying(255)[],
error_count integer
error_count integer,
created_at timestamp with time zone,
updated_at timestamp with time zone
Comment on lines +1558 to +1560
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add default values for timestamp columns

The created_at and updated_at columns should have default values to ensure data consistency:

  • created_at should default to now()
  • updated_at should either default to now() or be updated via a trigger

Consider this modification:

-    created_at timestamp with time zone,
-    updated_at timestamp with time zone
+    created_at timestamp with time zone DEFAULT now() NOT NULL,
+    updated_at timestamp with time zone DEFAULT now() NOT NULL

Also, consider adding a trigger to automatically update updated_at:

CREATE TRIGGER update_workspace_schedules_timestamp
    BEFORE UPDATE ON workspace_schedules
    FOR EACH ROW
    EXECUTE FUNCTION update_timestamp();

);


Expand Down Expand Up @@ -1615,7 +1617,8 @@ ALTER SEQUENCE public.workspaces_id_seq OWNED BY public.workspaces.id;
CREATE TABLE public.workspaces_user (
id integer NOT NULL,
workspace_id integer NOT NULL,
user_id integer NOT NULL
user_id integer NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL
);


Expand Down Expand Up @@ -2645,6 +2648,8 @@ COPY public.django_migrations (id, app, name, applied) FROM stdin;
158 workspaces 0039_alter_workspacegeneralsettings_change_accounting_period 2024-11-18 04:43:45.472917+00
159 fyle 0022_support_split_expense_grouping 2024-11-18 10:49:49.550689+00
160 workspaces 0040_workspacegeneralsettings_memo_structure 2024-12-03 21:13:46.617079+00
161 fyle_accounting_mappings 0027_alter_employeemapping_source_employee 2024-12-23 11:03:59.013177+00
162 workspaces 0041_auto_20241223_1102 2024-12-23 11:03:59.043589+00
\.


Expand Down Expand Up @@ -5077,7 +5082,7 @@ COPY public.workspace_general_settings (id, reimbursable_expenses_object, corpor
-- Data for Name: workspace_schedules; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.workspace_schedules (id, enabled, start_datetime, interval_hours, schedule_id, workspace_id, additional_email_options, emails_selected, error_count) FROM stdin;
COPY public.workspace_schedules (id, enabled, start_datetime, interval_hours, schedule_id, workspace_id, additional_email_options, emails_selected, error_count, created_at, updated_at) FROM stdin;
\.


Expand All @@ -5094,8 +5099,8 @@ COPY public.workspaces (id, name, fyle_org_id, last_synced_at, created_at, updat
-- Data for Name: workspaces_user; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.workspaces_user (id, workspace_id, user_id) FROM stdin;
1 1 1
COPY public.workspaces_user (id, workspace_id, user_id, created_at) FROM stdin;
1 1 1 2024-12-23 11:03:59.028742+00
\.


Expand Down Expand Up @@ -5182,7 +5187,7 @@ SELECT pg_catalog.setval('public.django_content_type_id_seq', 40, true);
-- Name: django_migrations_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--

SELECT pg_catalog.setval('public.django_migrations_id_seq', 160, true);
SELECT pg_catalog.setval('public.django_migrations_id_seq', 162, true);


--
Expand Down Expand Up @@ -5596,6 +5601,14 @@ ALTER TABLE ONLY public.employee_mappings
ADD CONSTRAINT employee_mappings_pkey PRIMARY KEY (id);


--
-- Name: employee_mappings employee_mappings_source_employee_id_dd9948ba_uniq; Type: CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.employee_mappings
ADD CONSTRAINT employee_mappings_source_employee_id_dd9948ba_uniq UNIQUE (source_employee_id);


--
-- Name: errors errors_expense_attribute_id_key; Type: CONSTRAINT; Schema: public; Owner: postgres
--
Expand Down Expand Up @@ -6145,13 +6158,6 @@ CREATE INDEX employee_mappings_destination_employee_id_b6764819 ON public.employ
CREATE INDEX employee_mappings_destination_vendor_id_c4bd73df ON public.employee_mappings USING btree (destination_vendor_id);


--
-- Name: employee_mappings_source_employee_id_dd9948ba; Type: INDEX; Schema: public; Owner: postgres
--

CREATE INDEX employee_mappings_source_employee_id_dd9948ba ON public.employee_mappings USING btree (source_employee_id);


--
-- Name: employee_mappings_workspace_id_4a25f8c9; Type: INDEX; Schema: public; Owner: postgres
--
Expand Down
Loading