Skip to content

Commit 2c226a9

Browse files
authored
feature/add-ability-to-perform-partail-work
Add ability to perform partail work
2 parents 79d33e1 + b693ebf commit 2c226a9

12 files changed

Lines changed: 122 additions & 108 deletions

File tree

.formatter.exs

Lines changed: 0 additions & 4 deletions
This file was deleted.

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ csv2sql-*.tar
2828
# schema file
2929
schema.sql
3030

31-
3231
# config file
3332
/config.env
33+
34+
# Formatting file
35+
.formatter.exs
36+
37+
# escipt binary
38+
csv2sql

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Csv2 sql automatically...
4343

4444
Download the [csv2sql executable](https://github.com/Arpan-Kreeti/csv2sql/blob/master/executable/csv2sql.zip).
4545

46-
Extract the zip and follow the instructions in the ```readme.md``` file
46+
Extract the zip and follow the instructions in the ```read me.md``` file
4747

4848
### From repository
4949

@@ -55,13 +55,21 @@ Once that is done, source the environment variables by ```source ./config.env.sa
5555

5656
Then start the application by ```iex -S mix```
5757

58+
Execute the followinf function to begin the process ```Csv2sql.main(nil)```
59+
5860
Thats all !
5961

62+
### Troubleshooting
6063

61-
## Further improvements or features that might come...
64+
In case you face datatrucation errors or other issues when inserting data, delete the database in which
65+
csv2sql inserts data, so that it will be recreated with correct encoding and collation next time.
66+
By default csv2sql uses ```utf8mb4``` character set and ```utf8mb4_general_ci``` collation when
67+
creating a database.
6268

63-
* Make a standalone executable of the application
69+
If you face database connection timeout errors try reducing the worker and db_worker count in the configurations
70+
or change the database timeout, pool size and other related database configurations.
6471

65-
* Support other popular databases like postgres, mssql server, etc
6672

67-
* Allow users to perform partial tasks only like creating schema, inserting data or validating data.
73+
## Further improvements or features to expect...
74+
75+
* Support other popular databases like postgres, mssql server, etc

config.env.sample

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ export csv2sql_imported_csv_directory=?
1616
# The directory were the csvs will be moved after they are validated, make sure it is present and is empty
1717
export csv2sql_validated_csv_directory=?
1818

19+
# Set whether you want to make schema file
20+
export csv2sql_set_make_schema="true"
21+
22+
# Set whether you want to insert the schema file that is generated (this option will only work if set_make_schema="true")
23+
export csv2sql_set_insert_schema="true"
24+
25+
# Set whether to insert the CSVs into the database
26+
export csv2sql_set_insert_data="true"
27+
28+
# Set whether to validate if the csvs have been inserted correctly
29+
export csv2sql_set_validate="true"
30+
1931
# mysql username
2032
export csv2sql_username=?
2133

config/config.exs

Lines changed: 0 additions & 70 deletions
This file was deleted.

executable/csv2sql.zip

674 Bytes
Binary file not shown.

lib/csv2sql.ex

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Csv2sql do
2-
def main(args \\ []) do
2+
def main(_args \\ []) do
3+
Csv2sql.Helpers.greet()
34
# Load configuration varaibles dynamically for escripts, this is required
45
# since configuration variables are set to whatever they where when the
56
# escript was build and cannot be changed later
@@ -29,7 +30,18 @@ defmodule Csv2sql do
2930
db_worker_count: System.get_env("csv2sql_db_worker_count") |> String.to_integer(),
3031
source_csv_directory: System.get_env("csv2sql_source_csv_directory"),
3132
imported_csv_directory: System.get_env("csv2sql_imported_csv_directory"),
32-
validated_csv_directory: System.get_env("csv2sql_validated_csv_directory")
33+
validated_csv_directory: System.get_env("csv2sql_validated_csv_directory"),
34+
set_validate:
35+
if(System.get_env("csv2sql_set_validate") == "true", do: true, else: false)
36+
]},
37+
{Csv2sql.Worker,
38+
[
39+
set_make_schema:
40+
if(System.get_env("csv2sql_set_make_schema") == "true", do: true, else: false),
41+
set_insert_schema:
42+
if(System.get_env("csv2sql_set_insert_schema") == "true", do: true, else: false),
43+
set_insert_data:
44+
if(System.get_env("csv2sql_set_insert_data") == "true", do: true, else: false)
3345
]},
3446
{Csv2sql.Repo,
3547
[

lib/csv2sql/application.ex

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,29 @@ defmodule Csv2sql.Application do
22
use Application
33

44
def start(_type, _args) do
5-
children = [
6-
{Csv2sql.TimerServer, Time.utc_now()},
7-
Csv2sql.Repo,
8-
Csv2sql.FileServer,
9-
Csv2sql.JobQueueServer,
10-
Csv2sql.DbWorkerSupervisor,
11-
Csv2sql.WorkerSupervisor,
12-
Csv2sql.MainServer
13-
]
5+
set_validate = Application.get_env(:csv2sql, Csv2sql.MainServer)[:set_validate]
6+
set_insert_schema = Application.get_env(:csv2sql, Csv2sql.Worker)[:set_insert_schema]
7+
set_insert_data = Application.get_env(:csv2sql, Csv2sql.Worker)[:set_insert_data]
8+
9+
repo_supervisor =
10+
if set_validate || set_insert_schema || set_insert_data do
11+
[Csv2sql.Repo]
12+
else
13+
[]
14+
end
15+
16+
children =
17+
[
18+
{Csv2sql.TimerServer, Time.utc_now()}
19+
]
20+
|> Kernel.++(repo_supervisor)
21+
|> Kernel.++([
22+
Csv2sql.FileServer,
23+
Csv2sql.JobQueueServer,
24+
Csv2sql.DbWorkerSupervisor,
25+
Csv2sql.WorkerSupervisor,
26+
Csv2sql.MainServer
27+
])
1428

1529
opts = [strategy: :one_for_one, name: Csv2sql.Supervisor]
1630
Supervisor.start_link(children, opts)

lib/csv2sql/database.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ defmodule Csv2sql.Database do
5252

5353
Ecto.Adapters.SQL.query!(
5454
Repo,
55-
"SET GLOBAL SQL_MODE=\"NO_BACKSLASH_ESCAPES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_IN_DATE\";",
55+
"SET GLOBAL SQL_MODE=\"NO_BACKSLASH_ESCAPES,NO_ENGINE_SUBSTITUTION,NO_ZERO_IN_DATE\";",
5656
[]
5757
)
5858
end

lib/csv2sql/main_server.ex

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@ defmodule Csv2sql.MainServer do
2121
end
2222

2323
def handle_info(:kickoff, worker_count) do
24-
Csv2sql.Helpers.greet()
24+
set_insert_schema = Application.get_env(:csv2sql, Csv2sql.Worker)[:set_insert_schema]
25+
set_insert_data = Application.get_env(:csv2sql, Csv2sql.Worker)[:set_insert_data]
2526

26-
Csv2sql.Database.prepare_db()
27+
if set_insert_schema || set_insert_data do
28+
Csv2sql.Database.prepare_db()
29+
end
2730

2831
schema_file_path = Application.get_env(:csv2sql, Csv2sql.MainServer)[:db_worker_count]
2932
db_worker_count = Application.get_env(:csv2sql, Csv2sql.MainServer)[:db_worker_count]
3033

31-
File.rm("#{schema_file_path}/schema.sql")
34+
if Application.get_env(:csv2sql, Csv2sql.Worker)[:set_make_schema] do
35+
File.rm("#{schema_file_path}/schema.sql")
36+
end
3237

3338
1..worker_count
3439
|> Enum.each(fn _ -> Csv2sql.WorkerSupervisor.add_worker() end)
@@ -40,6 +45,8 @@ defmodule Csv2sql.MainServer do
4045
end
4146

4247
def handle_cast(:done, 1) do
48+
set_validate = Application.get_env(:csv2sql, Csv2sql.MainServer)[:set_validate]
49+
4350
wait_for_pending_jobs()
4451

4552
:timer.sleep(2000)
@@ -65,12 +72,16 @@ defmodule Csv2sql.MainServer do
6572
end
6673
)
6774

68-
Csv2sql.Helpers.print_msg("\nValidation Process Started...\n\n", :green)
75+
if(set_validate) do
76+
Csv2sql.Helpers.print_msg("\nValidation Process Started...\n\n", :green)
6977

70-
imported_csv_directory =
71-
Application.get_env(:csv2sql, Csv2sql.MainServer)[:imported_csv_directory]
78+
imported_csv_directory =
79+
Application.get_env(:csv2sql, Csv2sql.MainServer)[:imported_csv_directory]
7280

73-
Csv2sql.ImportValidator.validate_import(imported_csv_directory)
81+
Csv2sql.ImportValidator.validate_import(imported_csv_directory)
82+
else
83+
Csv2sql.Helpers.print_msg("\nValidation Process Skipped...\n\n", :green)
84+
end
7485

7586
Csv2sql.TimerServer.get_time_spend()
7687

0 commit comments

Comments
 (0)