Best way to pre-populate a database on start-up

171 visualizzazioni
Passa al primo messaggio da leggere

Ben Lawrence

da leggere,
30 ago 2016, 09:34:1730/08/16
a web2py-users
At the moment to pre-populate a database, I place this in the db.py model file:

db.define_table('color',
    Field('name','string'),
    Field('code','string'),
    format='%(name)s')
if db(db.color).isempty():
    # create instance of table
    for k,v in dict(lime="#00FF00",red="#FF0000",black="#000000",orange="#FFA500",\
        blue="#0000FF", yellow="#FFFF00", fuchsia="#FF00FF", aqua="#00FFFF",\
        maroon="#800000", green="#008000", navy="#000080", olive="#808000",\
        purple="#800080", teal="#008080", gray="#808080", silver="#C0C0C0").items():
        db.color.insert(name=k,code=v)



I wonder if this is the best way to pre-populate a database?

thanks,
Ben

Richard Vézina

da leggere,
30 ago 2016, 09:58:4330/08/16
a web2py-users
Seems alright do you have issue with this? I would put a blank line between model definition and the "if isempty()".

I production, once the system get initialize you should remove those populating fixture to save execution time as the get execute at each request.

What could wizely do is put all the populating fixtures in a models file or controller file function that you delete or comment out once you populate you empty new database instance. Putting it in a function controller file wouldn't requires you to delete them as you will never call this controller file later on and if can by mistake it will not do anything. But depend of the size  fo the populated data set the call of a function could lead to a time out from the web server which prevent completion of populating data. So you have to evaluate if it has chance or not to complete properly. You can split the populating process into a couple of function call so yo don't reach time out.

Good luck

Richard



--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yoel Benitez Fonseca

da leggere,
30 ago 2016, 09:58:4330/08/16
a web2py
You can use a .CSV file. What i have used to do is:

1.- Define the tables that are being pre-populate.
2.- Insert all the data with appadmin.
3.- Using the appadmin export the data to a .CSV (db_colors.csv) file
and put it in the private folder of your app.
4.- Do the same thing you are doing except for:

db.color.import_from_csv_file(
open(os.path.join(request.folder, os.path.join('private',
'db_colors.csv')), 'r')
)

5.- done.
> --
> Resources:
> - http://web2py.com
> - http://web2py.com/book (Documentation)
> - http://github.com/web2py/web2py (Source code)
> - https://code.google.com/p/web2py/issues/list (Report Issues)
> ---
> You received this message because you are subscribed to the Google Groups
> "web2py-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to web2py+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Yoel Benítez Fonseca
http://redevil.cubava.cu/
$ python -c "import this"

Dave S

da leggere,
30 ago 2016, 14:24:5330/08/16
a web2py-users


On Tuesday, August 30, 2016 at 6:58:43 AM UTC-7, Richard wrote:
Seems alright do you have issue with this? I would put a blank line between model definition and the "if isempty()".

I production, once the system get initialize you should remove those populating fixture to save execution time as the get execute at each request.

What could wizely do is put all the populating fixtures in a models file or controller file function that you delete or comment out once you populate you empty new database instance. Putting it in a function controller file wouldn't requires you to delete them as you will never call this controller file later on and if can by mistake it will not do anything. But depend of the size  fo the populated data set the call of a function could lead to a time out from the web server which prevent completion of populating data. So you have to evaluate if it has chance or not to complete properly. You can split the populating process into a couple of function call so yo don't reach time out.


Or give the function a dummy argument
def func(dummy=True):
   
...


 and then invoke it with -S from the command line.  This parallels Niphlod's advice for setting up the initial copy of a recurring task.  (The dummy arg keeps the function from being exposed as a URL.)

Good luck

Richard


/dps

 


On Tue, Aug 30, 2016 at 9:34 AM, Ben Lawrence <benla...@gmail.com> wrote:
At the moment to pre-populate a database, I place this in the db.py model file:

db.define_table('color',
    Field('name','string'),
    Field('code','string'),
    format='%(name)s')
if db(db.color).isempty():
    # create instance of table
    for k,v in dict(lime="#00FF00",red="#FF0000",black="#000000",orange="#FFA500",\
        blue="#0000FF", yellow="#FFFF00", fuchsia="#FF00FF", aqua="#00FFFF",\
        maroon="#800000", green="#008000", navy="#000080", olive="#808000",\
        purple="#800080", teal="#008080", gray="#808080", silver="#C0C0C0").items():
        db.color.insert(name=k,code=v)



I wonder if this is the best way to pre-populate a database?

thanks,
Ben

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.

Niphlod

da leggere,
30 ago 2016, 15:40:2930/08/16
a web2py-users
+1 for Dave .

with isempty() in a model you're executing a query for each and every request for absolutely NO REASON.

This are activities that needs to be carried on on a post-deployment fire-only-when-needed scenario, and outside the web environment.

Richard Vézina

da leggere,
30 ago 2016, 15:53:1830/08/16
a web2py-users
Don't have privileges, but this is a good thread and should be marked as informative somehow...

Richard

To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.

Richard Vézina

da leggere,
30 ago 2016, 15:54:2630/08/16
a web2py-users
To be added to the fabfile.py of web2py maybe?!

Niphlod

da leggere,
30 ago 2016, 17:48:0130/08/16
a web2py-users
fabfile ATM is a show-off, and we can't put in there code related to an app which doesn't exist :P
We can't pin each and every thread a "useful" information is written, as I really don't see how 40 pinned threads will contribute to web2py's knowledge for - and especially - newbies.


IMHO at most it should land on the book (deployment chapter), which everyone is highly encouraged to contribute, and doesn't need any python-web2py superpower to do so.

Dave S

da leggere,
30 ago 2016, 19:27:5330/08/16
a web2py-users


On Tuesday, August 30, 2016 at 2:48:01 PM UTC-7, Niphlod wrote:
fabfile ATM is a show-off, and we can't put in there code related to an app which doesn't exist :P

It should be possible to put a comment block in the fabfile that shows what might be done, but it would still require knowing-what-you-are-doing to turn it into the code for a specific deployment.
 
We can't pin each and every thread a "useful" information is written, as I really don't see how 40 pinned threads will contribute to web2py's knowledge for - and especially - newbies.


IMHO at most it should land on the book (deployment chapter), which everyone is highly encouraged to contribute, and doesn't need any python-web2py superpower to do so.


[mulls]

/dps
 

Manuele Pesenti

da leggere,
31 ago 2016, 08:33:1531/08/16
a web...@googlegroups.com
Il 30/08/16 21:40, Niphlod ha scritto:
> +1 for Dave .
>
> with isempty() in a model you're executing a query for each and every
> request for absolutely NO REASON.

I usually create a script directory and put there scripts with commands
to be performed only once, such in the case of pre-populating or more in
general of application initialization with the sintax:

$ python web2py.py -S myapp -M -R applications/myapp/scripts/myscript.py

in case of model is not needed (and it's not the case of pre-population)
you can even not use the -M option.

Do you think it could be a good way to suggest?

Cheers


Manuele

Dave S

da leggere,
31 ago 2016, 14:13:1031/08/16
a web2py-users


Seems like a good note to make.  I do something similar with my fabfile[s].

/dps
 

Niphlod

da leggere,
31 ago 2016, 15:15:5831/08/16
a web2py-users
same deal, "different" organization. 
With an external script you can't call that function if not from an external process. 
Having it in a separated controller incurs in None performance-penalties (having it in a standard controller has None to a handful of ms penalty when that controller is hit for another function)... and you can offload the task, e.g., to the scheduler.

Richard Vézina

da leggere,
1 set 2016, 14:52:3601/09/16
a web2py-users
private/* would be a good place to store it.

Richard

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.
Rispondi a tutti
Rispondi all'autore
Inoltra
0 nuovi messaggi