Currently I interact with specific table in database through ActiveRecord. I’ll know the table name in advance and I’ll create table by rails generate model modelname column1:string ....
But now, I get this requirement that the table name is unknown as well as how many tables there will be.
For example : In directory Projects, there’re many sub directories such as : Projects_A, Project_B, Project_C … I need to create separate tables for each of these projects. But you don’t know how many project there’ll be. And I need to dynamically create these tables in my code. How to achieve that in my code? Do I use ActiveRecord to achieve that or any other methods?
Codes like:
list = maintain_a_dir_list()
Dir.entries( dir ).each { |e|
if e is not in list
create_a_table_after_e_name
end
}
Here I will explain why I want to create such tables :
The directory name and its sub folder name I mentioned above is unreal. So let’s told this story all over again.
I am a load tester. I do load test for one of our company’s projects. Let’s say the project’s name is Project_A. Project_A has many versions, like 1.0, 2.0 and so on. So in every release I’ll do many rounds of load test to ensure the product’s quality.
So the relationship between project, verson and loadtest report is simple : project contains many versions. And each version contains many loadtest reports which executed in different days.
The information in loadtest report is as following :
transaction name | min time | average response time | max time
transaction_01 | 1.0s | 2.0s | 3.0s
transaction_02 | 0.5s | 1.5s | 4.0s
…
To store the reports information in database, I create a table named transactions_table. The table is like this :
report_name | transaction_name | min | average | max
20121001_100users_4hours | transaction_01 | 1.0 | 2.0 | 3.0
20121001_100users_4hours | transaction_02 | 1.0 | 2.0 | 3.0
…
20121002_100users_2hours | transaction_01 | 1.5 | 2.1 | 2.5
20121002_100users_2hours | transaction_02 | 1.1 | 2.1 | 2.7
…
…
Then I want to get the version information involved. So I create another table named reports_table. The table is like this :
version | report_name
project_a_1.0 | 20121001_100users_4hours
project_a_1.0 | 20121002_100users_2hours
…
project_a_2.0 | 20121101_50users_1hours
project_a_2.0 | 20121102_50users_2hours
…
…
And ofcourse there’s more than one project in my company. There’ll be Project_B, Project_C and D… So again comes the version table consist of the Project name and its versions.
It’s the first time I design the relationship of DB tables. So I don’t know whether the way i organize table is right.
Firstly, I simply think I can put all the transaction data–no matter which project or version it belongs–in transactions_table. Then I realize the report name from different projects or versions may be the same. Then it cannot seperate transactions_table using select * from transactions_table where report_name = "xxxx". And the transactions_table‘s size will increase more rapidly than the other two tables.
So that’s why I am considering to store the reports in different projects to different table.
May better to use folder_name field in projects table?